|
| 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 Symfony\Component\Console\Input\InputInterface; |
| 10 | +use Exception; |
| 11 | +use Magento\FunctionalTestingFramework\Util\Script\ScriptUtil; |
| 12 | +use Symfony\Component\Finder\SplFileInfo; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class ClassFileNamingCheck |
| 16 | + * @package Magento\FunctionalTestingFramework\StaticCheck |
| 17 | + */ |
| 18 | +class ClassFileNamingCheck implements StaticCheckInterface |
| 19 | +{ |
| 20 | + const ERROR_LOG_FILENAME = 'mftf-class-file-naming-check'; |
| 21 | + const ERROR_LOG_MESSAGE = 'MFTF Class File Naming Check'; |
| 22 | + const ALLOW_LIST_FILENAME = 'class-file-naming-allowlist'; |
| 23 | + const WARNING_LOG_FILENAME = 'mftf-class-file-naming-warnings'; |
| 24 | + |
| 25 | + /** |
| 26 | + * Array containing all warnings found after running the execute() function. |
| 27 | + * @var array |
| 28 | + */ |
| 29 | + private $warnings = []; |
| 30 | + /** |
| 31 | + * Array containing all errors found after running the execute() function. |
| 32 | + * @var array |
| 33 | + */ |
| 34 | + private $errors = []; |
| 35 | + |
| 36 | + /** |
| 37 | + * String representing the output summary found after running the execute() function. |
| 38 | + * @var string |
| 39 | + */ |
| 40 | + private $output; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var array $allowFailureEntities |
| 44 | + */ |
| 45 | + private $allowFailureEntities = []; |
| 46 | + |
| 47 | + /** |
| 48 | + * ScriptUtil instance |
| 49 | + * |
| 50 | + * @var ScriptUtil |
| 51 | + */ |
| 52 | + private $scriptUtil; |
| 53 | + /** |
| 54 | + * Checks usage of pause action in action groups, tests and suites and prints out error to file. |
| 55 | + * |
| 56 | + * @param InputInterface $input |
| 57 | + * @return void |
| 58 | + * @throws Exception |
| 59 | + */ |
| 60 | + public function execute(InputInterface $input) |
| 61 | + { |
| 62 | + $this->scriptUtil = new ScriptUtil(); |
| 63 | + $modulePaths = []; |
| 64 | + $path = $input->getOption('path'); |
| 65 | + if ($path) { |
| 66 | + if (!realpath($path)) { |
| 67 | + throw new \InvalidArgumentException('Invalid --path option: ' . $path); |
| 68 | + } |
| 69 | + $modulePaths[] = realpath($path); |
| 70 | + } else { |
| 71 | + $modulePaths = $this->scriptUtil->getAllModulePaths(); |
| 72 | + } |
| 73 | + foreach ($modulePaths as $modulePath) { |
| 74 | + if (file_exists($modulePath . DIRECTORY_SEPARATOR . self::ALLOW_LIST_FILENAME)) { |
| 75 | + $contents = file_get_contents($modulePath . DIRECTORY_SEPARATOR . self::ALLOW_LIST_FILENAME); |
| 76 | + foreach (explode("\n", $contents) as $entity) { |
| 77 | + $this->allowFailureEntities[$entity] = true; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + $testXmlFiles = $this->scriptUtil->getModuleXmlFilesByScope($modulePaths, "Test"); |
| 82 | + $actionGroupXmlFiles = $this->scriptUtil->getModuleXmlFilesByScope($modulePaths, "ActionGroup"); |
| 83 | + $pageXmlFiles = $this->scriptUtil->getModuleXmlFilesByScope($modulePaths, "Page"); |
| 84 | + $sectionXmlFiles = $this->scriptUtil->getModuleXmlFilesByScope($modulePaths, "Section"); |
| 85 | + $suiteXmlFiles = $this->scriptUtil->getModuleXmlFilesByScope($modulePaths, 'Suite'); |
| 86 | + $this->errors = []; |
| 87 | + $this->errors += $this->findErrorsInFileSet($testXmlFiles, 'test'); |
| 88 | + $this->errors += $this->findErrorsInFileSet($actionGroupXmlFiles, 'actionGroup'); |
| 89 | + $this->errors += $this->findErrorsInFileSet($pageXmlFiles, 'page'); |
| 90 | + $this->errors += $this->findErrorsInFileSet($sectionXmlFiles, 'section'); |
| 91 | + $this->errors += $this->findErrorsInFileSet($suiteXmlFiles, 'suite'); |
| 92 | + |
| 93 | + // hold on to the output and print any errors to a file |
| 94 | + $this->output = $this->scriptUtil->printErrorsToFile( |
| 95 | + $this->errors, |
| 96 | + StaticChecksList::getErrorFilesPath() . DIRECTORY_SEPARATOR . self::ERROR_LOG_FILENAME . '.txt', |
| 97 | + self::ERROR_LOG_MESSAGE |
| 98 | + ); |
| 99 | + if (!empty($this->warnings)) { |
| 100 | + $this->output .= "\n " . $this->scriptUtil->printWarningsToFile( |
| 101 | + $this->warnings, |
| 102 | + StaticChecksList::getErrorFilesPath() . DIRECTORY_SEPARATOR . self::WARNING_LOG_FILENAME . '.txt', |
| 103 | + self::ERROR_LOG_MESSAGE |
| 104 | + ); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Return array containing all errors found after running the execute() function. |
| 110 | + * @return array |
| 111 | + */ |
| 112 | + public function getErrors() |
| 113 | + { |
| 114 | + return $this->errors; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Return string of a short human readable result of the check. For example: "No errors found." |
| 119 | + * @return string |
| 120 | + */ |
| 121 | + public function getOutput() |
| 122 | + { |
| 123 | + return $this->output; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Returns Violations if found |
| 128 | + * @param SplFileInfo $files |
| 129 | + * @param string $fileType |
| 130 | + * @return array |
| 131 | + */ |
| 132 | + public function findErrorsInFileSet($files, $fileType) |
| 133 | + { |
| 134 | + $errors = []; |
| 135 | + /** @var SplFileInfo $filePath */ |
| 136 | + |
| 137 | + foreach ($files as $filePath) { |
| 138 | + $fileNameWithoutExtension = pathinfo($filePath->getFilename(), PATHINFO_FILENAME); |
| 139 | + $domDocument = new \DOMDocument(); |
| 140 | + $domDocument->load($filePath); |
| 141 | + $testResult = $this->getAttributesFromDOMNodeList( |
| 142 | + $domDocument->getElementsByTagName($fileType), |
| 143 | + ["type" => 'name'] |
| 144 | + ); |
| 145 | + if ($fileNameWithoutExtension != array_values($testResult[0])[0]) { |
| 146 | + $isInAllowList = array_key_exists(array_values($testResult[0])[0], $this->allowFailureEntities); |
| 147 | + if ($isInAllowList) { |
| 148 | + $errorOutput = ucfirst($fileType). " name does not match with file name |
| 149 | + {$filePath->getRealPath()}. ".ucfirst($fileType)." ".array_values($testResult[0])[0]; |
| 150 | + $this->warnings[$filePath->getFilename()][] = $errorOutput; |
| 151 | + continue; |
| 152 | + } |
| 153 | + $errorOutput = ucfirst($fileType). " name does not match with file name |
| 154 | + {$filePath->getRealPath()}. ".ucfirst($fileType)." ".array_values($testResult[0])[0]; |
| 155 | + $errors[$filePath->getFilename()][] = $errorOutput; |
| 156 | + } |
| 157 | + } |
| 158 | + return $errors; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Return attribute value for each node in DOMNodeList as an array |
| 163 | + * |
| 164 | + * @param DOMNodeList $nodes |
| 165 | + * @param string $attributeName |
| 166 | + * @return array |
| 167 | + */ |
| 168 | + public function getAttributesFromDOMNodeList($nodes, $attributeName) |
| 169 | + { |
| 170 | + $attributes = []; |
| 171 | + foreach ($nodes as $node) { |
| 172 | + if (is_string($attributeName)) { |
| 173 | + $attributeValue = $node->getAttribute($attributeName); |
| 174 | + } else { |
| 175 | + $attributeValue = [$node->getAttribute(key($attributeName)) => |
| 176 | + $node->getAttribute($attributeName[key($attributeName)])]; |
| 177 | + } |
| 178 | + if (!empty($attributeValue)) { |
| 179 | + $attributes[] = $attributeValue; |
| 180 | + } |
| 181 | + } |
| 182 | + return $attributes; |
| 183 | + } |
| 184 | +} |
0 commit comments