Skip to content

Add WP_CLI_TEST_XDEBUG environment variable for step debugging in Behat tests - #289

Merged
swissspidy merged 10 commits into
mainfrom
copilot/enable-xdebug-in-behat-tests-again
Nov 24, 2026
Merged

swissspidy merged 10 commits into
mainfrom
copilot/enable-xdebug-in-behat-tests-again

Conversation

Copilot AI commented Dec 1, 2025 •

Copy link
Copy Markdown
Contributor

Adds support for enabling Xdebug step debugging when running Behat tests, allowing developers to debug the actual WP-CLI + WordPress code executed during tests.

Changes:

  • Add running_with_xdebug() method in FeatureContext.php (mirrors existing running_with_code_coverage() pattern)
  • When enabled, set Xdebug environment variables for the spawned WP-CLI processes, without overriding existing settings:
    • XDEBUG_MODE: debug is added to the modes set through XDEBUG_MODE or xdebug.mode (minus off), plus coverage when WP_CLI_TEST_COVERAGE is on
    • XDEBUG_SESSION=1, unless already set
    • XDEBUG_CONFIG: log_level=0 is appended unless a log level is set, so "Could not connect to debugging client" warnings don't end up in the checked output
  • bin/run-behat-tests runs without Docker when step debugging, like it does for coverage
  • Document usage in .readme-partials/USING.md and README.md
  • Unit tests in tests/tests/TestXdebugEnv.php

Usage:

WP_CLI_TEST_XDEBUG=true composer behat -- features/cli-info.feature:12

Avoid parallel workers while step debugging.

Fixes #257

…ugging in Behat tests

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Enable Xdebug in Behat tests Add WP_CLI_TEST_XDEBUG environment variable for step debugging in Behat tests Dec 1, 2025
Copilot AI requested a review from swissspidy December 1, 2025 16:22
@codecov

codecov Bot commented Dec 1, 2025 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.95652% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Context/FeatureContext.php 86.95% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

@swissspidy

Copy link
Copy Markdown
Member

@BrianHenryIE wanna test this one?

@BrianHenryIE

BrianHenryIE commented Dec 2, 2025 •

Copy link
Copy Markdown
Member

I took a look at using xdebug_info() function, thinking of possibly always enabling Xdebug when it is enabled for the current process. Then rather than running WP_CLI_TEST_XDEBUG=true behat the existing XDEBUG_MODE=debug behat could be used. But I'm not sure that it's a good idea or not. Maybe it could default to using XDEBUG_MODE on the inner process and have WP_CLI_TEST_XDEBUG as a means to disable that.

For visual: php -r "xdebug_info();"

