1414use 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}
0 commit comments