Skip to content
Draft
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
24 changes: 24 additions & 0 deletions features/behat-steps.feature
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,30 @@ Feature: Test that WP-CLI Behat steps work as expected
Given a non-existent test-dir directory
Then the test-dir directory should not exist

Scenario: Always run the current version of files created by a scenario
Given an empty directory
And a script.php file:
"""
<?php echo 'first';
"""

When I run `wp --require=script.php --skip-wordpress eval ";"`
Then STDOUT should be:
"""
first
"""

Given a script.php file:
"""
<?php echo 'other';
"""

When I run `wp --require=script.php --skip-wordpress eval ";"`
Then STDOUT should be:
"""
other
"""

Scenario: Test "Given an empty cache" step
Given a WP installation
And an empty cache
Expand Down
61 changes: 61 additions & 0 deletions src/Context/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,8 @@ private static function get_process_env_variables(): array {
$env['WP_CLI_REQUIRE'] = $updated;
}

$env = array_merge( $env, self::get_php_tuning_env_variables() );

$config_path = getenv( 'WP_CLI_CONFIG_PATH' );
if ( false !== $config_path ) {
$env['WP_CLI_CONFIG_PATH'] = $config_path;
Expand Down Expand Up @@ -555,6 +557,65 @@ private static function get_process_env_variables(): array {
return $env;
}

/**
* Get the environment variables that make the PHP processes launched by the tests cheaper.
*
* Xdebug is switched off unless code coverage is collected or a mode or session is requested
* explicitly. The code of WP-CLI and of the project under test is kept in a file-based opcode
* cache shared by all processes. Everything below the temporary directory is excluded from that
* cache, as scenarios create and rewrite PHP files there and must always run the current version.
*
* Set WP_CLI_TEST_PHP_TUNING=off to launch the processes with the PHP configuration as it is.
*
* @return array<string, string>
*/
private static function get_php_tuning_env_variables(): array {
if ( 'off' === getenv( 'WP_CLI_TEST_PHP_TUNING' ) || self::running_with_code_coverage() ) {
return [];
}

$env = [];

if ( false === getenv( 'XDEBUG_MODE' ) && false === getenv( 'XDEBUG_SESSION' ) && false === getenv( 'XDEBUG_CONFIG' ) ) {
$env['XDEBUG_MODE'] = 'off';
}

// The file cache cannot be used on its own on Windows.
if ( Utils\is_windows() ) {
return $env;
}

$temp_dir = rtrim( sys_get_temp_dir(), '/' );
$config_dir = "{$temp_dir}/wp-cli-test-php-config";
$cache_dir = "{$temp_dir}/wp-cli-test-opcache";

foreach ( [ $config_dir, $cache_dir ] as $dir ) {
if ( ! is_dir( $dir ) && ! @mkdir( $dir, 0777, true ) && ! is_dir( $dir ) ) {
return $env;
}
}

$excluded = array_unique( array_filter( [ $temp_dir, realpath( $temp_dir ) ] ) );
file_put_contents( "{$config_dir}/opcache-blacklist.txt", implode( "/\n", $excluded ) . "/\n" );

$ini = [
'opcache.enable_cli=1',
"opcache.file_cache={$cache_dir}",
'opcache.file_cache_only=1',
"opcache.blacklist_filename={$config_dir}/opcache-blacklist.txt",
];
file_put_contents( "{$config_dir}/wp-cli-tests.ini", implode( "\n", $ini ) . "\n" );

// Add to the directories PHP scans for configuration files rather than replacing them.
$scan_dirs = getenv( 'PHP_INI_SCAN_DIR' );
if ( false === $scan_dirs ) {
$scan_dirs = PHP_CONFIG_FILE_SCAN_DIR;
}
$env['PHP_INI_SCAN_DIR'] = ( '' !== $scan_dirs ? $scan_dirs . PATH_SEPARATOR : '' ) . $config_dir;

return $env;
}

/**
* Get the internal variables to use within tests.
*
Expand Down
Loading