Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions features/behat-steps.feature
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,78 @@ Feature: Test that WP-CLI Behat steps work as expected
404 Not Found
"""

Scenario: Test "a PHP built-in web server" step without an index.php does not use the server-command router
Given an empty directory
And a vendor/wp-cli/server-command/router.php file:
"""
<?php
// Like the router of wp-cli/server-command, send requests for missing files to index.php.
$root = (string) ( $_SERVER['DOCUMENT_ROOT'] ?? '' );
$path = (string) parse_url( (string) ( $_SERVER['REQUEST_URI'] ?? '/' ), PHP_URL_PATH );
if ( is_file( $root . $path ) ) {
return false;
}
echo 'Routed by router.php: ';
require $root . '/index.php';
"""
And a fetch.php file:
"""
<?php
$url = (string) ( $_SERVER['argv'][1] ?? '' );
$headers = @get_headers( $url );
echo is_array( $headers ) ? $headers[0] : 'no response', PHP_EOL, (string) @file_get_contents( $url );
"""
And a PHP built-in web server

# The router would require a missing index.php, so the directory is served as-is.
When I run `php fetch.php http://localhost:8080/missing.html`
Then STDOUT should contain:
"""
404 Not Found
"""
And STDOUT should not contain:
"""
Routed by router.php
"""

Scenario: Test "a PHP built-in web server" step with an index.php uses the server-command router
Given an empty directory
And a vendor/wp-cli/server-command/router.php file:
"""
<?php
// Like the router of wp-cli/server-command, send requests for missing files to index.php.
$root = (string) ( $_SERVER['DOCUMENT_ROOT'] ?? '' );
$path = (string) parse_url( (string) ( $_SERVER['REQUEST_URI'] ?? '/' ), PHP_URL_PATH );
if ( is_file( $root . $path ) ) {
return false;
}
echo 'Routed by router.php: ';
require $root . '/index.php';
"""
And an index.php file:
"""
<?php
echo 'Hello from index.php';
"""
And a fetch.php file:
"""
<?php
$url = (string) ( $_SERVER['argv'][1] ?? '' );
$headers = @get_headers( $url );
echo is_array( $headers ) ? $headers[0] : 'no response', PHP_EOL, (string) @file_get_contents( $url );
"""
And a PHP built-in web server

When I run `php fetch.php http://localhost:8080/hello-world/`
Then STDOUT should contain:
"""
200 OK
"""
And STDOUT should contain:
"""
Routed by router.php: Hello from index.php
"""

@require-wp
Scenario: Test background process launch
Given a WP installation
Expand Down
10 changes: 6 additions & 4 deletions src/Context/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -2095,10 +2095,12 @@ public function start_php_server( $subdir = '' ): void {

// Route requests through the router script of wp-cli/server-command when
// it is available, so that a WordPress installation gets pretty permalinks.
// Without a router, PHP's built-in web server serves the directory as-is,
// which is what a plain directory of static files needs. Passing a router
// path that does not exist would make every request fail with a fatal error.
$router = $this->get_php_server_router();
// The router sends every request for a missing file to index.php, so it is
// only used when the directory has one. Without a router, PHP's built-in
// web server serves the directory as-is, which is what a plain directory of
// static files needs, and a missing file is a 404. Passing a router path
// that does not exist would make every request fail with a fatal error.
$router = is_file( $dir . 'index.php' ) ? $this->get_php_server_router() : null;
if ( null !== $router ) {
$cmd .= ' ' . escapeshellarg( $router );
}
Expand Down
Loading