|
| 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 InvalidArgumentException; |
| 11 | +use Exception; |
| 12 | +use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException; |
| 13 | +use Magento\FunctionalTestingFramework\Exceptions\XmlException; |
| 14 | +use Magento\FunctionalTestingFramework\Page\Objects\SectionObject; |
| 15 | +use Magento\FunctionalTestingFramework\Test\Objects\ActionObject; |
| 16 | +use Magento\FunctionalTestingFramework\Page\Objects\ElementObject; |
| 17 | +use Magento\FunctionalTestingFramework\Test\Objects\ActionGroupObject; |
| 18 | +use Magento\FunctionalTestingFramework\Page\Objects\PageObject; |
| 19 | +use Magento\FunctionalTestingFramework\Test\Objects\TestObject; |
| 20 | +use Symfony\Component\Console\Input\InputInterface; |
| 21 | +use Symfony\Component\Finder\Finder; |
| 22 | +use Magento\FunctionalTestingFramework\Util\Script\ScriptUtil; |
| 23 | +use Magento\FunctionalTestingFramework\DataGenerator\Handlers\OperationDefinitionObjectHandler; |
| 24 | +use Magento\FunctionalTestingFramework\DataGenerator\Objects\OperationDefinitionObject; |
| 25 | +use Magento\FunctionalTestingFramework\DataGenerator\Handlers\DataObjectHandler; |
| 26 | +use Magento\FunctionalTestingFramework\DataGenerator\Objects\EntityDataObject; |
| 27 | +use Symfony\Component\Finder\SplFileInfo; |
| 28 | +use DOMNodeList; |
| 29 | +use DOMElement; |
| 30 | + |
| 31 | +/** |
| 32 | + * Class CreatedDataFromOutsideActionGroupCheck |
| 33 | + * @package Magento\FunctionalTestingFramework\StaticCheck |
| 34 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 35 | + */ |
| 36 | +class CreatedDataFromOutsideActionGroupCheck implements StaticCheckInterface |
| 37 | +{ |
| 38 | + const ACTIONGROUP_REGEX_PATTERN = '/\$(\$)*([\w.]+)(\$)*\$/'; |
| 39 | + |
| 40 | + const ERROR_LOG_FILENAME = 'create-data-from-outside-action-group'; |
| 41 | + const ERROR_LOG_MESSAGE = 'Created Data From Outside Action Group'; |
| 42 | + |
| 43 | + /** |
| 44 | + * Array containing all errors found after running the execute() function |
| 45 | + * |
| 46 | + * @var array |
| 47 | + */ |
| 48 | + private $errors = []; |
| 49 | + |
| 50 | + /** |
| 51 | + * String representing the output summary found after running the execute() function |
| 52 | + * |
| 53 | + * @var string |
| 54 | + */ |
| 55 | + private $output; |
| 56 | + |
| 57 | + /** |
| 58 | + * ScriptUtil instance |
| 59 | + * |
| 60 | + * @var ScriptUtil |
| 61 | + */ |
| 62 | + private $scriptUtil; |
| 63 | + |
| 64 | + /** |
| 65 | + * Action group xml files to scan |
| 66 | + * |
| 67 | + * @var Finder|array |
| 68 | + */ |
| 69 | + private $actionGroupXmlFiles = []; |
| 70 | + |
| 71 | + /** |
| 72 | + * Checks test dependencies, determined by references in tests versus the dependencies listed in the Magento module |
| 73 | + * |
| 74 | + * @param InputInterface $input |
| 75 | + * @return void |
| 76 | + * @throws Exception |
| 77 | + */ |
| 78 | + public function execute(InputInterface $input) |
| 79 | + { |
| 80 | + $this->scriptUtil = new ScriptUtil(); |
| 81 | + $this->loadAllXmlFiles($input); |
| 82 | + $this->errors = []; |
| 83 | + |
| 84 | + $this->errors += $this->findReferenceErrorsInActionFiles($this->actionGroupXmlFiles); |
| 85 | + |
| 86 | + $this->output = $this->scriptUtil->printErrorsToFile( |
| 87 | + $this->errors, |
| 88 | + StaticChecksList::getErrorFilesPath() . DIRECTORY_SEPARATOR . self::ERROR_LOG_FILENAME . '.txt', |
| 89 | + self::ERROR_LOG_MESSAGE |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Return array containing all errors found after running the execute() function |
| 95 | + * |
| 96 | + * @return array |
| 97 | + */ |
| 98 | + public function getErrors() |
| 99 | + { |
| 100 | + return $this->errors; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Return string of a short human readable result of the check. For example: "No Dependency errors found." |
| 105 | + * |
| 106 | + * @return string |
| 107 | + */ |
| 108 | + public function getOutput() |
| 109 | + { |
| 110 | + return $this->output; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Read all XML files for scanning |
| 115 | + * |
| 116 | + * @param InputInterface $input |
| 117 | + * @return void |
| 118 | + * @throws Exception |
| 119 | + * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
| 120 | + */ |
| 121 | + private function loadAllXmlFiles($input) |
| 122 | + { |
| 123 | + $modulePaths = []; |
| 124 | + $includeRootPath = true; |
| 125 | + $path = $input->getOption('path'); |
| 126 | + if ($path) { |
| 127 | + if (!realpath($path)) { |
| 128 | + throw new InvalidArgumentException('Invalid --path option: ' . $path); |
| 129 | + } |
| 130 | + MftfApplicationConfig::create( |
| 131 | + true, |
| 132 | + MftfApplicationConfig::UNIT_TEST_PHASE, |
| 133 | + false, |
| 134 | + MftfApplicationConfig::LEVEL_DEFAULT, |
| 135 | + true |
| 136 | + ); |
| 137 | + $modulePaths[] = realpath($path); |
| 138 | + $includeRootPath = false; |
| 139 | + } else { |
| 140 | + $modulePaths = $this->scriptUtil->getAllModulePaths(); |
| 141 | + } |
| 142 | + |
| 143 | + // These files can contain references to other entities |
| 144 | + $this->actionGroupXmlFiles = $this->scriptUtil->getModuleXmlFilesByScope($modulePaths, 'ActionGroup'); |
| 145 | + |
| 146 | + if (empty($this->actionGroupXmlFiles)) { |
| 147 | + if ($path) { |
| 148 | + throw new InvalidArgumentException( |
| 149 | + 'Invalid --path option: ' |
| 150 | + . $path |
| 151 | + . PHP_EOL |
| 152 | + . 'Please make sure --path points to a valid MFTF Test Module.' |
| 153 | + ); |
| 154 | + } elseif (empty($this->rootSuiteXmlFiles)) { |
| 155 | + throw new TestFrameworkException('No xml file to scan.'); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Find reference errors in set of action files |
| 162 | + * |
| 163 | + * @param Finder $files |
| 164 | + * @return array |
| 165 | + * @throws XmlException |
| 166 | + */ |
| 167 | + private function findReferenceErrorsInActionFiles($files) |
| 168 | + { |
| 169 | + $testErrors = []; |
| 170 | + /** @var SplFileInfo $filePath */ |
| 171 | + foreach ($files as $filePath) { |
| 172 | + $contents = file_get_contents($filePath); |
| 173 | + preg_match_all(self::ACTIONGROUP_REGEX_PATTERN, $contents, $actionGroupReferences); |
| 174 | + if(count( $actionGroupReferences) > 0) { |
| 175 | + $testErrors = array_merge($testErrors, $this->setErrorOutput($actionGroupReferences, $filePath)); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + return $testErrors; |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Build and return error output for violating references |
| 184 | + * |
| 185 | + * @param array $actionGroupReferences |
| 186 | + * @param SplFileInfo $path |
| 187 | + * @return mixed |
| 188 | + */ |
| 189 | + private function setErrorOutput( $actionGroupReferences , $path) |
| 190 | + { |
| 191 | + $testErrors = []; |
| 192 | + $errorOutput = ""; |
| 193 | + $filePath = StaticChecksList::getFilePath($path->getRealPath()); |
| 194 | + |
| 195 | + foreach($actionGroupReferences as $key => $actionGroupReferencesData) { |
| 196 | + foreach($actionGroupReferencesData as $actionGroupReferencesDataResult) { |
| 197 | + |
| 198 | + $errorOutput .= "\nFile \"{$filePath}\" contains: ". "\n\t {$actionGroupReferencesDataResult} in {$filePath}"; |
| 199 | + $testErrors[$filePath][] = $errorOutput; |
| 200 | + } |
| 201 | + |
| 202 | + |
| 203 | + } |
| 204 | + return $testErrors; |
| 205 | + } |
| 206 | + |
| 207 | +} |
0 commit comments