Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Testing/PHPStanTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Testing;

use Override;
use PHPStan\Analyser\ConstantResolver;
use PHPStan\Analyser\DirectInternalScopeFactoryFactory;
use PHPStan\Analyser\Error;
Expand Down Expand Up @@ -40,6 +41,24 @@ abstract class PHPStanTestCase extends TestCase

use PHPStanTestCaseTrait;

/**
* Re-register the runtime container as the global static reflection provider before
* every test. Enable this in tests that construct Type objects directly and assert
* PHP-version-dependent reflection results, so a foreign PhpVersion leaked by another
* test can't flake them. See https://github.com/phpstan/phpstan/issues/14860
*/
protected bool $reinitializeContainerBeforeEachTest = false;

#[Override]
protected function setUp(): void
{
if (!$this->reinitializeContainerBeforeEachTest) {
return;
}

self::getContainer();
}

public static function getParser(): Parser
{
/** @var Parser $parser */
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/AnalyserTraitsIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class AnalyserTraitsIntegrationTest extends PHPStanTestCase
#[Override]
protected function setUp(): void
{
parent::setUp();
$this->fileHelper = self::getContainer()->getByType(FileHelper::class);
}

Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/TypeSpecifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class TypeSpecifierTest extends PHPStanTestCase
#[Override]
protected function setUp(): void
{
parent::setUp();
$reflectionProvider = self::createReflectionProvider();
$this->printer = new Printer();
$this->typeSpecifier = self::getContainer()->getService('typeSpecifier');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class RawErrorFormatterTest extends ErrorFormatterTestCase
#[Override]
protected function setUp(): void
{
parent::setUp();
foreach (AgentDetector::ENV_VARS as $var) {
$this->originalEnvVars[$var] = getenv($var);
putenv($var);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class TableErrorFormatterTest extends ErrorFormatterTestCase
#[Override]
protected function setUp(): void
{
parent::setUp();
putenv('GITHUB_ACTIONS');

$this->terminalEmulator = getenv('TERMINAL_EMULATOR');
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/PhpDoc/DefaultStubFilesProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class DefaultStubFilesProviderTest extends PHPStanTestCase
#[Override]
protected function setUp(): void
{
parent::setUp();
$this->currentWorkingDirectory = $this->getContainer()->getParameter('currentWorkingDirectory');
}

Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Rules/Functions/PrintfHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class PrintfHelperTest extends PHPStanTestCase
#[Override]
protected function setUp(): void
{
parent::setUp();
$this->printf = new PrintfHelper(new PhpVersion(PHP_VERSION_ID));
}

Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Type/Accessory/HasPropertyTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
class HasPropertyTypeTest extends PHPStanTestCase
{

// Pin the runtime container so a foreign PhpVersion leaked by another test
// can't flake the version-dependent Closure data set below.
// See https://github.com/phpstan/phpstan/issues/14860
protected bool $reinitializeContainerBeforeEachTest = true;

public static function dataIsSuperTypeOf(): array
{
return [
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Type/Generic/GenericObjectTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
class GenericObjectTypeTest extends PHPStanTestCase
{

// Pin the runtime container so a foreign PhpVersion leaked by another test
// can't flake the version-dependent data sets (reflected variance of built-in
// generics). See https://github.com/phpstan/phpstan/issues/14860
protected bool $reinitializeContainerBeforeEachTest = true;

public static function dataIsSuperTypeOf(): array
{
return [
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Type/ObjectTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
class ObjectTypeTest extends PHPStanTestCase
{

// Pin the runtime container so a foreign PhpVersion leaked by another test
// can't flake the version-dependent Closure data sets (dynamic-property
// handling). See https://github.com/phpstan/phpstan/issues/14860
protected bool $reinitializeContainerBeforeEachTest = true;

public static function dataIsIterable(): array
{
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class GmpOperatorTypeSpecifyingExtensionTest extends PHPStanTestCase
#[Override]
protected function setUp(): void
{
parent::setUp();
$this->extension = new GmpOperatorTypeSpecifyingExtension();
}

Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Type/TypeCombinatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
class TypeCombinatorTest extends PHPStanTestCase
{

// Pin the runtime container so a foreign PhpVersion leaked by another test
// can't flake the version-dependent data sets (dynamic-property handling of
// final classes). See https://github.com/phpstan/phpstan/issues/14860
protected bool $reinitializeContainerBeforeEachTest = true;

public static function dataAddNull(): array
{
return [
Expand Down
Loading