|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Flyeralarm\Sniffer\Tests; |
| 6 | + |
| 7 | +class TestRunner |
| 8 | +{ |
| 9 | + /** |
| 10 | + * @param string $dirPath |
| 11 | + */ |
| 12 | + public function processDir(string $dirPath) |
| 13 | + { |
| 14 | + $hasError = $this->recursiveDirProcess($dirPath); |
| 15 | + if ($hasError) { |
| 16 | + exit(1); |
| 17 | + } |
| 18 | + exit(0); |
| 19 | + } |
| 20 | + |
| 21 | + public function recursiveDirProcess(string $dirPath, bool $hasError = false): bool |
| 22 | + { |
| 23 | + $dir = opendir($dirPath); |
| 24 | + while (($file = readdir($dir)) !== false) { |
| 25 | + if (strpos($file, '.') === 0) { |
| 26 | + continue; |
| 27 | + } |
| 28 | + if (is_dir($dirPath . $file)) { |
| 29 | + $hasError = $this->recursiveDirProcess($dirPath . $file . '/', $hasError); |
| 30 | + continue; |
| 31 | + } |
| 32 | + |
| 33 | + $fileContent = file_get_contents($dirPath . $file); |
| 34 | + $snifferOutput = shell_exec( |
| 35 | + sprintf( |
| 36 | + "%s -w -p -s --report-width=120 --standard=%s %s", |
| 37 | + escapeshellcmd(__DIR__ . '/../vendor/bin/phpcs'), |
| 38 | + escapeshellarg(__DIR__ . '/ruleset.xml'), |
| 39 | + escapeshellarg($dirPath . $file) |
| 40 | + ) |
| 41 | + ); |
| 42 | + |
| 43 | + // expectedPass |
| 44 | + if (preg_match('|//\s@expectedPass$|m', $fileContent)) { |
| 45 | + if (preg_match('|^FOUND.*AFFECTING.*LINE|m', $snifferOutput) === 0) { |
| 46 | + echo 'OK - [' . $dirPath . $file . ']' . PHP_EOL; |
| 47 | + continue; |
| 48 | + } |
| 49 | + |
| 50 | + $hasError = true; |
| 51 | + echo "ERROR - [" . $dirPath . $file . "]:' |
| 52 | + . ' Test was expected to fully pass. Result: " . PHP_EOL . $snifferOutput . PHP_EOL; |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + // expectedError |
| 57 | + preg_match('|//\s@expectedError\s(.*)$|m', $fileContent, $expectedMatch); |
| 58 | + if (count($expectedMatch) !== 2) { |
| 59 | + echo 'WARNING - [' . $dirPath . $file . ']:' |
| 60 | + . ' File must contain exactly one "@expectedError <EXPECTATION MESSAGE>"' |
| 61 | + . ' or "@expectedPass" comment' . PHP_EOL; |
| 62 | + continue; |
| 63 | + } |
| 64 | + $expected = $expectedMatch[1]; |
| 65 | + if (preg_match('/ERROR\s\|\s?[\[\]x\s]*\s' . preg_quote($expected, '/') . '/', $snifferOutput) === 0) { |
| 66 | + $hasError = true; |
| 67 | + echo 'ERROR - [' . $dirPath . $file . ']:' |
| 68 | + . ' Expectation <<' . $expected . '>>' |
| 69 | + . ' not found in result: ' . PHP_EOL . $snifferOutput . PHP_EOL; |
| 70 | + } else { |
| 71 | + echo 'OK - [' . $dirPath . $file . ']' . PHP_EOL; |
| 72 | + } |
| 73 | + } |
| 74 | + return $hasError; |
| 75 | + } |
| 76 | +} |
0 commit comments