From f802ab2bceb3840ec3dc3113516e7d9163d609f5 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 1 Dec 2025 15:29:09 +0000 Subject: [PATCH 1/5] Initial plan From 21b82d34511bc81bb2096209afc29c1f69b0808e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 1 Dec 2025 16:20:49 +0000 Subject: [PATCH 2/5] Add WP_CLI_TEST_XDEBUG environment variable to enable Xdebug step debugging in Behat tests Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> --- README.md | 17 +++++++++++++++++ src/Context/FeatureContext.php | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/README.md b/README.md index 3be233521..0003e568a 100644 --- a/README.md +++ b/README.md @@ -147,6 +147,23 @@ chmod +x ~/wp-cli/wp WP_CLI_BIN_DIR=~/wp-cli composer behat ``` +#### Xdebug Step Debugging + +You can enable Xdebug step debugging for Behat tests by setting the `WP_CLI_TEST_XDEBUG` environment variable. + +This is useful when you want to debug the actual WP-CLI + WordPress code that is being run during the tests. + +```bash +WP_CLI_TEST_XDEBUG=true composer behat +``` + +This will set the following Xdebug environment variables for all WP-CLI processes spawned by the tests: +- `XDEBUG_MODE=debug` +- `XDEBUG_SESSION=1` +- `XDEBUG_CONFIG="idekey=WP_CLI_TEST_XDEBUG log_level=0"` + +Note: You need to have Xdebug installed and your IDE configured to listen for debug connections. + ### Setting up the tests in Travis CI Basic rules for setting up the test framework with Travis CI: diff --git a/src/Context/FeatureContext.php b/src/Context/FeatureContext.php index 4ec3c15a3..739dd0e29 100644 --- a/src/Context/FeatureContext.php +++ b/src/Context/FeatureContext.php @@ -282,6 +282,17 @@ private static function running_with_code_coverage() { return \in_array( $with_code_coverage, [ 'true', '1' ], true ); } + /** + * Whether tests are currently running with Xdebug step debugging enabled. + * + * @return bool + */ + private static function running_with_xdebug() { + $with_xdebug = (string) getenv( 'WP_CLI_TEST_XDEBUG' ); + + return \in_array( $with_xdebug, [ 'true', '1' ], true ); + } + /** * @AfterSuite */ @@ -466,6 +477,12 @@ private static function get_process_env_variables(): array { $env['WP_CLI_REQUIRE'] = $updated; } + if ( self::running_with_xdebug() ) { + $env['XDEBUG_MODE'] = 'debug'; + $env['XDEBUG_SESSION'] = '1'; + $env['XDEBUG_CONFIG'] = 'idekey=WP_CLI_TEST_XDEBUG log_level=0'; + } + $config_path = getenv( 'WP_CLI_CONFIG_PATH' ); if ( false !== $config_path ) { $env['WP_CLI_CONFIG_PATH'] = $config_path; From 54466123c36d75322c40716d1750f5539fe4c0db Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Thu, 12 Mar 2026 15:18:32 +0100 Subject: [PATCH 3/5] Update src/Context/FeatureContext.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/Context/FeatureContext.php | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Context/FeatureContext.php b/src/Context/FeatureContext.php index 934b056b1..de6c3a709 100644 --- a/src/Context/FeatureContext.php +++ b/src/Context/FeatureContext.php @@ -471,9 +471,32 @@ private static function get_process_env_variables(): array { } if ( self::running_with_xdebug() ) { - $env['XDEBUG_MODE'] = 'debug'; - $env['XDEBUG_SESSION'] = '1'; - $env['XDEBUG_CONFIG'] = 'idekey=WP_CLI_TEST_XDEBUG log_level=0'; + $xdebug_mode = getenv( 'XDEBUG_MODE' ); + if ( false !== $xdebug_mode && '' !== $xdebug_mode ) { + $modes = array_filter( + array_map( + 'trim', + explode( ',', $xdebug_mode ) + ), + static function ( $mode ) { + return '' !== $mode; + } + ); + if ( ! in_array( 'debug', $modes, true ) ) { + $modes[] = 'debug'; + } + $env['XDEBUG_MODE'] = implode( ',', $modes ); + } else { + $env['XDEBUG_MODE'] = 'debug'; + } + + if ( false === getenv( 'XDEBUG_SESSION' ) ) { + $env['XDEBUG_SESSION'] = '1'; + } + + if ( false === getenv( 'XDEBUG_CONFIG' ) ) { + $env['XDEBUG_CONFIG'] = 'idekey=WP_CLI_TEST_XDEBUG log_level=0'; + } } $config_path = getenv( 'WP_CLI_CONFIG_PATH' ); From 7efac11932590ac3061a754bcd4544dedf898e07 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 24 Nov 2026 10:39:38 +0000 Subject: [PATCH 4/5] Keep coverage and user Xdebug settings when step debugging Add the debug mode to the modes set through XDEBUG_MODE or php.ini instead of replacing them, and keep the coverage mode when code coverage is collected. Append the lowered log level to a user's XDEBUG_CONFIG rather than leaving it out, as the warnings Xdebug prints when no IDE is listening make scenarios fail, and drop the IDE key, which only matters for DBGp proxies and overrode the one set in php.ini. Don't use Docker when step debugging, as the containers would neither receive the variable nor reach the IDE. Move the documentation into the README partial and add unit tests. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx --- .readme-partials/USING.md | 20 +++++- README.md | 21 ++++--- bin/run-behat-tests | 2 + tests/tests/TestXdebugEnv.php | 112 ++++++++++++++++++++++++++++++++++ 4 files changed, 144 insertions(+), 11 deletions(-) create mode 100644 tests/tests/TestXdebugEnv.php diff --git a/.readme-partials/USING.md b/.readme-partials/USING.md index 38d523381..252ba6b28 100644 --- a/.readme-partials/USING.md +++ b/.readme-partials/USING.md @@ -276,7 +276,7 @@ WP_CLI_TEST_DOCKER=auto WP_CLI_TEST_PARALLEL=4 composer behat | `auto` | The scenarios run in containers where possible, and without Docker otherwise, which is reported. | | `1`, `yes`, `on` | The scenarios run in containers, and the run fails where that is not possible. | -Docker is not used on Windows, when collecting code coverage, when Docker is not available, when the image cannot be built or when the database server cannot be identified. +Docker is not used on Windows, when collecting code coverage, when step debugging, when Docker is not available, when the image cannot be built or when the database server cannot be identified. Each worker gets its own network, temporary directory and database server, of the same type and version as the one configured through the `WP_CLI_TEST_DB*` variables (or set the image through `WP_CLI_TEST_DOCKER_DB`, e.g. `mysql:8.0`, so that the host needs neither a database server nor a client). Nothing is shared between workers, so they can run any scenario. The database servers keep their data in memory. Scenarios selected by path or name run in a single container. @@ -426,6 +426,24 @@ chmod +x ~/wp-cli/wp WP_CLI_BIN_DIR=~/wp-cli composer behat ``` +#### Xdebug Step Debugging + +Set `WP_CLI_TEST_XDEBUG` to step debug the WP-CLI and WordPress code that the scenarios run, with Xdebug 3: + +```bash +WP_CLI_TEST_XDEBUG=true composer behat -- features/cli-info.feature:12 +``` + +Every WP-CLI process launched by the tests then starts a debugging session, which your IDE needs to listen for. The test runner itself is not debugged; set `XDEBUG_MODE` and `XDEBUG_SESSION` for that. + +The launched processes get the following environment variables: + +* `XDEBUG_MODE`: `debug`, added to the modes set through `XDEBUG_MODE` or `xdebug.mode`, and to `coverage` when collecting code coverage. +* `XDEBUG_SESSION=1`, unless `XDEBUG_SESSION` is set already. +* `XDEBUG_CONFIG`: `log_level=0`, added to the settings in `XDEBUG_CONFIG` unless they set a log level, so that Xdebug does not print warnings into the output the scenarios check when no IDE is listening. + +As every process connects to the debugger, run a single scenario or feature file rather than the whole suite, and without parallel workers. Docker is not used when step debugging. + ### Setting up the tests in Travis CI Basic rules for setting up the test framework with Travis CI: diff --git a/README.md b/README.md index 43e36f910..bcf13dc90 100644 --- a/README.md +++ b/README.md @@ -287,7 +287,7 @@ WP_CLI_TEST_DOCKER=auto WP_CLI_TEST_PARALLEL=4 composer behat | `auto` | The scenarios run in containers where possible, and without Docker otherwise, which is reported. | | `1`, `yes`, `on` | The scenarios run in containers, and the run fails where that is not possible. | -Docker is not used on Windows, when collecting code coverage, when Docker is not available, when the image cannot be built or when the database server cannot be identified. +Docker is not used on Windows, when collecting code coverage, when step debugging, when Docker is not available, when the image cannot be built or when the database server cannot be identified. Each worker gets its own network, temporary directory and database server, of the same type and version as the one configured through the `WP_CLI_TEST_DB*` variables (or set the image through `WP_CLI_TEST_DOCKER_DB`, e.g. `mysql:8.0`, so that the host needs neither a database server nor a client). Nothing is shared between workers, so they can run any scenario. The database servers keep their data in memory. Scenarios selected by path or name run in a single container. @@ -439,20 +439,21 @@ WP_CLI_BIN_DIR=~/wp-cli composer behat #### Xdebug Step Debugging -You can enable Xdebug step debugging for Behat tests by setting the `WP_CLI_TEST_XDEBUG` environment variable. - -This is useful when you want to debug the actual WP-CLI + WordPress code that is being run during the tests. +Set `WP_CLI_TEST_XDEBUG` to step debug the WP-CLI and WordPress code that the scenarios run, with Xdebug 3: ```bash -WP_CLI_TEST_XDEBUG=true composer behat +WP_CLI_TEST_XDEBUG=true composer behat -- features/cli-info.feature:12 ``` -This will set the following Xdebug environment variables for all WP-CLI processes spawned by the tests: -- `XDEBUG_MODE=debug` -- `XDEBUG_SESSION=1` -- `XDEBUG_CONFIG="idekey=WP_CLI_TEST_XDEBUG log_level=0"` +Every WP-CLI process launched by the tests then starts a debugging session, which your IDE needs to listen for. The test runner itself is not debugged; set `XDEBUG_MODE` and `XDEBUG_SESSION` for that. + +The launched processes get the following environment variables: + +* `XDEBUG_MODE`: `debug`, added to the modes set through `XDEBUG_MODE` or `xdebug.mode`, and to `coverage` when collecting code coverage. +* `XDEBUG_SESSION=1`, unless `XDEBUG_SESSION` is set already. +* `XDEBUG_CONFIG`: `log_level=0`, added to the settings in `XDEBUG_CONFIG` unless they set a log level, so that Xdebug does not print warnings into the output the scenarios check when no IDE is listening. -Note: You need to have Xdebug installed and your IDE configured to listen for debug connections. +As every process connects to the debugger, run a single scenario or feature file rather than the whole suite, and without parallel workers. Docker is not used when step debugging. ### Setting up the tests in Travis CI diff --git a/bin/run-behat-tests b/bin/run-behat-tests index ab068ce41..d011a770e 100755 --- a/bin/run-behat-tests +++ b/bin/run-behat-tests @@ -60,6 +60,8 @@ if [ -n "${DOCKER}" ]; then without_docker "Docker is not available." elif [[ "${WP_CLI_TEST_COVERAGE}" == "true" ]]; then without_docker "Code coverage cannot be collected in Docker containers." + elif [[ "${WP_CLI_TEST_XDEBUG}" == "true" || "${WP_CLI_TEST_XDEBUG}" == "1" ]]; then + without_docker "Step debugging is not available in Docker containers." fi fi diff --git a/tests/tests/TestXdebugEnv.php b/tests/tests/TestXdebugEnv.php new file mode 100644 index 000000000..4dea96d64 --- /dev/null +++ b/tests/tests/TestXdebugEnv.php @@ -0,0 +1,112 @@ + + */ + private $original_env = []; + + protected function set_up(): void { + parent::set_up(); + + foreach ( self::VARIABLES as $name ) { + $this->original_env[ $name ] = getenv( $name ); + putenv( $name ); + } + } + + protected function tear_down(): void { + foreach ( $this->original_env as $name => $value ) { + putenv( false === $value ? $name : "{$name}={$value}" ); + } + + parent::tear_down(); + } + + /** + * @return array + */ + private function get_xdebug_env_variables(): array { + $method = new ReflectionMethod( FeatureContext::class, 'get_xdebug_env_variables' ); + if ( PHP_VERSION_ID < 80100 ) { + $method->setAccessible( true ); + } + + /** @var array */ + return $method->invoke( null ); + } + + public function test_nothing_is_set_without_the_variable(): void { + putenv( 'WP_CLI_TEST_XDEBUG=false' ); + + $this->assertSame( [], $this->get_xdebug_env_variables() ); + } + + public function test_defaults(): void { + putenv( 'WP_CLI_TEST_XDEBUG=1' ); + putenv( 'XDEBUG_MODE=' ); + + $this->assertSame( + [ + 'XDEBUG_MODE' => 'debug', + 'XDEBUG_SESSION' => '1', + 'XDEBUG_CONFIG' => 'log_level=0', + ], + $this->get_xdebug_env_variables() + ); + } + + public function test_debug_mode_is_added_to_the_ini_setting(): void { + putenv( 'WP_CLI_TEST_XDEBUG=true' ); + + $modes = explode( ',', $this->get_xdebug_env_variables()['XDEBUG_MODE'] ); + + $this->assertContains( 'debug', $modes ); + $this->assertNotContains( 'off', $modes ); + } + + public function test_debug_mode_is_added_to_the_requested_modes(): void { + putenv( 'WP_CLI_TEST_XDEBUG=true' ); + putenv( 'XDEBUG_MODE=develop, off,trace' ); + + $this->assertSame( 'develop,trace,debug', $this->get_xdebug_env_variables()['XDEBUG_MODE'] ); + } + + public function test_coverage_mode_is_kept_when_collecting_code_coverage(): void { + putenv( 'WP_CLI_TEST_XDEBUG=true' ); + putenv( 'WP_CLI_TEST_COVERAGE=true' ); + putenv( 'XDEBUG_MODE=debug' ); + + $this->assertSame( 'debug,coverage', $this->get_xdebug_env_variables()['XDEBUG_MODE'] ); + } + + public function test_session_is_not_overridden(): void { + putenv( 'WP_CLI_TEST_XDEBUG=true' ); + putenv( 'XDEBUG_SESSION=PHPSTORM' ); + + $this->assertArrayNotHasKey( 'XDEBUG_SESSION', $this->get_xdebug_env_variables() ); + } + + public function test_log_level_is_added_to_the_config(): void { + putenv( 'WP_CLI_TEST_XDEBUG=true' ); + putenv( 'XDEBUG_CONFIG=client_host=192.168.1.2' ); + + $this->assertSame( 'client_host=192.168.1.2 log_level=0', $this->get_xdebug_env_variables()['XDEBUG_CONFIG'] ); + } + + public function test_config_with_a_log_level_is_not_overridden(): void { + putenv( 'WP_CLI_TEST_XDEBUG=true' ); + putenv( 'XDEBUG_CONFIG=log_level=7' ); + + $this->assertArrayNotHasKey( 'XDEBUG_CONFIG', $this->get_xdebug_env_variables() ); + } +} From 1da5344867e6f4e4c53b7dd8d4c2d81bb5251c26 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 24 Nov 2026 15:17:00 +0000 Subject: [PATCH 5/5] Call the private method through a bound closure in the Xdebug tests `ReflectionMethod::setAccessible()` is deprecated as of PHP 8.5, which PHPStan reports even behind the PHP version check. A closure bound to the FeatureContext scope works on all supported PHP versions. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx --- tests/tests/TestXdebugEnv.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/tests/tests/TestXdebugEnv.php b/tests/tests/TestXdebugEnv.php index 4dea96d64..d94ef5ff3 100644 --- a/tests/tests/TestXdebugEnv.php +++ b/tests/tests/TestXdebugEnv.php @@ -2,7 +2,7 @@ namespace WP_CLI\Tests\Tests; -use ReflectionMethod; +use Closure; use WP_CLI\Tests\Context\FeatureContext; use WP_CLI\Tests\TestCase; @@ -36,13 +36,15 @@ protected function tear_down(): void { * @return array */ private function get_xdebug_env_variables(): array { - $method = new ReflectionMethod( FeatureContext::class, 'get_xdebug_env_variables' ); - if ( PHP_VERSION_ID < 80100 ) { - $method->setAccessible( true ); - } + $get_variables = Closure::bind( + static function (): array { + return FeatureContext::get_xdebug_env_variables(); + }, + null, + FeatureContext::class + ); - /** @var array */ - return $method->invoke( null ); + return $get_variables(); } public function test_nothing_is_set_without_the_variable(): void {