Skip to content

Commit 8448009

Browse files
committed
MQE-1957: Entity Deprecation Reference - Static Check
1 parent 4debae6 commit 8448009

File tree

4 files changed

+82
-28
lines changed

4 files changed

+82
-28
lines changed

src/Magento/FunctionalTestingFramework/Console/StaticChecksCommand.php

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Console\Input\InputOption;
1919
use Symfony\Component\Console\Output\OutputInterface;
2020
use Exception;
21+
use Symfony\Component\Console\Style\SymfonyStyle;
2122

2223
class StaticChecksCommand extends Command
2324
{
@@ -35,6 +36,13 @@ class StaticChecksCommand extends Command
3536
*/
3637
private $staticCheckObjects;
3738

39+
/**
40+
* Console output style
41+
*
42+
* @var SymfonyStyle
43+
*/
44+
protected $ioStyle;
45+
3846
/**
3947
* Configures the current command.
4048
*
@@ -45,8 +53,8 @@ protected function configure()
4553
$list = new StaticChecksList();
4654
$this->allStaticCheckObjects = $list->getStaticChecks();
4755
$staticCheckNames = implode(', ', array_keys($this->allStaticCheckObjects));
48-
$description = "This command will run all static checks on xml test materials. "
49-
. "Available static check scripts are:\n{$staticCheckNames}";
56+
$description = 'This command will run all static checks on xml test materials. '
57+
. 'Available static check scripts are:' . PHP_EOL . $staticCheckNames;
5058
$this->setName('static-checks')
5159
->setDescription($description)
5260
->addArgument(
@@ -72,36 +80,41 @@ protected function configure()
7280
*/
7381
protected function execute(InputInterface $input, OutputInterface $output)
7482
{
83+
$this->ioStyle = new SymfonyStyle($input, $output);
7584
try {
76-
$this->validateInputArguments($input);
85+
$this->validateInput($input);
7786
} catch (InvalidArgumentException $e) {
7887
LoggingUtil::getInstance()->getLogger(StaticChecksCommand::class)->error($e->getMessage());
79-
$output->writeln($e->getMessage() . " Please fix input arguments and rerun.");
88+
$this->ioStyle->error($e->getMessage() . ' Please fix input argument(s) or option(s) and rerun.');
8089
return 1;
8190
}
8291

92+
$cmdFailed = false;
8393
$errors = [];
8494
foreach ($this->staticCheckObjects as $name => $staticCheck) {
8595
LoggingUtil::getInstance()->getLogger(get_class($staticCheck))->info(
86-
"\nRunning static check script for: " . $name
87-
);
88-
$output->writeln(
89-
"\nRunning static check script for: " . $name
96+
'Running static check script for: ' . $name . PHP_EOL
9097
);
98+
99+
$this->ioStyle->text(PHP_EOL . 'Running static check script for: ' . $name . PHP_EOL);
91100
$start = microtime(true);
92-
$staticCheck->execute($input);
101+
try {
102+
$staticCheck->execute($input);
103+
} catch (Exception $e) {
104+
$cmdFailed = true;
105+
LoggingUtil::getInstance()->getLogger(get_class($staticCheck))->error($e->getMessage() . PHP_EOL);
106+
$this->ioStyle->error($e->getMessage());
107+
}
93108
$end = microtime(true);
109+
$errors += $staticCheck->getErrors();
94110

95111
$staticOutput = $staticCheck->getOutput();
96112
LoggingUtil::getInstance()->getLogger(get_class($staticCheck))->info($staticOutput);
97-
$output->writeln($staticOutput);
98-
$errors += $staticCheck->getErrors();
99-
$output->writeln(
100-
"\nTotal execution time is " . (string)($end - $start) . " seconds."
101-
);
102-
}
113+
$this->ioStyle->text($staticOutput);
103114

104-
if (empty($errors)) {
115+
$this->ioStyle->text('Total execution time is ' . (string)($end - $start) . ' seconds.' . PHP_EOL);
116+
}
117+
if (!$cmdFailed && empty($errors)) {
105118
return 0;
106119
} else {
107120
return 1;
@@ -115,7 +128,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
115128
* @return void
116129
* @throws InvalidArgumentException
117130
*/
118-
private function validateInputArguments(InputInterface $input)
131+
private function validateInput(InputInterface $input)
119132
{
120133
$this->staticCheckObjects = [];
121134
$requiredChecksNames = $input->getArgument('names');
@@ -137,8 +150,18 @@ private function validateInputArguments(InputInterface $input)
137150

138151
if (!empty($invalidCheckNames)) {
139152
throw new InvalidArgumentException(
140-
"Invalid static check script(s): " . implode(', ', $invalidCheckNames) . "."
153+
'Invalid static check script(s): ' . implode(', ', $invalidCheckNames) . '.'
141154
);
142155
}
156+
157+
if ($input->getOption('path')) {
158+
if ( (count($this->staticCheckObjects) !== 1)
159+
|| array_keys($this->staticCheckObjects)[0] !== StaticChecksList::DEPRECATED_ENTITY_USAGE_CHECK_NAME )
160+
throw new InvalidArgumentException(
161+
'--path option can only be used for "'
162+
. StaticChecksList::DEPRECATED_ENTITY_USAGE_CHECK_NAME
163+
. '".'
164+
);
165+
}
143166
}
144167
}

src/Magento/FunctionalTestingFramework/StaticCheck/DeprecatedEntityUsageCheck.php

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
namespace Magento\FunctionalTestingFramework\StaticCheck;
88

99
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
10+
use InvalidArgumentException;
11+
use Exception;
1012
use Magento\FunctionalTestingFramework\Exceptions\XmlException;
1113
use Magento\FunctionalTestingFramework\Page\Objects\SectionObject;
1214
use Magento\FunctionalTestingFramework\Test\Objects\ActionObject;
@@ -16,7 +18,6 @@
1618
use Magento\FunctionalTestingFramework\Test\Objects\TestObject;
1719
use Symfony\Component\Console\Input\InputInterface;
1820
use Symfony\Component\Finder\Finder;
19-
use Exception;
2021
use Magento\FunctionalTestingFramework\Util\Script\ScriptUtil;
2122
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\OperationDefinitionObjectHandler;
2223
use Magento\FunctionalTestingFramework\DataGenerator\Objects\OperationDefinitionObject;
@@ -71,7 +72,7 @@ class DeprecatedEntityUsageCheck implements StaticCheckInterface
7172
* Checks test dependencies, determined by references in tests versus the dependencies listed in the Magento module
7273
*
7374
* @param InputInterface $input
74-
* @return string
75+
* @return void
7576
* @throws Exception
7677
*/
7778
public function execute(InputInterface $input)
@@ -83,7 +84,7 @@ public function execute(InputInterface $input)
8384
$path = $input->getOption('path');
8485
if ($path) {
8586
if (!realpath($path)) {
86-
return "Invalid --path option: " . $path;
87+
throw new InvalidArgumentException("Invalid --path option: " . $path);
8788
}
8889
MftfApplicationConfig::create(
8990
true,
@@ -92,6 +93,7 @@ public function execute(InputInterface $input)
9293
MftfApplicationConfig::LEVEL_DEFAULT,
9394
true
9495
);
96+
putenv('CUSTOM_MODULE_PATHS=' . realpath($path));
9597
$modulePaths[] = realpath($path);
9698
$includeRootPath = false;
9799
} else {
@@ -110,9 +112,9 @@ public function execute(InputInterface $input)
110112
$this->errors = [];
111113
$this->errors += $this->findReferenceErrorsInActionFiles($testXmlFiles);
112114
$this->errors += $this->findReferenceErrorsInActionFiles($actionGroupXmlFiles);
113-
$this->errors += $this->findReferenceErrorsInActionFiles($suiteXmlFiles);
115+
$this->errors += $this->findReferenceErrorsInActionFiles($suiteXmlFiles, true);
114116
if ($includeRootPath && !empty($rootSuiteXmlFiles)) {
115-
$this->errors += $this->findReferenceErrorsInActionFiles($rootSuiteXmlFiles);
117+
$this->errors += $this->findReferenceErrorsInActionFiles($rootSuiteXmlFiles, true);
116118
}
117119
$this->errors += $this->findReferenceErrorsInDataFiles($dataXmlFiles);
118120

@@ -147,11 +149,12 @@ public function getOutput()
147149
/**
148150
* Find reference errors in set of action files
149151
*
150-
* @param Finder $files
152+
* @param Finder $files
153+
* @param boolean $checkTestRef
151154
* @return array
152155
* @throws XmlException
153156
*/
154-
private function findReferenceErrorsInActionFiles($files)
157+
private function findReferenceErrorsInActionFiles($files, $checkTestRef = false)
155158
{
156159
$testErrors = [];
157160
/** @var SplFileInfo $filePath */
@@ -224,6 +227,23 @@ private function findReferenceErrorsInActionFiles($files)
224227
$this->scriptUtil->resolveEntityByNames($getDataReferences)
225228
);
226229

230+
// Find test references if needed
231+
if ($checkTestRef) {
232+
$testReferences = $this->getAttributesFromDOMNodeList(
233+
$domDocument->getElementsByTagName('test'),
234+
'name'
235+
);
236+
237+
// Remove Duplicates
238+
$testReferences = array_unique($testReferences);
239+
240+
// Resolve test entity by names
241+
$entityReferences = array_merge(
242+
$entityReferences,
243+
$this->scriptUtil->resolveEntityByNames($testReferences)
244+
);
245+
}
246+
227247
// Find violating references
228248
$violatingReferences = $this->findViolatingReferences($entityReferences);
229249

@@ -404,12 +424,18 @@ private function getMetadataFromData($references, $type)
404424
* [
405425
* $dataName1 => [
406426
* $metaDataName1 => 'all',
427+
* $metaDataName2 => 'all',
428+
* ...
407429
* ],
408430
* $dataName2 => [
409431
* $metaDataName2 => 'all',
432+
* ...
410433
* ],
411434
* $dataName5 => [
412435
* $metaDataName5 => 'all',
436+
* $metaDataName4 => 'all',
437+
* $metaDataName1 => 'all',
438+
* ...
413439
* ],
414440
* ...
415441
* ]

src/Magento/FunctionalTestingFramework/StaticCheck/StaticChecksList.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*/
1414
class StaticChecksList implements StaticCheckListInterface
1515
{
16+
const DEPRECATED_ENTITY_USAGE_CHECK_NAME = 'deprecatedEntityUsage';
17+
1618
/**
1719
* Property contains all static check scripts.
1820
*
@@ -30,7 +32,7 @@ public function __construct(array $checks = [])
3032
$this->checks = [
3133
'testDependencies' => new TestDependencyCheck(),
3234
'actionGroupArguments' => new ActionGroupArgumentsCheck(),
33-
'deprecatedEntityUsage' => new DeprecatedEntityUsageCheck(),
35+
self::DEPRECATED_ENTITY_USAGE_CHECK_NAME => new DeprecatedEntityUsageCheck(),
3436
] + $checks;
3537
}
3638

src/Magento/FunctionalTestingFramework/StaticCheck/TestDependencyCheck.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Magento\FunctionalTestingFramework\StaticCheck;
88

9+
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
910
use Magento\FunctionalTestingFramework\Exceptions\XmlException;
1011
use Magento\FunctionalTestingFramework\Test\Objects\ActionObject;
1112
use Symfony\Component\Console\Input\InputInterface;
@@ -84,7 +85,7 @@ class TestDependencyCheck implements StaticCheckInterface
8485
* Checks test dependencies, determined by references in tests versus the dependencies listed in the Magento module
8586
*
8687
* @param InputInterface $input
87-
* @return string
88+
* @return void
8889
* @throws Exception
8990
*/
9091
public function execute(InputInterface $input)
@@ -93,7 +94,9 @@ public function execute(InputInterface $input)
9394
$allModules = $this->scriptUtil->getAllModulePaths();
9495

9596
if (!class_exists('\Magento\Framework\Component\ComponentRegistrar')) {
96-
return "TEST DEPENDENCY CHECK ABORTED: MFTF must be attached or pointing to Magento codebase.";
97+
throw new TestFrameworkException(
98+
"TEST DEPENDENCY CHECK ABORTED: MFTF must be attached or pointing to Magento codebase."
99+
);
97100
}
98101
$registrar = new \Magento\Framework\Component\ComponentRegistrar();
99102
$this->moduleNameToPath = $registrar->getPaths(\Magento\Framework\Component\ComponentRegistrar::MODULE);

0 commit comments

Comments
 (0)