|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Tester\Assert; |
| 6 | + |
| 7 | +require __DIR__ . '/bootstrap.php'; |
| 8 | + |
| 9 | + |
| 10 | +/** |
| 11 | + * This runner creates a temporary, isolated ruleset to ensure only the target sniff is tested. |
| 12 | + */ |
| 13 | +class SniffTestRunner |
| 14 | +{ |
| 15 | + private $sniffPath; |
| 16 | + private $phpcbfPath; |
| 17 | + private $testsDir; |
| 18 | + private $tempRulesetPath; |
| 19 | + |
| 20 | + |
| 21 | + public function __construct() |
| 22 | + { |
| 23 | + $this->sniffPath = realpath(__DIR__ . '/../src/NetteCodingStandard/Sniffs/Namespaces/OptimizeGlobalCallsSniff.php'); |
| 24 | + $this->phpcbfPath = realpath(__DIR__ . '/../vendor/bin/phpcbf'); |
| 25 | + $this->testsDir = __DIR__ . '/fixtures'; |
| 26 | + |
| 27 | + if (!$this->sniffPath || !file_exists($this->sniffPath)) { |
| 28 | + throw new Exception("FATAL ERROR: Sniff file not found at: {$this->sniffPath}"); |
| 29 | + } |
| 30 | + if (!$this->phpcbfPath || !file_exists($this->phpcbfPath)) { |
| 31 | + throw new Exception("FATAL ERROR: phpcbf executable not found at: {$this->phpcbfPath}"); |
| 32 | + } |
| 33 | + |
| 34 | + $this->createTempRuleset(); |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + public function __destruct() |
| 39 | + { |
| 40 | + if ($this->tempRulesetPath && file_exists($this->tempRulesetPath)) { |
| 41 | + unlink($this->tempRulesetPath); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + /** |
| 47 | + * Creates a temporary ruleset XML file that references only the sniff under test. |
| 48 | + */ |
| 49 | + private function createTempRuleset(): void |
| 50 | + { |
| 51 | + $ruleset = '<?xml version="1.0"?>' . PHP_EOL; |
| 52 | + $ruleset .= '<ruleset name="IsolatedSniffTest">' . PHP_EOL; |
| 53 | + $ruleset .= ' <description>A temporary ruleset to test a single sniff in isolation.</description>' . PHP_EOL; |
| 54 | + $ruleset .= ' <rule ref="' . htmlspecialchars($this->sniffPath, ENT_XML1) . '"/>' . PHP_EOL; |
| 55 | + $ruleset .= '</ruleset>' . PHP_EOL; |
| 56 | + |
| 57 | + $this->tempRulesetPath = sys_get_temp_dir() . '/phpcs_temp_ruleset_' . uniqid() . '.xml'; |
| 58 | + file_put_contents($this->tempRulesetPath, $ruleset); |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + /** |
| 63 | + * Discovers and runs all test cases from the fixtures directory. |
| 64 | + */ |
| 65 | + public function run(): void |
| 66 | + { |
| 67 | + $testFiles = glob($this->testsDir . '/*.inc'); |
| 68 | + |
| 69 | + foreach ($testFiles as $testFile) { |
| 70 | + test(basename($testFile, '.inc'), function () use ($testFile) { |
| 71 | + $this->runTestFromFile($testFile); |
| 72 | + }); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + /** |
| 78 | + * Runs a single test case. |
| 79 | + */ |
| 80 | + private function runTestFromFile(string $inputFile): void |
| 81 | + { |
| 82 | + $inputCode = file_get_contents($inputFile); |
| 83 | + |
| 84 | + $expectedFile = $inputFile . '.expected'; |
| 85 | + $expectedFixedCode = file_exists($expectedFile) ? file_get_contents($expectedFile) : $inputCode; |
| 86 | + |
| 87 | + $actualFixedContent = $this->getFixedContent($inputCode); |
| 88 | + |
| 89 | + $expectedFixedCode = str_replace("\r\n", "\n", $expectedFixedCode); |
| 90 | + $actualFixedContent = str_replace("\r\n", "\n", $actualFixedContent); |
| 91 | + |
| 92 | + Assert::same($expectedFixedCode, $actualFixedContent, 'Mismatch for ' . basename($inputFile)); |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + /** |
| 97 | + * Executes phpcbf with the isolated ruleset and returns the fixed content. |
| 98 | + * Writes to a temporary file to avoid issues with PHP CGI headers. |
| 99 | + * If phpcbf fails, it returns the content of STDERR instead. |
| 100 | + */ |
| 101 | + private function getFixedContent(string $inputCode): string |
| 102 | + { |
| 103 | + $tempInputFile = sys_get_temp_dir() . '/phpcs_test_input_' . uniqid() . '.php'; |
| 104 | + file_put_contents($tempInputFile, $inputCode); |
| 105 | + |
| 106 | + $properties = $this->parsePropertiesFromJsonComment($inputCode); |
| 107 | + $sniffCode = 'NetteCodingStandard.Namespaces.OptimizeGlobalCalls'; |
| 108 | + |
| 109 | + $command = escapeshellarg(PHP_BINARY) . ' ' . escapeshellarg($this->phpcbfPath); |
| 110 | + $command .= ' --standard=' . escapeshellarg($this->tempRulesetPath); |
| 111 | + $command .= ' --no-cache'; |
| 112 | + $command .= ' ' . escapeshellarg($tempInputFile); // Process the temp file |
| 113 | + |
| 114 | + foreach ($properties as $key => $value) { |
| 115 | + $valueString = match (gettype($value)) { |
| 116 | + 'array' => implode(',', $value), |
| 117 | + 'boolean' => $value ? 'true' : 'false', |
| 118 | + default => (string) $value, |
| 119 | + }; |
| 120 | + $command .= ' --runtime-set ' . escapeshellarg($sniffCode . '.' . $key) . ' ' . escapeshellarg($valueString); |
| 121 | + } |
| 122 | + |
| 123 | + $descriptorSpec = [ |
| 124 | + 1 => ['pipe', 'w'], |
| 125 | + 2 => ['pipe', 'w'], |
| 126 | + ]; |
| 127 | + |
| 128 | + $process = proc_open($command, $descriptorSpec, $pipes); |
| 129 | + $fixedContent = ''; |
| 130 | + $errors = ''; |
| 131 | + $exitCode = -1; |
| 132 | + |
| 133 | + if (is_resource($process)) { |
| 134 | + // We don't need stdout as phpcbf modifies the file in place |
| 135 | + fclose($pipes[1]); |
| 136 | + |
| 137 | + $errors = stream_get_contents($pipes[2]); |
| 138 | + fclose($pipes[2]); |
| 139 | + |
| 140 | + $exitCode = proc_close($process); |
| 141 | + } |
| 142 | + |
| 143 | + if ($exitCode !== 0) { |
| 144 | + // If phpcbf failed, return the error output for easier debugging |
| 145 | + unlink($tempInputFile); |
| 146 | + return "PHPCBF FAILED WITH EXIT CODE {$exitCode}:\n" . $errors; |
| 147 | + } |
| 148 | + |
| 149 | + $fixedContent = file_get_contents($tempInputFile); |
| 150 | + unlink($tempInputFile); |
| 151 | + |
| 152 | + return $fixedContent; |
| 153 | + } |
| 154 | + |
| 155 | + |
| 156 | + /** |
| 157 | + * Parses a special JSON comment on the first line of the code to set sniff properties. |
| 158 | + * E.g., <?php // {"optimizedFunctionsOnly": true, "ignoredFunctions": ["dd"]} |
| 159 | + */ |
| 160 | + private function parsePropertiesFromJsonComment(string $code): array |
| 161 | + { |
| 162 | + return preg_match('~^\s*<\?php\s*//\s*({.+})~', $code, $matches) |
| 163 | + ? json_decode($matches[1], true, 512, JSON_THROW_ON_ERROR) |
| 164 | + : []; |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | + |
| 169 | +(new SniffTestRunner)->run(); |
0 commit comments