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 \Suite ;
7
9
8
- use AspectMock \ Test as AspectMock ;
10
+ use Exception ;
9
11
use Magento \FunctionalTestingFramework \Exceptions \TestReferenceException ;
10
- use Magento \FunctionalTestingFramework \ObjectManager \ ObjectManager ;
12
+ use Magento \FunctionalTestingFramework \ObjectManager ;
11
13
use Magento \FunctionalTestingFramework \ObjectManagerFactory ;
14
+ use Magento \FunctionalTestingFramework \Suite \Service \SuiteGeneratorService ;
12
15
use Magento \FunctionalTestingFramework \Suite \SuiteGenerator ;
13
16
use Magento \FunctionalTestingFramework \Suite \Generators \GroupClassGenerator ;
14
17
use Magento \FunctionalTestingFramework \Suite \Handlers \SuiteObjectHandler ;
18
21
use Magento \FunctionalTestingFramework \Test \Parsers \TestDataParser ;
19
22
use Magento \FunctionalTestingFramework \Util \GenerationErrorHandler ;
20
23
use Magento \FunctionalTestingFramework \Util \Manifest \DefaultTestManifest ;
24
+ use ReflectionProperty ;
21
25
use tests \unit \Util \MagentoTestCase ;
22
26
use Magento \FunctionalTestingFramework \Util \Manifest \TestManifestFactory ;
23
27
use tests \unit \Util \SuiteDataArrayBuilder ;
24
28
use tests \unit \Util \TestDataArrayBuilder ;
25
29
use tests \unit \Util \TestLoggingUtil ;
26
- use tests \unit \Util \MockModuleResolverBuilder ;
27
30
28
31
class SuiteGeneratorTest extends MagentoTestCase
29
32
{
30
33
/**
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
+ *
43
36
* @return void
44
37
*/
45
- public function setUp (): void
38
+ protected function setUp (): void
46
39
{
47
40
TestLoggingUtil::getInstance ()->setMockLoggingUtil ();
48
- $ resolverMock = new MockModuleResolverBuilder ();
49
- $ resolverMock ->setup ();
50
41
}
51
42
52
43
/**
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
54
48
*/
55
- public function testGenerateSuite ()
49
+ public function testGenerateSuite (): void
56
50
{
57
51
$ suiteDataArrayBuilder = new SuiteDataArrayBuilder ();
58
52
$ mockData = $ suiteDataArrayBuilder
@@ -74,20 +68,23 @@ public function testGenerateSuite()
74
68
75
69
// parse and generate suite object with mocked data
76
70
$ mockSuiteGenerator = SuiteGenerator::getInstance ();
77
- $ mockSuiteGenerator ->generateSuite (" basicTestSuite " );
71
+ $ mockSuiteGenerator ->generateSuite (' basicTestSuite ' );
78
72
79
73
// assert that expected suite is generated
80
74
TestLoggingUtil::getInstance ()->validateMockLogStatement (
81
75
'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 ' ]
84
78
);
85
79
}
86
80
87
81
/**
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
89
86
*/
90
- public function testGenerateAllSuites ()
87
+ public function testGenerateAllSuites (): void
91
88
{
92
89
$ suiteDataArrayBuilder = new SuiteDataArrayBuilder ();
93
90
$ mockData = $ suiteDataArrayBuilder
@@ -108,22 +105,25 @@ public function testGenerateAllSuites()
108
105
$ this ->setMockTestAndSuiteParserOutput ($ mockTestData , $ mockData );
109
106
110
107
// parse and retrieve suite object with mocked data
111
- $ exampleTestManifest = new DefaultTestManifest ([], " sample " . DIRECTORY_SEPARATOR . " path " );
108
+ $ exampleTestManifest = new DefaultTestManifest ([], ' sample ' . DIRECTORY_SEPARATOR . ' path ' );
112
109
$ mockSuiteGenerator = SuiteGenerator::getInstance ();
113
110
$ mockSuiteGenerator ->generateAllSuites ($ exampleTestManifest );
114
111
115
112
// assert that expected suites are generated
116
113
TestLoggingUtil::getInstance ()->validateMockLogStatement (
117
114
'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 ' ]
120
117
);
121
118
}
122
119
123
120
/**
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
125
125
*/
126
- public function testGenerateEmptySuite ()
126
+ public function testGenerateEmptySuite (): void
127
127
{
128
128
$ testDataArrayBuilder = new TestDataArrayBuilder ();
129
129
$ mockTestData = $ testDataArrayBuilder
@@ -142,17 +142,20 @@ public function testGenerateEmptySuite()
142
142
$ this ->setMockTestAndSuiteParserOutput ($ mockTestData , $ mockData );
143
143
144
144
// 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 ' );
146
146
147
147
// parse and generate suite object with mocked data
148
148
$ mockSuiteGenerator = SuiteGenerator::getInstance ();
149
- $ mockSuiteGenerator ->generateSuite (" basicTestSuite " );
149
+ $ mockSuiteGenerator ->generateSuite (' basicTestSuite ' );
150
150
}
151
151
152
152
/**
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
154
157
*/
155
- public function testInvalidSuiteTestPair ()
158
+ public function testInvalidSuiteTestPair (): void
156
159
{
157
160
// Mock Suite1 => Test1 and Suite2 => Test2
158
161
$ suiteDataArrayBuilder = new SuiteDataArrayBuilder ();
@@ -198,9 +201,12 @@ public function testInvalidSuiteTestPair()
198
201
}
199
202
200
203
/**
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
202
208
*/
203
- public function testNonExistentSuiteTestPair ()
209
+ public function testNonExistentSuiteTestPair (): void
204
210
{
205
211
$ testDataArrayBuilder = new TestDataArrayBuilder ();
206
212
$ mockSimpleTest = $ testDataArrayBuilder
@@ -227,9 +233,12 @@ public function testNonExistentSuiteTestPair()
227
233
}
228
234
229
235
/**
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
231
240
*/
232
- public function testGenerateSplitSuiteFromTest ()
241
+ public function testGenerateSplitSuiteFromTest (): void
233
242
{
234
243
$ suiteDataArrayBuilder = new SuiteDataArrayBuilder ();
235
244
$ mockSuiteData = $ suiteDataArrayBuilder
@@ -272,8 +281,8 @@ public function testGenerateSplitSuiteFromTest()
272
281
// assert last split suite group generated
273
282
TestLoggingUtil::getInstance ()->validateMockLogStatement (
274
283
'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 ' ]
277
286
);
278
287
}
279
288
@@ -282,75 +291,127 @@ public function testGenerateSplitSuiteFromTest()
282
291
*
283
292
* @param array $testData
284
293
* @param array $suiteData
285
- * @throws \Exception
294
+ *
295
+ * @return void
296
+ * @throws Exception
286
297
*/
287
- private function setMockTestAndSuiteParserOutput ($ testData , $ suiteData )
298
+ private function setMockTestAndSuiteParserOutput (array $ testData , array $ suiteData ): void
288
299
{
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 ' );
290
378
$ property ->setAccessible (true );
291
379
$ property ->setValue (null );
292
380
293
381
// clear test object handler value to inject parsed content
294
- $ property = new \ ReflectionProperty (TestObjectHandler::class, 'testObjectHandler ' );
382
+ $ property = new ReflectionProperty (TestObjectHandler::class, 'testObjectHandler ' );
295
383
$ property ->setAccessible (true );
296
384
$ property ->setValue (null );
297
385
298
386
// clear suite object handler value to inject parsed content
299
- $ property = new \ ReflectionProperty (SuiteObjectHandler::class, 'instance ' );
387
+ $ property = new ReflectionProperty (SuiteObjectHandler::class, 'instance ' );
300
388
$ property ->setAccessible (true );
301
389
$ 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 );
338
390
}
339
391
340
392
/**
341
- * clean up function runs after each test
393
+ * @inheritDoc
342
394
*/
343
- public function tearDown (): void
395
+ protected function tearDown (): void
344
396
{
345
397
GenerationErrorHandler::getInstance ()->reset ();
346
398
}
347
399
348
400
/**
349
- * clean up function runs after all tests
401
+ * @inheritDoc
350
402
*/
351
403
public static function tearDownAfterClass (): void
352
404
{
353
- TestLoggingUtil::getInstance ()->clearMockLoggingUtil ();
354
405
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 ();
355
416
}
356
417
}
0 commit comments