Skip to content

Commit 21b82d3

Browse files
Copilotswissspidy
andcommitted
Add WP_CLI_TEST_XDEBUG environment variable to enable Xdebug step debugging in Behat tests
Co-authored-by: swissspidy <[email protected]>
1 parent f802ab2 commit 21b82d3

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,23 @@ chmod +x ~/wp-cli/wp
147147
WP_CLI_BIN_DIR=~/wp-cli composer behat
148148
```
149149

150+
#### Xdebug Step Debugging
151+
152+
You can enable Xdebug step debugging for Behat tests by setting the `WP_CLI_TEST_XDEBUG` environment variable.
153+
154+
This is useful when you want to debug the actual WP-CLI + WordPress code that is being run during the tests.
155+
156+
```bash
157+
WP_CLI_TEST_XDEBUG=true composer behat
158+
```
159+
160+
This will set the following Xdebug environment variables for all WP-CLI processes spawned by the tests:
161+
- `XDEBUG_MODE=debug`
162+
- `XDEBUG_SESSION=1`
163+
- `XDEBUG_CONFIG="idekey=WP_CLI_TEST_XDEBUG log_level=0"`
164+
165+
Note: You need to have Xdebug installed and your IDE configured to listen for debug connections.
166+
150167
### Setting up the tests in Travis CI
151168

152169
Basic rules for setting up the test framework with Travis CI:

src/Context/FeatureContext.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,17 @@ private static function running_with_code_coverage() {
282282
return \in_array( $with_code_coverage, [ 'true', '1' ], true );
283283
}
284284

285+
/**
286+
* Whether tests are currently running with Xdebug step debugging enabled.
287+
*
288+
* @return bool
289+
*/
290+
private static function running_with_xdebug() {
291+
$with_xdebug = (string) getenv( 'WP_CLI_TEST_XDEBUG' );
292+
293+
return \in_array( $with_xdebug, [ 'true', '1' ], true );
294+
}
295+
285296
/**
286297
* @AfterSuite
287298
*/
@@ -466,6 +477,12 @@ private static function get_process_env_variables(): array {
466477
$env['WP_CLI_REQUIRE'] = $updated;
467478
}
468479

480+
if ( self::running_with_xdebug() ) {
481+
$env['XDEBUG_MODE'] = 'debug';
482+
$env['XDEBUG_SESSION'] = '1';
483+
$env['XDEBUG_CONFIG'] = 'idekey=WP_CLI_TEST_XDEBUG log_level=0';
484+
}
485+
469486
$config_path = getenv( 'WP_CLI_CONFIG_PATH' );
470487
if ( false !== $config_path ) {
471488
$env['WP_CLI_CONFIG_PATH'] = $config_path;

0 commit comments

Comments
 (0)