6
6
7
7
namespace Magento \FunctionalTestingFramework \StaticCheck ;
8
8
9
- use Magento \FunctionalTestingFramework \Config \MftfApplicationConfig ;
10
- use Magento \FunctionalTestingFramework \DataGenerator \Handlers \DataObjectHandler ;
11
- use Magento \FunctionalTestingFramework \Exceptions \TestReferenceException ;
12
9
use Magento \FunctionalTestingFramework \Exceptions \XmlException ;
13
- use Magento \FunctionalTestingFramework \Page \Handlers \PageObjectHandler ;
14
- use Magento \FunctionalTestingFramework \Page \Handlers \SectionObjectHandler ;
15
- use Magento \FunctionalTestingFramework \Test \Handlers \ActionGroupObjectHandler ;
16
- use Magento \FunctionalTestingFramework \Test \Handlers \TestObjectHandler ;
17
10
use Magento \FunctionalTestingFramework \Test \Objects \ActionObject ;
18
- use Magento \FunctionalTestingFramework \Util \TestGenerator ;
19
11
use Symfony \Component \Console \Input \InputInterface ;
20
12
use Symfony \Component \Finder \Finder ;
21
13
use Exception ;
24
16
/**
25
17
* Class TestDependencyCheck
26
18
* @package Magento\FunctionalTestingFramework\StaticCheck
27
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
28
19
*/
29
20
class TestDependencyCheck implements StaticCheckInterface
30
21
{
31
22
const EXTENDS_REGEX_PATTERN = '/extends=[" \']([^ \'"]*)/ ' ;
32
23
const ACTIONGROUP_REGEX_PATTERN = '/ref=[" \']([^ \'"]*)/ ' ;
33
- const ACTIONGROUP_ARGUMENT_REGEX_PATTERN = '/<argument[^\/>]*name="([^" \']*)/ ' ;
34
24
35
25
const ERROR_LOG_FILENAME = 'mftf-dependency-checks ' ;
36
26
const ERROR_LOG_MESSAGE = 'MFTF File Dependency Check ' ;
@@ -83,6 +73,13 @@ class TestDependencyCheck implements StaticCheckInterface
83
73
*/
84
74
private $ allEntities = [];
85
75
76
+ /**
77
+ * ScriptUtil instance
78
+ *
79
+ * @var ScriptUtil
80
+ */
81
+ private $ scriptUtil ;
82
+
86
83
/**
87
84
* Checks test dependencies, determined by references in tests versus the dependencies listed in the Magento module
88
85
*
@@ -92,7 +89,8 @@ class TestDependencyCheck implements StaticCheckInterface
92
89
*/
93
90
public function execute (InputInterface $ input )
94
91
{
95
- $ allModules = ScriptUtil::getAllModulePaths ();
92
+ $ this ->scriptUtil = new ScriptUtil ();
93
+ $ allModules = $ this ->scriptUtil ->getAllModulePaths ();
96
94
97
95
if (!class_exists ('\Magento\Framework\Component\ComponentRegistrar ' )) {
98
96
return "TEST DEPENDENCY CHECK ABORTED: MFTF must be attached or pointing to Magento codebase. " ;
@@ -108,17 +106,17 @@ public function execute(InputInterface $input)
108
106
DIRECTORY_SEPARATOR . 'Data ' . DIRECTORY_SEPARATOR ,
109
107
];
110
108
// These files can contain references to other modules.
111
- $ testXmlFiles = ScriptUtil:: getModuleXmlFilesByScope ($ allModules , $ filePaths [0 ]);
112
- $ actionGroupXmlFiles = ScriptUtil:: getModuleXmlFilesByScope ($ allModules , $ filePaths [1 ]);
113
- $ dataXmlFiles = ScriptUtil:: getModuleXmlFilesByScope ($ allModules , $ filePaths [2 ]);
109
+ $ testXmlFiles = $ this -> scriptUtil -> getModuleXmlFilesByScope ($ allModules , $ filePaths [0 ]);
110
+ $ actionGroupXmlFiles = $ this -> scriptUtil -> getModuleXmlFilesByScope ($ allModules , $ filePaths [1 ]);
111
+ $ dataXmlFiles = $ this -> scriptUtil -> getModuleXmlFilesByScope ($ allModules , $ filePaths [2 ]);
114
112
115
113
$ this ->errors = [];
116
114
$ this ->errors += $ this ->findErrorsInFileSet ($ testXmlFiles );
117
115
$ this ->errors += $ this ->findErrorsInFileSet ($ actionGroupXmlFiles );
118
116
$ this ->errors += $ this ->findErrorsInFileSet ($ dataXmlFiles );
119
117
120
118
// hold on to the output and print any errors to a file
121
- $ this ->output = ScriptUtil:: printErrorsToFile (
119
+ $ this ->output = $ this -> scriptUtil -> printErrorsToFile (
122
120
$ this ->errors ,
123
121
self ::ERROR_LOG_FILENAME ,
124
122
self ::ERROR_LOG_MESSAGE
@@ -147,17 +145,16 @@ public function getOutput()
147
145
* Finds all reference errors in given set of files
148
146
* @param Finder $files
149
147
* @return array
150
- * @throws TestReferenceException
151
148
* @throws XmlException
152
149
*/
153
150
private function findErrorsInFileSet ($ files )
154
151
{
155
152
$ testErrors = [];
156
153
foreach ($ files as $ filePath ) {
157
- $ modulePath = dirname ( dirname ( dirname ( dirname ( $ filePath )))) ;
158
- $ moduleFullName = array_search ( $ modulePath , $ this ->moduleNameToPath ) ?? null ;
154
+ $ this -> allEntities = [] ;
155
+ $ moduleName = $ this ->getModuleName ( $ filePath ) ;
159
156
// Not a module, is either dev/tests/acceptance or loose folder with test materials
160
- if ($ moduleFullName == null ) {
157
+ if ($ moduleName == null ) {
161
158
continue ;
162
159
}
163
160
@@ -172,108 +169,37 @@ private function findErrorsInFileSet($files)
172
169
$ braceReferences [1 ] = array_unique ($ braceReferences [1 ]);
173
170
$ braceReferences [2 ] = array_filter (array_unique ($ braceReferences [2 ]));
174
171
175
- // resolve data entity references
176
- $ this ->resolveDataEntityReferences ($ braceReferences [0 ], $ contents );
172
+ // resolve entity references
173
+ $ this ->allEntities = array_merge (
174
+ $ this ->allEntities ,
175
+ $ this ->scriptUtil ->resolveEntityReferences ($ braceReferences [0 ], $ contents )
176
+ );
177
177
178
- //resolve entity references
179
- $ this ->resolveParametrizedReferences ($ braceReferences [2 ], $ contents );
178
+ // resolve parameterized references
179
+ $ this ->allEntities = array_merge (
180
+ $ this ->allEntities ,
181
+ $ this ->scriptUtil ->resolveParametrizedReferences ($ braceReferences [2 ], $ contents )
182
+ );
180
183
181
- // Check actionGroup references
182
- $ this ->resolveEntityReferences ($ actionGroupReferences [1 ]);
184
+ // resolve entity by names
185
+ $ this ->allEntities = array_merge (
186
+ $ this ->allEntities ,
187
+ $ this ->scriptUtil ->resolveEntityByNames ($ actionGroupReferences [1 ])
188
+ );
183
189
184
- // Check extended objects
185
- $ this ->resolveEntityReferences ($ extendReferences [1 ]);
190
+ // resolve entity by names
191
+ $ this ->allEntities = array_merge (
192
+ $ this ->allEntities ,
193
+ $ this ->scriptUtil ->resolveEntityByNames ($ extendReferences [1 ])
194
+ );
186
195
187
196
// Find violating references and set error output
188
- $ violatingReferences = $ this ->findViolatingReferences ($ moduleFullName );
189
- $ testErrors = $ this ->setErrorOutput ($ violatingReferences , $ filePath );
197
+ $ violatingReferences = $ this ->findViolatingReferences ($ moduleName );
198
+ $ testErrors = array_merge ( $ testErrors , $ this ->setErrorOutput ($ violatingReferences , $ filePath) );
190
199
}
191
200
return $ testErrors ;
192
201
}
193
202
194
- /**
195
- * Drill down into params in {{ref.params('string', $data.key$, entity.reference)}}
196
- * and resolve references.
197
- *
198
- * @param array $braceReferences
199
- * @param string $contents
200
- * @return void
201
- * @throws XmlException
202
- */
203
- private function resolveParametrizedReferences ($ braceReferences , $ contents )
204
- {
205
- foreach ($ braceReferences as $ parameterizedReference ) {
206
- preg_match (
207
- ActionObject::ACTION_ATTRIBUTE_VARIABLE_REGEX_PARAMETER ,
208
- $ parameterizedReference ,
209
- $ arguments
210
- );
211
- $ splitArguments = explode (', ' , ltrim (rtrim ($ arguments [0 ], ") " ), "( " ));
212
- foreach ($ splitArguments as $ argument ) {
213
- // Do nothing for 'string' or $persisted.data$
214
- if (preg_match (ActionObject::STRING_PARAMETER_REGEX , $ argument )) {
215
- continue ;
216
- } elseif (preg_match (TestGenerator::PERSISTED_OBJECT_NOTATION_REGEX , $ argument )) {
217
- continue ;
218
- }
219
- // trim `data.field` to `data`
220
- preg_match ('/([^.]+)/ ' , $ argument , $ entityName );
221
- // Double check that {{data.field}} isn't an argument for an ActionGroup
222
- $ entity = $ this ->findEntity ($ entityName [1 ]);
223
- preg_match_all (self ::ACTIONGROUP_ARGUMENT_REGEX_PATTERN , $ contents , $ possibleArgument );
224
- if (array_search ($ entityName [1 ], $ possibleArgument [1 ]) !== false ) {
225
- continue ;
226
- }
227
- if ($ entity !== null ) {
228
- $ this ->allEntities [$ entity ->getName ()] = $ entity ;
229
- }
230
- }
231
- }
232
- }
233
-
234
- /**
235
- * Check `data` entities in {{data.field}} or {{data.field('param')}} and resolve references
236
- *
237
- * @param array $braceReferences
238
- * @param string $contents
239
- * @return void
240
- * @throws XmlException
241
-
242
- */
243
- private function resolveDataEntityReferences ($ braceReferences , $ contents )
244
- {
245
- foreach ($ braceReferences as $ reference ) {
246
- // trim `{{data.field}}` to `data`
247
- preg_match ('/{{([^.]+)/ ' , $ reference , $ entityName );
248
- // Double check that {{data.field}} isn't an argument for an ActionGroup
249
- $ entity = $ this ->findEntity ($ entityName [1 ]);
250
- preg_match_all (self ::ACTIONGROUP_ARGUMENT_REGEX_PATTERN , $ contents , $ possibleArgument );
251
- if (array_search ($ entityName [1 ], $ possibleArgument [1 ]) !== false ) {
252
- continue ;
253
- }
254
- if ($ entity !== null ) {
255
- $ this ->allEntities [$ entity ->getName ()] = $ entity ;
256
- }
257
- }
258
- }
259
-
260
- /**
261
- * Resolve entity references
262
- *
263
- * @param array $references
264
- * @return void
265
- * @throws XmlException
266
- */
267
- private function resolveEntityReferences ($ references )
268
- {
269
- foreach ($ references as $ reference ) {
270
- $ entity = $ this ->findEntity ($ reference );
271
- if ($ entity !== null ) {
272
- $ this ->allEntities [$ entity ->getName ()] = $ entity ;
273
- }
274
- }
275
- }
276
-
277
203
/**
278
204
* Find violating references
279
205
*
@@ -351,7 +277,10 @@ private function buildComposerDependencyList()
351
277
$ flattenedDependencies = [];
352
278
353
279
foreach ($ this ->moduleNameToPath as $ moduleName => $ pathToModule ) {
354
- $ composerData = json_decode (file_get_contents ($ pathToModule . DIRECTORY_SEPARATOR . "composer.json " ), true );
280
+ $ composerData = json_decode (
281
+ file_get_contents ($ pathToModule . DIRECTORY_SEPARATOR . "composer.json " ),
282
+ true
283
+ );
355
284
$ this ->allDependencies [$ moduleName ] = $ composerData ['require ' ];
356
285
}
357
286
foreach ($ this ->allDependencies as $ moduleName => $ dependencies ) {
@@ -403,43 +332,31 @@ private function getModuleDependenciesFromReferences($array)
403
332
// Should it append ALL filenames, including merges?
404
333
$ allFiles = explode (", " , $ item ->getFilename ());
405
334
foreach ($ allFiles as $ file ) {
406
- $ modulePath = dirname (dirname (dirname (dirname ($ file ))));
407
- $ fullModuleName = array_search ($ modulePath , $ this ->moduleNameToPath );
408
- $ composerModuleName = $ this ->moduleNameToComposerName [$ fullModuleName ];
409
- $ filenames [$ item ->getName ()][] = $ composerModuleName ;
335
+ $ moduleName = $ this ->getModuleName ($ file );
336
+ if (isset ($ this ->moduleNameToComposerName [$ moduleName ])) {
337
+ $ composerModuleName = $ this ->moduleNameToComposerName [$ moduleName ];
338
+ $ filenames [$ item ->getName ()][] = $ composerModuleName ;
339
+ }
410
340
}
411
341
}
412
342
return $ filenames ;
413
343
}
414
344
415
345
/**
416
- * Attempts to find any MFTF entity by its name. Returns null if none are found.
417
- * @param string $name
418
- * @return mixed
419
- * @throws XmlException
346
+ * Return module name for a file path
347
+ *
348
+ * @param string $filePath
349
+ * @return string|null
420
350
*/
421
- private function findEntity ( $ name )
351
+ private function getModuleName ( $ filePath )
422
352
{
423
- if ($ name == '_ENV ' || $ name == '_CREDS ' ) {
424
- return null ;
425
- }
426
-
427
- if (DataObjectHandler::getInstance ()->getObject ($ name )) {
428
- return DataObjectHandler::getInstance ()->getObject ($ name );
429
- } elseif (PageObjectHandler::getInstance ()->getObject ($ name )) {
430
- return PageObjectHandler::getInstance ()->getObject ($ name );
431
- } elseif (SectionObjectHandler::getInstance ()->getObject ($ name )) {
432
- return SectionObjectHandler::getInstance ()->getObject ($ name );
433
- }
434
-
435
- try {
436
- return ActionGroupObjectHandler::getInstance ()->getObject ($ name );
437
- } catch (TestReferenceException $ e ) {
438
- }
439
- try {
440
- return TestObjectHandler::getInstance ()->getObject ($ name );
441
- } catch (TestReferenceException $ e ) {
353
+ $ moduleName = null ;
354
+ foreach ($ this ->moduleNameToPath as $ name => $ path ) {
355
+ if (strpos ($ filePath , $ path ) !== false ) {
356
+ $ moduleName = $ name ;
357
+ break ;
358
+ }
442
359
}
443
- return null ;
360
+ return $ moduleName ;
444
361
}
445
362
}
0 commit comments