From 9f20e615cdc9f12e024acd19a844035f8424e54d Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 18 Sep 2025 09:45:03 +0200 Subject: [PATCH 1/8] Init container before DataProvider is called --- tests/bootstrap.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index f88bfe4c9d..77b1e18e5b 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,5 +1,10 @@ registerSubscriber(new InitContainerBeforeDataProvider()); From d929ac4d9691d382d902818981f657ac855687b9 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 18 Sep 2025 09:49:18 +0200 Subject: [PATCH 2/8] invoke from within the test --- src/Testing/PHPStanTestCase.php | 7 ------- tests/bootstrap.php | 8 +++++++- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/Testing/PHPStanTestCase.php b/src/Testing/PHPStanTestCase.php index 7cf7d673b6..d294f81d24 100644 --- a/src/Testing/PHPStanTestCase.php +++ b/src/Testing/PHPStanTestCase.php @@ -2,7 +2,6 @@ namespace PHPStan\Testing; -use Override; use PHPStan\Analyser\ConstantResolver; use PHPStan\Analyser\DirectInternalScopeFactory; use PHPStan\Analyser\Error; @@ -97,12 +96,6 @@ public static function getContainer(): Container return self::$containers[$cacheKey]; } - #[Override] - public static function setUpBeforeClass(): void - { - self::getContainer(); - } - /** * @return string[] */ diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 77b1e18e5b..d9d6d2fa43 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -30,7 +30,13 @@ class InitContainerBeforeDataProvider implements DataProviderMethodCalledSubscri public function notify(DataProviderMethodCalled $event): void { - PHPStanTestCase::getContainer(); + $testClassName = $event->testMethod()->className(); + + if (!is_a($testClassName, PhpStanTestCase::class, true)) { + return; + } + + $testClassName::getContainer(); } } From 3d7c9d28b2e99036b6fb4a3f881e28e6ab37e972 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 18 Sep 2025 10:05:58 +0200 Subject: [PATCH 3/8] try to be phpunit 9.x compatible --- phpunit.xml | 3 +++ ...tContainerBeforeDataProviderSubscriber.php | 24 +++++++++++++++++ src/Testing/PHPUnit/PHPUnitExtension.php | 26 +++++++++++++++++++ tests/bootstrap.php | 24 ----------------- 4 files changed, 53 insertions(+), 24 deletions(-) create mode 100644 src/Testing/PHPUnit/InitContainerBeforeDataProviderSubscriber.php create mode 100644 src/Testing/PHPUnit/PHPUnitExtension.php diff --git a/phpunit.xml b/phpunit.xml index 062728282f..e8d9031978 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,5 +1,8 @@ + + + tests/PHPStan diff --git a/src/Testing/PHPUnit/InitContainerBeforeDataProviderSubscriber.php b/src/Testing/PHPUnit/InitContainerBeforeDataProviderSubscriber.php new file mode 100644 index 0000000000..c796e9cfdd --- /dev/null +++ b/src/Testing/PHPUnit/InitContainerBeforeDataProviderSubscriber.php @@ -0,0 +1,24 @@ +testMethod()->className(); + + if (!is_a($testClassName, PhpStanTestCase::class, true)) { + return; + } + + $testClassName::getContainer(); + } + +} diff --git a/src/Testing/PHPUnit/PHPUnitExtension.php b/src/Testing/PHPUnit/PHPUnitExtension.php new file mode 100644 index 0000000000..efb4a00e61 --- /dev/null +++ b/src/Testing/PHPUnit/PHPUnitExtension.php @@ -0,0 +1,26 @@ +registerSubscriber( + new InitContainerBeforeDataProviderSubscriber(), + ); + } + +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index d9d6d2fa43..f88bfe4c9d 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,10 +1,5 @@ testMethod()->className(); - - if (!is_a($testClassName, PhpStanTestCase::class, true)) { - return; - } - - $testClassName::getContainer(); - } - -} - - -Facade::instance()->registerSubscriber(new InitContainerBeforeDataProvider()); From fb049822194f6d99e26521608361ac673dc68d3b Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 18 Sep 2025 10:07:55 +0200 Subject: [PATCH 4/8] Update InitContainerBeforeDataProviderSubscriber.php --- .../PHPUnit/InitContainerBeforeDataProviderSubscriber.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Testing/PHPUnit/InitContainerBeforeDataProviderSubscriber.php b/src/Testing/PHPUnit/InitContainerBeforeDataProviderSubscriber.php index c796e9cfdd..525984fb4c 100644 --- a/src/Testing/PHPUnit/InitContainerBeforeDataProviderSubscriber.php +++ b/src/Testing/PHPUnit/InitContainerBeforeDataProviderSubscriber.php @@ -2,14 +2,16 @@ namespace PHPStan\Testing\PHPUnit; +use Override; use PHPStan\Testing\PHPStanTestCase; use PHPUnit\Event\Test\DataProviderMethodCalled; use PHPUnit\Event\Test\DataProviderMethodCalledSubscriber; use function is_a; -class InitContainerBeforeDataProviderSubscriber implements DataProviderMethodCalledSubscriber +final class InitContainerBeforeDataProviderSubscriber implements DataProviderMethodCalledSubscriber { + #[Override] public function notify(DataProviderMethodCalled $event): void { $testClassName = $event->testMethod()->className(); From 6bd06e1801aabe007757eb8b89a105867e7f7304 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 18 Sep 2025 10:12:41 +0200 Subject: [PATCH 5/8] Update phpunit.xml --- phpunit.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit.xml b/phpunit.xml index e8d9031978..405b4e4113 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,7 +1,7 @@ - + From f4ce2bfdd4ff4eaf3bfda17fb43a44750595468c Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 18 Sep 2025 10:55:38 +0200 Subject: [PATCH 6/8] Update phpstan.neon --- build/phpstan.neon | 1 + 1 file changed, 1 insertion(+) diff --git a/build/phpstan.neon b/build/phpstan.neon index 017fd4f8fa..9fb0fa3708 100644 --- a/build/phpstan.neon +++ b/build/phpstan.neon @@ -36,6 +36,7 @@ parameters: - ../tests/PHPStan/Rules/Properties/UninitializedPropertyRuleTest.php - ../tests/PHPStan/Command/IgnoredRegexValidatorTest.php - ../src/Command/IgnoredRegexValidator.php + - ../src/Testing/PHPUnit/* exceptions: uncheckedExceptionClasses: - 'PHPStan\ShouldNotHappenException' From 0f1d4da8cb934c15b90de8cae83d67087cb925b9 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 18 Sep 2025 11:26:34 +0200 Subject: [PATCH 7/8] fix --- phpunit.xml | 2 +- .../{PHPUnitExtension.php => PHPStanPHPUnitExtension.php} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/Testing/PHPUnit/{PHPUnitExtension.php => PHPStanPHPUnitExtension.php} (89%) diff --git a/phpunit.xml b/phpunit.xml index 405b4e4113..59224af805 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,7 +1,7 @@ - + diff --git a/src/Testing/PHPUnit/PHPUnitExtension.php b/src/Testing/PHPUnit/PHPStanPHPUnitExtension.php similarity index 89% rename from src/Testing/PHPUnit/PHPUnitExtension.php rename to src/Testing/PHPUnit/PHPStanPHPUnitExtension.php index efb4a00e61..ff52d637b9 100644 --- a/src/Testing/PHPUnit/PHPUnitExtension.php +++ b/src/Testing/PHPUnit/PHPStanPHPUnitExtension.php @@ -8,7 +8,7 @@ use PHPUnit\Runner\Extension\ParameterCollection; use PHPUnit\TextUI\Configuration\Configuration; -final class PHPUnitExtension implements Extension +final class PHPStanPHPUnitExtension implements Extension { #[Override] From 8f1d82de7337a3931a7a994a70bf168e781fe972 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 18 Sep 2025 11:33:28 +0200 Subject: [PATCH 8/8] move exclude into old-phpunit.neon --- build/old-phpunit.neon | 3 +++ build/phpstan.neon | 1 - 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/build/old-phpunit.neon b/build/old-phpunit.neon index 634de52560..52e9beb14d 100644 --- a/build/old-phpunit.neon +++ b/build/old-phpunit.neon @@ -1,4 +1,7 @@ parameters: + excludePaths: + - ../src/Testing/PHPUnit/* + ignoreErrors: - message: '#^Instanceof references internal interface PHPUnit\\Exception\.$#' diff --git a/build/phpstan.neon b/build/phpstan.neon index 9fb0fa3708..017fd4f8fa 100644 --- a/build/phpstan.neon +++ b/build/phpstan.neon @@ -36,7 +36,6 @@ parameters: - ../tests/PHPStan/Rules/Properties/UninitializedPropertyRuleTest.php - ../tests/PHPStan/Command/IgnoredRegexValidatorTest.php - ../src/Command/IgnoredRegexValidator.php - - ../src/Testing/PHPUnit/* exceptions: uncheckedExceptionClasses: - 'PHPStan\ShouldNotHappenException'