Repository navigation
Expand file tree
/
Copy pathOnlyRuleResolverTest.php
More file actions
137 lines (115 loc) · 5 KB
/
Copy pathOnlyRuleResolverTest.php
File metadata and controls
137 lines (115 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
declare(strict_types=1);
namespace Rector\Tests\Configuration;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Configuration\OnlyRuleResolver;
use Rector\Contract\Rector\RectorInterface;
use Rector\DeadCode\Rector\Assign\RemoveDoubleAssignRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector;
use Rector\Exception\Configuration\RectorRuleNameAmbiguousException;
use Rector\Exception\Configuration\RectorRuleNotFoundException;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
use Rector\TypeDeclaration\Rector\StmtsAwareInterface\SafeDeclareStrictTypesRector;
final class OnlyRuleResolverTest extends AbstractLazyTestCase
{
private OnlyRuleResolver $onlyRuleResolver;
protected function setUp(): void
{
parent::setUp();
$this->bootFromConfigFiles([__DIR__ . '/config/only_rule_resolver_config.php']);
$rectorConfig = self::getContainer();
$this->onlyRuleResolver = new OnlyRuleResolver(
iterator_to_array($rectorConfig->tagged(RectorInterface::class)),
);
}
public function testResolveOk(): void
{
$this->assertSame(
RemoveDoubleAssignRector::class,
$this->onlyRuleResolver->resolve('Rector\\DeadCode\\Rector\\Assign\\RemoveDoubleAssignRector')
);
}
public function testResolveOkLeadingBackslash(): void
{
$this->assertSame(
RemoveDoubleAssignRector::class,
$this->onlyRuleResolver->resolve('\\Rector\\DeadCode\\Rector\\Assign\\RemoveDoubleAssignRector')
);
}
public function testResolveOkDoubleBackslashes(): void
{
$this->assertSame(
RemoveDoubleAssignRector::class,
$this->onlyRuleResolver->resolve('\\\\Rector\\\\DeadCode\\\\Rector\\\\Assign\\\\RemoveDoubleAssignRector'),
'We want to fix wrongly double-quoted backslashes automatically'
);
}
public function testResolveOkSingleQuotes(): void
{
$this->assertSame(
RemoveDoubleAssignRector::class,
$this->onlyRuleResolver->resolve("'Rector\\DeadCode\\Rector\\Assign\\RemoveDoubleAssignRector'"),
'Remove stray single quotes on Windows systems'
);
}
public function testResolveMissingBackslash(): void
{
$this->assertSame(
RemoveDoubleAssignRector::class,
$this->onlyRuleResolver->resolve('RectorDeadCodeRectorAssignRemoveDoubleAssignRector'),
'The shell eats unescaped backslashes, resolve the flattened rule name anyway'
);
}
public function testResolveMissingBackslashNotFound(): void
{
$this->expectExceptionMessageIsOrContains(
'Rule "ThisRuleDoesNotExist" was not found.' . PHP_EOL
. 'The rule has no namespace. Make sure to escape the backslashes, and add quotes around the rule name: --only="My\\Rector\\Rule"'
);
$this->expectException(RectorRuleNotFoundException::class);
$this->onlyRuleResolver->resolve('ThisRuleDoesNotExist');
}
public function testResolveNotFound(): void
{
$this->expectExceptionMessageIsOrContains(
'Rule "This\Rule\Does\Not\Exist" was not found.' . PHP_EOL
. 'Make sure it is registered in your config or in one of the sets'
);
$this->expectException(RectorRuleNotFoundException::class);
$this->onlyRuleResolver->resolve('This\\Rule\\Does\\Not\\Exist');
}
public function testResolveShortOk(): void
{
$this->assertSame(
RemoveUnusedPrivateMethodRector::class,
$this->onlyRuleResolver->resolve('RemoveUnusedPrivateMethodRector'),
);
}
public function testResolveShortOkTwoLevels(): void
{
$this->assertSame(
RemoveDoubleAssignRector::class,
$this->onlyRuleResolver->resolve('Assign\\RemoveDoubleAssignRector'),
);
}
public function testResolveExistingButNotRegistered(): void
{
$this->expectExceptionMessageIsOrContains(
'Rule "Rector\TypeDeclaration\Rector\StmtsAwareInterface\SafeDeclareStrictTypesRector" exists, but is not registered in your Rector config.'
. PHP_EOL . 'Register it in your rector.php:' . PHP_EOL . PHP_EOL
. ' ->withRules([SafeDeclareStrictTypesRector::class])'
);
$this->expectException(RectorRuleNotFoundException::class);
$this->onlyRuleResolver->resolve(SafeDeclareStrictTypesRector::class);
}
public function testResolveShortAmbiguous(): void
{
$this->expectExceptionMessageIsOrContains(
'Short rule name "RemoveDoubleAssignRector" is ambiguous. Specify the full rule name:' . PHP_EOL
. '- Rector\\DeadCode\\Rector\\Assign\\RemoveDoubleAssignRector' . PHP_EOL
. '- Rector\\Tests\\Configuration\\Source\\RemoveDoubleAssignRector'
);
$this->expectException(RectorRuleNameAmbiguousException::class);
$this->onlyRuleResolver->resolve('RemoveDoubleAssignRector');
}
}