-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathGenerateTestsCommand.php
178 lines (153 loc) · 6.59 KB
/
GenerateTestsCommand.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
<?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\Exceptions\TestFrameworkException;
use Magento\FunctionalTestingFramework\Suite\SuiteGenerator;
use Magento\FunctionalTestingFramework\Test\Handlers\TestObjectHandler;
use Magento\FunctionalTestingFramework\Util\Manifest\ParallelTestManifest;
use Magento\FunctionalTestingFramework\Util\Manifest\TestManifestFactory;
use Magento\FunctionalTestingFramework\Util\TestGenerator;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class GenerateTestsCommand extends BaseGenerateCommand
{
/**
* Configures the current command.
*
* @return void
*/
protected function configure()
{
$this->setName('generate:tests')
->setDescription('Run validation and generate all test files and suites based on xml declarations')
->addArgument(
'name',
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
'name(s) of specific tests to generate'
)->addOption("config", 'c', InputOption::VALUE_REQUIRED, 'default, singleRun, or parallel', 'default')
->addOption(
'time',
'i',
InputOption::VALUE_REQUIRED,
'Used in combination with a parallel configuration, determines desired group size (in minutes)',
10
)->addOption(
'tests',
't',
InputOption::VALUE_REQUIRED,
'A parameter accepting a JSON string used to determine the test configuration'
);
parent::configure();
}
/**
* Executes the current command.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return void
* @throws TestFrameworkException
* @throws \Magento\FunctionalTestingFramework\Exceptions\TestReferenceException
* @throws \Magento\FunctionalTestingFramework\Exceptions\XmlException
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$tests = $input->getArgument('name');
$config = $input->getOption('config');
$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
$remove = $input->getOption('remove');
$verbose = $output->isVerbose();
$allowSkipped = $input->getOption('allow-skipped');
// Set application configuration so we can references the user options in our framework
MftfApplicationConfig::create(
$force,
MftfApplicationConfig::GENERATION_PHASE,
$verbose,
$debug,
$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());
}
if ($config === 'parallel' && $time <= 0) {
// stop execution if the user has given us an invalid argument for time argument during parallel generation
throw new TestFrameworkException("time option cannot be less than or equal to 0");
}
// Remove previous GENERATED_DIR if --remove option is used
if ($remove) {
$this->removeGeneratedDirectory($output, $verbose ||
($debug !== MftfApplicationConfig::LEVEL_NONE));
}
$testConfiguration = $this->createTestConfiguration($json, $tests);
// create our manifest file here
$testManifest = TestManifestFactory::makeManifest($config, $testConfiguration['suites']);
TestGenerator::getInstance(null, $testConfiguration['tests'])->createAllTestFiles($testManifest);
if ($config == 'parallel') {
/** @var ParallelTestManifest $testManifest */
$testManifest->createTestGroups($time);
}
SuiteGenerator::getInstance()->generateAllSuites($testManifest);
$testManifest->generate();
$output->writeln("Generate Tests Command Run");
}
/**
* Function which builds up a configuration including test and suites for consumption of Magento generation methods.
*
* @param string $json
* @param array $tests
* @return array
* @throws \Magento\FunctionalTestingFramework\Exceptions\TestReferenceException
* @throws \Magento\FunctionalTestingFramework\Exceptions\XmlException
*/
private function createTestConfiguration(
$json,
array $tests
) {
$testConfiguration = [];
$testConfiguration['tests'] = $tests;
$testConfiguration['suites'] = [];
$testConfiguration = $this->parseTestsConfigJson($json, $testConfiguration);
// if we have references to specific tests, we resolve the test objects and pass them to the config
if (!empty($testConfiguration['tests'])) {
$testObjects = [];
foreach ($testConfiguration['tests'] as $test) {
$testObjects[$test] = TestObjectHandler::getInstance()->getObject($test);
}
$testConfiguration['tests'] = $testObjects;
}
return $testConfiguration;
}
/**
* Function which takes a json string of potential custom configuration and parses/validates the resulting json
* passed in by the user. The result is a testConfiguration array.
*
* @param string $json
* @param array $testConfiguration
* @return array
*/
private function parseTestsConfigJson($json, array $testConfiguration)
{
if ($json === null) {
return $testConfiguration;
}
$jsonTestConfiguration = [];
$testConfigArray = json_decode($json, true);
$jsonTestConfiguration['tests'] = $testConfigArray['tests'] ?? null;
;
$jsonTestConfiguration['suites'] = $testConfigArray['suites'] ?? null;
return $jsonTestConfiguration;
}
}