|
8 | 8 | namespace Magento\Test\Php;
|
9 | 9 |
|
10 | 10 | use Magento\Framework\App\Utility\Files;
|
11 |
| -use Magento\Framework\Component\ComponentRegistrar; |
12 | 11 | use Magento\TestFramework\CodingStandard\Tool\CodeMessDetector;
|
13 | 12 | use Magento\TestFramework\CodingStandard\Tool\CodeSniffer;
|
14 | 13 | use Magento\TestFramework\CodingStandard\Tool\CodeSniffer\Wrapper;
|
15 | 14 | use Magento\TestFramework\CodingStandard\Tool\CopyPasteDetector;
|
| 15 | +use Magento\TestFramework\CodingStandard\Tool\PhpCompatibility; |
16 | 16 | use PHPMD\TextUI\Command;
|
17 | 17 |
|
18 | 18 | /**
|
@@ -164,6 +164,7 @@ private static function getFilesFromListFile($listsBaseDir, $listFilePattern, $n
|
164 | 164 | $listFiles = glob($globFilesListPattern);
|
165 | 165 | if (!empty($listFiles)) {
|
166 | 166 | foreach ($listFiles as $listFile) {
|
| 167 | + // phpcs:ignore Magento2.Performance.ForeachArrayMerge.ForeachArrayMerge |
167 | 168 | $filesDefinedInList = array_merge(
|
168 | 169 | $filesDefinedInList,
|
169 | 170 | file($listFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)
|
@@ -218,9 +219,12 @@ private static function filterFiles(array $files, array $allowedFileTypes, array
|
218 | 219 | };
|
219 | 220 | } else {
|
220 | 221 | $allowedDirectories = array_map('realpath', $allowedDirectories);
|
221 |
| - usort($allowedDirectories, function ($dir1, $dir2) { |
222 |
| - return strlen($dir1) - strlen($dir2); |
223 |
| - }); |
| 222 | + usort( |
| 223 | + $allowedDirectories, |
| 224 | + function ($dir1, $dir2) { |
| 225 | + return strlen($dir1) - strlen($dir2); |
| 226 | + } |
| 227 | + ); |
224 | 228 | $fileIsInAllowedDirectory = function ($file) use ($allowedDirectories) {
|
225 | 229 | foreach ($allowedDirectories as $directory) {
|
226 | 230 | if (strpos($file, $directory) === 0) {
|
@@ -260,18 +264,70 @@ private function getFullWhitelist()
|
260 | 264 | }
|
261 | 265 | }
|
262 | 266 |
|
| 267 | + /** |
| 268 | + * Retrieves the lowest PHP version specified in <kbd>composer.json</var> of project. |
| 269 | + * |
| 270 | + * @return string |
| 271 | + */ |
| 272 | + private function getLowestPhpVersion(): string |
| 273 | + { |
| 274 | + $composerJson = json_decode(file_get_contents(BP . '/composer.json'), true); |
| 275 | + $phpVersion = '7.0'; |
| 276 | + |
| 277 | + if (isset($composerJson['require']['php'])) { |
| 278 | + $versions = explode('||', $composerJson['require']['php']); |
| 279 | + |
| 280 | + //normalize version constraints |
| 281 | + foreach ($versions as $key => $version) { |
| 282 | + $version = ltrim($version, '^~'); |
| 283 | + $version = str_replace('*', '999', $version); |
| 284 | + |
| 285 | + $versions[$key] = $version; |
| 286 | + } |
| 287 | + |
| 288 | + //sort versions |
| 289 | + usort($versions, 'version_compare'); |
| 290 | + |
| 291 | + $lowestVersion = array_shift($versions); |
| 292 | + $versionParts = explode('.', $lowestVersion); |
| 293 | + $phpVersion = sprintf('%s.%s', $versionParts[0], $versionParts[1] ?? '0'); |
| 294 | + } |
| 295 | + |
| 296 | + return $phpVersion; |
| 297 | + } |
| 298 | + |
| 299 | + /** |
| 300 | + * Returns whether a full scan was requested. |
| 301 | + * |
| 302 | + * This can be set in the `phpunit.xml` used to run these test cases, by setting the constant |
| 303 | + * `TESTCODESTYLE_IS_FULL_SCAN` to `1`, e.g.: |
| 304 | + * ```xml |
| 305 | + * <php> |
| 306 | + * <!-- TESTCODESTYLE_IS_FULL_SCAN - specify if full scan should be performed for test code style test --> |
| 307 | + * <const name="TESTCODESTYLE_IS_FULL_SCAN" value="0"/> |
| 308 | + * </php> |
| 309 | + * ``` |
| 310 | + * |
| 311 | + * @return bool |
| 312 | + */ |
| 313 | + private function isFullScan(): bool |
| 314 | + { |
| 315 | + return defined('TESTCODESTYLE_IS_FULL_SCAN') && TESTCODESTYLE_IS_FULL_SCAN === '1'; |
| 316 | + } |
| 317 | + |
263 | 318 | /**
|
264 | 319 | * Test code quality using phpcs
|
265 | 320 | */
|
266 | 321 | public function testCodeStyle()
|
267 | 322 | {
|
268 |
| - $isFullScan = defined('TESTCODESTYLE_IS_FULL_SCAN') && TESTCODESTYLE_IS_FULL_SCAN === '1'; |
269 | 323 | $reportFile = self::$reportDir . '/phpcs_report.txt';
|
270 | 324 | if (!file_exists($reportFile)) {
|
271 | 325 | touch($reportFile);
|
272 | 326 | }
|
273 | 327 | $codeSniffer = new CodeSniffer('Magento', $reportFile, new Wrapper());
|
274 |
| - $result = $codeSniffer->run($isFullScan ? $this->getFullWhitelist() : self::getWhitelist(['php', 'phtml'])); |
| 328 | + $result = $codeSniffer->run( |
| 329 | + $this->isFullScan() ? $this->getFullWhitelist() : self::getWhitelist(['php', 'phtml']) |
| 330 | + ); |
275 | 331 | $report = file_get_contents($reportFile);
|
276 | 332 | $this->assertEquals(
|
277 | 333 | 0,
|
@@ -325,6 +381,7 @@ public function testCopyPaste()
|
325 | 381 |
|
326 | 382 | $blackList = [];
|
327 | 383 | foreach (glob(__DIR__ . '/_files/phpcpd/blacklist/*.txt') as $list) {
|
| 384 | + // phpcs:ignore Magento2.Performance.ForeachArrayMerge.ForeachArrayMerge |
328 | 385 | $blackList = array_merge($blackList, file($list, FILE_IGNORE_NEW_LINES));
|
329 | 386 | }
|
330 | 387 |
|
@@ -380,4 +437,32 @@ public function testStrictTypes()
|
380 | 437 | . implode(PHP_EOL, $filesMissingStrictTyping)
|
381 | 438 | );
|
382 | 439 | }
|
| 440 | + |
| 441 | + /** |
| 442 | + * Test for compatibility to lowest PHP version declared in <kbd>composer.json</kbd>. |
| 443 | + */ |
| 444 | + public function testPhpCompatibility() |
| 445 | + { |
| 446 | + $targetVersion = $this->getLowestPhpVersion(); |
| 447 | + $reportFile = self::$reportDir . '/phpcompatibility_report.txt'; |
| 448 | + $rulesetDir = __DIR__ . '/_files/PHPCompatibilityMagento'; |
| 449 | + |
| 450 | + if (!file_exists($reportFile)) { |
| 451 | + touch($reportFile); |
| 452 | + } |
| 453 | + |
| 454 | + $codeSniffer = new PhpCompatibility($rulesetDir, $reportFile, new Wrapper()); |
| 455 | + $codeSniffer->setTestVersion($targetVersion); |
| 456 | + |
| 457 | + $result = $codeSniffer->run( |
| 458 | + $this->isFullScan() ? $this->getFullWhitelist() : self::getWhitelist(['php', 'phtml']) |
| 459 | + ); |
| 460 | + $report = file_get_contents($reportFile); |
| 461 | + |
| 462 | + $this->assertEquals( |
| 463 | + 0, |
| 464 | + $result, |
| 465 | + 'PHP Compatibility detected violation(s):' . PHP_EOL . $report |
| 466 | + ); |
| 467 | + } |
383 | 468 | }
|
0 commit comments