-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathStaticChecksCommand.php
207 lines (190 loc) · 7.11 KB
/
StaticChecksCommand.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
<?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\StaticCheck\StaticCheckInterface;
use Magento\FunctionalTestingFramework\StaticCheck\StaticChecksList;
use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidArgumentException;
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 Exception;
use Symfony\Component\Console\Style\SymfonyStyle;
class StaticChecksCommand extends Command
{
/**
* Associative array containing static ruleset properties.
*
* @var array
*/
private $ruleSet;
/**
* Pool of all existing static check objects
*
* @var StaticCheckInterface[]
*/
private $allStaticCheckObjects;
/**
* Static checks to run
*
* @var StaticCheckInterface[]
*/
private $staticCheckObjects;
/**
* Console output style
*
* @var SymfonyStyle
*/
protected $ioStyle;
/**
* Configures the current command.
*
* @return void
*/
protected function configure()
{
$list = new StaticChecksList();
$this->allStaticCheckObjects = $list->getStaticChecks();
$staticCheckNames = implode(', ', array_keys($this->allStaticCheckObjects));
$description = 'This command will run all static checks on xml test materials. '
. 'Available static check scripts are:' . PHP_EOL . $staticCheckNames;
$this->setName('static-checks')
->setDescription($description)
->addArgument(
'names',
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
'name(s) of specific static check script(s) to run'
)->addOption(
'path',
'p',
InputOption::VALUE_OPTIONAL,
'Path to a MFTF test module to run "deprecatedEntityUsage" static check script. ' . PHP_EOL
. 'Option is ignored by other static check scripts.' . PHP_EOL
);
}
/**
* Run required static check scripts
*
* @param InputInterface $input
* @param OutputInterface $output
* @return int
* @throws Exception
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->ioStyle = new SymfonyStyle($input, $output);
try {
$this->validateInput($input);
} catch (InvalidArgumentException $e) {
LoggingUtil::getInstance()->getLogger(StaticChecksCommand::class)->error($e->getMessage());
$this->ioStyle->error($e->getMessage() . ' Please fix input argument(s) or option(s) and rerun.');
return 1;
}
$cmdFailed = false;
$errors = [];
foreach ($this->staticCheckObjects as $name => $staticCheck) {
LoggingUtil::getInstance()->getLogger(get_class($staticCheck))->info(
'Running static check script for: ' . $name . PHP_EOL
);
$this->ioStyle->text(PHP_EOL . 'Running static check script for: ' . $name . PHP_EOL);
$start = microtime(true);
try {
$staticCheck->execute($input);
} catch (Exception $e) {
$cmdFailed = true;
LoggingUtil::getInstance()->getLogger(get_class($staticCheck))->error($e->getMessage() . PHP_EOL);
$this->ioStyle->error($e->getMessage());
}
$end = microtime(true);
$errors += $staticCheck->getErrors();
$staticOutput = $staticCheck->getOutput();
LoggingUtil::getInstance()->getLogger(get_class($staticCheck))->info($staticOutput);
$this->ioStyle->text($staticOutput);
$this->ioStyle->text('Total execution time is ' . (string)($end - $start) . ' seconds.' . PHP_EOL);
}
if (!$cmdFailed && empty($errors)) {
return 0;
} else {
return 1;
}
}
/**
* Validate input arguments
*
* @param InputInterface $input
* @return void
* @throws InvalidArgumentException
*/
private function validateInput(InputInterface $input)
{
$this->staticCheckObjects = [];
$requiredChecksNames = $input->getArgument('names');
// Build list of static check names to run.
if (empty($requiredChecksNames)) {
$this->parseRulesetJson();
$requiredChecksNames = $this->ruleSet['tests'] ?? null;
}
if (empty($requiredChecksNames)) {
$this->staticCheckObjects = $this->allStaticCheckObjects;
} else {
$this->validateTestNames($requiredChecksNames);
}
if ($input->getOption('path')) {
if ( (count($this->staticCheckObjects) !== 1)
|| array_keys($this->staticCheckObjects)[0] !== StaticChecksList::DEPRECATED_ENTITY_USAGE_CHECK_NAME )
throw new InvalidArgumentException(
'--path option can only be used for "'
. StaticChecksList::DEPRECATED_ENTITY_USAGE_CHECK_NAME
. '".'
);
}
}
/**
* Validates that all passed in static-check names match an existing static check
* @param string[] $requiredChecksNames
* @return void
*/
private function validateTestNames($requiredChecksNames)
{
$invalidCheckNames = [];
for ($index = 0; $index < count($requiredChecksNames); $index++) {
if (in_array($requiredChecksNames[$index], array_keys($this->allStaticCheckObjects))) {
$this->staticCheckObjects[$requiredChecksNames[$index]] =
$this->allStaticCheckObjects[$requiredChecksNames[$index]];
} else {
$invalidCheckNames[] = $requiredChecksNames[$index];
}
}
if (!empty($invalidCheckNames)) {
throw new InvalidArgumentException(
'Invalid static check script(s): ' . implode(', ', $invalidCheckNames) . '.'
);
}
}
/**
* Parses and sets local ruleSet. If not found, simply returns and lets script continue.
* @return void;
*/
private function parseRulesetJson()
{
$pathAddition = "/dev/tests/acceptance/";
// MFTF is both NOT attached and no MAGENTO_BP defined in .env
if (MAGENTO_BP === FW_BP) {
$pathAddition = "/dev/";
}
$pathToRuleset = MAGENTO_BP . $pathAddition . "staticRuleset.json";
if (!file_exists($pathToRuleset)) {
$this->ioStyle->text("No ruleset under $pathToRuleset" . PHP_EOL);
return;
}
$this->ioStyle->text("Using ruleset under $pathToRuleset" . PHP_EOL);
$this->ruleSet = json_decode(file_get_contents($pathToRuleset), true);
}
}