@@ -508,7 +508,12 @@ private static function get_process_env_variables(): array {
508508 $ env ['WP_CLI_REQUIRE ' ] = $ updated ;
509509 }
510510
511- $ env = array_merge ( $ env , self ::get_php_tuning_env_variables () );
511+ $ env = array_filter (
512+ array_merge ( $ env , self ::get_php_tuning_env_variables () ),
513+ static function ( $ value ) {
514+ return null !== $ value ;
515+ }
516+ );
512517
513518 $ config_path = getenv ( 'WP_CLI_CONFIG_PATH ' );
514519 if ( false !== $ config_path ) {
@@ -569,7 +574,7 @@ private static function get_process_env_variables(): array {
569574 *
570575 * Set WP_CLI_TEST_PHP_TUNING=off to launch the processes with the PHP configuration as it is.
571576 *
572- * @return array<string, string>
577+ * @return array<string, string|null> Null for variables to leave out.
573578 */
574579 private static function get_php_tuning_env_variables (): array {
575580 if ( 'off ' === getenv ( 'WP_CLI_TEST_PHP_TUNING ' ) || self ::running_with_code_coverage () ) {
@@ -587,9 +592,13 @@ private static function get_php_tuning_env_variables(): array {
587592 return $ env ;
588593 }
589594
595+ // The directories are named after the settings, so that a cache written with other settings,
596+ // such as by an older version of this package, is never used, nor its configuration file.
597+ $ settings = [ 'opcache.enable_cli=1 ' , 'opcache.file_cache_only=1 ' , 'opcache.optimization_level=0 ' ];
598+ $ key = substr ( md5 ( implode ( "\n" , $ settings ) ), 0 , 8 );
590599 $ temp_dir = rtrim ( sys_get_temp_dir (), '/ ' );
591- $ config_dir = "{$ temp_dir }/wp-cli-test-php-config " ;
592- $ cache_dir = "{$ temp_dir }/wp-cli-test-opcache " ;
600+ $ config_dir = "{$ temp_dir }/wp-cli-test-php-config- { $ key } " ;
601+ $ cache_dir = "{$ temp_dir }/wp-cli-test-opcache- { $ key } " ;
593602
594603 foreach ( [ $ config_dir , $ cache_dir ] as $ dir ) {
595604 if ( ! is_dir ( $ dir ) && ! @mkdir ( $ dir , 0777 , true ) && ! is_dir ( $ dir ) ) {
@@ -602,25 +611,27 @@ private static function get_php_tuning_env_variables(): array {
602611
603612 // The optimizer bakes in what it finds at compile time, such as whether a function exists,
604613 // which differs between processes with other settings, such as disabled functions.
605- $ ini = [
606- ' opcache.enable_cli=1 ' ,
607- " opcache.file_cache= { $ cache_dir }" ,
608- ' opcache.file_cache_only=1 ' ,
609- ' opcache.optimization_level=0 ' ,
610- " opcache.blacklist_filename= { $ config_dir } /opcache-blacklist.txt " ,
611- ] ;
614+ $ ini = array_merge (
615+ $ settings ,
616+ [
617+ " opcache.file_cache= { $ cache_dir }" ,
618+ " opcache.blacklist_filename= { $ config_dir } /opcache-blacklist.txt " ,
619+ ]
620+ ) ;
612621 file_put_contents ( "{$ config_dir }/wp-cli-tests.ini " , implode ( "\n" , $ ini ) . "\n" );
613622
614623 // Add to the directories PHP scans for configuration files rather than replacing them.
615624 $ scan_dirs = getenv ( 'PHP_INI_SCAN_DIR ' );
616625
617626 // Behat and Composer restart without Xdebug through composer/xdebug-handler, which clears
618627 // the directories, and keeps the original ones in its settings, with "*" if there were none.
628+ // The configuration file it points PHPRC to holds the scanned files already, so restore that too.
619629 $ restart_settings = getenv ( 'XDEBUG_HANDLER_SETTINGS ' );
620630 if ( '' === $ scan_dirs && is_string ( $ restart_settings ) ) {
621- $ settings = explode ( '| ' , $ restart_settings );
622- if ( isset ( $ settings [2 ] ) ) {
623- $ scan_dirs = '* ' === $ settings [2 ] ? false : $ settings [2 ];
631+ $ restart = explode ( '| ' , $ restart_settings );
632+ if ( isset ( $ restart [2 ], $ restart [3 ] ) ) {
633+ $ scan_dirs = '* ' === $ restart [2 ] ? false : $ restart [2 ];
634+ $ env ['PHPRC ' ] = '* ' === $ restart [3 ] ? null : $ restart [3 ];
624635 }
625636 }
626637
0 commit comments