Skip to content

Commit 16f77f8

Browse files
swissspidyclaude
andauthored
Make the PHP built-in web server step work without server-command (#363)
* Make the PHP built-in web server step work without server-command `start_php_server()` always passed `{RUN_DIR}/vendor/wp-cli/server-command/router.php` as the router script, but no built-in step puts wp-cli/server-command into the run directory (`Given a WP installation with Composer` only requires WordPress core). PHP then fails every request with a fatal error while trying to open the router, and with `display_errors=On` that error page is even served with a 200 status, so a scenario serving static files gets an error page instead of its fixture and a missing file never yields a 404. Only pass the router when it actually exists, looking in the run directory first and then in the vendor directory of the project under test. Without a router, PHP's built-in web server serves the directory as-is, which is what a plain directory of files needs. The `Then the HTTP status code should be` step relied on the Requests v1 class, which WP-CLI no longer autoloads, so it failed with a fatal error before the request was made. Use whichever Requests version WP-CLI ships. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019g8sd6U4w59qiQW7LTt1jG * Honor a custom Composer vendor-dir and assert the served response Look up the server-command router in the vendor directory configured in the run directory's composer.json, so that a WP installation with a custom vendor directory is covered as well. Extend the scenarios to check the response body and a real 404, so that they fail against the old behavior of serving PHP's fatal-error page with a 200 status. PHP's built-in web server falls back to index.html for unknown paths when it exists, so the 404 case lives in its own scenario without an index file. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019g8sd6U4w59qiQW7LTt1jG * Avoid $argv in the feature file fixture PHPStan flags `$argv` as possibly undefined in the PHP blocks of feature files, so read the argument from `$_SERVER['argv']` instead. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019g8sd6U4w59qiQW7LTt1jG * Use get_headers() in the feature file fixture PHP 8.5 deprecates the `$http_response_header` variable, and the deprecation notice on STDERR fails the `I run` step. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019g8sd6U4w59qiQW7LTt1jG --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 902de26 commit 16f77f8

3 files changed

Lines changed: 135 additions & 7 deletions

File tree

‎features/behat-steps.feature‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,65 @@ Feature: Test that WP-CLI Behat steps work as expected
403403
No syntax errors detected
404404
"""
405405

406+
Scenario: Test "a PHP built-in web server" step in a plain directory
407+
Given an empty directory
408+
And an index.html file:
409+
"""
410+
Hello from the built-in web server
411+
"""
412+
And a fetch.php file:
413+
"""
414+
<?php
415+
$url = (string) ( $_SERVER['argv'][1] ?? '' );
416+
$headers = @get_headers( $url );
417+
echo is_array( $headers ) ? $headers[0] : 'no response', PHP_EOL, (string) @file_get_contents( $url );
418+
"""
419+
And a PHP built-in web server
420+
Then the HTTP status code should be 200
421+
422+
# Without a router, the directory is served as-is: the file itself comes back, not a PHP error page.
423+
When I run `php fetch.php http://localhost:8080/`
424+
Then STDOUT should contain:
425+
"""
426+
200 OK
427+
"""
428+
And STDOUT should contain:
429+
"""
430+
Hello from the built-in web server
431+
"""
432+
433+
Scenario: Test "a PHP built-in web server" step returns 404 for a missing file
434+
Given an empty directory
435+
And a hello.html file:
436+
"""
437+
Hello from the built-in web server
438+
"""
439+
And a fetch.php file:
440+
"""
441+
<?php
442+
$url = (string) ( $_SERVER['argv'][1] ?? '' );
443+
$headers = @get_headers( $url );
444+
echo is_array( $headers ) ? $headers[0] : 'no response', PHP_EOL, (string) @file_get_contents( $url );
445+
"""
446+
And a PHP built-in web server
447+
448+
When I run `php fetch.php http://localhost:8080/hello.html`
449+
Then STDOUT should contain:
450+
"""
451+
200 OK
452+
"""
453+
And STDOUT should contain:
454+
"""
455+
Hello from the built-in web server
456+
"""
457+
458+
# No index file means nothing to fall back to, so a missing file is a real 404.
459+
When I run `php fetch.php http://localhost:8080/missing.html`
460+
Then STDOUT should contain:
461+
"""
462+
404 Not Found
463+
"""
464+
406465
@require-wp
407466
Scenario: Test background process launch
408467
Given a WP installation

‎src/Context/FeatureContext.php‎

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2122,16 +2122,82 @@ public function start_php_server( $subdir = '' ): void {
21222122
$dir .= trim( $subdir, '/' ) . '/';
21232123
}
21242124
$cmd = Utils\esc_cmd(
2125-
'%s -S %s -t %s -c %s %s',
2125+
'%s -S %s -t %s -c %s',
21262126
Utils\get_php_binary(),
21272127
'localhost:8080',
21282128
$dir,
2129-
get_cfg_var( 'cfg_file_path' ),
2130-
$this->variables['RUN_DIR'] . '/vendor/wp-cli/server-command/router.php'
2129+
get_cfg_var( 'cfg_file_path' )
21312130
);
2131+
2132+
// Route requests through the router script of wp-cli/server-command when
2133+
// it is available, so that a WordPress installation gets pretty permalinks.
2134+
// Without a router, PHP's built-in web server serves the directory as-is,
2135+
// which is what a plain directory of static files needs. Passing a router
2136+
// path that does not exist would make every request fail with a fatal error.
2137+
$router = $this->get_php_server_router();
2138+
if ( null !== $router ) {
2139+
$cmd .= ' ' . escapeshellarg( $router );
2140+
}
2141+
21322142
$this->background_proc( $cmd );
21332143
}
21342144

2145+
/**
2146+
* Locate the router script of wp-cli/server-command, if it is installed.
2147+
*
2148+
* The Composer vendor directory of the run directory is checked first, so
2149+
* that a scenario which installs the package itself takes precedence,
2150+
* followed by the vendor directory of the project under test.
2151+
*
2152+
* @return string|null Absolute path to the router script, or null if none was found.
2153+
*/
2154+
private function get_php_server_router(): ?string {
2155+
$candidates = [
2156+
$this->variables['RUN_DIR'] . '/' . $this->get_run_dir_composer_vendor_dir(),
2157+
self::get_vendor_dir(),
2158+
];
2159+
2160+
foreach ( $candidates as $vendor_dir ) {
2161+
$router = $vendor_dir . '/wp-cli/server-command/router.php';
2162+
if ( is_file( $router ) ) {
2163+
return $router;
2164+
}
2165+
}
2166+
2167+
return null;
2168+
}
2169+
2170+
/**
2171+
* Get the Composer vendor directory configured in the run directory.
2172+
*
2173+
* Honors a custom `vendor-dir` in the run directory's composer.json, as set
2174+
* up by `Given a WP installation with Composer and a custom vendor directory`.
2175+
*
2176+
* @return string Vendor directory, relative to the run directory.
2177+
*/
2178+
private function get_run_dir_composer_vendor_dir(): string {
2179+
$composer_json = $this->variables['RUN_DIR'] . '/composer.json';
2180+
2181+
if ( ! is_file( $composer_json ) ) {
2182+
return 'vendor';
2183+
}
2184+
2185+
$composer_data = json_decode( (string) file_get_contents( $composer_json ), true );
2186+
2187+
if (
2188+
is_array( $composer_data )
2189+
&& isset( $composer_data['config'] )
2190+
&& is_array( $composer_data['config'] )
2191+
&& isset( $composer_data['config']['vendor-dir'] )
2192+
&& is_string( $composer_data['config']['vendor-dir'] )
2193+
&& '' !== $composer_data['config']['vendor-dir']
2194+
) {
2195+
return trim( $composer_data['config']['vendor-dir'], '/' );
2196+
}
2197+
2198+
return 'vendor';
2199+
}
2200+
21352201
/**
21362202
* @param string $cmd
21372203
*/

‎src/Context/ThenStepDefinitions.php‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
use Behat\Gherkin\Node\PyStringNode;
66
use Behat\Gherkin\Node\TableNode;
77
use WP_CLI\Path;
8+
use WP_CLI\RequestsLibrary;
89
use Exception;
9-
use Requests;
1010
use RuntimeException;
1111

1212
trait ThenStepDefinitions {
@@ -613,8 +613,11 @@ public function then_an_email_should_be_sent( $expected ): void {
613613
* @param int $return_code Expected HTTP status code.
614614
*/
615615
public function then_the_http_status_code_should_be( $return_code ): void {
616-
// @phpstan-ignore staticMethod.deprecatedClass
617-
$response = Requests::request( 'http://localhost:8080' );
618-
$this->assert_equals( $return_code, $response->status_code );
616+
// Use whichever version of the Requests library WP-CLI ships with.
617+
RequestsLibrary::register_autoloader();
618+
$requests_class = RequestsLibrary::get_class_name();
619+
620+
$response = $requests_class::request( 'http://localhost:8080' );
621+
$this->assert_equals( (int) $return_code, (int) $response->status_code );
619622
}
620623
}

0 commit comments

Comments
 (0)