Skip to content

Commit d5ff5f5

Browse files
authored
Merge branch 'develop' into MQE-1729
2 parents 55c30ac + a915bd1 commit d5ff5f5

File tree

3 files changed

+78
-12
lines changed

3 files changed

+78
-12
lines changed

src/Magento/FunctionalTestingFramework/Console/BaseGenerateCommand.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\FunctionalTestingFramework\Util\Filesystem\DirSetupUtil;
1515
use Magento\FunctionalTestingFramework\Util\TestGenerator;
1616
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
17+
use Magento\FunctionalTestingFramework\Suite\Handlers\SuiteObjectHandler;
1718

1819
class BaseGenerateCommand extends Command
1920
{
@@ -67,4 +68,39 @@ protected function removeGeneratedDirectory(OutputInterface $output, bool $verbo
6768
}
6869
}
6970
}
71+
72+
/**
73+
* Returns an array of test configuration to be used as an argument for generation of tests
74+
* @param array $tests
75+
* @return false|string
76+
* @throws \Magento\FunctionalTestingFramework\Exceptions\XmlException
77+
*/
78+
79+
protected function getTestAndSuiteConfiguration(array $tests)
80+
{
81+
$testConfiguration['tests'] = null;
82+
$testConfiguration['suites'] = null;
83+
$testsReferencedInSuites = SuiteObjectHandler::getInstance()->getAllTestReferences();
84+
$suiteToTestPair = [];
85+
86+
foreach($tests as $test) {
87+
if (array_key_exists($test, $testsReferencedInSuites)) {
88+
$suites = $testsReferencedInSuites[$test];
89+
foreach ($suites as $suite) {
90+
$suiteToTestPair[] = "$suite:$test";
91+
}
92+
}
93+
// configuration for tests
94+
else {
95+
$testConfiguration['tests'][] = $test;
96+
}
97+
}
98+
// configuration for suites
99+
foreach ($suiteToTestPair as $pair) {
100+
list($suite, $test) = explode(":", $pair);
101+
$testConfiguration['suites'][$suite][] = $test;
102+
}
103+
$testConfigurationJson = json_encode($testConfiguration);
104+
return $testConfigurationJson;
105+
}
70106
}

src/Magento/FunctionalTestingFramework/Console/GenerateTestsCommand.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
6565
{
6666
$tests = $input->getArgument('name');
6767
$config = $input->getOption('config');
68-
$json = $input->getOption('tests');
68+
$json = $input->getOption('tests'); // for backward compatibility
6969
$force = $input->getOption('force');
7070
$time = $input->getOption('time') * 60 * 1000; // convert from minutes to milliseconds
7171
$debug = $input->getOption('debug') ?? MftfApplicationConfig::LEVEL_DEVELOPER; // for backward compatibility
7272
$remove = $input->getOption('remove');
7373
$verbose = $output->isVerbose();
7474
$allowSkipped = $input->getOption('allowSkipped');
7575

76+
if (!empty($tests)) {
77+
$json = $this->getTestAndSuiteConfiguration($tests);
78+
}
79+
7680
if ($json !== null && !json_decode($json)) {
7781
// stop execution if we have failed to properly parse any json passed in by the user
7882
throw new TestFrameworkException("JSON could not be parsed: " . json_last_error_msg());
@@ -100,9 +104,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
100104
$testManifest->createTestGroups($time);
101105
}
102106

103-
if (empty($tests)) {
104-
SuiteGenerator::getInstance()->generateAllSuites($testManifest);
105-
}
107+
SuiteGenerator::getInstance()->generateAllSuites($testManifest);
106108

107109
$testManifest->generate();
108110

src/Magento/FunctionalTestingFramework/Console/RunTestCommand.php

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,35 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6363
);
6464
}
6565

66+
$testConfiguration = $this->getTestAndSuiteConfiguration($tests);
67+
6668
if (!$skipGeneration) {
6769
$command = $this->getApplication()->find('generate:tests');
6870
$args = [
69-
'--tests' => json_encode([
70-
'tests' => $tests,
71-
'suites' => null
72-
]),
71+
'--tests' => $testConfiguration,
7372
'--force' => $force,
7473
'--remove' => $remove,
7574
'--debug' => $debug,
7675
'--allowSkipped' => $allowSkipped
7776
];
7877
$command->run(new ArrayInput($args), $output);
7978
}
79+
// tests with resolved suite references
80+
$resolvedTests = $this->resolveSuiteReferences($testConfiguration);
8081

81-
$returnCode = 0;
8282
$codeceptionCommand = realpath(PROJECT_ROOT . '/vendor/bin/codecept') . ' run functional ';
8383
$testsDirectory = TESTS_MODULE_PATH . DIRECTORY_SEPARATOR . TestGenerator::GENERATED_DIR . DIRECTORY_SEPARATOR;
84+
$returnCode = 0;
8485
//execute only tests specified as arguments in run command
85-
foreach ($tests as $test) {
86-
$testGroup = TestGenerator::DEFAULT_DIR . DIRECTORY_SEPARATOR;
87-
$testName = $test . 'Cest.php';
86+
foreach ($resolvedTests as $test) {
87+
//set directory as suite name for tests in suite, if not set to "default"
88+
if (strpos($test, ':')) {
89+
list($testGroup, $testName) = explode(":", $test);
90+
} else {
91+
list($testGroup, $testName) = [TestGenerator::DEFAULT_DIR, $test];
92+
}
93+
$testGroup = $testGroup . DIRECTORY_SEPARATOR;
94+
$testName = $testName . 'Cest.php';
8895
if (!realpath($testsDirectory . $testGroup . $testName)) {
8996
throw new TestFrameworkException(
9097
$testName . " is not available under " . $testsDirectory . $testGroup
@@ -104,4 +111,25 @@ function ($type, $buffer) use ($output) {
104111
}
105112
return $returnCode;
106113
}
114+
115+
/**
116+
* Get an array of tests with resolved suite references from $testConfiguration
117+
* eg: if test is referenced in a suite, it'll be stored in format suite:test
118+
* @param string $testConfigurationJson
119+
* @return array
120+
*/
121+
private function resolveSuiteReferences($testConfigurationJson)
122+
{
123+
$testConfiguration = json_decode($testConfigurationJson, true);
124+
$testsArray = $testConfiguration['tests'] ?? [];
125+
$suitesArray = $testConfiguration['suites'] ?? [];
126+
$testArrayBuilder = [];
127+
128+
foreach ($suitesArray as $suite => $tests) {
129+
foreach ($tests as $test) {
130+
$testArrayBuilder[] = "$suite:$test";
131+
}
132+
}
133+
return array_merge($testArrayBuilder, $testsArray);
134+
}
107135
}

0 commit comments

Comments
 (0)