|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/* |
| 3 | + * This file is part of PHPUnit. |
| 4 | + * |
| 5 | + * (c) Sebastian Bergmann <[email protected]> |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | +namespace PHPUnit\Util\PHP; |
| 11 | + |
| 12 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 13 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 14 | +use PHPUnit\Framework\Attributes\Small; |
| 15 | +use PHPUnit\Framework\Attributes\UsesClass; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | + |
| 18 | +#[CoversClass(DefaultJobRunner::class)] |
| 19 | +#[UsesClass(Job::class)] |
| 20 | +#[UsesClass(Result::class)] |
| 21 | +#[Small] |
| 22 | +final class DefaultJobRunnerTest extends TestCase |
| 23 | +{ |
| 24 | + public static function provider(): array |
| 25 | + { |
| 26 | + return [ |
| 27 | + 'output to stdout' => [ |
| 28 | + new Result('test', ''), |
| 29 | + new Job( |
| 30 | + <<<'EOT' |
| 31 | +<?php declare(strict_types=1); |
| 32 | +fwrite(STDOUT, 'test'); |
| 33 | + |
| 34 | +EOT |
| 35 | + ), |
| 36 | + ], |
| 37 | + 'output to stderr: yes' => [ |
| 38 | + new Result('', 'test'), |
| 39 | + new Job( |
| 40 | + <<<'EOT' |
| 41 | +<?php declare(strict_types=1); |
| 42 | +fwrite(STDERR, 'test'); |
| 43 | + |
| 44 | +EOT |
| 45 | + ), |
| 46 | + ], |
| 47 | + 'output to stdout and stderr' => [ |
| 48 | + new Result('test-stdout', 'test-stderr'), |
| 49 | + new Job( |
| 50 | + <<<'EOT' |
| 51 | +<?php declare(strict_types=1); |
| 52 | +fwrite(STDOUT, 'test-stdout'); |
| 53 | +fwrite(STDERR, 'test-stderr'); |
| 54 | + |
| 55 | +EOT |
| 56 | + ), |
| 57 | + ], |
| 58 | + 'redirect stderr to stdout' => [ |
| 59 | + new Result('test', ''), |
| 60 | + new Job( |
| 61 | + <<<'EOT' |
| 62 | +<?php declare(strict_types=1); |
| 63 | +fwrite(STDERR, 'test'); |
| 64 | + |
| 65 | +EOT, |
| 66 | + redirectErrors: true, |
| 67 | + ), |
| 68 | + ], |
| 69 | + 'environment variables' => [ |
| 70 | + new Result('test', ''), |
| 71 | + new Job( |
| 72 | + <<<'EOT' |
| 73 | +<?php declare(strict_types=1); |
| 74 | +print getenv('test'); |
| 75 | + |
| 76 | +EOT, |
| 77 | + environmentVariables: ['test' => 'test'], |
| 78 | + ), |
| 79 | + ], |
| 80 | + 'arguments' => [ |
| 81 | + new Result('test', ''), |
| 82 | + new Job( |
| 83 | + <<<'EOT' |
| 84 | +<?php declare(strict_types=1); |
| 85 | +print $argv[1]; |
| 86 | + |
| 87 | +EOT, |
| 88 | + arguments: ['test'], |
| 89 | + ), |
| 90 | + ], |
| 91 | + ]; |
| 92 | + } |
| 93 | + |
| 94 | + #[DataProvider('provider')] |
| 95 | + public function testRunsJobInSeparateProcess(Result $expected, Job $job): void |
| 96 | + { |
| 97 | + $jobRunner = new DefaultJobRunner; |
| 98 | + |
| 99 | + $result = $jobRunner->run($job); |
| 100 | + |
| 101 | + $this->assertSame($expected->stdout(), $result->stdout()); |
| 102 | + $this->assertSame($expected->stderr(), $result->stderr()); |
| 103 | + } |
| 104 | +} |
0 commit comments