Skip to content

Commit 5bcff71

Browse files
phpstan-botstaabmclaude
authored
Re-register the DI container in setUp() of tests with PHP-version-dependent reflection assertions - fixes racy/flaky tests (#5917)
Co-authored-by: staabm <120441+staabm@users.noreply.github.com> Co-authored-by: Markus Staab <maggus.staab@googlemail.com> Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent ef28940 commit 5bcff71

12 files changed

Lines changed: 46 additions & 0 deletions

src/Testing/PHPStanTestCase.php

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

33
namespace PHPStan\Testing;
44

5+
use Override;
56
use PHPStan\Analyser\ConstantResolver;
67
use PHPStan\Analyser\DirectInternalScopeFactoryFactory;
78
use PHPStan\Analyser\Error;
@@ -40,6 +41,24 @@ abstract class PHPStanTestCase extends TestCase
4041

4142
use PHPStanTestCaseTrait;
4243

44+
/**
45+
* Re-register the runtime container as the global static reflection provider before
46+
* every test. Enable this in tests that construct Type objects directly and assert
47+
* PHP-version-dependent reflection results, so a foreign PhpVersion leaked by another
48+
* test can't flake them. See https://github.com/phpstan/phpstan/issues/14860
49+
*/
50+
protected bool $reinitializeContainerBeforeEachTest = false;
51+
52+
#[Override]
53+
protected function setUp(): void
54+
{
55+
if (!$this->reinitializeContainerBeforeEachTest) {
56+
return;
57+
}
58+
59+
self::getContainer();
60+
}
61+
4362
public static function getParser(): Parser
4463
{
4564
/** @var Parser $parser */

tests/PHPStan/Analyser/AnalyserTraitsIntegrationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class AnalyserTraitsIntegrationTest extends PHPStanTestCase
2222
#[Override]
2323
protected function setUp(): void
2424
{
25+
parent::setUp();
2526
$this->fileHelper = self::getContainer()->getByType(FileHelper::class);
2627
}
2728

tests/PHPStan/Analyser/TypeSpecifierTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class TypeSpecifierTest extends PHPStanTestCase
6161
#[Override]
6262
protected function setUp(): void
6363
{
64+
parent::setUp();
6465
$reflectionProvider = self::createReflectionProvider();
6566
$this->printer = new Printer();
6667
$this->typeSpecifier = self::getContainer()->getService('typeSpecifier');

tests/PHPStan/Command/ErrorFormatter/RawErrorFormatterTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class RawErrorFormatterTest extends ErrorFormatterTestCase
1919
#[Override]
2020
protected function setUp(): void
2121
{
22+
parent::setUp();
2223
foreach (AgentDetector::ENV_VARS as $var) {
2324
$this->originalEnvVars[$var] = getenv($var);
2425
putenv($var);

tests/PHPStan/Command/ErrorFormatter/TableErrorFormatterTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class TableErrorFormatterTest extends ErrorFormatterTestCase
2323
#[Override]
2424
protected function setUp(): void
2525
{
26+
parent::setUp();
2627
putenv('GITHUB_ACTIONS');
2728

2829
$this->terminalEmulator = getenv('TERMINAL_EMULATOR');

tests/PHPStan/PhpDoc/DefaultStubFilesProviderTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class DefaultStubFilesProviderTest extends PHPStanTestCase
1717
#[Override]
1818
protected function setUp(): void
1919
{
20+
parent::setUp();
2021
$this->currentWorkingDirectory = $this->getContainer()->getParameter('currentWorkingDirectory');
2122
}
2223

tests/PHPStan/Rules/Functions/PrintfHelperTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class PrintfHelperTest extends PHPStanTestCase
1515
#[Override]
1616
protected function setUp(): void
1717
{
18+
parent::setUp();
1819
$this->printf = new PrintfHelper(new PhpVersion(PHP_VERSION_ID));
1920
}
2021

tests/PHPStan/Type/Accessory/HasPropertyTypeTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
class HasPropertyTypeTest extends PHPStanTestCase
2424
{
2525

26+
// Pin the runtime container so a foreign PhpVersion leaked by another test
27+
// can't flake the version-dependent Closure data set below.
28+
// See https://github.com/phpstan/phpstan/issues/14860
29+
protected bool $reinitializeContainerBeforeEachTest = true;
30+
2631
public static function dataIsSuperTypeOf(): array
2732
{
2833
return [

tests/PHPStan/Type/Generic/GenericObjectTypeTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
class GenericObjectTypeTest extends PHPStanTestCase
3333
{
3434

35+
// Pin the runtime container so a foreign PhpVersion leaked by another test
36+
// can't flake the version-dependent data sets (reflected variance of built-in
37+
// generics). See https://github.com/phpstan/phpstan/issues/14860
38+
protected bool $reinitializeContainerBeforeEachTest = true;
39+
3540
public static function dataIsSuperTypeOf(): array
3641
{
3742
return [

tests/PHPStan/Type/ObjectTypeTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
class ObjectTypeTest extends PHPStanTestCase
5252
{
5353

54+
// Pin the runtime container so a foreign PhpVersion leaked by another test
55+
// can't flake the version-dependent Closure data sets (dynamic-property
56+
// handling). See https://github.com/phpstan/phpstan/issues/14860
57+
protected bool $reinitializeContainerBeforeEachTest = true;
58+
5459
public static function dataIsIterable(): array
5560
{
5661
return [

0 commit comments

Comments
 (0)