33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+ declare (strict_types=1 );
7+
68namespace tests \unit \Magento \FunctionalTestFramework \Config \Reader ;
79
10+ use Magento \FunctionalTestingFramework \Config \ConverterInterface ;
811use Magento \FunctionalTestingFramework \Config \FileResolver \Module ;
912use Magento \FunctionalTestingFramework \Config \Reader \Filesystem ;
13+ use Magento \FunctionalTestingFramework \Config \SchemaLocatorInterface ;
1014use Magento \FunctionalTestingFramework \Config \ValidationState ;
1115use Magento \FunctionalTestingFramework \Util \Iterator \File ;
16+ use PHPUnit \Framework \MockObject \MockObject ;
1217use PHPUnit \Framework \TestCase ;
13- use AspectMock \Test as AspectMock ;
1418use tests \unit \Util \TestLoggingUtil ;
1519
1620class 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
0 commit comments