|
| 1 | +<?php |
| 2 | + |
| 3 | +class PHPUnitTextUICommandShimTest extends \PHPUnit\Framework\TestCase |
| 4 | +{ |
| 5 | + public function testRunConsumesSpaceSeparatedLongOptionValue(): void |
| 6 | + { |
| 7 | + [ $exitCode, $output ] = $this->runShimProcess( |
| 8 | + [ 'phpunit', '--help', '--dsn', 'mysql://root@127.0.0.1/testdb' ] |
| 9 | + ); |
| 10 | + |
| 11 | + $this->assertSame( 0, $exitCode, $output ); |
| 12 | + $this->assertStringNotContainsString( 'Unknown option "--dsn"', $output ); |
| 13 | + } |
| 14 | + |
| 15 | + public function testRunConsumesEqualsSeparatedLongOptionValue(): void |
| 16 | + { |
| 17 | + [ $exitCode, $output ] = $this->runShimProcess( |
| 18 | + [ 'phpunit', '--help', '--dsn=mysql://root@127.0.0.1/testdb' ] |
| 19 | + ); |
| 20 | + |
| 21 | + $this->assertSame( 0, $exitCode, $output ); |
| 22 | + $this->assertStringNotContainsString( 'Unknown option "--dsn"', $output ); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * @return array{int,string} |
| 27 | + */ |
| 28 | + private function runShimProcess( array $argv ): array |
| 29 | + { |
| 30 | + $script = tempnam( sys_get_temp_dir(), 'phpunit-shim-' ); |
| 31 | + if ( $script === false ) |
| 32 | + $this->fail( 'Unable to create temporary script.' ); |
| 33 | + |
| 34 | + $repoRoot = realpath( __DIR__ . '/../../../../..' ); |
| 35 | + if ( $repoRoot === false ) |
| 36 | + $this->fail( 'Unable to resolve repository root.' ); |
| 37 | + |
| 38 | + $payload = var_export( $argv, true ); |
| 39 | + $code = <<<PHP |
| 40 | +<?php |
| 41 | +require_once '{$repoRoot}/tests/bootstrap.php'; |
| 42 | +class ShimCommandForCli extends PHPUnit_TextUI_Command |
| 43 | +{ |
| 44 | + public function __construct() |
| 45 | + { |
| 46 | + \$this->longOptions['dsn='] = 'handleDsn'; |
| 47 | + \$this->arguments['dsn'] = ''; |
| 48 | + } |
| 49 | + public function handleDsn( \$value ): void |
| 50 | + { |
| 51 | + \$this->arguments['dsn'] = \$value; |
| 52 | + } |
| 53 | +} |
| 54 | +\$cmd = new ShimCommandForCli(); |
| 55 | +\$cmd->run( {$payload} ); |
| 56 | +PHP; |
| 57 | + |
| 58 | + $written = file_put_contents( $script, $code ); |
| 59 | + if ( $written === false ) |
| 60 | + { |
| 61 | + @unlink( $script ); |
| 62 | + $this->fail( 'Unable to write temporary shim script.' ); |
| 63 | + } |
| 64 | + |
| 65 | + $lines = []; |
| 66 | + $exitCode = 1; |
| 67 | + try |
| 68 | + { |
| 69 | + exec( escapeshellarg( PHP_BINARY ) . ' ' . escapeshellarg( $script ) . ' 2>&1', $lines, $exitCode ); |
| 70 | + } |
| 71 | + finally |
| 72 | + { |
| 73 | + if ( file_exists( $script ) ) |
| 74 | + unlink( $script ); |
| 75 | + } |
| 76 | + |
| 77 | + return [ $exitCode, implode( "\n", $lines ) ]; |
| 78 | + } |
| 79 | +} |
0 commit comments