Skip to content

Commit 2df5ab0

Browse files
authored
Fix autoload preload on PHPUnit 12+ on global installation (#7468)
1 parent 8b44b1d commit 2df5ab0

File tree

4 files changed

+11
-26
lines changed

4 files changed

+11
-26
lines changed

build/target-repository/bootstrap.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,6 @@
22

33
declare(strict_types=1);
44

5-
use PHPParser\Node;
6-
use PHPUnit\Runner\Version;
7-
8-
/**
9-
* The preload.php contains 2 dependencies
10-
* - phpstan/phpdoc-parser
11-
* - nikic/php-parser
12-
*
13-
* They need to be loaded early to avoid conflict version between rector prefixed vendor and Project vendor
14-
* For example, a project may use phpstan/phpdoc-parser v1, while rector uses phpstan/phpdoc-parser uses v2, that will error as class or logic are different.
15-
*/
16-
if (
17-
// verify PHPUnit is running
18-
defined('PHPUNIT_COMPOSER_INSTALL')
19-
20-
// no need to preload if Node interface exists
21-
&& ! interface_exists(Node::class, false)
22-
23-
// ensure force autoload version with class_exists() with true argument as not yet loaded
24-
// for phpunit 12+ only
25-
&& class_exists(Version::class, true) && (int) Version::id() >= 12
26-
) {
27-
require_once __DIR__ . '/preload.php';
28-
}
29-
305
// inspired by https://github.com/phpstan/phpstan/blob/master/bootstrap.php
316
spl_autoload_register(function (string $class): void {
327
static $composerAutoloader;

phpstan.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ parameters:
154154
# for config class reflection
155155
- src/Bootstrap/ExtensionConfigResolver.php
156156
- src/Validation/RectorConfigValidator.php
157+
# for phpunit version check
158+
- src/Testing/PHPUnit/AbstractLazyTestCase.php
157159

158160
# use of internal phpstan classes
159161
-

scoper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
'exclude-classes' => [
3434
'PHPUnit\Framework\Constraint\IsEqual',
3535
'PHPUnit\Framework\TestCase',
36+
'PHPUnit\Runner\Version',
3637
'PHPUnit\Framework\ExpectationFailedException',
3738

3839
// native class on php 8.3+

src/Testing/PHPUnit/AbstractLazyTestCase.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Rector\Testing\PHPUnit;
66

77
use PHPUnit\Framework\TestCase;
8+
use PHPUnit\Runner\Version;
89
use Rector\Config\RectorConfig;
910
use Rector\DependencyInjection\LazyContainerFactory;
1011

@@ -63,7 +64,13 @@ private function includePreloadFilesAndScoperAutoload(): void
6364
{
6465
if (file_exists(__DIR__ . '/../../../preload.php')) {
6566
if (file_exists(__DIR__ . '/../../../vendor')) {
66-
require_once __DIR__ . '/../../../preload.php';
67+
/**
68+
* On PHPUnit 12+, when classmap autoloaded, it means preload already loaded early
69+
*/
70+
if (! class_exists(Version::class, true) || (int) Version::id() < 12) {
71+
require_once __DIR__ . '/../../../preload.php';
72+
}
73+
6774
// test case in rector split package
6875
} elseif (file_exists(__DIR__ . '/../../../../../../vendor')) {
6976
require_once __DIR__ . '/../../../preload-split-package.php';

0 commit comments

Comments
 (0)