Skip to content

Commit b1a100c

Browse files
committed
MQE-1959: Static-checks command can be configured
1 parent afcae2c commit b1a100c

File tree

1 file changed

+69
-9
lines changed

1 file changed

+69
-9
lines changed

src/Magento/FunctionalTestingFramework/Console/StaticChecksCommand.php

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,31 @@
88

99
namespace Magento\FunctionalTestingFramework\Console;
1010

11+
use Magento\FunctionalTestingFramework\StaticCheck\StaticCheckInterface;
1112
use Magento\FunctionalTestingFramework\StaticCheck\StaticChecksList;
12-
use Magento\FunctionalTestingFramework\StaticCheck\StaticCheckListInterface;
1313
use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil;
1414
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Exception\InvalidArgumentException;
16+
use Symfony\Component\Console\Input\InputArgument;
1517
use Symfony\Component\Console\Input\InputInterface;
1618
use Symfony\Component\Console\Output\OutputInterface;
1719
use Exception;
1820

1921
class StaticChecksCommand extends Command
2022
{
2123
/**
22-
* Pool of static check scripts to run
24+
* Pool of all existing static check objects
2325
*
24-
* @var StaticCheckListInterface
26+
* @var StaticCheckInterface[]
2527
*/
26-
private $staticChecksList;
28+
private $allStaticCheckObjects;
29+
30+
/**
31+
* Static checks to run
32+
*
33+
* @var StaticCheckInterface[]
34+
*/
35+
private $staticCheckObjects;
2736

2837
/**
2938
* Configures the current command.
@@ -32,13 +41,22 @@ class StaticChecksCommand extends Command
3241
*/
3342
protected function configure()
3443
{
44+
$list = new StaticChecksList();
45+
$this->allStaticCheckObjects = $list->getStaticChecks();
46+
$staticCheckNames = implode(', ', array_keys($this->allStaticCheckObjects));
47+
$description = "This command will run all static checks on xml test materials. "
48+
. "Available static check scripts are:\n{$staticCheckNames}";
3549
$this->setName('static-checks')
36-
->setDescription('This command will run all static checks on xml test materials.');
37-
$this->staticChecksList = new StaticChecksList();
50+
->setDescription($description)
51+
->addArgument(
52+
'names',
53+
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
54+
'name(s) of specific static check script(s) to run'
55+
);
3856
}
3957

4058
/**
41-
*
59+
* Run required static check scripts
4260
*
4361
* @param InputInterface $input
4462
* @param OutputInterface $output
@@ -47,11 +65,23 @@ protected function configure()
4765
*/
4866
protected function execute(InputInterface $input, OutputInterface $output)
4967
{
50-
$staticCheckObjects = $this->staticChecksList->getStaticChecks();
68+
try {
69+
$this->validateInputArguments($input, $output);
70+
} catch (InvalidArgumentException $e) {
71+
LoggingUtil::getInstance()->getLogger(StaticChecksCommand::class)->error($e->getMessage());
72+
$output->writeln($e->getMessage() . " Please fix input arguments and rerun.");
73+
return 1;
74+
}
5175

5276
$errors = [];
77+
foreach ($this->staticCheckObjects as $name => $staticCheck) {
78+
LoggingUtil::getInstance()->getLogger(get_class($staticCheck))->info(
79+
'Running static check script for: ' . $name
80+
);
81+
$output->writeln(
82+
'Running static check script for: ' . $name
83+
);
5384

54-
foreach ($staticCheckObjects as $staticCheck) {
5585
$staticCheck->execute($input);
5686

5787
$staticOutput = $staticCheck->getOutput();
@@ -66,4 +96,34 @@ protected function execute(InputInterface $input, OutputInterface $output)
6696
return 1;
6797
}
6898
}
99+
100+
/**
101+
* Validate input arguments
102+
*
103+
* @param InputInterface $input
104+
* @return void
105+
* @throws InvalidArgumentException
106+
*/
107+
private function validateInputArguments(InputInterface $input)
108+
{
109+
$this->staticCheckObjects = [];
110+
$requiredChecksNames = $input->getArgument('names');
111+
// Found user required static check script(s) to run,
112+
// If no static check name is supplied, run all static check scripts
113+
if (empty($requiredChecksNames)) {
114+
$this->staticCheckObjects = $this->allStaticCheckObjects;
115+
} else {
116+
foreach ($requiredChecksNames as $checksName) {
117+
if (in_array($checksName, $this->allStaticCheckObjects)) {
118+
$this->staticCheckObjects[$checksName] = $this->allStaticCheckObjects[$checksName];
119+
unset($requiredChecksNames[$checksName]);
120+
}
121+
}
122+
}
123+
124+
$invalidCheckNames = implode(', ', $requiredChecksNames);
125+
if (!empty($invalidCheckNames)) {
126+
throw new InvalidArgumentException("Invalid static check script(s): {$invalidCheckNames}.");
127+
}
128+
}
69129
}

0 commit comments

Comments
 (0)