9
9
use Magento \FunctionalTestingFramework \Config \MftfApplicationConfig ;
10
10
use Magento \FunctionalTestingFramework \Test \Handlers \ActionGroupObjectHandler ;
11
11
use Magento \FunctionalTestingFramework \Test \Objects \ActionGroupObject ;
12
- use Magento \FunctionalTestingFramework \Test \Objects \ActionObject ;
13
12
use Symfony \Component \Console \Input \InputInterface ;
13
+ use Magento \FunctionalTestingFramework \Util \ModuleResolver ;
14
+ use Symfony \Component \Finder \Finder ;
14
15
use Exception ;
15
16
16
17
/**
19
20
*/
20
21
class ActionGroupArgumentsCheck implements StaticCheckInterface
21
22
{
23
+
24
+ const ACTIONGROUP_XML_REGEX_PATTERN = '/<actionGroup\sname=(?: (?!<\/actionGroup>).)*/mxs ' ;
25
+ const ACTIONGROUP_ARGUMENT_REGEX_PATTERN = '/<argument[^\/>]*name="([^" \']*)/mxs ' ;
26
+ const ACTIONGROUP_NAME_REGEX_PATTERN = '/<actionGroup name=[" \']([^ \'"]*)/ ' ;
27
+
22
28
const ERROR_LOG_FILENAME = 'mftf-arguments-checks ' ;
23
29
const ERROR_LOG_MESSAGE = 'MFTF Unused Arguments Check ' ;
24
30
@@ -43,7 +49,6 @@ class ActionGroupArgumentsCheck implements StaticCheckInterface
43
49
*/
44
50
public function execute (InputInterface $ input )
45
51
{
46
-
47
52
MftfApplicationConfig::create (
48
53
true ,
49
54
MftfApplicationConfig::UNIT_TEST_PHASE ,
@@ -52,11 +57,14 @@ public function execute(InputInterface $input)
52
57
true
53
58
);
54
59
55
- $ actionGroups = ActionGroupObjectHandler ::getInstance ()->initActionGroups ();
60
+ $ allModules = ModuleResolver ::getInstance ()->getModulesPath ();
56
61
57
- $ unusedArgumentList = $ this ->buildUnusedArgumentList ($ actionGroups );
62
+ $ actionGroupXmlFiles = StaticCheckHelper::buildFileList (
63
+ $ allModules ,
64
+ DIRECTORY_SEPARATOR . 'ActionGroup ' . DIRECTORY_SEPARATOR
65
+ );
58
66
59
- $ this ->errors + = $ this ->setErrorOutput ( $ unusedArgumentList );
67
+ $ this ->errors = $ this ->findErrorsInFileSet ( $ actionGroupXmlFiles );
60
68
61
69
$ this ->output = StaticCheckHelper::printErrorsToFile (
62
70
$ this ->errors ,
@@ -84,54 +92,73 @@ public function getOutput()
84
92
}
85
93
86
94
/**
87
- * Builds array of action groups => unused arguments
95
+ * Finds all unused arguments in given set of actionGroup files
96
+ * @param Finder $files
97
+ * @return array $testErrors
98
+ */
99
+ private function findErrorsInFileSet ($ files )
100
+ {
101
+ $ actionGroupErrors = [];
102
+ foreach ($ files as $ filePath ) {
103
+ $ contents = file_get_contents ($ filePath );
104
+ preg_match_all (self ::ACTIONGROUP_XML_REGEX_PATTERN , $ contents , $ actionGroups );
105
+ $ actionGroupToArguments = $ this ->buildUnusedArgumentList ($ actionGroups [0 ]);
106
+ $ actionGroupErrors += $ this ->setErrorOutput ($ actionGroupToArguments , $ filePath );
107
+ }
108
+ return $ actionGroupErrors ;
109
+ }
110
+
111
+ /**
112
+ * Builds array of action group => unused arguments
88
113
* @param array $actionGroups
89
114
* @return array $actionGroupToArguments
90
115
*/
91
116
private function buildUnusedArgumentList ($ actionGroups )
92
117
{
93
118
$ actionGroupToArguments = [];
94
119
95
- foreach ($ actionGroups as $ actionGroup ) {
96
- $ unusedArguments = $ this ->findUnusedArguments ($ actionGroup );
120
+ foreach ($ actionGroups as $ actionGroupXml ) {
121
+ preg_match (self ::ACTIONGROUP_NAME_REGEX_PATTERN , $ actionGroupXml , $ actionGroupName );
122
+ $ unusedArguments = $ this ->findUnusedArguments ($ actionGroupXml );
97
123
if (!empty ($ unusedArguments )) {
98
- $ actionGroupToArguments [$ actionGroup -> getFilename ()][ $ actionGroup -> getName () ] = $ unusedArguments ;
124
+ $ actionGroupToArguments [$ actionGroupName [ 1 ] ] = $ unusedArguments ;
99
125
}
100
126
}
101
127
return $ actionGroupToArguments ;
102
128
}
103
129
104
130
/**
105
- * Returns unused arguments in an action group.
106
- * @param ActionGroupObject $actionGroup
107
- * @return array $unusedArguments
131
+ * @param $actionGroupXml
132
+ * @return array
108
133
*/
109
- private function findUnusedArguments ($ actionGroup )
134
+ private function findUnusedArguments ($ actionGroupXml )
110
135
{
111
136
$ unusedArguments = [];
112
- //extract all action attribute values
113
- $ actionAttributeValues = $ this ->getAllActionAttributeValues ($ actionGroup );
114
- $ argumentList = $ actionGroup ->getArguments ();
115
- foreach ($ argumentList as $ argument ) {
116
- $ argumentName = $ argument ->getName ();
137
+
138
+ preg_match_all (self ::ACTIONGROUP_ARGUMENT_REGEX_PATTERN , $ actionGroupXml , $ arguments );
139
+ preg_match (self ::ACTIONGROUP_NAME_REGEX_PATTERN , $ actionGroupXml , $ actionGroupName );
140
+
141
+ $ actionGroup = ActionGroupObjectHandler::getInstance ()->getObject ($ actionGroupName [1 ]);
142
+
143
+ foreach ($ arguments [1 ] as $ argument ) {
117
144
//pattern to match all argument references
118
145
$ patterns = [
119
- '(\{{2} ' . $ argumentName . '(\.[a-zA-Z0-9_\[\]\(\)., \'\/ ]+)?}{2}) ' ,
120
- '([(,\s \'] ' . $ argumentName . '(\.[a-zA-Z0-9_\[\]]+)?[),\s \']) '
146
+ '(\{{2} ' . $ argument . '(\.[a-zA-Z0-9_\[\]\(\)., \'\/ ]+)?}{2}) ' ,
147
+ '([(,\s \'] ' . $ argument . '(\.[a-zA-Z0-9_\[\]]+)?[),\s \']) '
121
148
];
122
149
// matches entity references
123
- if (preg_grep ($ patterns [0 ], $ actionAttributeValues )) {
150
+ if (preg_match ($ patterns [0 ], $ actionGroupXml )) {
124
151
continue ;
125
152
}
126
153
//matches parametrized references
127
- if (preg_grep ($ patterns [1 ], $ actionAttributeValues )) {
154
+ if (preg_match ($ patterns [1 ], $ actionGroupXml )) {
128
155
continue ;
129
156
}
130
- //exclude arguments that are also defined in parent action group for extending action groups
157
+ //for extending action groups, exclude arguments that are also defined in parent action group
131
158
if ($ this ->isParentActionGroupArgument ($ argument , $ actionGroup )) {
132
159
continue ;
133
160
}
134
- $ unusedArguments [] = $ argumentName ;
161
+ $ unusedArguments [] = $ argument ;
135
162
}
136
163
return $ unusedArguments ;
137
164
}
@@ -147,70 +174,34 @@ private function isParentActionGroupArgument($argument, $actionGroup)
147
174
if ($ actionGroup ->getParentName () !== null ) {
148
175
$ parentActionGroup = ActionGroupObjectHandler::getInstance ()->getObject ($ actionGroup ->getParentName ());
149
176
$ parentArguments = $ parentActionGroup ->getArguments ();
150
- if ($ parentArguments !== null ) {
151
- return in_array ($ argument , $ parentArguments );
152
- }
153
- return false ;
154
- }
155
- }
156
-
157
- /**
158
- * Returns array of all action attribute values in an action group.
159
- * @param ActionGroupObject $actionGroup
160
- * @return array $allAttributeValues
161
- */
162
- private function getAllActionAttributeValues ($ actionGroup )
163
- {
164
- $ allAttributeValues = [];
165
- $ actions = $ actionGroup ->getActions ();
166
- foreach ($ actions as $ action ) {
167
- $ actionAttributeValues = $ this ->extractAttributeValues ($ action );
168
- $ allAttributeValues = array_merge ($ allAttributeValues , $ actionAttributeValues );
169
- }
170
- return array_unique ($ allAttributeValues );
171
- }
172
-
173
- /**
174
- * Builds and returns flattened attribute value list for an action.
175
- * @param ActionObject $action
176
- * @return array $flattenedAttributeValues
177
- */
178
- private function extractAttributeValues ($ action )
179
- {
180
- $ flattenedAttributeValues = [];
181
- $ actionAttributes = $ action ->getCustomActionAttributes ();
182
- //check if action has nodes eg. expectedResult, actualResult and flatten array
183
- foreach ($ actionAttributes as $ attributeName => $ attributeValue ) {
184
- if (is_array ($ attributeValue )) {
185
- $ flattenedAttributeValues = array_merge ($ flattenedAttributeValues , array_values ($ attributeValue ));
186
- } else {
187
- $ flattenedAttributeValues [] = $ attributeValue ;
177
+ foreach ($ parentArguments as $ parentArgument ) {
178
+ if ($ argument === $ parentArgument ->getName ()) {
179
+ return true ;
180
+ }
188
181
}
189
182
}
190
- return $ flattenedAttributeValues ;
183
+ return false ;
191
184
}
192
185
193
186
/**
194
- * Builds and returns error output for unused arguments
187
+ * Builds and returns error output for violating references
195
188
*
196
- * @param array $unusedArgumentList
189
+ * @param array $actionGroupToArguments
190
+ * @param string $path
197
191
* @return mixed
198
192
*/
199
- private function setErrorOutput ($ unusedArgumentList )
193
+ private function setErrorOutput ($ actionGroupToArguments , $ path )
200
194
{
201
- $ testErrors = [];
202
- if (!empty ($ unusedArgumentList )) {
195
+ $ actionGroupErrors = [];
196
+ if (!empty ($ actionGroupToArguments )) {
203
197
// Build error output
204
- foreach ($ unusedArgumentList as $ path => $ actionGroupToArguments ) {
205
- $ errorOutput = "\nFile \"{$ path }\"" ;
206
- $ errorOutput .= "\ncontains action group(s) with unused arguments. \n\t\t" ;
207
-
208
- foreach ($ actionGroupToArguments as $ actionGroup => $ arguments ) {
209
- $ errorOutput .= "\n\t {$ actionGroup } has unused argument(s): " . implode (", " , $ arguments );
210
- }
211
- $ testErrors [$ path ][] = $ errorOutput ;
198
+ $ errorOutput = "\nFile \"{$ path ->getRealPath ()}\"" ;
199
+ $ errorOutput .= "\ncontains action group(s) with unused arguments. \n\t\t" ;
200
+ foreach ($ actionGroupToArguments as $ actionGroup => $ arguments ) {
201
+ $ errorOutput .= "\n\t {$ actionGroup } has unused argument(s): " . implode (", " , $ arguments );
212
202
}
203
+ $ actionGroupErrors [$ path ->getRealPath ()][] = $ errorOutput ;
213
204
}
214
- return $ testErrors ;
205
+ return $ actionGroupErrors ;
215
206
}
216
207
}
0 commit comments