-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathRunTestCommand.php
180 lines (162 loc) · 6.07 KB
/
RunTestCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types = 1);
namespace Magento\FunctionalTestingFramework\Console;
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
use Magento\FunctionalTestingFramework\Util\Path\FilePathFormatter;
use Magento\FunctionalTestingFramework\Util\TestGenerator;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
class RunTestCommand extends BaseGenerateCommand
{
/**
* The return code. Determined by all tests that run.
*
* @var integer
*/
private $returnCode = 0;
/**
* Configures the current command.
*
* @return void
*/
protected function configure()
{
$this->setName("run:test")
->setDescription("generation and execution of test(s) defined in xml")
->addArgument(
'name',
InputArgument::REQUIRED | InputArgument::IS_ARRAY,
"name of tests to generate and execute"
)->addOption(
'skip-generate',
'k',
InputOption::VALUE_NONE,
"skip generation and execute existing test"
);
parent::configure();
}
/**
* Executes the current command.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return integer
* @throws \Exception
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$tests = $input->getArgument('name');
$skipGeneration = $input->getOption('skip-generate');
$force = $input->getOption('force');
$remove = $input->getOption('remove');
$debug = $input->getOption('debug') ?? MftfApplicationConfig::LEVEL_DEVELOPER; // for backward compatibility
$allowSkipped = $input->getOption('allow-skipped');
$verbose = $output->isVerbose();
if ($skipGeneration and $remove) {
// "skip-generate" and "remove" options cannot be used at the same time
throw new TestFrameworkException(
"\"skip-generate\" and \"remove\" options can not be used at the same time."
);
}
// Set application configuration so we can references the user options in our framework
MftfApplicationConfig::create(
$force,
MftfApplicationConfig::EXECUTION_PHASE,
$verbose,
$debug,
$allowSkipped
);
$testConfiguration = $this->getTestAndSuiteConfiguration($tests);
if (!$skipGeneration) {
$command = $this->getApplication()->find('generate:tests');
$args = [
'--tests' => $testConfiguration,
'--force' => $force,
'--remove' => $remove,
'--debug' => $debug,
'--allow-skipped' => $allowSkipped,
'-v' => $verbose
];
$command->run(new ArrayInput($args), $output);
}
$testConfigArray = json_decode($testConfiguration, true);
if (isset($testConfigArray['tests'])) {
$this->runTests($testConfigArray['tests'], $output);
}
if (isset($testConfigArray['suites'])) {
$this->runTestsInSuite($testConfigArray['suites'], $output);
}
return $this->returnCode;
}
/**
* Run tests not referenced in suites
*
* @param array $tests
* @param OutputInterface $output
* @return void
* @throws TestFrameworkException
*/
private function runTests(array $tests, OutputInterface $output)
{
$codeceptionCommand = realpath(PROJECT_ROOT . '/vendor/bin/codecept') . ' run functional ';
$testsDirectory = FilePathFormatter::format(TESTS_MODULE_PATH) .
TestGenerator::GENERATED_DIR .
DIRECTORY_SEPARATOR .
TestGenerator::DEFAULT_DIR .
DIRECTORY_SEPARATOR ;
foreach ($tests as $test) {
$testName = $test . 'Cest.php';
if (!realpath($testsDirectory . $testName)) {
throw new TestFrameworkException(
$testName . " is not available under " . $testsDirectory
);
}
$fullCommand = $codeceptionCommand . $testsDirectory . $testName . ' --verbose --steps';
$this->returnCode = max($this->returnCode, $this->executeTestCommand($fullCommand, $output));
}
}
/**
* Run tests referenced in suites within suites' context.
*
* @param array $suitesConfig
* @param OutputInterface $output
* @return void
*/
private function runTestsInSuite(array $suitesConfig, OutputInterface $output)
{
$codeceptionCommand = realpath(PROJECT_ROOT . '/vendor/bin/codecept') . ' run functional --verbose --steps ';
//for tests in suites, run them as a group to run before and after block
foreach (array_keys($suitesConfig) as $suite) {
$fullCommand = $codeceptionCommand . " -g {$suite}";
$this->returnCode = max($this->returnCode, $this->executeTestCommand($fullCommand, $output));
}
}
/**
* Runs the codeception test command and returns exit code
*
* @param string $command
* @param OutputInterface $output
* @return integer
*
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
private function executeTestCommand(string $command, OutputInterface $output)
{
$process = new Process($command);
$process->setWorkingDirectory(TESTS_BP);
$process->setIdleTimeout(600);
$process->setTimeout(0);
return $process->run(function ($type, $buffer) use ($output) {
$output->write($buffer);
});
}
}