Skip to content

Commit ca55c2f

Browse files
committed
Eliminated Aspect Mock usage from FilesystemTest
1 parent 5a6bdf5 commit ca55c2f

File tree

2 files changed

+50
-46
lines changed

2 files changed

+50
-46
lines changed

dev/tests/unit/Magento/FunctionalTestFramework/Config/Reader/FilesystemTest.php

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,23 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace tests\unit\Magento\FunctionalTestFramework\Config\Reader;
79

10+
use Magento\FunctionalTestingFramework\Config\ConverterInterface;
811
use Magento\FunctionalTestingFramework\Config\FileResolver\Module;
912
use Magento\FunctionalTestingFramework\Config\Reader\Filesystem;
13+
use Magento\FunctionalTestingFramework\Config\SchemaLocatorInterface;
1014
use Magento\FunctionalTestingFramework\Config\ValidationState;
1115
use Magento\FunctionalTestingFramework\Util\Iterator\File;
16+
use PHPUnit\Framework\MockObject\MockObject;
1217
use PHPUnit\Framework\TestCase;
13-
use AspectMock\Test as AspectMock;
1418
use tests\unit\Util\TestLoggingUtil;
1519

1620
class FilesystemTest extends TestCase
1721
{
1822
/**
19-
* Before test functionality
2023
* @return void
2124
*/
2225
public function setUp(): void
@@ -25,79 +28,80 @@ public function setUp(): void
2528
}
2629

2730
/**
28-
* Test Reading Empty Files
2931
* @throws \Exception
3032
*/
3133
public function testEmptyXmlFile()
3234
{
33-
// create mocked items and read the file
34-
$someFile = $this->setMockFile("somepath.xml", "");
35-
$filesystem = $this->createPseudoFileSystem($someFile);
36-
$filesystem->read();
35+
$filesystem = $this->getFilesystem($this->getFileIterator('somepath.xml', ''));
36+
$this->assertEquals([], $filesystem->read());
3737

38-
// validate log statement
3938
TestLoggingUtil::getInstance()->validateMockLogStatement(
40-
"warning",
41-
"XML File is empty.",
42-
["File" => "somepath.xml"]
39+
'warning',
40+
'XML File is empty.',
41+
['File' => 'somepath.xml']
4342
);
4443
}
4544

4645
/**
47-
* Function used to set mock for File created in test
46+
* Retrieve mocked file iterator
4847
*
4948
* @param string $fileName
5049
* @param string $content
51-
* @return object
50+
* @return File|MockObject
5251
* @throws \Exception
5352
*/
54-
public function setMockFile($fileName, $content)
53+
public function getFileIterator(string $fileName, string $content): File
5554
{
56-
$file = AspectMock::double(
57-
File::class,
58-
[
59-
'current' => "",
60-
'count' => 1,
61-
'getFilename' => $fileName
62-
]
63-
)->make();
55+
$iterator = new \ArrayIterator([$content]);
56+
57+
$file = $this->createMock(File::class);
58+
59+
$file->method('current')
60+
->willReturn($content);
61+
$file->method('getFilename')
62+
->willReturn($fileName);
63+
$file->method('count')
64+
->willReturn(1);
65+
66+
$file->method('next')
67+
->willReturnCallback(function () use ($iterator): void {
68+
$iterator->next();
69+
});
6470

65-
//set mocked data property for File
66-
$property = new \ReflectionProperty(File::class, 'data');
67-
$property->setAccessible(true);
68-
$property->setValue($file, [$fileName => $content]);
71+
$file->method('valid')
72+
->willReturnCallback(function () use ($iterator): bool {
73+
return $iterator->valid();
74+
});
6975

7076
return $file;
7177
}
7278

7379
/**
74-
* Function used to set mock for filesystem class during test
80+
* Get real instance of Filesystem class with mocked dependencies
7581
*
76-
* @param string $fileList
77-
* @return object
78-
* @throws \Exception
82+
* @param File $fileIterator
83+
* @return Filesystem
7984
*/
80-
public function createPseudoFileSystem($fileList)
85+
public function getFilesystem(File $fileIterator): Filesystem
8186
{
82-
$filesystem = AspectMock::double(Filesystem::class)->make();
83-
84-
//set resolver to use mocked resolver
85-
$mockFileResolver = AspectMock::double(Module::class, ['get' => $fileList])->make();
86-
$property = new \ReflectionProperty(Filesystem::class, 'fileResolver');
87-
$property->setAccessible(true);
88-
$property->setValue($filesystem, $mockFileResolver);
89-
90-
//set validator to use mocked validator
91-
$mockValidation = AspectMock::double(ValidationState::class, ['isValidationRequired' => false])->make();
92-
$property = new \ReflectionProperty(Filesystem::class, 'validationState');
93-
$property->setAccessible(true);
94-
$property->setValue($filesystem, $mockValidation);
87+
$fileResolver = $this->createMock(Module::class);
88+
$fileResolver->method('get')
89+
->willReturn($fileIterator);
90+
$validationState = $this->createMock(ValidationState::class);
91+
$validationState->method('isValidationRequired')
92+
->willReturn(false);
93+
$filesystem = new Filesystem(
94+
$fileResolver,
95+
$this->createMock(ConverterInterface::class),
96+
$this->createMock(SchemaLocatorInterface::class),
97+
$validationState,
98+
''
99+
);
95100

96101
return $filesystem;
97102
}
98103

99104
/**
100-
* After class functionality
101105
* @return void
102106
*/
103107
public static function tearDownAfterClass(): void

src/Magento/FunctionalTestingFramework/Config/Reader/Filesystem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ protected function readFiles($fileList)
148148
/** @var \Magento\FunctionalTestingFramework\Config\Dom $configMerger */
149149
$configMerger = null;
150150
$debugLevel = MftfApplicationConfig::getConfig()->getDebugLevel();
151-
foreach ($fileList as $key => $content) {
151+
foreach ($fileList as $content) {
152152
//check if file is empty and continue to next if it is
153153
if (!$this->verifyFileEmpty($content, $fileList->getFilename())) {
154154
continue;

0 commit comments

Comments
 (0)