Full xdebug_info output (expand)
__   __   _      _                 
\ \ / /  | |    | |                
 \ V / __| | ___| |__  _   _  __ _ 
  > < / _` |/ _ \ '_ \| | | |/ _` |
 / . \ (_| |  __/ |_) | |_| | (_| |
/_/ \_\__,_|\___|_.__/ \__,_|\__, |
                              __/ |
                             |___/ 

Version => 3.4.0alpha2-dev
Support Xdebug on Patreon, GitHub, or as a business: https://xdebug.org/support

             Enabled Features (through 'xdebug.mode' setting)             
Feature => Enabled/Disabled
Development Helpers => ✔ enabled
Coverage => ✘ disabled
GC Stats => ✘ disabled
Profiler => ✘ disabled
Step Debugger => ✘ disabled
Tracing => ✘ disabled

                            Optional Features                            
Compressed File Support => yes (gzip)
Clock Source => clock_gettime_nsec_np
'xdebug://gateway' pseudo-host support => no
'xdebug://nameserver' pseudo-host support => no
Systemd Private Temp Directory => not enabled

                              Diagnostic Log                              
No messages

                                   PHP                                   
                           Build Configuration                           
Version (Run Time) => 8.4.14
Version (Compile Time) => 8.4.0-dev
Debug Build => no
Thread Safety => disabled
                                 Settings                                 
Configuration File (php.ini) Path => /opt/homebrew/etc/php/8.4
Loaded Configuration File => (none)
Scan this dir for additional .ini files => /Users/brian.henry/Library/Application Support/Herd/config/php/84/
Additional .ini files parsed => /Users/brian.henry/Library/Application Support/Herd/config/php/84/php.ini


Directive => Local Value => Master Value
xdebug.mode => develop => develop
xdebug.start_with_request => default => default
xdebug.start_upon_error => default => default
xdebug.output_dir => /var/tmp/ => /var/tmp/
xdebug.use_compression => 1 => 1
xdebug.trigger_value => no value => no value
xdebug.file_link_format => no value => no value
xdebug.filename_format => no value => no value
xdebug.log => no value => no value
xdebug.log_level => 7 => 7
xdebug.var_display_max_children => 128 => 128
xdebug.var_display_max_data => 512 => 512
xdebug.var_display_max_depth => 3 => 3
xdebug.max_nesting_level => 512 => 512
xdebug.cli_color => 0 => 0
xdebug.force_display_errors => Off => Off
xdebug.force_error_reporting => 0 => 0
xdebug.halt_level => 0 => 0
xdebug.max_stack_frames => -1 => -1
xdebug.show_error_trace => Off => Off
xdebug.show_exception_trace => Off => Off
xdebug.show_local_vars => Off => Off
xdebug.dump.COOKIE => no value => no value
xdebug.dump.ENV => no value => no value
xdebug.dump.FILES => no value => no value
xdebug.dump.GET => no value => no value
xdebug.dump.POST => no value => no value
xdebug.dump.REQUEST => no value => no value
xdebug.dump.SERVER => no value => no value
xdebug.dump.SESSION => no value => no value
xdebug.dump_globals => On => On
xdebug.dump_once => On => On
xdebug.dump_undefined => Off => Off
xdebug.profiler_output_name => cachegrind.out.%p => cachegrind.out.%p
xdebug.profiler_append => Off => Off
xdebug.cloud_id => no value => no value
xdebug.client_host => localhost => localhost
xdebug.client_port => 9003 => 9003
xdebug.discover_client_host => Off => Off
xdebug.client_discovery_header => HTTP_X_FORWARDED_FOR,REMOTE_ADDR => HTTP_X_FORWARDED_FOR,REMOTE_ADDR
xdebug.idekey => no value => no value
xdebug.connect_timeout_ms => 200 => 200
xdebug.scream => Off => Off
xdebug.gc_stats_output_name => gcstats.%p => gcstats.%p
xdebug.trace_output_name => trace.%c => trace.%c
xdebug.trace_format => 0 => 0
xdebug.trace_options => 0 => 0
xdebug.collect_assignments => Off => Off
xdebug.collect_params => On => On
xdebug.collect_return => Off => Off

Partial xdebug_info output:

Directive => Local Value => Master Value
xdebug.mode => develop => develop
...
xdebug.log_level => 7 => 7
...
xdebug.idekey => no value => no value

It accepts a category parameter:

% php -r "print_r(xdebug_info('mode'));"

Array
(
    [0] => develop
)
% XDEBUG_MODE=debug php -r "print_r(xdebug_info('mode'));"

Array
(
    [0] => debug
)

It seems there is only 'mode' as a value that can be passed:

% php -r "print_r(xdebug_info('idekey'));"   

Warning: xdebug_info(): The information group 'idekey' is not available in Command line code on line 1

No tests in xdebug/xdebug suggested any other valid values.

I wrote the code to allow specifying the idekey: BrianHenryIE@951eb46

But after reading a bit more I don't know if it's worthwhile:

https://xdebug.org/docs/all_settings#idekey

Controls which IDE Key Xdebug should pass on to the debugging client or proxy. The IDE Key is only important for use with the DBGp Proxy Tool, although some IDEs are incorrectly picky as to what its value is.

https://www.jetbrains.com/help/phpstorm/configuring-xdebug.html#updatingPhpIni

To enable multi-user debugging via Xdebug proxies, locate the xdebug.idekey setting and assign it a value of your choice. This value will be used to register your IDE on Xdebug proxy servers.

I've still to actually run the code to test it but it reads like how I remember doing it before.

TODO:

  • Get opinions on always enabling Xdebug when the parent process has it enabled
  • Get feedback on value of idekey configuration
  • Test the code by running and confirm functionality
  • Compare changes between Xdebug 2 and Xdebug 3 to determine if this is compatible with both or requires v3 (xdebug_info is v3 only)
  • Consider adding wp-cli.yml/behat.yml config option

@swissspidy

Copy link
Copy Markdown
Member

Get opinions on always enabling Xdebug when the parent process has it enabled

Personally I'd want separate options, to have more flexibility and not accidentally slow down my whole test suite if I want to only debug the test runner and not the commands executed by it.

Compare changes between Xdebug 2 and Xdebug 3 to determine if this is compatible with both or requires v3 (xdebug_info is v3 only)

Should be reasonable to support only Xdebug 3

This comment was marked as resolved.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@swissspidy
swissspidy marked this pull request as ready for review March 13, 2026 21:03
@swissspidy
swissspidy requested a review from a team as a code owner March 13, 2026 21:03
Comment thread src/Context/FeatureContext.php Outdated
$env['XDEBUG_MODE'] = 'debug';
$env['XDEBUG_SESSION'] = '1';
$env['XDEBUG_CONFIG'] = 'idekey=WP_CLI_TEST_XDEBUG log_level=0';
$xdebug_mode = getenv( 'XDEBUG_MODE' );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially I was thinking using getenv() wouldn't be enough, but if XDebug's mode is set already in the php.ini then it should pass through anyway.

Comment thread src/Context/FeatureContext.php Outdated
}

