@@ -270,12 +270,21 @@ public static function forget_feature( AfterFeatureScope $scope ): void {
270270 }
271271
272272 /**
273- * @AfterSuite
273+ * Whether tests are currently running with code coverage collection.
274+ *
275+ * @return bool
274276 */
275- public static function merge_coverage_reports (): void {
277+ private static function running_with_code_coverage () {
276278 $ with_code_coverage = (string ) getenv ( 'WP_CLI_TEST_COVERAGE ' );
277279
278- if ( ! \in_array ( $ with_code_coverage , [ 'true ' , '1 ' ], true ) ) {
280+ return \in_array ( $ with_code_coverage , [ 'true ' , '1 ' ], true );
281+ }
282+
283+ /**
284+ * @AfterSuite
285+ */
286+ public static function merge_coverage_reports (): void {
287+ if ( ! self ::running_with_code_coverage () ) {
279288 return ;
280289 }
281290
@@ -437,9 +446,7 @@ private static function get_process_env_variables(): array {
437446 'TEST_RUN_DIR ' => self ::$ behat_run_dir ,
438447 ];
439448
440- $ with_code_coverage = (string ) getenv ( 'WP_CLI_TEST_COVERAGE ' );
441-
442- if ( \in_array ( $ with_code_coverage , [ 'true ' , '1 ' ], true ) ) {
449+ if ( self ::running_with_code_coverage () ) {
443450 $ has_coverage_driver = ( new Runtime () )->hasXdebug () || ( new Runtime () )->hasPCOV ();
444451
445452 if ( ! $ has_coverage_driver ) {
@@ -1023,14 +1030,27 @@ public function create_run_dir(): void {
10231030 public function build_phar ( $ version = 'same ' ): void {
10241031 $ this ->variables ['PHAR_PATH ' ] = $ this ->variables ['RUN_DIR ' ] . '/ ' . uniqid ( 'wp-cli-build- ' , true ) . '.phar ' ;
10251032
1033+ $ is_bundle = false ;
1034+
10261035 // Test running against a package installed as a WP-CLI dependency
10271036 // WP-CLI bundle installed as a project dependency
10281037 $ make_phar_path = self ::get_vendor_dir () . '/wp-cli/wp-cli-bundle/utils/make-phar.php ' ;
10291038 if ( ! file_exists ( $ make_phar_path ) ) {
10301039 // Running against WP-CLI bundle proper
1040+ $ is_bundle = true ;
1041+
10311042 $ make_phar_path = self ::get_vendor_dir () . '/../utils/make-phar.php ' ;
10321043 }
10331044
1045+ // Temporarily modify the Composer autoloader used within the Phar
1046+ // so that it doesn't clash if autoloading is already happening outside of it,
1047+ // for example when generating code coverage.
1048+ // This modifies composer.json.
1049+ if ( $ is_bundle && self ::running_with_code_coverage () ) {
1050+ $ this ->composer_command ( 'config autoloader-suffix "WpCliTestsPhar" --working-dir= ' . dirname ( self ::get_vendor_dir () ) );
1051+ $ this ->composer_command ( 'dump-autoload --working-dir= ' . dirname ( self ::get_vendor_dir () ) );
1052+ }
1053+
10341054 $ this ->proc (
10351055 Utils \esc_cmd (
10361056 'php -dphar.readonly=0 %1$s %2$s --version=%3$s && chmod +x %2$s ' ,
@@ -1039,6 +1059,12 @@ public function build_phar( $version = 'same' ): void {
10391059 $ version
10401060 )
10411061 )->run_check ();
1062+
1063+ // Revert the suffix change again
1064+ if ( $ is_bundle && self ::running_with_code_coverage () ) {
1065+ $ this ->composer_command ( 'config autoloader-suffix "WpCliBundle" --working-dir= ' . dirname ( self ::get_vendor_dir () ) );
1066+ $ this ->composer_command ( 'dump-autoload --working-dir= ' . dirname ( self ::get_vendor_dir () ) );
1067+ }
10421068 }
10431069
10441070 /**
0 commit comments