forked from magento/magento2-functional-testing-framework
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaseGenerateCommand.php
269 lines (243 loc) · 8.26 KB
/
BaseGenerateCommand.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
// @codingStandardsIgnoreFile
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types = 1);
namespace Magento\FunctionalTestingFramework\Console;
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
use Magento\FunctionalTestingFramework\Test\Handlers\TestObjectHandler;
use Magento\FunctionalTestingFramework\Util\Path\FilePathFormatter;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\OutputInterface;
use Magento\FunctionalTestingFramework\Util\Filesystem\DirSetupUtil;
use Magento\FunctionalTestingFramework\Util\TestGenerator;
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
use Magento\FunctionalTestingFramework\Suite\Handlers\SuiteObjectHandler;
use Symfony\Component\Console\Style\SymfonyStyle;
/**
* Class BaseGenerateCommand
* @package Magento\FunctionalTestingFramework\Console
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class BaseGenerateCommand extends Command
{
const MFTF_NOTICES = "Placeholder text for MFTF notices\n";
const CODECEPT_RUN = 'codecept:run ';
const CODECEPT_RUN_FUNCTIONAL = self::CODECEPT_RUN . 'functional ';
/**
* Enable pause()
*
* @var boolean
*/
private $enablePause = null;
/**
* Console output style
*
* @var SymfonyStyle
*/
protected $ioStyle = null;
/**
* Configures the base command.
*
* @return void
*/
protected function configure()
{
$this->addOption(
'remove',
'r',
InputOption::VALUE_NONE,
'remove previous generated suites and tests'
)->addOption(
"force",
'f',
InputOption::VALUE_NONE,
'force generation and running of tests regardless of Magento Instance Configuration'
)->addOption(
"allow-skipped",
'a',
InputOption::VALUE_NONE,
'Allows MFTF to generate and run skipped tests.'
)->addOption(
'debug',
'd',
InputOption::VALUE_OPTIONAL,
'Run extra validation when generating and running tests.',
MftfApplicationConfig::LEVEL_DEFAULT
);
}
/**
* Remove GENERATED_DIR if exists when running generate:tests.
*
* @param OutputInterface $output
* @param bool $verbose
* @return void
* @throws TestFrameworkException
*/
protected function removeGeneratedDirectory(OutputInterface $output, bool $verbose)
{
$generatedDirectory = FilePathFormatter::format(TESTS_MODULE_PATH) . TestGenerator::GENERATED_DIR;
if (file_exists($generatedDirectory)) {
DirSetupUtil::rmdirRecursive($generatedDirectory);
if ($verbose) {
$output->writeln("removed files and directory $generatedDirectory");
}
}
}
/**
* 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 (strpos($test, ':') !== false) {
$suiteToTestPair[] = $test;
continue;
}
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;
}
/**
* Returns an array of test configuration to be used as an argument for generation of tests
* This function uses group or suite names for generation
* @return false|string
* @throws \Magento\FunctionalTestingFramework\Exceptions\XmlException
*/
protected function getGroupAndSuiteConfiguration(array $groupOrSuiteNames)
{
$result['tests'] = [];
$result['suites'] = [];
$groups = [];
$suites = [];
$allSuites = SuiteObjectHandler::getInstance()->getAllObjects();
$testsInSuites = SuiteObjectHandler::getInstance()->getAllTestReferences();
foreach ($groupOrSuiteNames as $groupOrSuiteName) {
if (array_key_exists($groupOrSuiteName, $allSuites)) {
$suites[] = $groupOrSuiteName;
} else {
$groups[] = $groupOrSuiteName;
}
}
foreach ($suites as $suite) {
$result['suites'][$suite] = [];
}
foreach ($groups as $group) {
$testsInGroup = TestObjectHandler::getInstance()->getTestsByGroup($group);
$testsInGroupAndNotInAnySuite = array_diff(
array_keys($testsInGroup),
array_keys($testsInSuites)
);
$testsInGroupAndInAnySuite = array_diff(
array_keys($testsInGroup),
$testsInGroupAndNotInAnySuite
);
foreach ($testsInGroupAndInAnySuite as $testInGroupAndInAnySuite) {
$suiteName = $testsInSuites[$testInGroupAndInAnySuite][0];
if (array_search($suiteName, $suites) !== false) {
// Suite is already being called to run in its entirety, do not filter list
continue;
}
$result['suites'][$suiteName][] = $testInGroupAndInAnySuite;
}
$result['tests'] = array_merge(
$result['tests'],
$testsInGroupAndNotInAnySuite
);
}
if (empty($result['tests'])) {
$result['tests'] = null;
}
if (empty($result['suites'])) {
$result['suites'] = null;
}
$json = json_encode($result);
return $json;
}
/**
* Set Symfony IO Style
*
* @param InputInterface $input
* @param OutputInterface $output
* @return void
*/
protected function setIOStyle(InputInterface $input, OutputInterface $output)
{
// For IO style
if (null === $this->ioStyle) {
$this->ioStyle = new SymfonyStyle($input, $output);
}
}
/**
* Show predefined global notice messages
*
* @param OutputInterface $output
* @return void
*/
protected function showMftfNotices(OutputInterface $output)
{
if (null !== $this->ioStyle) {
$this->ioStyle->note(self::MFTF_NOTICES);
} else {
$output->writeln(self::MFTF_NOTICES);
}
}
/**
* Return if pause() is enabled
*
* @return boolean
*/
protected function pauseEnabled()
{
if (null === $this->enablePause) {
if (getenv('ENABLE_PAUSE') === 'true') {
$this->enablePause = true;
} else {
$this->enablePause = false;
}
}
return $this->enablePause;
}
/**
* Runs the bin/mftf codecept:run command and returns exit code
*
* @param string $commandStr
* @param OutputInterface $output
* @return integer
* @throws \Exception
*/
protected function codeceptRunTest(string $commandStr, OutputInterface $output)
{
$input = new StringInput($commandStr);
$command = $this->getApplication()->find(self::CODECEPT_RUN);
return $command->run($input, $output);
}
}