diff --git a/scoper.php b/scoper.php index 73d00ecf873..e8893e4b34f 100644 --- a/scoper.php +++ b/scoper.php @@ -33,6 +33,7 @@ 'exclude-classes' => [ 'PHPUnit\Framework\Constraint\IsEqual', 'PHPUnit\Framework\TestCase', + 'PHPUnit\Runner\Version', 'PHPUnit\Framework\ExpectationFailedException', // native class on php 8.3+ diff --git a/src/Testing/PHPUnit/AbstractLazyTestCase.php b/src/Testing/PHPUnit/AbstractLazyTestCase.php index 720990bc0ac..f4236153043 100644 --- a/src/Testing/PHPUnit/AbstractLazyTestCase.php +++ b/src/Testing/PHPUnit/AbstractLazyTestCase.php @@ -5,6 +5,7 @@ namespace Rector\Testing\PHPUnit; use PHPUnit\Framework\TestCase; +use PHPUnit\Runner\Version; use Rector\Config\RectorConfig; use Rector\DependencyInjection\LazyContainerFactory; @@ -63,7 +64,12 @@ private function includePreloadFilesAndScoperAutoload(): void { if (file_exists(__DIR__ . '/../../../preload.php')) { if (file_exists(__DIR__ . '/../../../vendor')) { - require_once __DIR__ . '/../../../preload.php'; + /** + * On PHPUnit 12+, when classmap autoloaded, it means preload already loaded early + */ + if (! class_exists(Version::class, true) || (int) Version::id() < 12) { + require_once __DIR__ . '/../../../preload.php'; + } // test case in rector split package } elseif (file_exists(__DIR__ . '/../../../../../../vendor')) { require_once __DIR__ . '/../../../preload-split-package.php';