Skip to content

Commit 4a4fc38

Browse files
committed
MQE-1676: Add a static-check that ensures action groups do not have unused arguments
modified regex, merged develop, fixed static checks
1 parent 97f7a37 commit 4a4fc38

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

src/Magento/FunctionalTestingFramework/StaticCheck/UnusedArgumentsCheck.php

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
1010
use Magento\FunctionalTestingFramework\Test\Handlers\ActionGroupObjectHandler;
11+
use Magento\FunctionalTestingFramework\Test\Objects\ActionGroupObject;
12+
use Magento\FunctionalTestingFramework\Test\Objects\ActionObject;
1113
use Symfony\Component\Console\Input\InputInterface;
1214
use Exception;
1315

@@ -35,7 +37,7 @@ class UnusedArgumentsCheck implements StaticCheckInterface
3537
* Checks unused arguments in action groups and prints out error to file.
3638
*
3739
* @param InputInterface $input
38-
* @return string
40+
* @return void
3941
* @throws Exception;
4042
*/
4143
public function execute(InputInterface $input)
@@ -55,7 +57,6 @@ public function execute(InputInterface $input)
5557
$this->errors += $this->setErrorOutput($unusedArgumentList);
5658

5759
$this->output = $this->printErrorsToFile();
58-
5960
}
6061

6162
/**
@@ -78,16 +79,16 @@ public function getOutput()
7879

7980
/**
8081
* Builds array of action groups => unused arguments
81-
* @param $actionGroups
82-
* @return array
82+
* @param array $actionGroups
83+
* @return array $actionGroupToArguments
8384
*/
84-
private function buildUnusedArgumentList($actionGroups) {
85-
85+
private function buildUnusedArgumentList($actionGroups)
86+
{
8687
$actionGroupToArguments = [];
8788

8889
foreach ($actionGroups as $actionGroup) {
8990
$unusedArguments = $this->findUnusedArguments($actionGroup);
90-
if(!empty($unusedArguments)) {
91+
if (!empty($unusedArguments)) {
9192
$actionGroupToArguments[$actionGroup->getFilename()][$actionGroup->getName()] = $unusedArguments;
9293
}
9394
}
@@ -96,19 +97,19 @@ private function buildUnusedArgumentList($actionGroups) {
9697

9798
/**
9899
* Returns unused arguments in an action group.
99-
* @param $actionGroup
100-
* @return array
100+
* @param ActionGroupObject $actionGroup
101+
* @return array $unusedArguments
101102
*/
102-
private function findUnusedArguments($actionGroup) {
103-
103+
private function findUnusedArguments($actionGroup)
104+
{
104105
$unusedArguments = [];
105106
//extract all action attribute values
106107
$actionAttributeValues = $this->getAllActionAttributeValues($actionGroup);
107108
$argumentList = $actionGroup->getArguments();
108109
foreach ($argumentList as $argument) {
109110
$argumentName = $argument->getName();
110111
//pattern to match all argument references
111-
$pattern = '(.*\.*[\W]+(?<!\.)' . $argumentName . '[\W]+.*)';
112+
$pattern = '(.*[\W]+(?<!\.)' . $argumentName . '[\W]+.*)';
112113
if (preg_grep($pattern, $actionAttributeValues)) {
113114
continue;
114115
}
@@ -122,8 +123,8 @@ private function findUnusedArguments($actionGroup) {
122123
* @param $actionGroup
123124
* @return array
124125
*/
125-
private function getAllActionAttributeValues($actionGroup) {
126-
126+
private function getAllActionAttributeValues($actionGroup)
127+
{
127128
$allAttributeValues = [];
128129
$actions = $actionGroup->getActions();
129130
foreach ($actions as $action) {
@@ -133,22 +134,20 @@ private function getAllActionAttributeValues($actionGroup) {
133134
return array_unique($allAttributeValues);
134135
}
135136

136-
137137
/**
138138
* Builds and returns flattened attribute value list for an action.
139-
* @param $action
140-
* @return array
139+
* @param ActionObject $action
140+
* @return array $flattenedAttributeValues
141141
*/
142-
private function extractAttributeValues($action) {
143-
142+
private function extractAttributeValues($action)
143+
{
144144
$flattenedAttributeValues = [];
145145
$actionAttributes = $action->getCustomActionAttributes();
146146
//check if action has nodes eg. expectedResult, actualResult and flatten array
147147
foreach ($actionAttributes as $attributeName => $attributeValue) {
148148
if (is_array($attributeValue)) {
149149
$flattenedAttributeValues = array_merge($flattenedAttributeValues, array_values($attributeValue));
150-
}
151-
else {
150+
} else {
152151
$flattenedAttributeValues[] = $attributeValue;
153152
}
154153
}
@@ -158,7 +157,7 @@ private function extractAttributeValues($action) {
158157
/**
159158
* Builds and returns error output for unused arguments
160159
*
161-
* @param array $unusedArgumentList
160+
* @param array $unusedArgumentList
162161
* @return mixed
163162
*/
164163
private function setErrorOutput($unusedArgumentList)
@@ -168,7 +167,6 @@ private function setErrorOutput($unusedArgumentList)
168167
if (!empty($unusedArgumentList)) {
169168
// Build error output
170169
foreach ($unusedArgumentList as $path => $actionGroupToArguments) {
171-
172170
$errorOutput = "\nFile \"{$path}\"";
173171
$errorOutput .= "\ncontains action group(s) with unused arguments.\n\t\t";
174172

@@ -208,5 +206,4 @@ private function printErrorsToFile()
208206

209207
return $output;
210208
}
211-
212209
}

0 commit comments

Comments
 (0)