Skip to content

Commit f9f4256

Browse files
committed
[Testing] Run only registered rules in AbstractRectorTestCase
findByContract() returns every instance the container has cached that implements the contract. A rule injected into another service is built as a constructor dependency, cached like anything else, and therefore comes back from findByContract(RectorInterface::class) even though no config ever registered it as a rule. The test case then runs it against the fixtures. The registered set is already tracked: rule() records into $registeredRectorClasses and getMainRectorClasses() exposes it, and every registration path funnels through rule() -- rules(), ruleWithConfiguration() and ruleWithConfigurationComposerVersionBound(), the last of which returns before rule() when the package constraint is not satisfied. Intersecting the container result with that list therefore only ever removes rules nobody asked for; it cannot activate one that the composer-package filter excluded. Scope: findByContract(RectorInterface::class) is used only here, in AbstractRectorTestCase, so a production run is unaffected. Before 2.6.4 the same lines read tagged(RectorInterface::class), an explicit tag list filled by rule(), which had exactly the behaviour restored here. The added test covers it end to end: a config registers one rule whose constructor takes a second, unregistered one that rewrites the string 'marker'. Without the change the fixture comes back rewritten and the test fails; with it the fixture is untouched. Found while fixing ssch/typo3-rector for 2.6.4, where the Extbase test resolved two active rules against one registered: ValidateAttributeDecorator takes StringClassNameToClassConstantRector in its constructor, so that rule ran in every fixture of that test. Assisted-by: claude-code:claude-fable-5 Agent-Session: https://claude.ai/code/session_019wx8ueHuqUidp5yybDQ2CN Agent-Host: 0493f0 Signed-off-by: Sebastian Mendel <info@sebastianmendel.de>
1 parent c1c61c4 commit f9f4256

6 files changed

Lines changed: 160 additions & 1 deletion

File tree

‎src/Testing/PHPUnit/AbstractRectorTestCase.php‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,23 @@ protected function setUp(): void
106106
$this->bootFromConfigFiles([$configFile]);
107107

108108
// no rules at all, e.g. in case of only post rector run, yields an empty array
109-
$rectors = $rectorConfig->findByContract(RectorInterface::class);
109+
//
110+
// findByContract() returns every cached instance implementing the contract, so a rule
111+
// that exists only as a constructor dependency of another service is included even
112+
// though no config ever registered it. narrow the set to what was registered, or the
113+
// test runs rules its config never asked for.
114+
$registeredRectorClasses = $rectorConfig->getMainRectorClasses();
115+
116+
$rectors = array_values(
117+
array_filter(
118+
$rectorConfig->findByContract(RectorInterface::class),
119+
static fn (RectorInterface $rector): bool => in_array(
120+
$rector::class,
121+
$registeredRectorClasses,
122+
true
123+
)
124+
)
125+
);
110126

111127
/** @var RectorNodeTraverser $rectorNodeTraverser */
112128
$rectorNodeTraverser = $rectorConfig->make(RectorNodeTraverser::class);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Issues\DependencyOnlyRuleNotActive;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class DependencyOnlyRuleNotActiveTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\Issues\DependencyOnlyRuleNotActive\Fixture;
4+
5+
class SkipUnregisteredDependencyRule
6+
{
7+
public function run(): string
8+
{
9+
return 'marker';
10+
}
11+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Issues\DependencyOnlyRuleNotActive\Source;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Scalar\String_;
9+
use Rector\Rector\AbstractRector;
10+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
11+
12+
/**
13+
* The only rule the test config registers. It injects another rule, which makes the container
14+
* build and cache that one as well — the situation this test exists for.
15+
*/
16+
final class RegisteredRector extends AbstractRector
17+
{
18+
public function __construct(
19+
private readonly UnregisteredMarkerRector $unregisteredMarkerRector
20+
) {
21+
}
22+
23+
public function getRuleDefinition(): RuleDefinition
24+
{
25+
return new RuleDefinition('', []);
26+
}
27+
28+
/**
29+
* @return array<class-string<Node>>
30+
*/
31+
public function getNodeTypes(): array
32+
{
33+
return [String_::class];
34+
}
35+
36+
public function refactor(Node $node): ?String_
37+
{
38+
if (! $node instanceof String_) {
39+
return null;
40+
}
41+
42+
// delegate only for a value no fixture contains, so this rule changes nothing itself
43+
// while still holding a live dependency on the unregistered one
44+
if ($node->value === 'delegate-to-dependency') {
45+
return $this->unregisteredMarkerRector->refactor($node);
46+
}
47+
48+
return null;
49+
}
50+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Issues\DependencyOnlyRuleNotActive\Source;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Scalar\String_;
9+
use Rector\Rector\AbstractRector;
10+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
11+
12+
/**
13+
* Never registered by the test config. It only exists as a constructor dependency of
14+
* @see RegisteredRector, and must therefore never run.
15+
*/
16+
final class UnregisteredMarkerRector extends AbstractRector
17+
{
18+
public function getRuleDefinition(): RuleDefinition
19+
{
20+
return new RuleDefinition('', []);
21+
}
22+
23+
/**
24+
* @return array<class-string<Node>>
25+
*/
26+
public function getNodeTypes(): array
27+
{
28+
return [String_::class];
29+
}
30+
31+
public function refactor(Node $node): ?String_
32+
{
33+
if (! $node instanceof String_) {
34+
return null;
35+
}
36+
37+
if ($node->value !== 'marker') {
38+
return null;
39+
}
40+
41+
return new String_('rewritten by an unregistered rule');
42+
}
43+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Tests\Issues\DependencyOnlyRuleNotActive\Source\RegisteredRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
// only this one; its constructor dependency must not become active as well
10+
$rectorConfig->rule(RegisteredRector::class);
11+
};

0 commit comments

Comments
 (0)