Skip to content

Commit 3e70199

Browse files
authored
[Testing] Reset source locator after each Rector test (#8513)
1 parent bb8cdb5 commit 3e70199

5 files changed

Lines changed: 68 additions & 0 deletions

File tree

‎src/Testing/PHPUnit/AbstractRectorTestCase.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ protected function tearDown(): void
134134
if (is_string($this->inputFilePath)) {
135135
FileSystem::delete($this->inputFilePath);
136136
}
137+
138+
// a next test that does not set its own paths would still locate the deleted file
139+
$this->dynamicSourceLocatorProvider->reset();
137140
}
138141

139142
protected static function yieldFilesFromDirectory(string $directory, string $suffix = '*.php.inc'): Iterator

‎tests/NodeManipulator/ClassDependencyManipulatorTest.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Rector\NodeManipulator\ClassDependencyManipulator;
2323
use Rector\PostRector\ValueObject\PropertyMetadata;
2424
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
25+
use Rector\ValueObject\PhpVersion;
2526
use Rector\ValueObject\PhpVersionFeature;
2627

2728
final class ClassDependencyManipulatorTest extends AbstractLazyTestCase
@@ -42,6 +43,12 @@ protected function setUp(): void
4243
SimpleParameterProvider::setParameter(Option::PHP_VERSION_FEATURES, PhpVersionFeature::READONLY_PROPERTY);
4344
}
4445

46+
protected function tearDown(): void
47+
{
48+
// reset to the test default, so the next test class in the same process does not run on PHP 8.1
49+
SimpleParameterProvider::setParameter(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_10);
50+
}
51+
4552
public function testEmptyClass(): void
4653
{
4754
$someClass = new Class_(new Identifier('EmptyClass'));
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Rector\Tests\Testing\ResetSourceLocator\Fixture;
4+
5+
function some_function()
6+
{
7+
}
8+
9+
?>
10+
-----
11+
<?php
12+
13+
namespace Rector\Tests\Testing\ResetSourceLocator\Fixture;
14+
15+
function some_function(): void
16+
{
17+
}
18+
19+
?>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Testing\ResetSourceLocator;
6+
7+
use PHPUnit\Framework\Attributes\Depends;
8+
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class ResetSourceLocatorTest extends AbstractRectorTestCase
12+
{
13+
public function testProcessFixture(): void
14+
{
15+
$this->doTestFile(__DIR__ . '/Fixture/some_function.php.inc');
16+
}
17+
18+
#[Depends('testProcessFixture')]
19+
public function testNextTestDoesNotLocateDeletedInputFile(): void
20+
{
21+
$dynamicSourceLocatorProvider = $this->make(DynamicSourceLocatorProvider::class);
22+
23+
$this->assertTrue($dynamicSourceLocatorProvider->arePathsEmpty());
24+
}
25+
26+
public function provideConfigFilePath(): string
27+
{
28+
return __DIR__ . '/config/configured_rule.php';
29+
}
30+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\TypeDeclaration\Rector\Function_\AddFunctionVoidReturnTypeWhereNoReturnRector;
7+
8+
return RectorConfig::configure()
9+
->withRules([AddFunctionVoidReturnTypeWhereNoReturnRector::class]);

0 commit comments

Comments
 (0)