|
19 | 19 |
|
20 | 20 | class RunTestCommand extends BaseGenerateCommand
|
21 | 21 | {
|
| 22 | + /** |
| 23 | + * The return code. Determined by all tests that run. |
| 24 | + * |
| 25 | + * @var integer |
| 26 | + */ |
| 27 | + private $returnCode = 0; |
| 28 | + |
22 | 29 | /**
|
23 | 30 | * Configures the current command.
|
24 | 31 | *
|
@@ -76,60 +83,77 @@ protected function execute(InputInterface $input, OutputInterface $output): int
|
76 | 83 | ];
|
77 | 84 | $command->run(new ArrayInput($args), $output);
|
78 | 85 | }
|
79 |
| - // tests with resolved suite references |
80 |
| - $resolvedTests = $this->resolveSuiteReferences($testConfiguration); |
81 | 86 |
|
| 87 | + $testConfigArray = json_decode($testConfiguration, true); |
| 88 | + |
| 89 | + // run tests not referenced in suites |
| 90 | + $this->runTests($testConfigArray['tests'], $output); |
| 91 | + |
| 92 | + // run tests in suites |
| 93 | + $this->runTestsInSuite($testConfigArray['suites'], $output); |
| 94 | + |
| 95 | + return $this->returnCode; |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Run tests not referenced in suites |
| 101 | + * @param array $testsConfig |
| 102 | + * @param OutputInterface $output |
| 103 | + * @throws TestFrameworkException |
| 104 | + */ |
| 105 | + private function runTests($testsConfig, OutputInterface $output) { |
| 106 | + |
| 107 | + |
| 108 | + $tests = $testsConfig ?? []; |
82 | 109 | $codeceptionCommand = realpath(PROJECT_ROOT . '/vendor/bin/codecept') . ' run functional ';
|
83 |
| - $testsDirectory = TESTS_MODULE_PATH . DIRECTORY_SEPARATOR . TestGenerator::GENERATED_DIR . DIRECTORY_SEPARATOR; |
84 |
| - $returnCode = 0; |
85 |
| - //execute only tests specified as arguments in run command |
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'; |
95 |
| - if (!realpath($testsDirectory . $testGroup . $testName)) { |
| 110 | + $testsDirectory = TESTS_MODULE_PATH . |
| 111 | + DIRECTORY_SEPARATOR . |
| 112 | + TestGenerator::GENERATED_DIR . |
| 113 | + DIRECTORY_SEPARATOR . |
| 114 | + TestGenerator::DEFAULT_DIR . |
| 115 | + DIRECTORY_SEPARATOR ; |
| 116 | + |
| 117 | + foreach ($tests as $test) { |
| 118 | + $testName = $test . 'Cest.php'; |
| 119 | + if (!realpath($testsDirectory . $testName)) { |
96 | 120 | throw new TestFrameworkException(
|
97 |
| - $testName . " is not available under " . $testsDirectory . $testGroup |
| 121 | + $testName . " is not available under " . $testsDirectory |
98 | 122 | );
|
99 | 123 | }
|
100 |
| - $fullCommand = $codeceptionCommand . $testsDirectory . $testGroup . $testName . ' --verbose --steps'; |
| 124 | + $fullCommand = $codeceptionCommand . $testsDirectory . $testName . ' --verbose --steps'; |
101 | 125 | $process = new Process($fullCommand);
|
102 | 126 | $process->setWorkingDirectory(TESTS_BP);
|
103 | 127 | $process->setIdleTimeout(600);
|
104 | 128 | $process->setTimeout(0);
|
105 |
| - |
106 |
| - $returnCode = max($returnCode, $process->run( |
107 |
| - function ($type, $buffer) use ($output) { |
108 |
| - $output->write($buffer); |
109 |
| - } |
110 |
| - )); |
| 129 | + $subReturnCode = $process->run(function ($type, $buffer) use ($output) { |
| 130 | + $output->write($buffer); |
| 131 | + }); |
| 132 | + $this->returnCode = max($this->returnCode, $subReturnCode); |
111 | 133 | }
|
112 |
| - return $returnCode; |
113 | 134 | }
|
114 | 135 |
|
115 | 136 | /**
|
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 |
| 137 | + * Run tests referenced in suites within suites' context. |
| 138 | + * @param array $suitesConfig |
| 139 | + * @param OutputInterface $output |
120 | 140 | */
|
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 |
| - } |
| 141 | + private function runTestsInSuite($suitesConfig, OutputInterface $output) { |
| 142 | + |
| 143 | + $suites = $suitesConfig ?? []; |
| 144 | + $codeceptionCommand = realpath(PROJECT_ROOT . '/vendor/bin/codecept') . ' run functional --verbose --steps '; |
| 145 | + $testGroups = array_keys($suites); |
| 146 | + //for tests in suites, run them as a group to run before and after block |
| 147 | + foreach ($testGroups as $testGroup) { |
| 148 | + $fullCommand = $codeceptionCommand . " -g {$testGroup}"; |
| 149 | + $process = new Process($fullCommand); |
| 150 | + $process->setWorkingDirectory(TESTS_BP); |
| 151 | + $process->setIdleTimeout(600); |
| 152 | + $process->setTimeout(0); |
| 153 | + $subReturnCode = $process->run(function ($type, $buffer) use ($output) { |
| 154 | + $output->write($buffer); |
| 155 | + }); |
| 156 | + $this->returnCode = max($this->returnCode, $subReturnCode); |
132 | 157 | }
|
133 |
| - return array_merge($testArrayBuilder, $testsArray); |
134 | 158 | }
|
135 | 159 | }
|
0 commit comments