|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\FunctionalTestingFramework\StaticCheck; |
| 8 | + |
| 9 | +use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig; |
| 10 | +use Magento\FunctionalTestingFramework\Exceptions\XmlException; |
| 11 | +use Magento\FunctionalTestingFramework\Test\Handlers\ActionGroupObjectHandler; |
| 12 | +use Magento\FunctionalTestingFramework\Test\Objects\ActionGroupObject; |
| 13 | +use Symfony\Component\Console\Input\InputInterface; |
| 14 | +use Magento\FunctionalTestingFramework\Util\ModuleResolver; |
| 15 | +use Symfony\Component\Finder\Finder; |
| 16 | +use Exception; |
| 17 | + |
| 18 | +/** |
| 19 | + * Class ActionGroupArgumentsCheck |
| 20 | + * @package Magento\FunctionalTestingFramework\StaticCheck |
| 21 | + */ |
| 22 | +class ActionGroupArgumentsCheck implements StaticCheckInterface |
| 23 | +{ |
| 24 | + |
| 25 | + const ACTIONGROUP_XML_REGEX_PATTERN = '/<actionGroup\sname=(?: (?!<\/actionGroup>).)*/mxs'; |
| 26 | + const ACTIONGROUP_ARGUMENT_REGEX_PATTERN = '/<argument[^\/>]*name="([^"\']*)/mxs'; |
| 27 | + const ACTIONGROUP_NAME_REGEX_PATTERN = '/<actionGroup name=["\']([^\'"]*)/'; |
| 28 | + |
| 29 | + const ERROR_LOG_FILENAME = 'mftf-arguments-checks'; |
| 30 | + const ERROR_LOG_MESSAGE = 'MFTF Action Group Unused Arguments Check'; |
| 31 | + |
| 32 | + /** |
| 33 | + * Array containing all errors found after running the execute() function. |
| 34 | + * @var array |
| 35 | + */ |
| 36 | + private $errors = []; |
| 37 | + |
| 38 | + /** |
| 39 | + * String representing the output summary found after running the execute() function. |
| 40 | + * @var string |
| 41 | + */ |
| 42 | + private $output; |
| 43 | + |
| 44 | + /** |
| 45 | + * Checks unused arguments in action groups and prints out error to file. |
| 46 | + * |
| 47 | + * @param InputInterface $input |
| 48 | + * @return void |
| 49 | + * @throws Exception |
| 50 | + */ |
| 51 | + public function execute(InputInterface $input) |
| 52 | + { |
| 53 | + MftfApplicationConfig::create( |
| 54 | + true, |
| 55 | + MftfApplicationConfig::UNIT_TEST_PHASE, |
| 56 | + false, |
| 57 | + MftfApplicationConfig::LEVEL_NONE, |
| 58 | + true |
| 59 | + ); |
| 60 | + |
| 61 | + $allModules = ModuleResolver::getInstance()->getModulesPath(); |
| 62 | + |
| 63 | + $actionGroupXmlFiles = StaticCheckHelper::buildFileList( |
| 64 | + $allModules, |
| 65 | + DIRECTORY_SEPARATOR . 'ActionGroup' . DIRECTORY_SEPARATOR |
| 66 | + ); |
| 67 | + |
| 68 | + $this->errors = $this->findErrorsInFileSet($actionGroupXmlFiles); |
| 69 | + |
| 70 | + $this->output = StaticCheckHelper::printErrorsToFile( |
| 71 | + $this->errors, |
| 72 | + self::ERROR_LOG_FILENAME, |
| 73 | + self::ERROR_LOG_MESSAGE |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Return array containing all errors found after running the execute() function. |
| 79 | + * @return array |
| 80 | + */ |
| 81 | + public function getErrors() |
| 82 | + { |
| 83 | + return $this->errors; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Return string of a short human readable result of the check. For example: "No unused arguments found." |
| 88 | + * @return string |
| 89 | + */ |
| 90 | + public function getOutput() |
| 91 | + { |
| 92 | + return $this->output; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Finds all unused arguments in given set of actionGroup files |
| 97 | + * @param Finder $files |
| 98 | + * @return array $testErrors |
| 99 | + */ |
| 100 | + private function findErrorsInFileSet($files) |
| 101 | + { |
| 102 | + $actionGroupErrors = []; |
| 103 | + foreach ($files as $filePath) { |
| 104 | + $contents = file_get_contents($filePath); |
| 105 | + preg_match_all(self::ACTIONGROUP_XML_REGEX_PATTERN, $contents, $actionGroups); |
| 106 | + $actionGroupToArguments = $this->buildUnusedArgumentList($actionGroups[0]); |
| 107 | + $actionGroupErrors += $this->setErrorOutput($actionGroupToArguments, $filePath); |
| 108 | + } |
| 109 | + return $actionGroupErrors; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Builds array of action group => unused arguments |
| 114 | + * @param array $actionGroups |
| 115 | + * @return array $actionGroupToArguments |
| 116 | + */ |
| 117 | + private function buildUnusedArgumentList($actionGroups) |
| 118 | + { |
| 119 | + $actionGroupToArguments = []; |
| 120 | + |
| 121 | + foreach ($actionGroups as $actionGroupXml) { |
| 122 | + preg_match(self::ACTIONGROUP_NAME_REGEX_PATTERN, $actionGroupXml, $actionGroupName); |
| 123 | + $unusedArguments = $this->findUnusedArguments($actionGroupXml); |
| 124 | + if (!empty($unusedArguments)) { |
| 125 | + $actionGroupToArguments[$actionGroupName[1]] = $unusedArguments; |
| 126 | + } |
| 127 | + } |
| 128 | + return $actionGroupToArguments; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Returns unused arguments in an action group |
| 133 | + * @param string $actionGroupXml |
| 134 | + * @return array |
| 135 | + */ |
| 136 | + private function findUnusedArguments($actionGroupXml) |
| 137 | + { |
| 138 | + $unusedArguments = []; |
| 139 | + |
| 140 | + preg_match_all(self::ACTIONGROUP_ARGUMENT_REGEX_PATTERN, $actionGroupXml, $arguments); |
| 141 | + preg_match(self::ACTIONGROUP_NAME_REGEX_PATTERN, $actionGroupXml, $actionGroupName); |
| 142 | + try { |
| 143 | + $actionGroup = ActionGroupObjectHandler::getInstance()->getObject($actionGroupName[1]); |
| 144 | + } catch (XmlException $e) { |
| 145 | + } |
| 146 | + foreach ($arguments[1] as $argument) { |
| 147 | + //pattern to match all argument references |
| 148 | + $patterns = [ |
| 149 | + '(\{{2}' . $argument . '(\.[a-zA-Z0-9_\[\]\(\).,\'\/ ]+)?}{2})', |
| 150 | + '([(,\s\'$$]' . $argument . '(\.[a-zA-Z0-9_$\[\]]+)?[),\s\'])' |
| 151 | + ]; |
| 152 | + // matches entity references |
| 153 | + if (preg_match($patterns[0], $actionGroupXml)) { |
| 154 | + continue; |
| 155 | + } |
| 156 | + //matches parametrized references |
| 157 | + if (preg_match($patterns[1], $actionGroupXml)) { |
| 158 | + continue; |
| 159 | + } |
| 160 | + //for extending action groups, exclude arguments that are also defined in parent action group |
| 161 | + if ($this->isParentActionGroupArgument($argument, $actionGroup)) { |
| 162 | + continue; |
| 163 | + } |
| 164 | + $unusedArguments[] = $argument; |
| 165 | + } |
| 166 | + return $unusedArguments; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Checks if the argument is also defined in the parent for extending action groups. |
| 171 | + * @param string $argument |
| 172 | + * @param ActionGroupObject $actionGroup |
| 173 | + * @return boolean |
| 174 | + */ |
| 175 | + private function isParentActionGroupArgument($argument, $actionGroup) |
| 176 | + { |
| 177 | + $parentActionGroupName = $actionGroup->getParentName(); |
| 178 | + if ($parentActionGroupName !== null) { |
| 179 | + $parentActionGroup = ActionGroupObjectHandler::getInstance()->getObject($parentActionGroupName); |
| 180 | + $parentArguments = $parentActionGroup->getArguments(); |
| 181 | + foreach ($parentArguments as $parentArgument) { |
| 182 | + if ($argument === $parentArgument->getName()) { |
| 183 | + return true; |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + return false; |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Builds and returns error output for violating references |
| 192 | + * |
| 193 | + * @param array $actionGroupToArguments |
| 194 | + * @param string $path |
| 195 | + * @return mixed |
| 196 | + */ |
| 197 | + private function setErrorOutput($actionGroupToArguments, $path) |
| 198 | + { |
| 199 | + $actionGroupErrors = []; |
| 200 | + if (!empty($actionGroupToArguments)) { |
| 201 | + // Build error output |
| 202 | + $errorOutput = "\nFile \"{$path->getRealPath()}\""; |
| 203 | + $errorOutput .= "\ncontains action group(s) with unused arguments.\n\t\t"; |
| 204 | + foreach ($actionGroupToArguments as $actionGroup => $arguments) { |
| 205 | + $errorOutput .= "\n\t {$actionGroup} has unused argument(s): " . implode(", ", $arguments); |
| 206 | + } |
| 207 | + $actionGroupErrors[$path->getRealPath()][] = $errorOutput; |
| 208 | + } |
| 209 | + return $actionGroupErrors; |
| 210 | + } |
| 211 | +} |
0 commit comments