3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace tests \unit \Magento \FunctionalTestFramework \Config \Reader ;
7
9
10
+ use Magento \FunctionalTestingFramework \Config \ConverterInterface ;
8
11
use Magento \FunctionalTestingFramework \Config \FileResolver \Module ;
9
12
use Magento \FunctionalTestingFramework \Config \Reader \Filesystem ;
13
+ use Magento \FunctionalTestingFramework \Config \SchemaLocatorInterface ;
10
14
use Magento \FunctionalTestingFramework \Config \ValidationState ;
11
15
use Magento \FunctionalTestingFramework \Util \Iterator \File ;
16
+ use PHPUnit \Framework \MockObject \MockObject ;
12
17
use PHPUnit \Framework \TestCase ;
13
- use AspectMock \Test as AspectMock ;
14
18
use tests \unit \Util \TestLoggingUtil ;
15
19
16
20
class FilesystemTest extends TestCase
17
21
{
18
22
/**
19
- * Before test functionality
20
23
* @return void
21
24
*/
22
25
public function setUp (): void
@@ -25,79 +28,80 @@ public function setUp(): void
25
28
}
26
29
27
30
/**
28
- * Test Reading Empty Files
29
31
* @throws \Exception
30
32
*/
31
33
public function testEmptyXmlFile ()
32
34
{
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 ());
37
37
38
- // validate log statement
39
38
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 ' ]
43
42
);
44
43
}
45
44
46
45
/**
47
- * Function used to set mock for File created in test
46
+ * Retrieve mocked file iterator
48
47
*
49
48
* @param string $fileName
50
49
* @param string $content
51
- * @return object
50
+ * @return File|MockObject
52
51
* @throws \Exception
53
52
*/
54
- public function setMockFile ( $ fileName , $ content )
53
+ public function getFileIterator ( string $ fileName , string $ content ): File
55
54
{
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
+ });
64
70
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
+ } );
69
75
70
76
return $ file ;
71
77
}
72
78
73
79
/**
74
- * Function used to set mock for filesystem class during test
80
+ * Get real instance of Filesystem class with mocked dependencies
75
81
*
76
- * @param string $fileList
77
- * @return object
78
- * @throws \Exception
82
+ * @param File $fileIterator
83
+ * @return Filesystem
79
84
*/
80
- public function createPseudoFileSystem ( $ fileList )
85
+ public function getFilesystem ( File $ fileIterator ): Filesystem
81
86
{
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
+ );
95
100
96
101
return $ filesystem ;
97
102
}
98
103
99
104
/**
100
- * After class functionality
101
105
* @return void
102
106
*/
103
107
public static function tearDownAfterClass (): void
0 commit comments