if ( false === getenv( 'XDEBUG_CONFIG' ) ) {
$env['XDEBUG_CONFIG'] = 'idekey=WP_CLI_TEST_XDEBUG log_level=0';

@BrianHenryIE BrianHenryIE Mar 14, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...which makes me think this line is wrong.

If a user has defined these in php.ini (I've never done that myself) then this will apply defaults that are incorrect.

I'm starting to think the commit I wrote & mentioned which I dismissed might be a good idea after all: BrianHenryIE@951eb46

@BrianHenryIE BrianHenryIE left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😕 I haven't yet run this end to end. Your December 4 comments were reasonable. You didn't reply to the suggestion of having a wp-cli.yml config option and it's totally reasonable to address that in its own PR when/if needed.

Comment thread src/Context/FeatureContext.php Outdated
*
* @return bool
*/
private static function running_with_xdebug() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think return type : bool is acceptable since PHP 7.

I hate the WPCS rule that makes me define it twice.

@swissspidy

Copy link
Copy Markdown
Member

😕 I haven't yet run this end to end. Your December 4 comments were reasonable. You didn't reply to the suggestion of having a wp-cli.yml config option and it's totally reasonable to address that in its own PR when/if needed.

Thanks for the initial review! And sorry for not replying to that :) A behat.yml config is interesting, but needs testing to verify that Behat isn't confused by such a setting. wp-cli.yml is for WP-CLI runtime configs, not testing, so not the right place IMO

@swissspidy swissspidy added scope:testing Related to testing scope:distribution Related to distribution labels Apr 6, 2026
swissspidy and others added 4 commits August 6, 2026 14:30
Resolves the conflict with the PHP tuning added on main, which switches
Xdebug off for the launched processes: the Xdebug variables are now merged
after it, so that step debugging still works.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 13ea3d1b-21b7-4bab-a028-436fae07b1ee

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

`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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
@swissspidy swissspidy added this to the 5.5.5 milestone Nov 24, 2026
@swissspidy
swissspidy merged commit 6057bfa into main Nov 24, 2026
69 checks passed
@swissspidy
swissspidy deleted the copilot/enable-xdebug-in-behat-tests-again branch November 24, 2026 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope:distribution Related to distribution scope:testing Related to testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enable Xdebug in Behat tests

4 participants