Skip to content

Commit cc3601a

Browse files
committed
MQE-1676: Add a static-check that ensures action groups do not have unused arguments
Refactoring + added logic to exclude arguments found in parent action group
1 parent 58f0da6 commit cc3601a

File tree

4 files changed

+84
-67
lines changed

4 files changed

+84
-67
lines changed

src/Magento/FunctionalTestingFramework/StaticCheck/UnusedArgumentsCheck.php renamed to src/Magento/FunctionalTestingFramework/StaticCheck/ActionGroupArgumentsCheck.php

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
use Exception;
1515

1616
/**
17-
* Class UnusedArgumentsCheck
17+
* Class ActionGroupArgumentsCheck
1818
* @package Magento\FunctionalTestingFramework\StaticCheck
19-
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2019
*/
21-
class UnusedArgumentsCheck implements StaticCheckInterface
20+
class ActionGroupArgumentsCheck implements StaticCheckInterface
2221
{
22+
const ERROR_LOG_FILENAME = 'mftf-arguments-checks';
23+
const ERROR_LOG_MESSAGE = 'MFTF Unused Arguments Check';
2324

2425
/**
2526
* Array containing all errors found after running the execute() function.
@@ -38,10 +39,11 @@ class UnusedArgumentsCheck implements StaticCheckInterface
3839
*
3940
* @param InputInterface $input
4041
* @return void
41-
* @throws Exception;
42+
* @throws Exception
4243
*/
4344
public function execute(InputInterface $input)
4445
{
46+
4547
MftfApplicationConfig::create(
4648
true,
4749
MftfApplicationConfig::UNIT_TEST_PHASE,
@@ -56,7 +58,8 @@ public function execute(InputInterface $input)
5658

5759
$this->errors += $this->setErrorOutput($unusedArgumentList);
5860

59-
$this->output = $this->printErrorsToFile();
61+
$this->output = StaticCheckHelper::printErrorsToFile($this->errors,
62+
self::ERROR_LOG_FILENAME, self::ERROR_LOG_MESSAGE);
6063
}
6164

6265
/**
@@ -109,15 +112,45 @@ private function findUnusedArguments($actionGroup)
109112
foreach ($argumentList as $argument) {
110113
$argumentName = $argument->getName();
111114
//pattern to match all argument references
112-
$pattern = '(.*[\W]+(?<!\.)' . $argumentName . '[\W]+.*)';
113-
if (preg_grep($pattern, $actionAttributeValues)) {
115+
$patterns = [
116+
'(\{{2}' . $argumentName . '(\.[a-zA-Z0-9_\[\]\(\).,\'\/ ]+)?}{2})',
117+
'([(,\s\']' . $argumentName . '(\.[a-zA-Z0-9_\[\]]+)?[),\s\'])'
118+
];
119+
// matches entity references
120+
if (preg_grep($patterns[0], $actionAttributeValues)) {
121+
continue;
122+
}
123+
//matches parametrized references
124+
if (preg_grep($patterns[1], $actionAttributeValues)) {
114125
continue;
115126
}
116-
$unusedArguments[] = $argument->getName();
127+
//exclude arguments that are also defined in parent action group for extending action groups
128+
if ($this->isParentActionGroupArgument($argument, $actionGroup)) {
129+
continue;
130+
}
131+
$unusedArguments[] = $argumentName;
117132
}
118133
return $unusedArguments;
119134
}
120135

136+
/**
137+
* Checks if the argument is also defined in the parent for extending action groups.
138+
* @param string $argument
139+
* @param ActionGroupObject $actionGroup
140+
* @return bool
141+
*/
142+
private function isParentActionGroupArgument($argument, $actionGroup) {
143+
144+
if ($actionGroup->getParentName() !== null) {
145+
$parentActionGroup = ActionGroupObjectHandler::getInstance()->getObject($actionGroup->getParentName());
146+
$parentArguments = $parentActionGroup->getArguments();
147+
if ($parentArguments !== null) {
148+
return in_array($argument, $parentArguments);
149+
}
150+
return false;
151+
}
152+
}
153+
121154
/**
122155
* Returns array of all action attribute values in an action group.
123156
* @param ActionGroupObject $actionGroup
@@ -163,7 +196,6 @@ private function extractAttributeValues($action)
163196
private function setErrorOutput($unusedArgumentList)
164197
{
165198
$testErrors = [];
166-
167199
if (!empty($unusedArgumentList)) {
168200
// Build error output
169201
foreach ($unusedArgumentList as $path => $actionGroupToArguments) {
@@ -178,32 +210,4 @@ private function setErrorOutput($unusedArgumentList)
178210
}
179211
return $testErrors;
180212
}
181-
182-
/**
183-
* Prints out given errors to file, and returns summary result string
184-
* @return string
185-
*/
186-
private function printErrorsToFile()
187-
{
188-
$errors = $this->getErrors();
189-
190-
if (empty($errors)) {
191-
return "No unused arguments found.";
192-
}
193-
194-
$outputPath = getcwd() . DIRECTORY_SEPARATOR . "mftf-arguments-checks.txt";
195-
$fileResource = fopen($outputPath, 'w');
196-
$header = "MFTF ActionGroup Arguments Check:\n";
197-
fwrite($fileResource, $header);
198-
199-
foreach ($errors as $test => $error) {
200-
fwrite($fileResource, $error[0] . PHP_EOL);
201-
}
202-
203-
fclose($fileResource);
204-
$errorCount = count($errors);
205-
$output = "Unused arguments found across {$errorCount} actionGroup(s). Error details output to {$outputPath}";
206-
207-
return $output;
208-
}
209213
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\FunctionalTestingFramework\StaticCheck;
7+
8+
class StaticCheckHelper
9+
{
10+
/**
11+
* Prints out given errors to file, and returns summary result string
12+
* @param array $errors
13+
* @param string $filename
14+
* @param string $message
15+
* @return string
16+
*/
17+
public static function printErrorsToFile($errors, $filename, $message)
18+
{
19+
if (empty($errors)) {
20+
return $message . ": No errors found.";
21+
}
22+
23+
$outputPath = getcwd() . DIRECTORY_SEPARATOR . $filename . ".txt";
24+
$fileResource = fopen($outputPath, 'w');
25+
26+
foreach ($errors as $test => $error) {
27+
fwrite($fileResource, $error[0] . PHP_EOL);
28+
}
29+
30+
fclose($fileResource);
31+
$errorCount = count($errors);
32+
$output = $message . ": Errors found across {$errorCount} file(s). Error details output to {$outputPath}";
33+
34+
return $output;
35+
}
36+
}

src/Magento/FunctionalTestingFramework/StaticCheck/StaticChecksList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(array $checks = [])
2929
{
3030
$this->checks = [
3131
'testDependencies' => new TestDependencyCheck(),
32-
'unusedArgumentsCheck' => new UnusedArgumentsCheck(),
32+
'actionGroupArguments' => new ActionGroupArgumentsCheck(),
3333
] + $checks;
3434
}
3535

src/Magento/FunctionalTestingFramework/StaticCheck/TestDependencyCheck.php

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

77
namespace Magento\FunctionalTestingFramework\StaticCheck;
88

9+
use Magento\FunctionalTestingFramework\StaticCheck\StaticCheckHelper;
910
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
1011
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\DataObjectHandler;
1112
use Magento\FunctionalTestingFramework\Exceptions\TestReferenceException;
@@ -32,6 +33,9 @@ class TestDependencyCheck implements StaticCheckInterface
3233
const ACTIONGROUP_REGEX_PATTERN = '/ref=["\']([^\'"]*)/';
3334
const ACTIONGROUP_ARGUMENT_REGEX_PATTERN = '/<argument[^\/>]*name="([^"\']*)/';
3435

36+
const ERROR_LOG_FILENAME = 'mftf-dependency-checks';
37+
const ERROR_LOG_MESSAGE = 'MFTF File Dependency Check';
38+
3539
/**
3640
* Array of FullModuleName => [dependencies]
3741
* @var array
@@ -123,7 +127,8 @@ public function execute(InputInterface $input)
123127
$this->errors += $this->findErrorsInFileSet($dataXmlFiles);
124128

125129
// hold on to the output and print any errors to a file
126-
$this->output = $this->printErrorsToFile();
130+
$this->output = StaticCheckHelper::printErrorsToFile($this->errors,
131+
self::ERROR_LOG_FILENAME, self::ERROR_LOG_MESSAGE);
127132
}
128133

129134
/**
@@ -461,32 +466,4 @@ private function findEntity($name)
461466
}
462467
return null;
463468
}
464-
465-
/**
466-
* Prints out given errors to file, and returns summary result string
467-
* @return string
468-
*/
469-
private function printErrorsToFile()
470-
{
471-
$errors = $this->getErrors();
472-
473-
if (empty($errors)) {
474-
return "No Dependency errors found.";
475-
}
476-
477-
$outputPath = getcwd() . DIRECTORY_SEPARATOR . "mftf-dependency-checks.txt";
478-
$fileResource = fopen($outputPath, 'w');
479-
$header = "MFTF File Dependency Check:\n";
480-
fwrite($fileResource, $header);
481-
482-
foreach ($errors as $test => $error) {
483-
fwrite($fileResource, $error[0] . PHP_EOL);
484-
}
485-
486-
fclose($fileResource);
487-
$errorCount = count($errors);
488-
$output = "Dependency errors found across {$errorCount} file(s). Error details output to {$outputPath}";
489-
490-
return $output;
491-
}
492469
}

0 commit comments

Comments
 (0)