|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace ShipMonk\PHPStan\Errors; |
| 4 | + |
| 5 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | +use function fclose; |
| 8 | +use function fwrite; |
| 9 | +use function is_array; |
| 10 | +use function is_resource; |
| 11 | +use function proc_close; |
| 12 | +use function proc_open; |
| 13 | +use function stream_get_contents; |
| 14 | + |
| 15 | +class IoTest extends TestCase |
| 16 | +{ |
| 17 | + |
| 18 | + /** |
| 19 | + * @param list<string> $args |
| 20 | + */ |
| 21 | + #[DataProvider('optionsProvider')] |
| 22 | + public function testValidCliOptions(int $exitCode, string $input, array $args, string $expectedOutput): void |
| 23 | + { |
| 24 | + $result = $this->runCliCommand($args, $input); |
| 25 | + self::assertSame($exitCode, $result['exitCode']); |
| 26 | + self::assertStringContainsString($expectedOutput, $result['stdout']); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @return array<string, array{int, string, array<string>, string}> |
| 31 | + */ |
| 32 | + public static function optionsProvider(): array |
| 33 | + { |
| 34 | + $validInput = '{"files": {}}'; |
| 35 | + |
| 36 | + return [ |
| 37 | + 'no input' => [1, '', [], 'ERROR: Nothing found on input'], |
| 38 | + 'invalid json' => [1, 'invalid json', [], 'ERROR: Syntax error'], |
| 39 | + |
| 40 | + 'no options' => [0, $validInput, [], 'Done, 0 errors processed'], |
| 41 | + 'with comment' => [0, $validInput, ['--comment=test comment'], 'Done, 0 errors processed'], |
| 42 | + 'with single word comment' => [0, $validInput, ['--comment=test'], 'Done, 0 errors processed'], |
| 43 | + |
| 44 | + 'unexpected option' => [1, $validInput, ['--invalid'], 'ERROR: Unexpected option: --invalid'], |
| 45 | + 'comment without value' => [1, $validInput, ['--comment'], 'ERROR: Missing comment value for --comment option'], |
| 46 | + 'multiple comments' => [1, $validInput, ['--comment=first', '--comment=second'], 'ERROR: Only one comment can be provided'], |
| 47 | + ]; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @param list<string> $args |
| 52 | + * @return array{exitCode: int, stdout: string, stderr: string} |
| 53 | + */ |
| 54 | + private function runCliCommand(array $args, string $input): array |
| 55 | + { |
| 56 | + $binaryPath = __DIR__ . '/../bin/inline-phpstan-ignores'; |
| 57 | + $command = ['php', $binaryPath, ...$args]; |
| 58 | + |
| 59 | + $process = proc_open( |
| 60 | + $command, |
| 61 | + [ |
| 62 | + 0 => ['pipe', 'r'], // stdin |
| 63 | + 1 => ['pipe', 'w'], // stdout |
| 64 | + 2 => ['pipe', 'w'], // stderr |
| 65 | + ], |
| 66 | + $pipes, |
| 67 | + ); |
| 68 | + |
| 69 | + if (!is_resource($process)) { |
| 70 | + self::fail('Failed to start process'); |
| 71 | + } |
| 72 | + |
| 73 | + if (!is_array($pipes) || !isset($pipes[0], $pipes[1], $pipes[2])) { |
| 74 | + self::fail('Failed to create pipes'); |
| 75 | + } |
| 76 | + |
| 77 | + /** @var array{0: resource, 1: resource, 2: resource} $pipes */ |
| 78 | + fwrite($pipes[0], $input); |
| 79 | + fclose($pipes[0]); |
| 80 | + |
| 81 | + $stdout = stream_get_contents($pipes[1]); |
| 82 | + $stderr = stream_get_contents($pipes[2]); |
| 83 | + fclose($pipes[1]); |
| 84 | + fclose($pipes[2]); |
| 85 | + |
| 86 | + $exitCode = proc_close($process); |
| 87 | + |
| 88 | + self::assertNotFalse($stdout, 'Failed to read stdout'); |
| 89 | + self::assertNotFalse($stderr, 'Failed to read stderr'); |
| 90 | + |
| 91 | + return [ |
| 92 | + 'exitCode' => $exitCode, |
| 93 | + 'stdout' => $stdout, |
| 94 | + 'stderr' => $stderr, |
| 95 | + ]; |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments