14
14
use Exception ;
15
15
16
16
/**
17
- * Class UnusedArgumentsCheck
17
+ * Class ActionGroupArgumentsCheck
18
18
* @package Magento\FunctionalTestingFramework\StaticCheck
19
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
20
19
*/
21
- class UnusedArgumentsCheck implements StaticCheckInterface
20
+ class ActionGroupArgumentsCheck implements StaticCheckInterface
22
21
{
22
+ const ERROR_LOG_FILENAME = 'mftf-arguments-checks ' ;
23
+ const ERROR_LOG_MESSAGE = 'MFTF Unused Arguments Check ' ;
23
24
24
25
/**
25
26
* Array containing all errors found after running the execute() function.
@@ -38,10 +39,11 @@ class UnusedArgumentsCheck implements StaticCheckInterface
38
39
*
39
40
* @param InputInterface $input
40
41
* @return void
41
- * @throws Exception;
42
+ * @throws Exception
42
43
*/
43
44
public function execute (InputInterface $ input )
44
45
{
46
+
45
47
MftfApplicationConfig::create (
46
48
true ,
47
49
MftfApplicationConfig::UNIT_TEST_PHASE ,
@@ -56,7 +58,8 @@ public function execute(InputInterface $input)
56
58
57
59
$ this ->errors += $ this ->setErrorOutput ($ unusedArgumentList );
58
60
59
- $ this ->output = $ this ->printErrorsToFile ();
61
+ $ this ->output = StaticCheckHelper::printErrorsToFile ($ this ->errors ,
62
+ self ::ERROR_LOG_FILENAME , self ::ERROR_LOG_MESSAGE );
60
63
}
61
64
62
65
/**
@@ -109,15 +112,45 @@ private function findUnusedArguments($actionGroup)
109
112
foreach ($ argumentList as $ argument ) {
110
113
$ argumentName = $ argument ->getName ();
111
114
//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 )) {
114
125
continue ;
115
126
}
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 ;
117
132
}
118
133
return $ unusedArguments ;
119
134
}
120
135
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
+
121
154
/**
122
155
* Returns array of all action attribute values in an action group.
123
156
* @param ActionGroupObject $actionGroup
@@ -163,7 +196,6 @@ private function extractAttributeValues($action)
163
196
private function setErrorOutput ($ unusedArgumentList )
164
197
{
165
198
$ testErrors = [];
166
-
167
199
if (!empty ($ unusedArgumentList )) {
168
200
// Build error output
169
201
foreach ($ unusedArgumentList as $ path => $ actionGroupToArguments ) {
@@ -178,32 +210,4 @@ private function setErrorOutput($unusedArgumentList)
178
210
}
179
211
return $ testErrors ;
180
212
}
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
- }
209
213
}
0 commit comments