|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +use Magento\Framework\Component\ComponentRegistrar; |
| 8 | + |
| 9 | +require_once __DIR__ . '/../../../app/autoload.php'; |
| 10 | + |
| 11 | +$scriptName = basename(__FILE__); |
| 12 | + |
| 13 | +define( |
| 14 | + 'USAGE', |
| 15 | + <<<USAGE |
| 16 | +Usage: |
| 17 | +php -f $scriptName phpunit.xml(.dist) |
| 18 | +USAGE |
| 19 | +); |
| 20 | + |
| 21 | +assertUsage(empty($argv[1]) || !file_exists($argv[1]), 'missing or invalid phpunit.xml(.dist) file'); |
| 22 | +$xmlDom = new DOMDocument(); |
| 23 | +$xmlDom->preserveWhiteSpace = true; |
| 24 | +$xmlDom->formatOutput = true; |
| 25 | +assertUsage($xmlDom->load($argv[1]) == false, 'missing or invalid phpunit.xml(.dist) file'); |
| 26 | +$testType = getTestType($xmlDom); |
| 27 | +// Update testsuite based on magento installation |
| 28 | +$xmlDom = updateTestSuite($xmlDom, $testType); |
| 29 | +$xmlDom->save($argv[1] . '.new'); |
| 30 | +echo "{$testType} " . basename($argv[1]) . " is updated."; |
| 31 | + |
| 32 | +/** |
| 33 | + * Read DOMDocument to get test type. |
| 34 | + * |
| 35 | + * @param DOMDocument $dom |
| 36 | + * @return string |
| 37 | + */ |
| 38 | +function getTestType(DOMDocument $dom): string |
| 39 | +{ |
| 40 | + $testType = null; |
| 41 | + /** @var DOMElement $testsuite */ |
| 42 | + //$testsuite = null; |
| 43 | + foreach ($dom->getElementsByTagName('testsuite') as $testsuite) { |
| 44 | + if (stripos($testsuite->getAttribute('name'), 'real suite') === false) { |
| 45 | + continue; |
| 46 | + } |
| 47 | + if (stripos($testsuite->getAttribute('name'), 'rest') !== false) { |
| 48 | + $testType = 'REST'; |
| 49 | + } |
| 50 | + if (stripos($testsuite->getAttribute('name'), 'soap') !== false) { |
| 51 | + $testType = 'SOAP'; |
| 52 | + } |
| 53 | + if (stripos($testsuite->getAttribute('name'), 'graphql') !== false) { |
| 54 | + $testType = 'GraphQL'; |
| 55 | + } |
| 56 | + if (stripos($testsuite->getAttribute('name'), 'integration') !== false) { |
| 57 | + $testType = 'Integration'; |
| 58 | + } |
| 59 | + if ($testType) { |
| 60 | + break; |
| 61 | + } |
| 62 | + } |
| 63 | + return $testType; |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Find magento modules directories patterns through magento ComponentRegistrar. |
| 68 | + * |
| 69 | + * @param string $testType |
| 70 | + * @return string [] |
| 71 | + */ |
| 72 | +function findMagentoModuleDirs(string $testType): array |
| 73 | +{ |
| 74 | + $patterns = [ |
| 75 | + 'Integration' => 'Integration', |
| 76 | + 'REST' => 'Api', |
| 77 | + 'SOAP' => 'Api', |
| 78 | + // Is there a path pattern for 'GraphQL'? |
| 79 | + ]; |
| 80 | + $magentoBaseDir = realpath(__DIR__ . '/../../..') . DIRECTORY_SEPARATOR; |
| 81 | + $magentoBaseDirPattern = preg_quote($magentoBaseDir, '/'); |
| 82 | + $componentRegistrar = new ComponentRegistrar(); |
| 83 | + $modulePaths = $componentRegistrar->getPaths(ComponentRegistrar::MODULE); |
| 84 | + $testPathPatterns = []; |
| 85 | + foreach ($modulePaths as $modulePath) { |
| 86 | + preg_match('~' . $magentoBaseDirPattern . '(.+)\/[^\/]+~', $modulePath, $match); |
| 87 | + if (isset($match[1]) && isset($patterns[$testType])) { |
| 88 | + $testPathPatterns[] = '../../../' . $match[1] . '/*/Test/' . $patterns[$testType]; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return array_unique($testPathPatterns); |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Create a new testsuite DOMDocument based on installed magento module directories. |
| 97 | + * |
| 98 | + * @param string $testType |
| 99 | + * @return DOMDocument |
| 100 | + * @throws DOMException |
| 101 | + */ |
| 102 | +function createNewDomElement(string $testType): DOMDocument |
| 103 | +{ |
| 104 | + $defTestSuite = getDefaultSuites($testType); |
| 105 | + |
| 106 | + // Create the new element |
| 107 | + $newTestSuite = new DomDocument(); |
| 108 | + $newTestSuite->formatOutput = true; |
| 109 | + $newTestSuiteElement = $newTestSuite->createElement('testsuite'); |
| 110 | + if ($testType == 'Integration') { |
| 111 | + $newTestSuiteElement->setAttribute('name', 'Magento ' . $testType . ' Tests Real Suite'); |
| 112 | + } else { |
| 113 | + $newTestSuiteElement->setAttribute('name', 'Magento ' . $testType . ' Web API Functional Tests Real Suite'); |
| 114 | + } |
| 115 | + foreach ($defTestSuite['directory'] as $directory) { |
| 116 | + $newTestSuiteElement->appendChild($newTestSuite->createElement('directory', $directory)); |
| 117 | + } |
| 118 | + foreach (findMagentoModuleDirs($testType) as $directory) { |
| 119 | + $newTestSuiteElement->appendChild($newTestSuite->createElement('directory', $directory)); |
| 120 | + } |
| 121 | + foreach ($defTestSuite['exclude'] as $exclude) { |
| 122 | + $newTestSuiteElement->appendChild($newTestSuite->createElement('exclude', $exclude)); |
| 123 | + } |
| 124 | + |
| 125 | + $newTestSuite->appendChild($newTestSuiteElement); |
| 126 | + return $newTestSuite; |
| 127 | +} |
| 128 | + |
| 129 | +/** |
| 130 | + * Replace testsuite node with created new testsuite node in dom document passed in. |
| 131 | + * |
| 132 | + * @param DOMDocument $dom |
| 133 | + * @param string $testType |
| 134 | + * @return DOMDocument |
| 135 | + * @throws DOMException |
| 136 | + */ |
| 137 | +function updateTestSuite(DOMDocument $dom, string $testType): DOMDocument |
| 138 | +{ |
| 139 | + // Locate the old node |
| 140 | + $xpath = new DOMXpath($dom); |
| 141 | + $nodelist = $xpath->query('/phpunit/testsuites/testsuite'); |
| 142 | + for ( $index = 0; $index < $nodelist->count(); $index++) { |
| 143 | + $oldNode = $nodelist->item($index); |
| 144 | + if (stripos($oldNode->getAttribute('name'), 'real suite') !== false) { |
| 145 | + // Load the $parent document fragment into the current document |
| 146 | + $newNode = $dom->importNode(createNewDomElement($testType)->documentElement, true); |
| 147 | + // Replace |
| 148 | + $oldNode->parentNode->replaceChild($newNode, $oldNode); |
| 149 | + } |
| 150 | + } |
| 151 | + return $dom; |
| 152 | +} |
| 153 | + |
| 154 | +/** |
| 155 | + * Assert usage by throwing exception on condition evaluating to true |
| 156 | + * |
| 157 | + * @param bool $condition |
| 158 | + * @param string $error |
| 159 | + * @throws Exception |
| 160 | + */ |
| 161 | +function assertUsage(bool $condition, string $error): void |
| 162 | +{ |
| 163 | + if ($condition) { |
| 164 | + $error .= "\n" . USAGE; |
| 165 | + throw new Exception($error); |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +/** |
| 170 | + * Return suite default directories and excludes for a given test type. |
| 171 | + * |
| 172 | + * @param string $testType |
| 173 | + * @return array |
| 174 | + */ |
| 175 | +function getDefaultSuites(string $testType): array |
| 176 | +{ |
| 177 | + $suites = []; |
| 178 | + switch ($testType) { |
| 179 | + case 'Integration': |
| 180 | + $suites = [ |
| 181 | + 'directory' => [ |
| 182 | + 'testsuite' |
| 183 | + ], |
| 184 | + 'exclude' => [ |
| 185 | + 'testsuite/Magento/MemoryUsageTest.php', |
| 186 | + 'testsuite/Magento/IntegrationTest.php' |
| 187 | + ] |
| 188 | + ]; |
| 189 | + break; |
| 190 | + case 'REST': |
| 191 | + $suites = [ |
| 192 | + 'directory' => [ |
| 193 | + 'testsuite' |
| 194 | + ], |
| 195 | + 'exclude' => [ |
| 196 | + 'testsuite/Magento/GraphQl' |
| 197 | + ] |
| 198 | + ]; |
| 199 | + break; |
| 200 | + case 'SOAP': |
| 201 | + $suites = [ |
| 202 | + 'directory' => [ |
| 203 | + 'testsuite' |
| 204 | + ], |
| 205 | + 'exclude' => [ |
| 206 | + ] |
| 207 | + ]; |
| 208 | + break; |
| 209 | + case 'GraphQL': |
| 210 | + $suites = [ |
| 211 | + 'directory' => [ |
| 212 | + 'testsuite/Magento/GraphQl' |
| 213 | + ], |
| 214 | + 'exclude' => [ |
| 215 | + ] |
| 216 | + ]; |
| 217 | + } |
| 218 | + return $suites; |
| 219 | +} |
0 commit comments