@@ -271,12 +271,21 @@ public static function forget_feature( AfterFeatureScope $scope ): void {
271271 }
272272
273273 /**
274- * @AfterSuite
274+ * Whether tests are currently running with code coverage collection.
275+ *
276+ * @return bool
275277 */
276- public static function merge_coverage_reports (): void {
278+ private static function running_with_code_coverage () {
277279 $ with_code_coverage = (string ) getenv ( 'WP_CLI_TEST_COVERAGE ' );
278280
279- if ( ! \in_array ( $ with_code_coverage , [ 'true ' , '1 ' ], true ) ) {
281+ return \in_array ( $ with_code_coverage , [ 'true ' , '1 ' ], true );
282+ }
283+
284+ /**
285+ * @AfterSuite
286+ */
287+ public static function merge_coverage_reports (): void {
288+ if ( ! self ::running_with_code_coverage () ) {
280289 return ;
281290 }
282291
@@ -438,9 +447,7 @@ private static function get_process_env_variables(): array {
438447 'TEST_RUN_DIR ' => self ::$ behat_run_dir ,
439448 ];
440449
441- $ with_code_coverage = (string ) getenv ( 'WP_CLI_TEST_COVERAGE ' );
442-
443- if ( \in_array ( $ with_code_coverage , [ 'true ' , '1 ' ], true ) ) {
450+ if ( self ::running_with_code_coverage () ) {
444451 $ has_coverage_driver = ( new Runtime () )->hasXdebug () || ( new Runtime () )->hasPCOV ();
445452
446453 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