Skip to content

Commit c406697

Browse files
committed
MQE-1957: Entity Deprecation Reference - Static Check
1 parent 8448009 commit c406697

File tree

3 files changed

+158
-63
lines changed

3 files changed

+158
-63
lines changed

src/Magento/FunctionalTestingFramework/Config/FileResolver/Root.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function get($filename, $scope)
3333
);
3434

3535
// include root suite dir when running standalone version
36-
$altPath = MAGENTO_BP . DIRECTORY_SEPARATOR . 'dev/tests/acceptance';
36+
$altPath = FilePathFormatter::format(MAGENTO_BP) . 'dev/tests/acceptance';
3737

3838
if (realpath($altPath) && ($altPath !== TESTS_BP)) {
3939
$paths = array_merge(

src/Magento/FunctionalTestingFramework/StaticCheck/DeprecatedEntityUsageCheck.php

Lines changed: 125 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
1010
use InvalidArgumentException;
1111
use Exception;
12+
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
1213
use Magento\FunctionalTestingFramework\Exceptions\XmlException;
1314
use Magento\FunctionalTestingFramework\Page\Objects\SectionObject;
1415
use Magento\FunctionalTestingFramework\Test\Objects\ActionObject;
@@ -68,6 +69,41 @@ class DeprecatedEntityUsageCheck implements StaticCheckInterface
6869
*/
6970
private $dataOperations = ['create', 'update', 'get', 'delete'];
7071

72+
/**
73+
* Test xml files to scan
74+
*
75+
* @var Finder|array
76+
*/
77+
private $testXmlFiles = [];
78+
79+
/**
80+
* Action group xml files to scan
81+
*
82+
* @var Finder|array
83+
*/
84+
private $actionGroupXmlFiles = [];
85+
86+
/**
87+
* Suite xml files to scan
88+
*
89+
* @var Finder|array
90+
*/
91+
private $suiteXmlFiles = [];
92+
93+
/**
94+
* Root suite xml files to scan
95+
*
96+
* @var Finder|array
97+
*/
98+
private $rootSuiteXmlFiles = [];
99+
100+
/**
101+
* Data xml files to scan
102+
*
103+
* @var Finder|array
104+
*/
105+
private $dataXmlFiles = [];
106+
71107
/**
72108
* Checks test dependencies, determined by references in tests versus the dependencies listed in the Magento module
73109
*
@@ -78,45 +114,16 @@ class DeprecatedEntityUsageCheck implements StaticCheckInterface
78114
public function execute(InputInterface $input)
79115
{
80116
$this->scriptUtil = new ScriptUtil();
81-
82-
$modulePaths = [];
83-
$includeRootPath = true;
84-
$path = $input->getOption('path');
85-
if ($path) {
86-
if (!realpath($path)) {
87-
throw new InvalidArgumentException("Invalid --path option: " . $path);
88-
}
89-
MftfApplicationConfig::create(
90-
true,
91-
MftfApplicationConfig::UNIT_TEST_PHASE,
92-
false,
93-
MftfApplicationConfig::LEVEL_DEFAULT,
94-
true
95-
);
96-
putenv('CUSTOM_MODULE_PATHS=' . realpath($path));
97-
$modulePaths[] = realpath($path);
98-
$includeRootPath = false;
99-
} else {
100-
$modulePaths = $this->scriptUtil->getAllModulePaths();
101-
}
102-
103-
// These files can contain references to other entities
104-
$testXmlFiles = $this->scriptUtil->getModuleXmlFilesByScope($modulePaths, 'Test');
105-
$actionGroupXmlFiles = $this->scriptUtil->getModuleXmlFilesByScope($modulePaths, 'ActionGroup');
106-
$suiteXmlFiles = $this->scriptUtil->getModuleXmlFilesByScope($modulePaths, 'Suite');
107-
if ($includeRootPath) {
108-
$rootSuiteXmlFiles = $this->scriptUtil->getRootSuiteXmlFiles();
109-
}
110-
$dataXmlFiles= $this->scriptUtil->getModuleXmlFilesByScope($modulePaths, 'Data');
117+
$this->loadAllXmlFiles($input);
111118

112119
$this->errors = [];
113-
$this->errors += $this->findReferenceErrorsInActionFiles($testXmlFiles);
114-
$this->errors += $this->findReferenceErrorsInActionFiles($actionGroupXmlFiles);
115-
$this->errors += $this->findReferenceErrorsInActionFiles($suiteXmlFiles, true);
116-
if ($includeRootPath && !empty($rootSuiteXmlFiles)) {
117-
$this->errors += $this->findReferenceErrorsInActionFiles($rootSuiteXmlFiles, true);
120+
$this->errors += $this->findReferenceErrorsInActionFiles($this->testXmlFiles);
121+
$this->errors += $this->findReferenceErrorsInActionFiles($this->actionGroupXmlFiles);
122+
$this->errors += $this->findReferenceErrorsInActionFiles($this->suiteXmlFiles, true);
123+
if (!empty($this->rootSuiteXmlFiles)) {
124+
$this->errors += $this->findReferenceErrorsInActionFiles($this->rootSuiteXmlFiles, true);
118125
}
119-
$this->errors += $this->findReferenceErrorsInDataFiles($dataXmlFiles);
126+
$this->errors += $this->findReferenceErrorsInDataFiles($this->dataXmlFiles);
120127

121128
// Hold on to the output and print any errors to a file
122129
$this->output = $this->scriptUtil->printErrorsToFile(
@@ -146,6 +153,63 @@ public function getOutput()
146153
return $this->output;
147154
}
148155

156+
/**
157+
* Read all XML files for scanning
158+
*
159+
* @param InputInterface $input
160+
* @throws Exception
161+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
162+
*/
163+
private function loadAllXmlFiles($input)
164+
{
165+
$modulePaths = [];
166+
$includeRootPath = true;
167+
$path = $input->getOption('path');
168+
if ($path) {
169+
if (!realpath($path)) {
170+
throw new InvalidArgumentException('Invalid --path option: ' . $path);
171+
}
172+
MftfApplicationConfig::create(
173+
true,
174+
MftfApplicationConfig::UNIT_TEST_PHASE,
175+
false,
176+
MftfApplicationConfig::LEVEL_DEFAULT,
177+
true
178+
);
179+
putenv('CUSTOM_MODULE_PATHS=' . realpath($path));
180+
$modulePaths[] = realpath($path);
181+
$includeRootPath = false;
182+
} else {
183+
$modulePaths = $this->scriptUtil->getAllModulePaths();
184+
}
185+
186+
// These files can contain references to other entities
187+
$this->testXmlFiles = $this->scriptUtil->getModuleXmlFilesByScope($modulePaths, 'Test');
188+
$this->actionGroupXmlFiles = $this->scriptUtil->getModuleXmlFilesByScope($modulePaths, 'ActionGroup');
189+
$this->suiteXmlFiles = $this->scriptUtil->getModuleXmlFilesByScope($modulePaths, 'Suite');
190+
if ($includeRootPath) {
191+
$this->rootSuiteXmlFiles = $this->scriptUtil->getRootSuiteXmlFiles();
192+
}
193+
$this->dataXmlFiles= $this->scriptUtil->getModuleXmlFilesByScope($modulePaths, 'Data');
194+
195+
if (empty($this->thistestXmlFiles)
196+
&& empty($this->actionGroupXmlFiles)
197+
&& empty($this->suiteXmlFiles)
198+
&& empty($this->dataXmlFiles)
199+
) {
200+
if ($path) {
201+
throw new InvalidArgumentException(
202+
'Invalid --path option: '
203+
. $path
204+
. PHP_EOL
205+
. 'Please make sure --path points to a valid MFTF Test Module.'
206+
);
207+
} elseif (empty($this->rootSuiteXmlFiles)) {
208+
throw new TestFrameworkException('No xml file to scan.');
209+
}
210+
}
211+
}
212+
149213
/**
150214
* Find reference errors in set of action files
151215
*
@@ -190,58 +254,39 @@ private function findReferenceErrorsInActionFiles($files, $checkTestRef = false)
190254

191255
// Resolve entity references
192256
$entityReferences = $this->scriptUtil->resolveEntityReferences($braceReferences[0], $contents, true);
193-
194257
// Resolve parameterized references
195258
$entityReferences = array_merge(
196259
$entityReferences,
197260
$this->scriptUtil->resolveParametrizedReferences($braceReferences[2], $contents, true)
198261
);
199-
200262
// Resolve action group entity by names
201263
$entityReferences = array_merge(
202264
$entityReferences,
203265
$this->scriptUtil->resolveEntityByNames($actionGroupReferences[1])
204266
);
205-
206267
// Resolve extends entity by names
207268
$entityReferences = array_merge(
208269
$entityReferences,
209270
$this->scriptUtil->resolveEntityByNames($extendReferences[1])
210271
);
211-
212272
// Resolve create data entity by names
213273
$entityReferences = array_merge(
214274
$entityReferences,
215275
$this->scriptUtil->resolveEntityByNames($createdDataReferences)
216276
);
217-
218277
// Resolve update data entity by names
219278
$entityReferences = array_merge(
220279
$entityReferences,
221280
$this->scriptUtil->resolveEntityByNames($updatedDataReferences)
222281
);
223-
224282
// Resolve get data entity by names
225283
$entityReferences = array_merge(
226284
$entityReferences,
227285
$this->scriptUtil->resolveEntityByNames($getDataReferences)
228286
);
229-
230287
// Find test references if needed
231288
if ($checkTestRef) {
232-
$testReferences = $this->getAttributesFromDOMNodeList(
233-
$domDocument->getElementsByTagName('test'),
234-
'name'
235-
);
236-
237-
// Remove Duplicates
238-
$testReferences = array_unique($testReferences);
239-
240-
// Resolve test entity by names
241-
$entityReferences = array_merge(
242-
$entityReferences,
243-
$this->scriptUtil->resolveEntityByNames($testReferences)
244-
);
289+
$entityReferences = array_merge($entityReferences, $this->resolveTestEntityInSuite($domDocument));
245290
}
246291

247292
// Find violating references
@@ -602,6 +647,30 @@ private function setErrorOutput($violatingReferences, $path)
602647
return $testErrors;
603648
}
604649

650+
/**
651+
* Resolve test entity in suite
652+
*
653+
* @param \DOMDocument $domDocument
654+
* @return array
655+
*/
656+
private function resolveTestEntityInSuite($domDocument)
657+
{
658+
$testReferences = $this->getAttributesFromDOMNodeList(
659+
$domDocument->getElementsByTagName('test'),
660+
'name'
661+
);
662+
663+
// Remove Duplicates
664+
$testReferences = array_unique($testReferences);
665+
666+
// Resolve test entity by names
667+
try {
668+
return $this->scriptUtil->resolveEntityByNames($testReferences);
669+
} catch (XmlException $e) {
670+
return [];
671+
}
672+
}
673+
605674
/**
606675
* Return subject string for a class name
607676
*

src/Magento/FunctionalTestingFramework/Util/Script/ScriptUtil.php

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class ScriptUtil
3232
{
3333
const ACTIONGROUP_ARGUMENT_REGEX_PATTERN = '/<argument[^\/>]*name="([^"\']*)/';
3434
const ROOT_SUITE_DIR = 'tests/_suite';
35+
const DEV_TESTS_DIR = 'dev/tests/acceptance/';
3536

3637
/**
3738
* Return all installed Magento module paths
@@ -106,18 +107,43 @@ public function getModuleXmlFilesByScope($modulePaths, $scope)
106107
* Return suite XML files in TESTS_BP/ROOT_SUITE_DIR directory
107108
*
108109
* @return Finder|array
109-
* @throws TestFrameworkException
110+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
110111
*/
111112
public function getRootSuiteXmlFiles()
112113
{
113-
$rootSuitePath = FilePathFormatter::format(TESTS_BP) . self::ROOT_SUITE_DIR;
114-
if (!realpath($rootSuitePath)) {
115-
return [];
114+
$rootSuitePaths = [];
115+
$defaultTestPath = null;
116+
$devTestsPath = null;
117+
118+
try {
119+
$defaultTestPath = FilePathFormatter::format(TESTS_BP);
120+
} catch (TestFrameworkException $e) {
121+
}
122+
123+
try {
124+
$devTestsPath = FilePathFormatter::format(MAGENTO_BP) . self::DEV_TESTS_DIR;
125+
} catch (TestFrameworkException $e) {
126+
}
127+
128+
if ($defaultTestPath) {
129+
$rootSuitePaths[] = $defaultTestPath . self::ROOT_SUITE_DIR;
116130
}
131+
132+
if ($devTestsPath && realpath($devTestsPath) && $devTestsPath !== $defaultTestPath) {
133+
$rootSuitePaths[] = $devTestsPath . self::ROOT_SUITE_DIR;
134+
}
135+
136+
$found = false;
117137
$finder = new Finder();
118-
$finder->files()->followLinks()->in($rootSuitePath)->name("*.xml");
138+
foreach ($rootSuitePaths as $rootSuitePath) {
139+
if (!realpath($rootSuitePath)) {
140+
continue;
141+
}
142+
$finder->files()->followLinks()->in($rootSuitePath)->name("*.xml");
143+
$found = true;
144+
}
119145

120-
return $finder->files();
146+
return $found ? $finder->files() : [];
121147
}
122148

123149
/**

0 commit comments

Comments
 (0)