33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+ declare (strict_types=1 );
67
78namespace tests \unit \Magento \FunctionalTestFramework \Util ;
89
9- use AspectMock \Test as AspectMock ;
10-
10+ use Exception ;
11+ use Magento \FunctionalTestingFramework \Config \MftfApplicationConfig ;
12+ use Magento \FunctionalTestingFramework \Exceptions \TestReferenceException ;
1113use Magento \FunctionalTestingFramework \Filter \FilterList ;
12- use Magento \FunctionalTestingFramework \Test \Handlers \TestObjectHandler ;
1314use Magento \FunctionalTestingFramework \Test \Objects \ActionObject ;
1415use Magento \FunctionalTestingFramework \Test \Objects \TestHookObject ;
1516use Magento \FunctionalTestingFramework \Test \Objects \TestObject ;
16- use tests \unit \Util \MagentoTestCase ;
17+ use Magento \FunctionalTestingFramework \Util \Filesystem \CestFileCreatorUtil ;
18+ use Magento \FunctionalTestingFramework \Util \GenerationErrorHandler ;
1719use Magento \FunctionalTestingFramework \Util \TestGenerator ;
18- use Magento \FunctionalTestingFramework \Config \MftfApplicationConfig ;
20+ use ReflectionProperty ;
21+ use tests \unit \Util \MagentoTestCase ;
1922use tests \unit \Util \TestLoggingUtil ;
20- use Magento \FunctionalTestingFramework \Util \GenerationErrorHandler ;
2123
2224class TestGeneratorTest extends MagentoTestCase
2325{
2426 /**
25- * Before method functionality
27+ * Before method functionality.
28+ *
29+ * @return void
2630 */
27- public function setUp (): void
31+ protected function setUp (): void
2832 {
2933 TestLoggingUtil::getInstance ()->setMockLoggingUtil ();
3034 }
3135
3236 /**
33- * After method functionality
37+ * After method functionality.
3438 *
3539 * @return void
3640 */
37- public function tearDown (): void
41+ protected function tearDown (): void
3842 {
39- AspectMock::clean ();
4043 GenerationErrorHandler::getInstance ()->reset ();
4144 }
4245
4346 /**
4447 * Basic test to check exceptions for incorrect entities.
4548 *
46- * @throws \Exception
49+ * @return void
50+ * @throws Exception
4751 */
48- public function testEntityException ()
52+ public function testEntityException (): void
4953 {
5054 $ actionObject = new ActionObject ('fakeAction ' , 'comment ' , [
5155 'userInput ' => '{{someEntity.entity}} '
5256 ]);
5357
54- $ testObject = new TestObject ("sampleTest " , ["merge123 " => $ actionObject ], [], [], "filename " );
55-
56- AspectMock::double (TestObjectHandler::class, ['initTestData ' => '' ]);
57-
58- $ testGeneratorObject = TestGenerator::getInstance ("" , ["sampleTest " => $ testObject ]);
59-
60- AspectMock::double (TestGenerator::class, ['loadAllTestObjects ' => ["sampleTest " => $ testObject ]]);
61-
58+ $ testObject = new TestObject ('sampleTest ' , ['merge123 ' => $ actionObject ], [], [], 'filename ' );
59+ $ testGeneratorObject = TestGenerator::getInstance ('' , ['sampleTest ' => $ testObject ]);
6260 $ testGeneratorObject ->createAllTestFiles (null , []);
6361
6462 // assert that no exception for createAllTestFiles and generation error is stored in GenerationErrorHandler
65- $ errorMessage = '/ ' . preg_quote (" Removed invalid test object sampleTest " ) . '/ ' ;
63+ $ errorMessage = '/ ' . preg_quote (' Removed invalid test object sampleTest ' ) . '/ ' ;
6664 TestLoggingUtil::getInstance ()->validateMockLogStatmentRegex ('error ' , $ errorMessage , []);
6765 $ testErrors = GenerationErrorHandler::getInstance ()->getErrorsByType ('test ' );
6866 $ this ->assertArrayHasKey ('sampleTest ' , $ testErrors );
6967 }
7068
7169 /**
72- * Tests that skipped tests do not have a fully generated body
70+ * Tests that skipped tests do not have a fully generated body.
7371 *
74- * @throws \Magento\FunctionalTestingFramework\Exceptions\TestReferenceException
72+ * @return void
73+ * @throws TestReferenceException
7574 */
76- public function testSkippedNoGeneration ()
75+ public function testSkippedNoGeneration (): void
7776 {
7877 $ actionInput = 'fakeInput ' ;
7978 $ actionObject = new ActionObject ('fakeAction ' , 'comment ' , [
8079 'userInput ' => $ actionInput
8180 ]);
8281
8382 $ annotations = ['skip ' => ['issue ' ]];
84- $ testObject = new TestObject (" sampleTest " , [" merge123 " => $ actionObject ], $ annotations , [], " filename " );
83+ $ testObject = new TestObject (' sampleTest ' , [' merge123 ' => $ actionObject ], $ annotations , [], ' filename ' );
8584
86- $ testGeneratorObject = TestGenerator::getInstance ("" , [" sampleTest " => $ testObject ]);
85+ $ testGeneratorObject = TestGenerator::getInstance ('' , [' sampleTest ' => $ testObject ]);
8786 $ output = $ testGeneratorObject ->assembleTestPhp ($ testObject );
8887
8988 $ this ->assertStringContainsString ('This test is skipped ' , $ output );
9089 $ this ->assertStringNotContainsString ($ actionInput , $ output );
9190 }
9291
9392 /**
94- * Tests that skipped tests have a fully generated body when --allowSkipped is passed in
93+ * Tests that skipped tests have a fully generated body when --allowSkipped is passed in.
9594 *
96- * @throws \Magento\FunctionalTestingFramework\Exceptions\TestReferenceException
95+ * @return void
96+ * @throws TestReferenceException
9797 */
98- public function testAllowSkipped ()
98+ public function testAllowSkipped (): void
9999 {
100100 // Mock allowSkipped for TestGenerator
101- AspectMock::double (MftfApplicationConfig::class, ['allowSkipped ' => true ]);
101+ $ mockConfig = $ this ->createMock (MftfApplicationConfig::class);
102+ $ mockConfig
103+ ->method ('allowSkipped ' )
104+ ->willReturn (true );
105+
106+ $ property = new ReflectionProperty (MftfApplicationConfig::class, 'MFTF_APPLICATION_CONTEXT ' );
107+ $ property ->setAccessible (true );
108+ $ property ->setValue ($ mockConfig );
102109
103110 $ actionInput = 'fakeInput ' ;
104111 $ actionObject = new ActionObject ('fakeAction ' , 'comment ' , [
@@ -110,16 +117,16 @@ public function testAllowSkipped()
110117 ]);
111118
112119 $ annotations = ['skip ' => ['issue ' ]];
113- $ beforeHook = new TestHookObject (" before " , " sampleTest " , ['beforeAction ' => $ beforeActionObject ]);
120+ $ beforeHook = new TestHookObject (' before ' , ' sampleTest ' , ['beforeAction ' => $ beforeActionObject ]);
114121 $ testObject = new TestObject (
115- " sampleTest " ,
116- [" fakeAction " => $ actionObject ],
122+ ' sampleTest ' ,
123+ [' fakeAction ' => $ actionObject ],
117124 $ annotations ,
118- [" before " => $ beforeHook ],
119- " filename "
125+ [' before ' => $ beforeHook ],
126+ ' filename '
120127 );
121128
122- $ testGeneratorObject = TestGenerator::getInstance ("" , [" sampleTest " => $ testObject ]);
129+ $ testGeneratorObject = TestGenerator::getInstance ('' , [' sampleTest ' => $ testObject ]);
123130 $ output = $ testGeneratorObject ->assembleTestPhp ($ testObject );
124131
125132 $ this ->assertStringNotContainsString ('This test is skipped ' , $ output );
@@ -128,17 +135,22 @@ public function testAllowSkipped()
128135 }
129136
130137 /**
131- * Tests that TestGenerator createAllTestFiles correctly filters based on severity
138+ * Tests that TestGenerator createAllTestFiles correctly filters based on severity.
132139 *
133- * @throws \Magento\FunctionalTestingFramework\Exceptions\TestReferenceException
140+ * @return void
141+ * @throws TestReferenceException
134142 */
135- public function testFilter ()
143+ public function testFilter (): void
136144 {
137- // Mock filters for TestGenerator
138- AspectMock::double (
139- MftfApplicationConfig::class,
140- ['getFilterList ' => new FilterList (['severity ' => ["CRITICAL " ]])]
141- );
145+ $ mockConfig = $ this ->createMock (MftfApplicationConfig::class);
146+ $ fileList = new FilterList (['severity ' => ['CRITICAL ' ]]);
147+ $ mockConfig
148+ ->method ('getFilterList ' )
149+ ->willReturn ($ fileList );
150+
151+ $ property = new ReflectionProperty (MftfApplicationConfig::class, 'MFTF_APPLICATION_CONTEXT ' );
152+ $ property ->setAccessible (true );
153+ $ property ->setValue ($ mockConfig );
142154
143155 $ actionInput = 'fakeInput ' ;
144156 $ actionObject = new ActionObject ('fakeAction ' , 'comment ' , [
@@ -148,32 +160,58 @@ public function testFilter()
148160 $ annotation1 = ['severity ' => ['CRITICAL ' ]];
149161 $ annotation2 = ['severity ' => ['MINOR ' ]];
150162 $ test1 = new TestObject (
151- " test1 " ,
152- [" fakeAction " => $ actionObject ],
163+ ' test1 ' ,
164+ [' fakeAction ' => $ actionObject ],
153165 $ annotation1 ,
154166 [],
155- " filename "
167+ ' filename '
156168 );
157169 $ test2 = new TestObject (
158- " test2 " ,
159- [" fakeAction " => $ actionObject ],
170+ ' test2 ' ,
171+ [' fakeAction ' => $ actionObject ],
160172 $ annotation2 ,
161173 [],
162- " filename "
174+ ' filename '
163175 );
164- AspectMock::double (TestGenerator::class, ['loadAllTestObjects ' => ["sampleTest " => $ test1 , "test2 " => $ test2 ]]);
165176
166177 // Mock createCestFile to return name of tests that testGenerator tried to create
167178 $ generatedTests = [];
168- AspectMock::double (TestGenerator::class, ['createCestFile ' => function ($ arg1 , $ arg2 ) use (&$ generatedTests ) {
169- $ generatedTests [$ arg2 ] = true ;
170- }]);
171-
172- $ testGeneratorObject = TestGenerator::getInstance ("" , ["sampleTest " => $ test1 , "test2 " => $ test2 ]);
173- $ testGeneratorObject ->createAllTestFiles (null , []);
179+ $ cestFileCreatorUtil = $ this ->createMock (CestFileCreatorUtil::class);
180+ $ cestFileCreatorUtil
181+ ->method ('create ' )
182+ ->will (
183+ $ this ->returnCallback (
184+ function ($ filename ) use (&$ generatedTests ) {
185+ $ generatedTests [$ filename ] = true ;
186+ }
187+ )
188+ );
189+
190+ $ property = new ReflectionProperty (CestFileCreatorUtil::class, 'INSTANCE ' );
191+ $ property ->setAccessible (true );
192+ $ property ->setValue ($ cestFileCreatorUtil );
193+
194+ $ testGeneratorObject = TestGenerator::getInstance ('' , ['sampleTest ' => $ test1 , 'test2 ' => $ test2 ]);
195+ $ testGeneratorObject ->createAllTestFiles ();
174196
175197 // Ensure Test1 was Generated but not Test 2
176198 $ this ->assertArrayHasKey ('test1Cest ' , $ generatedTests );
177199 $ this ->assertArrayNotHasKey ('test2Cest ' , $ generatedTests );
178200 }
201+
202+ /**
203+ * @inheritDoc
204+ */
205+ public static function tearDownAfterClass (): void
206+ {
207+ parent ::tearDownAfterClass ();
208+
209+ $ cestFileCreatorUtilInstance = new ReflectionProperty (CestFileCreatorUtil::class, 'INSTANCE ' );
210+ $ cestFileCreatorUtilInstance ->setAccessible (true );
211+ $ cestFileCreatorUtilInstance ->setValue (null );
212+
213+ $ mftfAppConfigInstance = new ReflectionProperty (MftfApplicationConfig::class, 'MFTF_APPLICATION_CONTEXT ' );
214+ $ mftfAppConfigInstance ->setAccessible (true );
215+ $ mftfAppConfigInstance ->setValue (null );
216+ }
179217}
0 commit comments