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 \Suite ;
79
8- use AspectMock \ Test as AspectMock ;
10+ use Exception ;
911use Magento \FunctionalTestingFramework \Exceptions \TestReferenceException ;
10- use Magento \FunctionalTestingFramework \ObjectManager \ ObjectManager ;
12+ use Magento \FunctionalTestingFramework \ObjectManager ;
1113use Magento \FunctionalTestingFramework \ObjectManagerFactory ;
14+ use Magento \FunctionalTestingFramework \Suite \Service \SuiteGeneratorService ;
1215use Magento \FunctionalTestingFramework \Suite \SuiteGenerator ;
1316use Magento \FunctionalTestingFramework \Suite \Generators \GroupClassGenerator ;
1417use Magento \FunctionalTestingFramework \Suite \Handlers \SuiteObjectHandler ;
1821use Magento \FunctionalTestingFramework \Test \Parsers \TestDataParser ;
1922use Magento \FunctionalTestingFramework \Util \GenerationErrorHandler ;
2023use Magento \FunctionalTestingFramework \Util \Manifest \DefaultTestManifest ;
24+ use ReflectionProperty ;
2125use tests \unit \Util \MagentoTestCase ;
2226use Magento \FunctionalTestingFramework \Util \Manifest \TestManifestFactory ;
2327use tests \unit \Util \SuiteDataArrayBuilder ;
2428use tests \unit \Util \TestDataArrayBuilder ;
2529use tests \unit \Util \TestLoggingUtil ;
26- use tests \unit \Util \MockModuleResolverBuilder ;
2730
2831class SuiteGeneratorTest extends MagentoTestCase
2932{
3033 /**
31- * Setup entry append and clear for Suite Generator
32- */
33- public static function setUpBeforeClass (): void
34- {
35- AspectMock::double (SuiteGenerator::class, [
36- 'clearPreviousSessionConfigEntries ' => null ,
37- 'appendEntriesToConfig ' => null
38- ]);
39- }
40-
41- /**
42- * Before test functionality
34+ * Before test functionality.
35+ *
4336 * @return void
4437 */
45- public function setUp (): void
38+ protected function setUp (): void
4639 {
4740 TestLoggingUtil::getInstance ()->setMockLoggingUtil ();
48- $ resolverMock = new MockModuleResolverBuilder ();
49- $ resolverMock ->setup ();
5041 }
5142
5243 /**
53- * Tests generating a single suite given a set of parsed test data
44+ * Tests generating a single suite given a set of parsed test data.
45+ *
46+ * @return void
47+ * @throws Exception
5448 */
55- public function testGenerateSuite ()
49+ public function testGenerateSuite (): void
5650 {
5751 $ suiteDataArrayBuilder = new SuiteDataArrayBuilder ();
5852 $ mockData = $ suiteDataArrayBuilder
@@ -74,20 +68,23 @@ public function testGenerateSuite()
7468
7569 // parse and generate suite object with mocked data
7670 $ mockSuiteGenerator = SuiteGenerator::getInstance ();
77- $ mockSuiteGenerator ->generateSuite (" basicTestSuite " );
71+ $ mockSuiteGenerator ->generateSuite (' basicTestSuite ' );
7872
7973 // assert that expected suite is generated
8074 TestLoggingUtil::getInstance ()->validateMockLogStatement (
8175 'info ' ,
82- " suite generated " ,
83- ['suite ' => 'basicTestSuite ' , 'relative_path ' => " _generated " . DIRECTORY_SEPARATOR . " basicTestSuite " ]
76+ ' suite generated ' ,
77+ ['suite ' => 'basicTestSuite ' , 'relative_path ' => ' _generated ' . DIRECTORY_SEPARATOR . ' basicTestSuite ' ]
8478 );
8579 }
8680
8781 /**
88- * Tests generating all suites given a set of parsed test data
82+ * Tests generating all suites given a set of parsed test data.
83+ *
84+ * @return void
85+ * @throws Exception
8986 */
90- public function testGenerateAllSuites ()
87+ public function testGenerateAllSuites (): void
9188 {
9289 $ suiteDataArrayBuilder = new SuiteDataArrayBuilder ();
9390 $ mockData = $ suiteDataArrayBuilder
@@ -108,22 +105,25 @@ public function testGenerateAllSuites()
108105 $ this ->setMockTestAndSuiteParserOutput ($ mockTestData , $ mockData );
109106
110107 // parse and retrieve suite object with mocked data
111- $ exampleTestManifest = new DefaultTestManifest ([], " sample " . DIRECTORY_SEPARATOR . " path " );
108+ $ exampleTestManifest = new DefaultTestManifest ([], ' sample ' . DIRECTORY_SEPARATOR . ' path ' );
112109 $ mockSuiteGenerator = SuiteGenerator::getInstance ();
113110 $ mockSuiteGenerator ->generateAllSuites ($ exampleTestManifest );
114111
115112 // assert that expected suites are generated
116113 TestLoggingUtil::getInstance ()->validateMockLogStatement (
117114 'info ' ,
118- " suite generated " ,
119- ['suite ' => 'basicTestSuite ' , 'relative_path ' => " _generated " . DIRECTORY_SEPARATOR . " basicTestSuite " ]
115+ ' suite generated ' ,
116+ ['suite ' => 'basicTestSuite ' , 'relative_path ' => ' _generated ' . DIRECTORY_SEPARATOR . ' basicTestSuite ' ]
120117 );
121118 }
122119
123120 /**
124- * Tests attempting to generate a suite with no included/excluded tests and no hooks
121+ * Tests attempting to generate a suite with no included/excluded tests and no hooks.
122+ *
123+ * @return void
124+ * @throws Exception
125125 */
126- public function testGenerateEmptySuite ()
126+ public function testGenerateEmptySuite (): void
127127 {
128128 $ testDataArrayBuilder = new TestDataArrayBuilder ();
129129 $ mockTestData = $ testDataArrayBuilder
@@ -142,17 +142,20 @@ public function testGenerateEmptySuite()
142142 $ this ->setMockTestAndSuiteParserOutput ($ mockTestData , $ mockData );
143143
144144 // set expected error message
145- $ this ->expectExceptionMessage (" Suite basicTestSuite is not defined in xml or is invalid " );
145+ $ this ->expectExceptionMessage (' Suite basicTestSuite is not defined in xml or is invalid ' );
146146
147147 // parse and generate suite object with mocked data
148148 $ mockSuiteGenerator = SuiteGenerator::getInstance ();
149- $ mockSuiteGenerator ->generateSuite (" basicTestSuite " );
149+ $ mockSuiteGenerator ->generateSuite (' basicTestSuite ' );
150150 }
151151
152152 /**
153- * Tests generating all suites with a suite containing invalid test reference
153+ * Tests generating all suites with a suite containing invalid test reference.
154+ *
155+ * @return void
156+ * @throws TestReferenceException
154157 */
155- public function testInvalidSuiteTestPair ()
158+ public function testInvalidSuiteTestPair (): void
156159 {
157160 // Mock Suite1 => Test1 and Suite2 => Test2
158161 $ suiteDataArrayBuilder = new SuiteDataArrayBuilder ();
@@ -198,9 +201,12 @@ public function testInvalidSuiteTestPair()
198201 }
199202
200203 /**
201- * Tests generating all suites with a non-existing suite
204+ * Tests generating all suites with a non-existing suite.
205+ *
206+ * @return void
207+ * @throws TestReferenceException
202208 */
203- public function testNonExistentSuiteTestPair ()
209+ public function testNonExistentSuiteTestPair (): void
204210 {
205211 $ testDataArrayBuilder = new TestDataArrayBuilder ();
206212 $ mockSimpleTest = $ testDataArrayBuilder
@@ -227,9 +233,12 @@ public function testNonExistentSuiteTestPair()
227233 }
228234
229235 /**
230- * Tests generating split suites for parallel test generation
236+ * Tests generating split suites for parallel test generation.
237+ *
238+ * @return void
239+ * @throws TestReferenceException
231240 */
232- public function testGenerateSplitSuiteFromTest ()
241+ public function testGenerateSplitSuiteFromTest (): void
233242 {
234243 $ suiteDataArrayBuilder = new SuiteDataArrayBuilder ();
235244 $ mockSuiteData = $ suiteDataArrayBuilder
@@ -272,8 +281,8 @@ public function testGenerateSplitSuiteFromTest()
272281 // assert last split suite group generated
273282 TestLoggingUtil::getInstance ()->validateMockLogStatement (
274283 'info ' ,
275- " suite generated " ,
276- ['suite ' => 'mockSuite_1_G ' , 'relative_path ' => " _generated " . DIRECTORY_SEPARATOR . " mockSuite_1_G " ]
284+ ' suite generated ' ,
285+ ['suite ' => 'mockSuite_1_G ' , 'relative_path ' => ' _generated ' . DIRECTORY_SEPARATOR . ' mockSuite_1_G ' ]
277286 );
278287 }
279288
@@ -282,75 +291,127 @@ public function testGenerateSplitSuiteFromTest()
282291 *
283292 * @param array $testData
284293 * @param array $suiteData
285- * @throws \Exception
294+ *
295+ * @return void
296+ * @throws Exception
286297 */
287- private function setMockTestAndSuiteParserOutput ($ testData , $ suiteData )
298+ private function setMockTestAndSuiteParserOutput (array $ testData , array $ suiteData ): void
288299 {
289- $ property = new \ReflectionProperty (SuiteGenerator::class, 'instance ' );
300+ $ this ->clearMockResolverProperties ();
301+ $ mockSuiteGeneratorService = $ this ->createMock (SuiteGeneratorService::class);
302+ $ mockVoidReturnCallback = function () {};// phpcs:ignore
303+
304+ $ mockSuiteGeneratorService
305+ ->method ('clearPreviousSessionConfigEntries ' )
306+ ->will ($ this ->returnCallback ($ mockVoidReturnCallback ));
307+
308+ $ mockSuiteGeneratorService
309+ ->method ('appendEntriesToConfig ' )
310+ ->will ($ this ->returnCallback ($ mockVoidReturnCallback ));
311+
312+ $ mockSuiteGeneratorService
313+ ->method ('generateRelevantGroupTests ' )
314+ ->will ($ this ->returnCallback ($ mockVoidReturnCallback ));
315+
316+ $ suiteGeneratorServiceProperty = new ReflectionProperty (SuiteGeneratorService::class, 'INSTANCE ' );
317+ $ suiteGeneratorServiceProperty ->setAccessible (true );
318+ $ suiteGeneratorServiceProperty ->setValue ($ mockSuiteGeneratorService );
319+
320+ $ mockDataParser = $ this ->createMock (TestDataParser::class);
321+ $ mockDataParser
322+ ->method ('readTestData ' )
323+ ->willReturn ($ testData );
324+
325+ $ mockSuiteDataParser = $ this ->createMock (SuiteDataParser::class);
326+ $ mockSuiteDataParser
327+ ->method ('readSuiteData ' )
328+ ->willReturn ($ suiteData );
329+
330+ $ mockGroupClass = $ this ->createMock (GroupClassGenerator::class);
331+ $ mockGroupClass
332+ ->method ('generateGroupClass ' )
333+ ->willReturn ('namespace ' );
334+
335+ $ objectManager = ObjectManagerFactory::getObjectManager ();
336+ $ objectManagerMockInstance = $ this ->createMock (ObjectManager::class);
337+ $ objectManagerMockInstance
338+ ->method ('create ' )
339+ ->will (
340+ $ this ->returnCallback (
341+ function (
342+ string $ class ,
343+ array $ arguments = []
344+ ) use (
345+ $ mockDataParser ,
346+ $ mockSuiteDataParser ,
347+ $ mockGroupClass ,
348+ $ objectManager
349+ ) {
350+ if ($ class == TestDataParser::class) {
351+ return $ mockDataParser ;
352+ }
353+ if ($ class == SuiteDataParser::class) {
354+ return $ mockSuiteDataParser ;
355+ }
356+ if ($ class == GroupClassGenerator::class) {
357+ return $ mockGroupClass ;
358+ }
359+
360+ return $ objectManager ->create ($ class , $ arguments );
361+ }
362+ )
363+ );
364+
365+ $ objectManagerProperty = new ReflectionProperty (ObjectManager::class, 'instance ' );
366+ $ objectManagerProperty ->setAccessible (true );
367+ $ objectManagerProperty ->setValue ($ objectManagerMockInstance );
368+ }
369+
370+ /**
371+ * Function used to clear mock properties.
372+ *
373+ * @return void
374+ */
375+ private function clearMockResolverProperties (): void
376+ {
377+ $ property = new ReflectionProperty (SuiteGenerator::class, 'instance ' );
290378 $ property ->setAccessible (true );
291379 $ property ->setValue (null );
292380
293381 // clear test object handler value to inject parsed content
294- $ property = new \ ReflectionProperty (TestObjectHandler::class, 'testObjectHandler ' );
382+ $ property = new ReflectionProperty (TestObjectHandler::class, 'testObjectHandler ' );
295383 $ property ->setAccessible (true );
296384 $ property ->setValue (null );
297385
298386 // clear suite object handler value to inject parsed content
299- $ property = new \ ReflectionProperty (SuiteObjectHandler::class, 'instance ' );
387+ $ property = new ReflectionProperty (SuiteObjectHandler::class, 'instance ' );
300388 $ property ->setAccessible (true );
301389 $ property ->setValue (null );
302-
303- $ mockDataParser = AspectMock::double (TestDataParser::class, ['readTestData ' => $ testData ])->make ();
304- $ mockSuiteDataParser = AspectMock::double (SuiteDataParser::class, ['readSuiteData ' => $ suiteData ])->make ();
305- $ mockGroupClass = AspectMock::double (
306- GroupClassGenerator::class,
307- ['generateGroupClass ' => 'namespace ' ]
308- )->make ();
309- $ mockSuiteClass = AspectMock::double (SuiteGenerator::class, ['generateRelevantGroupTests ' => null ])->make ();
310- $ instance = AspectMock::double (
311- ObjectManager::class,
312- ['create ' => function ($ clazz ) use (
313- $ mockDataParser ,
314- $ mockSuiteDataParser ,
315- $ mockGroupClass ,
316- $ mockSuiteClass
317- ) {
318- if ($ clazz == TestDataParser::class) {
319- return $ mockDataParser ;
320- }
321- if ($ clazz == SuiteDataParser::class) {
322- return $ mockSuiteDataParser ;
323- }
324- if ($ clazz == GroupClassGenerator::class) {
325- return $ mockGroupClass ;
326- }
327- if ($ clazz == SuiteGenerator::class) {
328- return $ mockSuiteClass ;
329- }
330- }]
331- )->make ();
332- // bypass the private constructor
333- AspectMock::double (ObjectManagerFactory::class, ['getObjectManager ' => $ instance ]);
334-
335- $ property = new \ReflectionProperty (SuiteGenerator::class, 'groupClassGenerator ' );
336- $ property ->setAccessible (true );
337- $ property ->setValue ($ instance , $ instance );
338390 }
339391
340392 /**
341- * clean up function runs after each test
393+ * @inheritDoc
342394 */
343- public function tearDown (): void
395+ protected function tearDown (): void
344396 {
345397 GenerationErrorHandler::getInstance ()->reset ();
346398 }
347399
348400 /**
349- * clean up function runs after all tests
401+ * @inheritDoc
350402 */
351403 public static function tearDownAfterClass (): void
352404 {
353- TestLoggingUtil::getInstance ()->clearMockLoggingUtil ();
354405 parent ::tearDownAfterClass ();
406+
407+ $ objectManagerProperty = new ReflectionProperty (ObjectManager::class, 'instance ' );
408+ $ objectManagerProperty ->setAccessible (true );
409+ $ objectManagerProperty ->setValue (null );
410+
411+ $ suiteGeneratorServiceProperty = new ReflectionProperty (SuiteGeneratorService::class, 'INSTANCE ' );
412+ $ suiteGeneratorServiceProperty ->setAccessible (true );
413+ $ suiteGeneratorServiceProperty ->setValue (null );
414+
415+ TestLoggingUtil::getInstance ()->clearMockLoggingUtil ();
355416 }
356417}
0 commit comments