diff --git a/src/Magento/FunctionalTestingFramework/Console/BaseGenerateCommand.php b/src/Magento/FunctionalTestingFramework/Console/BaseGenerateCommand.php index 75c521379..40582db86 100644 --- a/src/Magento/FunctionalTestingFramework/Console/BaseGenerateCommand.php +++ b/src/Magento/FunctionalTestingFramework/Console/BaseGenerateCommand.php @@ -14,6 +14,7 @@ use Magento\FunctionalTestingFramework\Util\Filesystem\DirSetupUtil; use Magento\FunctionalTestingFramework\Util\TestGenerator; use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig; +use Magento\FunctionalTestingFramework\Suite\Handlers\SuiteObjectHandler; class BaseGenerateCommand extends Command { @@ -67,4 +68,39 @@ protected function removeGeneratedDirectory(OutputInterface $output, bool $verbo } } } + + /** + * Returns an array of test configuration to be used as an argument for generation of tests + * @param array $tests + * @return false|string + * @throws \Magento\FunctionalTestingFramework\Exceptions\XmlException + */ + + protected function getTestAndSuiteConfiguration(array $tests) + { + $testConfiguration['tests'] = null; + $testConfiguration['suites'] = null; + $testsReferencedInSuites = SuiteObjectHandler::getInstance()->getAllTestReferences(); + $suiteToTestPair = []; + + foreach($tests as $test) { + if (array_key_exists($test, $testsReferencedInSuites)) { + $suites = $testsReferencedInSuites[$test]; + foreach ($suites as $suite) { + $suiteToTestPair[] = "$suite:$test"; + } + } + // configuration for tests + else { + $testConfiguration['tests'][] = $test; + } + } + // configuration for suites + foreach ($suiteToTestPair as $pair) { + list($suite, $test) = explode(":", $pair); + $testConfiguration['suites'][$suite][] = $test; + } + $testConfigurationJson = json_encode($testConfiguration); + return $testConfigurationJson; + } } diff --git a/src/Magento/FunctionalTestingFramework/Console/GenerateTestsCommand.php b/src/Magento/FunctionalTestingFramework/Console/GenerateTestsCommand.php index ca9d6e3cc..c410973c7 100644 --- a/src/Magento/FunctionalTestingFramework/Console/GenerateTestsCommand.php +++ b/src/Magento/FunctionalTestingFramework/Console/GenerateTestsCommand.php @@ -65,7 +65,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { $tests = $input->getArgument('name'); $config = $input->getOption('config'); - $json = $input->getOption('tests'); + $json = $input->getOption('tests'); // for backward compatibility $force = $input->getOption('force'); $time = $input->getOption('time') * 60 * 1000; // convert from minutes to milliseconds $debug = $input->getOption('debug') ?? MftfApplicationConfig::LEVEL_DEVELOPER; // for backward compatibility @@ -73,6 +73,10 @@ protected function execute(InputInterface $input, OutputInterface $output) $verbose = $output->isVerbose(); $allowSkipped = $input->getOption('allowSkipped'); + if (!empty($tests)) { + $json = $this->getTestAndSuiteConfiguration($tests); + } + if ($json !== null && !json_decode($json)) { // stop execution if we have failed to properly parse any json passed in by the user throw new TestFrameworkException("JSON could not be parsed: " . json_last_error_msg()); @@ -100,9 +104,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $testManifest->createTestGroups($time); } - if (empty($tests)) { - SuiteGenerator::getInstance()->generateAllSuites($testManifest); - } + SuiteGenerator::getInstance()->generateAllSuites($testManifest); $testManifest->generate(); diff --git a/src/Magento/FunctionalTestingFramework/Console/RunTestCommand.php b/src/Magento/FunctionalTestingFramework/Console/RunTestCommand.php index b17131459..e1e30589d 100644 --- a/src/Magento/FunctionalTestingFramework/Console/RunTestCommand.php +++ b/src/Magento/FunctionalTestingFramework/Console/RunTestCommand.php @@ -63,13 +63,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int ); } + $testConfiguration = $this->getTestAndSuiteConfiguration($tests); + if (!$skipGeneration) { $command = $this->getApplication()->find('generate:tests'); $args = [ - '--tests' => json_encode([ - 'tests' => $tests, - 'suites' => null - ]), + '--tests' => $testConfiguration, '--force' => $force, '--remove' => $remove, '--debug' => $debug, @@ -77,14 +76,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int ]; $command->run(new ArrayInput($args), $output); } + // tests with resolved suite references + $resolvedTests = $this->resolveSuiteReferences($testConfiguration); - $returnCode = 0; $codeceptionCommand = realpath(PROJECT_ROOT . '/vendor/bin/codecept') . ' run functional '; $testsDirectory = TESTS_MODULE_PATH . DIRECTORY_SEPARATOR . TestGenerator::GENERATED_DIR . DIRECTORY_SEPARATOR; + $returnCode = 0; //execute only tests specified as arguments in run command - foreach ($tests as $test) { - $testGroup = TestGenerator::DEFAULT_DIR . DIRECTORY_SEPARATOR; - $testName = $test . 'Cest.php'; + foreach ($resolvedTests as $test) { + //set directory as suite name for tests in suite, if not set to "default" + if (strpos($test, ':')) { + list($testGroup, $testName) = explode(":", $test); + } else { + list($testGroup, $testName) = [TestGenerator::DEFAULT_DIR, $test]; + } + $testGroup = $testGroup . DIRECTORY_SEPARATOR; + $testName = $testName . 'Cest.php'; if (!realpath($testsDirectory . $testGroup . $testName)) { throw new TestFrameworkException( $testName . " is not available under " . $testsDirectory . $testGroup @@ -104,4 +111,25 @@ function ($type, $buffer) use ($output) { } return $returnCode; } + + /** + * Get an array of tests with resolved suite references from $testConfiguration + * eg: if test is referenced in a suite, it'll be stored in format suite:test + * @param string $testConfigurationJson + * @return array + */ + private function resolveSuiteReferences($testConfigurationJson) + { + $testConfiguration = json_decode($testConfigurationJson, true); + $testsArray = $testConfiguration['tests'] ?? []; + $suitesArray = $testConfiguration['suites'] ?? []; + $testArrayBuilder = []; + + foreach ($suitesArray as $suite => $tests) { + foreach ($tests as $test) { + $testArrayBuilder[] = "$suite:$test"; + } + } + return array_merge($testArrayBuilder, $testsArray); + } }