Skip to content

Commit 295bae7

Browse files
committed
MQE-2045: Upgrade script to remove unused arguments
Fixed static checks, extracted some code into methods
1 parent 5e1c121 commit 295bae7

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

src/Magento/FunctionalTestingFramework/StaticCheck/ActionGroupArgumentsCheck.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,8 @@ private function findErrorsInFileSet($files)
9999
/** @var SplFileInfo $filePath */
100100
foreach ($files as $filePath) {
101101
$contents = $filePath->getContents();
102-
$domDocument = new \DOMDocument();
103-
$domDocument->load($filePath);
104102
/** @var DOMElement $actionGroup */
105-
$actionGroup = $domDocument->getElementsByTagName('actionGroup')->item(0);
103+
$actionGroup = $this->getActionGroupDomElement($contents);
106104
$arguments = $this->extractActionGroupArguments($actionGroup);
107105
$unusedArguments = $this->findUnusedArguments($arguments, $contents);
108106
if (!empty($unusedArguments)) {
@@ -113,6 +111,18 @@ private function findErrorsInFileSet($files)
113111
return $actionGroupErrors;
114112
}
115113

114+
/**
115+
* Extract actionGroup DomElement from xml file
116+
* @param string $contents
117+
* @return \DOMElement
118+
*/
119+
public function getActionGroupDomElement($contents)
120+
{
121+
$domDocument = new \DOMDocument();
122+
$domDocument->loadXML($contents);
123+
return $domDocument->getElementsByTagName('actionGroup')[0];
124+
}
125+
116126
/**
117127
* Get list of action group arguments declared in an action group
118128
* @param \DOMElement $actionGroup
@@ -123,9 +133,7 @@ public function extractActionGroupArguments($actionGroup)
123133
$arguments = [];
124134
$argumentsNodes = $actionGroup->getElementsByTagName('arguments');
125135
if ($argumentsNodes->length > 0) {
126-
/** @var DOMElement $argumentsNode */
127-
$argumentsNode = $argumentsNodes->item(0);
128-
$argumentNodes = $argumentsNode->getElementsByTagName('argument');
136+
$argumentNodes = $argumentsNodes[0]->getElementsByTagName('argument');
129137
foreach ($argumentNodes as $argumentNode) {
130138
$arguments[] = $argumentNode->getAttribute('name');
131139
}

src/Magento/FunctionalTestingFramework/Upgrade/RemoveUnusedArguments.php

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
*/
2020
class RemoveUnusedArguments implements UpgradeInterface
2121
{
22+
const ARGUMENTS_BLOCK_REGEX_PATTERN = "/\s*<arguments.*\/arguments>/s";
23+
2224
/**
23-
* Upgrades all test xml files
25+
* Updates all actionGroup xml files
2426
*
2527
* @param InputInterface $input
2628
* @param OutputInterface $output
@@ -33,36 +35,30 @@ public function execute(InputInterface $input, OutputInterface $output)
3335
if (empty($testPaths[0])) {
3436
$testPaths = $scriptUtil->getAllModulePaths();
3537
}
36-
$actionGroupXmlFiles = $scriptUtil->getModuleXmlFilesByScope($testPaths, DIRECTORY_SEPARATOR . 'ActionGroup' . DIRECTORY_SEPARATOR);
38+
$xmlFiles = $scriptUtil->getModuleXmlFilesByScope($testPaths, 'ActionGroup');
3739
$actionGroupsUpdated = 0;
3840
$fileSystem = new Filesystem();
39-
foreach ($actionGroupXmlFiles as $actionGroupXml) {
40-
$contents = $actionGroupXml->getContents();
41-
$domDocument = new \DOMDocument();
42-
$domDocument->load($actionGroupXml);
43-
$actionGroupArgumentsCheck = new ActionGroupArgumentsCheck();
44-
$unusedArgumentsFound = false;
41+
foreach ($xmlFiles as $file) {
42+
$contents = $file->getContents();
43+
$argumentsCheck = new ActionGroupArgumentsCheck();
4544
/** @var DOMElement $actionGroup */
46-
$actionGroup = $domDocument->getElementsByTagName('actionGroup')->item(0);
47-
$arguments = $actionGroupArgumentsCheck->extractActionGroupArguments($actionGroup);
48-
$unusedArguments = $actionGroupArgumentsCheck->findUnusedArguments($arguments, $contents);
49-
if (sizeof($unusedArguments) == 0) {
45+
$actionGroup = $argumentsCheck->getActionGroupDomElement($contents);
46+
$allArguments = $argumentsCheck->extractActionGroupArguments($actionGroup);
47+
$unusedArguments = $argumentsCheck->findUnusedArguments($allArguments, $contents);
48+
if (empty($unusedArguments)) {
5049
continue;
5150
}
52-
foreach ($unusedArguments as $argument) {
53-
$unusedArgumentsFound = true;
54-
$contents = preg_replace("/\s*<argument.*".$argument.".*\/>/", "", $contents);
51+
//Remove <arguments> block if all arguments are unused
52+
if (!empty(array_diff($allArguments, $unusedArguments))) {
53+
$contents = preg_replace(self::ARGUMENTS_BLOCK_REGEX_PATTERN, '', $contents);
5554
}
56-
$newDomDocument = new \DOMDocument();
57-
$newDomDocument->loadXML($contents);
58-
if ($unusedArgumentsFound) {
59-
if ($newDomDocument->getElementsByTagName("argument")->length == 0) {
60-
$contents = preg_replace("/\s*<arguments.*\/arguments>/s", "", $contents);
55+
else {
56+
foreach ($unusedArguments as $argument) {
57+
$contents = preg_replace("/\s*<argument.*".$argument.".*\/>/", '', $contents);
6158
}
62-
$fileSystem->dumpFile($actionGroupXml->getRealPath(), $contents);
63-
$actionGroupsUpdated++;
64-
6559
}
60+
$fileSystem->dumpFile($file->getRealPath(), $contents);
61+
$actionGroupsUpdated++;
6662
}
6763
return "Finished removing unused action group arguments from " . $actionGroupsUpdated . " files.";
6864
}

0 commit comments

Comments
 (0)