diff --git a/dev/tests/unit/Magento/FunctionalTestFramework/Suite/SuiteGeneratorTest.php b/dev/tests/unit/Magento/FunctionalTestFramework/Suite/SuiteGeneratorTest.php new file mode 100644 index 000000000..e7c607729 --- /dev/null +++ b/dev/tests/unit/Magento/FunctionalTestFramework/Suite/SuiteGeneratorTest.php @@ -0,0 +1,149 @@ +withName('basicTestSuite') + ->withAfterHook() + ->withBeforeHook() + ->includeTests(['simpleTest']) + ->includeGroups(['group1']) + ->build(); + + $testDataArrayBuilder = new TestDataArrayBuilder(); + $mockSimpleTest = $testDataArrayBuilder + ->withName('simpleTest') + ->withTestActions() + ->build(); + + $mockTestData = ['tests' => array_merge($mockSimpleTest)]; + $this->setMockTestAndSuiteParserOutput($mockTestData, $mockData); + + // parse and generate suite object with mocked data + $mockSuiteGenerator = SuiteGenerator::getInstance(); + $mockSuiteGenerator->generateSuite("basicTestSuite"); + + // assert that expected suite is generated + $this->expectOutputString("Suite basicTestSuite generated to _generated/basicTestSuite." . PHP_EOL); + } + + /** + * Tests generating all suites given a set of parsed test data + * @throws \Exception + */ + public function testGenerateAllSuites() + { + $suiteDataArrayBuilder = new SuiteDataArrayBuilder(); + $mockData = $suiteDataArrayBuilder + ->withName('basicTestSuite') + ->withAfterHook() + ->withBeforeHook() + ->includeTests(['simpleTest']) + ->includeGroups(['group1']) + ->build(); + + $testDataArrayBuilder = new TestDataArrayBuilder(); + $mockSimpleTest = $testDataArrayBuilder + ->withName('simpleTest') + ->withTestActions() + ->build(); + + $mockTestData = ['tests' => array_merge($mockSimpleTest)]; + $this->setMockTestAndSuiteParserOutput($mockTestData, $mockData); + + // parse and retrieve suite object with mocked data + $exampleTestManifest = new DefaultTestManifest([], "sample/path"); + $mockSuiteGenerator = SuiteGenerator::getInstance(); + $mockSuiteGenerator->generateAllSuites($exampleTestManifest); + + // assert that expected suites are generated + $this->expectOutputString("Suite basicTestSuite generated to _generated/basicTestSuite." . PHP_EOL); + } + + /** + * Function used to set mock for parser return and force init method to run between tests. + * + * @param array $testData + * @throws \Exception + */ + private function setMockTestAndSuiteParserOutput($testData, $suiteData) + { + $property = new \ReflectionProperty(SuiteGenerator::class, 'SUITE_GENERATOR_INSTANCE'); + $property->setAccessible(true); + $property->setValue(null); + + // clear test object handler value to inject parsed content + $property = new \ReflectionProperty(TestObjectHandler::class, 'testObjectHandler'); + $property->setAccessible(true); + $property->setValue(null); + + // clear suite object handler value to inject parsed content + $property = new \ReflectionProperty(SuiteObjectHandler::class, 'SUITE_OBJECT_HANLDER_INSTANCE'); + $property->setAccessible(true); + $property->setValue(null); + + $mockDataParser = AspectMock::double(TestDataParser::class, ['readTestData' => $testData])->make(); + $mockSuiteDataParser = AspectMock::double(SuiteDataParser::class, ['readSuiteData' => $suiteData])->make(); + $mockGroupClass = AspectMock::double( + GroupClassGenerator::class, + ['generateGroupClass' => 'namespace'] + )->make(); + $mockSuiteClass = AspectMock::double(SuiteGenerator::class, ['generateRelevantGroupTests' => null])->make(); + $instance = AspectMock::double( + ObjectManager::class, + ['create' => function ($clazz) use ( + $mockDataParser, + $mockSuiteDataParser, + $mockGroupClass, + $mockSuiteClass + ) { + if ($clazz == TestDataParser::class) { + return $mockDataParser; + } + if ($clazz == SuiteDataParser::class) { + return $mockSuiteDataParser; + } + if ($clazz == GroupClassGenerator::class) { + return $mockGroupClass; + } + if ($clazz == SuiteGenerator::class) { + return $mockSuiteClass; + } + }] + )->make(); + // bypass the private constructor + AspectMock::double(ObjectManagerFactory::class, ['getObjectManager' => $instance]); + + $property = new \ReflectionProperty(SuiteGenerator::class, 'groupClassGenerator'); + $property->setAccessible(true); + $property->setValue($instance, $instance); + + } +} diff --git a/src/Magento/FunctionalTestingFramework/Suite/SuiteGenerator.php b/src/Magento/FunctionalTestingFramework/Suite/SuiteGenerator.php index 9c96742b4..f2af5cd31 100644 --- a/src/Magento/FunctionalTestingFramework/Suite/SuiteGenerator.php +++ b/src/Magento/FunctionalTestingFramework/Suite/SuiteGenerator.php @@ -74,10 +74,7 @@ public static function getInstance() */ public function generateAllSuites($testManifest) { - $suites = array_keys(SuiteObjectHandler::getInstance()->getAllObjects()); - if ($testManifest != null) { - $suites = $testManifest->getSuiteConfig(); - } + $suites = $testManifest->getSuiteConfig(); foreach ($suites as $suiteName => $suiteContent) { $firstElement = array_values($suiteContent)[0]; @@ -94,37 +91,6 @@ public function generateAllSuites($testManifest) } } - /** - * Returns an array of tests contained within suites as keys pointed at the name of their corresponding suite. - * - * @return array - */ - public function getTestsReferencedInSuites() - { - $testsReferencedInSuites = []; - $suites = SuiteObjectHandler::getInstance()->getAllObjects(); - - // see if we have a specific suite configuration. - if (!empty($this->suiteReferences)) { - $suites = array_intersect_key($suites, $this->suiteReferences); - } - - foreach ($suites as $suite) { - /** @var SuiteObject $suite */ - $test_keys = array_keys($suite->getTests()); - - // see if we need to filter which tests we'll be generating. - if (array_key_exists($suite->getName(), $this->suiteReferences)) { - $test_keys = $this->suiteReferences[$suite->getName()] ?? $test_keys; - } - - $testToSuiteName = array_fill_keys($test_keys, [$suite->getName()]); - $testsReferencedInSuites = array_merge_recursive($testsReferencedInSuites, $testToSuiteName); - } - - return $testsReferencedInSuites; - } - /** * Function which takes a suite name and generates corresponding dir, test files, group class, and updates * yml configuration for group run.