|
| 1 | +<?php |
| 2 | + |
| 3 | +use PHPUnit\Framework\Attributes\CoversNothing; |
| 4 | +use PHPUnit\Framework\Attributes\RequiresPhp; |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | + |
| 7 | +/** |
| 8 | + * @internal |
| 9 | + */ |
| 10 | +#[CoversNothing] |
| 11 | +final class RoundingModeGuardTest extends TestCase |
| 12 | +{ |
| 13 | + /** |
| 14 | + * Regression test for issue #56. |
| 15 | + * |
| 16 | + * Rector 2.4.x's scoped polyfill declares a global `class RoundingMode` |
| 17 | + * via `class_alias` on PHP < 8.4. The polyfill in lib/RoundingMode.php |
| 18 | + * must detect this and skip enum declaration. |
| 19 | + */ |
| 20 | + #[RequiresPhp('>= 8.1 < 8.4')] |
| 21 | + public function testPolyfillSkipsWhenRoundingModeClassAlreadyDefined(): void |
| 22 | + { |
| 23 | + $result = $this->runFixture(__DIR__.'/fixtures/rector-scenario.php'); |
| 24 | + |
| 25 | + $this->assertSame(0, $result['exitCode'], 'Fixture exited with non-zero status. stderr: '.$result['stderr']); |
| 26 | + $this->assertStringNotContainsString('Cannot declare enum', $result['stderr']); |
| 27 | + $this->assertStringNotContainsString('Fatal error', $result['stderr']); |
| 28 | + $this->assertStringContainsString('OK', $result['stdout']); |
| 29 | + } |
| 30 | + |
| 31 | + #[RequiresPhp('>= 8.1 < 8.4')] |
| 32 | + public function testPolyfillDefinesEnumOnCleanLoad(): void |
| 33 | + { |
| 34 | + $result = $this->runFixture(__DIR__.'/fixtures/clean-load.php'); |
| 35 | + |
| 36 | + $this->assertSame(0, $result['exitCode'], 'Fixture exited with non-zero status. stderr: '.$result['stderr']); |
| 37 | + $this->assertStringContainsString('ENUM_DEFINED', $result['stdout']); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * On PHP 8.4+ the native RoundingMode enum is already registered, so the polyfill |
| 42 | + * guard in lib/RoundingMode.php must short-circuit. This asserts that loading the |
| 43 | + * polyfill produces no fatal, and that the (native) enum is visible afterwards. |
| 44 | + */ |
| 45 | + #[RequiresPhp('>= 8.4')] |
| 46 | + public function testPolyfillSkipsOnPhp84WithNativeEnum(): void |
| 47 | + { |
| 48 | + $result = $this->runFixture(__DIR__.'/fixtures/clean-load.php'); |
| 49 | + |
| 50 | + $this->assertSame(0, $result['exitCode'], 'Fixture exited with non-zero status. stderr: '.$result['stderr']); |
| 51 | + $this->assertStringNotContainsString('Cannot declare enum', $result['stderr']); |
| 52 | + $this->assertStringNotContainsString('Fatal error', $result['stderr']); |
| 53 | + $this->assertStringContainsString('ENUM_DEFINED', $result['stdout']); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @return array{stdout: string, stderr: string, exitCode: int} |
| 58 | + */ |
| 59 | + private function runFixture(string $fixturePath, int $timeoutSeconds = 10): array |
| 60 | + { |
| 61 | + $descriptors = [ |
| 62 | + 0 => ['pipe', 'r'], |
| 63 | + 1 => ['pipe', 'w'], |
| 64 | + 2 => ['pipe', 'w'], |
| 65 | + ]; |
| 66 | + |
| 67 | + $process = proc_open([PHP_BINARY, $fixturePath], $descriptors, $pipes); |
| 68 | + if (!is_resource($process)) { |
| 69 | + $this->fail('Failed to spawn PHP subprocess'); |
| 70 | + } |
| 71 | + |
| 72 | + fclose($pipes[0]); |
| 73 | + stream_set_blocking($pipes[1], false); |
| 74 | + stream_set_blocking($pipes[2], false); |
| 75 | + |
| 76 | + $stdout = ''; |
| 77 | + $stderr = ''; |
| 78 | + $exitCode = -1; |
| 79 | + $deadline = microtime(true) + $timeoutSeconds; |
| 80 | + |
| 81 | + while (true) { |
| 82 | + $chunkOut = stream_get_contents($pipes[1]); |
| 83 | + if ($chunkOut !== false) { |
| 84 | + $stdout .= $chunkOut; |
| 85 | + } |
| 86 | + $chunkErr = stream_get_contents($pipes[2]); |
| 87 | + if ($chunkErr !== false) { |
| 88 | + $stderr .= $chunkErr; |
| 89 | + } |
| 90 | + |
| 91 | + $status = proc_get_status($process); |
| 92 | + if (!$status['running']) { |
| 93 | + // Capture exit code from status: on PHP < 8.3 calling proc_get_status() |
| 94 | + // reaps the process so the subsequent proc_close() returns -1. |
| 95 | + $exitCode = $status['exitcode']; |
| 96 | + // Drain any remaining buffered output after the process exited. |
| 97 | + $chunkOut = stream_get_contents($pipes[1]); |
| 98 | + if ($chunkOut !== false) { |
| 99 | + $stdout .= $chunkOut; |
| 100 | + } |
| 101 | + $chunkErr = stream_get_contents($pipes[2]); |
| 102 | + if ($chunkErr !== false) { |
| 103 | + $stderr .= $chunkErr; |
| 104 | + } |
| 105 | + |
| 106 | + break; |
| 107 | + } |
| 108 | + |
| 109 | + if (microtime(true) > $deadline) { |
| 110 | + proc_terminate($process); |
| 111 | + fclose($pipes[1]); |
| 112 | + fclose($pipes[2]); |
| 113 | + proc_close($process); |
| 114 | + $this->fail(sprintf('Subprocess for %s timed out after %ds. stderr so far: %s', $fixturePath, $timeoutSeconds, $stderr)); |
| 115 | + } |
| 116 | + |
| 117 | + usleep(10000); |
| 118 | + } |
| 119 | + |
| 120 | + fclose($pipes[1]); |
| 121 | + fclose($pipes[2]); |
| 122 | + proc_close($process); |
| 123 | + |
| 124 | + return [ |
| 125 | + 'stdout' => $stdout, |
| 126 | + 'stderr' => $stderr, |
| 127 | + 'exitCode' => $exitCode, |
| 128 | + ]; |
| 129 | + } |
| 130 | +} |
0 commit comments