Skip to content

Commit ec71ca9

Browse files
authored
Merge pull request #80 from se7enxweb/copilot/fix-database-tests-failure
Fix PHPUnit shim arg parsing for DB test runner (`--dsn` with space-separated value)
2 parents 1221270 + 8ec58ea commit ec71ca9

2 files changed

Lines changed: 94 additions & 1 deletion

File tree

tests/bootstrap.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,11 @@ class PHPUnit_TextUI_Command
155155
public function run( array $argv, bool $exit = true ): int
156156
{
157157
$filtered = [ $argv[0] ?? 'phpunit' ];
158-
foreach ( array_slice( $argv, 1 ) as $arg )
158+
$index = 1;
159+
$argc = count( $argv );
160+
while ( $index < $argc )
159161
{
162+
$arg = $argv[$index];
160163
foreach ( $this->longOptions as $opt => $handler )
161164
{
162165
$key = rtrim( $opt, '=' );
@@ -165,16 +168,27 @@ public function run( array $argv, bool $exit = true ): int
165168
$value = substr( $arg, strlen( "--{$key}=" ) );
166169
if ( method_exists( $this, $handler ) )
167170
$this->$handler( $value );
171+
$index++;
172+
continue 2;
173+
}
174+
if ( str_ends_with( $opt, '=' ) && $arg === "--{$key}" && ( $index + 1 ) < $argc )
175+
{
176+
$value = $argv[$index + 1];
177+
if ( method_exists( $this, $handler ) )
178+
$this->$handler( $value );
179+
$index += 2;
168180
continue 2;
169181
}
170182
if ( !str_ends_with( $opt, '=' ) && $arg === "--{$key}" )
171183
{
172184
if ( method_exists( $this, $handler ) )
173185
$this->$handler();
186+
$index++;
174187
continue 2;
175188
}
176189
}
177190
$filtered[] = $arg;
191+
$index++;
178192
}
179193

180194
$app = new PHPUnit\TextUI\Application();
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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

Comments
 (0)