@@ -508,6 +508,8 @@ 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 () );
512+
511513 $ config_path = getenv ( 'WP_CLI_CONFIG_PATH ' );
512514 if ( false !== $ config_path ) {
513515 $ env ['WP_CLI_CONFIG_PATH ' ] = $ config_path ;
@@ -557,6 +559,65 @@ private static function get_process_env_variables(): array {
557559 return $ env ;
558560 }
559561
562+ /**
563+ * Get the environment variables that make the PHP processes launched by the tests cheaper.
564+ *
565+ * Xdebug is switched off unless code coverage is collected or a mode or session is requested
566+ * explicitly. The code of WP-CLI and of the project under test is kept in a file-based opcode
567+ * cache shared by all processes. Everything below the temporary directory is excluded from that
568+ * cache, as scenarios create and rewrite PHP files there and must always run the current version.
569+ *
570+ * Set WP_CLI_TEST_PHP_TUNING=off to launch the processes with the PHP configuration as it is.
571+ *
572+ * @return array<string, string>
573+ */
574+ private static function get_php_tuning_env_variables (): array {
575+ if ( 'off ' === getenv ( 'WP_CLI_TEST_PHP_TUNING ' ) || self ::running_with_code_coverage () ) {
576+ return [];
577+ }
578+
579+ $ env = [];
580+
581+ if ( false === getenv ( 'XDEBUG_MODE ' ) && false === getenv ( 'XDEBUG_SESSION ' ) && false === getenv ( 'XDEBUG_CONFIG ' ) ) {
582+ $ env ['XDEBUG_MODE ' ] = 'off ' ;
583+ }
584+
585+ // The file cache cannot be used on its own on Windows.
586+ if ( Utils \is_windows () ) {
587+ return $ env ;
588+ }
589+
590+ $ 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 " ;
593+
594+ foreach ( [ $ config_dir , $ cache_dir ] as $ dir ) {
595+ if ( ! is_dir ( $ dir ) && ! @mkdir ( $ dir , 0777 , true ) && ! is_dir ( $ dir ) ) {
596+ return $ env ;
597+ }
598+ }
599+
600+ $ excluded = array_unique ( array_filter ( [ $ temp_dir , realpath ( $ temp_dir ) ] ) );
601+ file_put_contents ( "{$ config_dir }/opcache-blacklist.txt " , implode ( "/ \n" , $ excluded ) . "/ \n" );
602+
603+ $ ini = [
604+ 'opcache.enable_cli=1 ' ,
605+ "opcache.file_cache= {$ cache_dir }" ,
606+ 'opcache.file_cache_only=1 ' ,
607+ "opcache.blacklist_filename= {$ config_dir }/opcache-blacklist.txt " ,
608+ ];
609+ file_put_contents ( "{$ config_dir }/wp-cli-tests.ini " , implode ( "\n" , $ ini ) . "\n" );
610+
611+ // Add to the directories PHP scans for configuration files rather than replacing them.
612+ $ scan_dirs = getenv ( 'PHP_INI_SCAN_DIR ' );
613+ if ( false === $ scan_dirs ) {
614+ $ scan_dirs = PHP_CONFIG_FILE_SCAN_DIR ;
615+ }
616+ $ env ['PHP_INI_SCAN_DIR ' ] = ( '' !== $ scan_dirs ? $ scan_dirs . PATH_SEPARATOR : '' ) . $ config_dir ;
617+
618+ return $ env ;
619+ }
620+
560621 /**
561622 * Get the internal variables to use within tests.
562623 *
0 commit comments