Skip to content

Enable Xdebug in Behat tests #257

Open

Description

@BrianHenryIE

Feature Request

Describe your use case and the problem you are facing

When working with the Behat tests, I'd like to be able to step debug the actual WP CLI + WordPress code which is being run.

Describe the solution you'd like

Add a WP CLI environmental variable to enable Xdebug, which would prepend any WP CLI commands run by Behat with Xdebug environmental variables to enable it.

WP_CLI_TEST_XDEBUG=true composer behat

In wp-cli/wp-cli/php/WP_CLI/Process.php:73 add:

if( getenv( 'WP_CLI_TEST_XDEBUG' ) ) {
  $this->command = "XDEBUG_CONFIG=\"idekey=WP_CLI_TEST_XDEBUG remote_connect_back=1 log_level=0\" XDEBUG_MODE=debug $this->command";
}

Drawbacks:

Definitely slower. And I seem to have to click play often, but maybe that's just a PhpStorm setting I've got wrong.

Alternatives:

Edit every Behat feature to add XDEBUG_CONFIG="idekey=WP_CLI_XDEBUG remote_connect_back=1 log_level=0" XDEBUG_MODE=debug as needed.

I seem to need XDEBUG_SESSION=1 sometimes – I certainly did not need it when I opened the issue.

Activity

  1. transferred this issue fromwp-cli/ideason May 13, 2025
  2. swissspidy commented on May 13, 2025

    @swissspidy
    Member

    We do something similar already when running tests with code coverage:

    $with_code_coverage = (string) getenv( 'WP_CLI_TEST_COVERAGE' );
    if ( \in_array( $with_code_coverage, [ 'true', '1' ], true ) ) {
    $has_coverage_driver = ( new Runtime() )->hasXdebug() || ( new Runtime() )->hasPCOV();
    if ( ! $has_coverage_driver ) {
    throw new RuntimeException( 'No coverage driver available. Re-run script with `--xdebug` flag, i.e. `composer behat -- --xdebug`.' );
    }
    $coverage_require_file = self::$behat_run_dir . '/vendor/wp-cli/wp-cli-tests/utils/generate-coverage.php';
    if ( ! file_exists( $coverage_require_file ) ) {
    // This file is not vendored inside the wp-cli-tests project
    $coverage_require_file = self::$behat_run_dir . '/utils/generate-coverage.php';
    }
    $current = getenv( 'WP_CLI_REQUIRE' );
    $updated = $current ? "{$current},{$coverage_require_file}" : $coverage_require_file;
    $env['WP_CLI_REQUIRE'] = $updated;
    }

    So this could be an easy addition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions