Skip to content

Commit 82a7251

Browse files
authored
[PHPUnit60] Change AddDoesNotPerformAssertionToNonAssertingTestRector node from ClassMethod to Class_ (#830)
1 parent f34c2b2 commit 82a7251

5 files changed

Lines changed: 75 additions & 127 deletions

File tree

‎bench/does_not_perform_assertion_abstract_class_bench.php‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
/**
66
* Benchmark for AddDoesNotPerformAssertionToNonAssertingTestRector on abstract and final test classes.
77
*
8-
* Half of the test classes are abstract, so the abstract class check runs on every test method.
8+
* Half of the test classes are abstract, so the abstract class check runs on every test class.
99
* Run on both branches to compare. CPU (user+sys) is the reliable metric; wall is noisy.
1010
*/
1111

12-
use PhpParser\Node\Stmt\ClassMethod;
12+
use PhpParser\Node\Stmt\Class_;
1313
use Rector\DependencyInjection\LazyContainerFactory;
1414
use Rector\PhpParser\Node\BetterNodeFinder;
1515
use Rector\PHPUnit\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector;
@@ -68,10 +68,10 @@
6868

6969
$rector = $rectorConfig->make(AddDoesNotPerformAssertionToNonAssertingTestRector::class);
7070

71-
$classMethods = [];
71+
$classes = [];
7272
foreach ($testFilePaths as $testFilePath) {
7373
$stmts = $testingParser->parseFileToDecoratedNodes($testFilePath);
74-
$classMethods = [...$classMethods, ...$betterNodeFinder->findInstanceOf($stmts, ClassMethod::class)];
74+
$classes = [...$classes, ...$betterNodeFinder->findInstanceOf($stmts, Class_::class)];
7575
}
7676

7777
$cpuSeconds = static function (): float {
@@ -84,8 +84,8 @@
8484
$cpuStart = $cpuSeconds();
8585

8686
$changedCount = 0;
87-
foreach ($classMethods as $classMethod) {
88-
if ($rector->refactor($classMethod) instanceof ClassMethod) {
87+
foreach ($classes as $class) {
88+
if ($rector->refactor($class) instanceof Class_) {
8989
++$changedCount;
9090
}
9191
}
@@ -96,8 +96,8 @@
9696
array_map('unlink', glob($directory . '/*.php'));
9797
@rmdir($directory);
9898

99-
printf('test classes: %d, test methods: %d, changed: %d%s', count($testFilePaths), count($classMethods), $changedCount, PHP_EOL);
100-
printf('CPU: %.4f s (%.3f ms/method)%s', $cpu, $cpu / count($classMethods) * 1000, PHP_EOL);
99+
printf('test classes: %d, changed: %d%s', count($classes), $changedCount, PHP_EOL);
100+
printf('CPU: %.4f s (%.3f ms/class)%s', $cpu, $cpu / count($classes) * 1000, PHP_EOL);
101101
printf('wall: %.4f s%s', $wall, PHP_EOL);
102102

103103
if ($cpu > MAX_CPU_SECONDS) {

‎bench/does_not_perform_assertion_lifecycle_bench.php‎

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@
1111
* Run on both branches to compare. CPU (user+sys) is the reliable metric; wall is noisy.
1212
*/
1313

14-
use PhpParser\Node\Stmt\ClassMethod;
15-
use Rector\Application\Provider\CurrentFileProvider;
14+
use PhpParser\Node\Stmt\Class_;
1615
use Rector\DependencyInjection\LazyContainerFactory;
1716
use Rector\PhpParser\Node\BetterNodeFinder;
1817
use Rector\PHPUnit\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector;
1918
use Rector\Testing\TestingParser\TestingParser;
20-
use Rector\ValueObject\Application\File;
2119

2220
require __DIR__ . '/../vendor/autoload.php';
2321

@@ -100,17 +98,11 @@
10098

10199
$rector = $rectorConfig->make(AddDoesNotPerformAssertionToNonAssertingTestRector::class);
102100

103-
/** @var CurrentFileProvider $currentFileProvider */
104-
$currentFileProvider = $rectorConfig->make(CurrentFileProvider::class);
105-
106101
// parse test files up front, parent files are left for the rule to resolve
107-
// the rule finds the class of a method in the stmts of the current file, as in a real run
108-
$classMethodsByFile = [];
102+
$classes = [];
109103
foreach ($testFilePaths as $testFilePath) {
110104
$stmts = $testingParser->parseFileToDecoratedNodes($testFilePath);
111-
$file = new File($testFilePath, (string) file_get_contents($testFilePath));
112-
$file->hydrateStmtsAndTokens($stmts, $stmts, []);
113-
$classMethodsByFile[] = [$file, $betterNodeFinder->findInstanceOf($stmts, ClassMethod::class)];
105+
$classes = [...$classes, ...$betterNodeFinder->findInstanceOf($stmts, Class_::class)];
114106
}
115107

116108
$cpuSeconds = static function (): float {
@@ -123,15 +115,9 @@
123115
$cpuStart = $cpuSeconds();
124116

125117
$changedCount = 0;
126-
$classMethodCount = 0;
127-
foreach ($classMethodsByFile as [$file, $classMethods]) {
128-
$currentFileProvider->setFile($file);
129-
130-
foreach ($classMethods as $classMethod) {
131-
++$classMethodCount;
132-
if ($rector->refactor($classMethod) instanceof ClassMethod) {
133-
++$changedCount;
134-
}
118+
foreach ($classes as $class) {
119+
if ($rector->refactor($class) instanceof Class_) {
120+
++$changedCount;
135121
}
136122
}
137123

@@ -141,8 +127,8 @@
141127
array_map('unlink', glob($directory . '/*.php'));
142128
@rmdir($directory);
143129

144-
printf('test classes: %d, test methods: %d, changed: %d%s', count($testFilePaths), $classMethodCount, $changedCount, PHP_EOL);
145-
printf('CPU: %.4f s (%.3f ms/method)%s', $cpu, $cpu / $classMethodCount * 1000, PHP_EOL);
130+
printf('test classes: %d, changed: %d%s', count($classes), $changedCount, PHP_EOL);
131+
printf('CPU: %.4f s (%.3f ms/class)%s', $cpu, $cpu / count($classes) * 1000, PHP_EOL);
146132
printf('wall: %.4f s%s', $wall, PHP_EOL);
147133

148134
if ($cpu > MAX_CPU_SECONDS) {

‎bench/does_not_perform_assertion_own_lifecycle_bench.php‎

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@
55
/**
66
* Benchmark for AddDoesNotPerformAssertionToNonAssertingTestRector on test classes with own lifecycle methods.
77
*
8-
* Every test class has its own setUp() and tearDown(), every other one asserts in setUp(). The class is taken from
9-
* the current file, so the rule looks into its lifecycle methods for every test method.
8+
* Every test class has its own setUp() and tearDown(), every other one asserts in setUp().
109
* Run on both branches to compare. CPU (user+sys) is the reliable metric; wall is noisy.
1110
*/
1211

13-
use PhpParser\Node\Stmt\ClassMethod;
14-
use Rector\Application\Provider\CurrentFileProvider;
12+
use PhpParser\Node\Stmt\Class_;
1513
use Rector\DependencyInjection\LazyContainerFactory;
1614
use Rector\PhpParser\Node\BetterNodeFinder;
1715
use Rector\PHPUnit\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector;
1816
use Rector\Testing\TestingParser\TestingParser;
19-
use Rector\ValueObject\Application\File;
2017

2118
require __DIR__ . '/../vendor/autoload.php';
2219

@@ -72,16 +69,10 @@
7269

7370
$rector = $rectorConfig->make(AddDoesNotPerformAssertionToNonAssertingTestRector::class);
7471

75-
/** @var CurrentFileProvider $currentFileProvider */
76-
$currentFileProvider = $rectorConfig->make(CurrentFileProvider::class);
77-
78-
// the rule finds the class of a method in the stmts of the current file, as in a real run
79-
$classMethodsByFile = [];
72+
$classes = [];
8073
foreach ($testFilePaths as $testFilePath) {
8174
$stmts = $testingParser->parseFileToDecoratedNodes($testFilePath);
82-
$file = new File($testFilePath, (string) file_get_contents($testFilePath));
83-
$file->hydrateStmtsAndTokens($stmts, $stmts, []);
84-
$classMethodsByFile[] = [$file, $betterNodeFinder->findInstanceOf($stmts, ClassMethod::class)];
75+
$classes = [...$classes, ...$betterNodeFinder->findInstanceOf($stmts, Class_::class)];
8576
}
8677

8778
$cpuSeconds = static function (): float {
@@ -94,15 +85,9 @@
9485
$cpuStart = $cpuSeconds();
9586

9687
$changedCount = 0;
97-
$classMethodCount = 0;
98-
foreach ($classMethodsByFile as [$file, $classMethods]) {
99-
$currentFileProvider->setFile($file);
100-
101-
foreach ($classMethods as $classMethod) {
102-
++$classMethodCount;
103-
if ($rector->refactor($classMethod) instanceof ClassMethod) {
104-
++$changedCount;
105-
}
88+
foreach ($classes as $class) {
89+
if ($rector->refactor($class) instanceof Class_) {
90+
++$changedCount;
10691
}
10792
}
10893

@@ -112,8 +97,8 @@
11297
array_map('unlink', glob($directory . '/*.php'));
11398
@rmdir($directory);
11499

115-
printf('test classes: %d, test methods: %d, changed: %d%s', count($testFilePaths), $classMethodCount, $changedCount, PHP_EOL);
116-
printf('CPU: %.4f s (%.3f ms/method)%s', $cpu, $cpu / $classMethodCount * 1000, PHP_EOL);
100+
printf('test classes: %d, changed: %d%s', count($classes), $changedCount, PHP_EOL);
101+
printf('CPU: %.4f s (%.3f ms/class)%s', $cpu, $cpu / count($classes) * 1000, PHP_EOL);
117102
printf('wall: %.4f s%s', $wall, PHP_EOL);
118103

119104
if ($cpu > MAX_CPU_SECONDS) {

‎rules/PHPUnit60/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector.php‎

Lines changed: 49 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@
1414
use PhpParser\Node\Stmt\Expression;
1515
use PhpParser\Node\Stmt\Nop;
1616
use PhpParser\NodeVisitor;
17+
use PHPStan\Analyser\Scope;
1718
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
1819
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
1920
use PHPStan\Reflection\ClassReflection;
2021
use PHPStan\Reflection\ReflectionProvider;
2122
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
2223
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
2324
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
24-
use Rector\PhpParser\Node\BetterNodeFinder;
25+
use Rector\PHPStan\ScopeFetcher;
2526
use Rector\PHPUnit\Enum\PHPUnitAttribute;
2627
use Rector\PHPUnit\Enum\PHPUnitClassName;
2728
use Rector\PHPUnit\NodeAnalyzer\AssertCallAnalyzer;
2829
use Rector\PHPUnit\NodeAnalyzer\LifecycleMethodAssertAnalyzer;
2930
use Rector\PHPUnit\NodeAnalyzer\MockedVariableAnalyzer;
3031
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
3132
use Rector\Rector\AbstractRector;
32-
use Rector\Reflection\ReflectionResolver;
3333
use Rector\RuleDoc\CodeSample\CodeSample;
3434
use Rector\RuleDoc\RuleDefinition;
3535
use Rector\VersionBonding\Contract\ComposerPackageConstraintInterface;
@@ -58,10 +58,8 @@ public function __construct(
5858
private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer,
5959
private readonly DocBlockUpdater $docBlockUpdater,
6060
private readonly PhpDocInfoFactory $phpDocInfoFactory,
61-
private readonly ReflectionResolver $reflectionResolver,
6261
private readonly ReflectionProvider $reflectionProvider,
6362
private readonly LifecycleMethodAssertAnalyzer $lifecycleMethodAssertAnalyzer,
64-
private readonly BetterNodeFinder $betterNodeFinder,
6563
) {
6664
}
6765

@@ -105,43 +103,74 @@ public function test()
105103
*/
106104
public function getNodeTypes(): array
107105
{
108-
return [ClassMethod::class];
106+
return [Class_::class];
109107
}
110108

111109
/**
112-
* @param ClassMethod $node
110+
* @param Class_ $node
113111
*/
114112
public function refactor(Node $node): ?Node
115113
{
116-
if ($this->shouldSkipClassMethod($node)) {
114+
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
117115
return null;
118116
}
119117

120-
$this->removeAddToAssertionCountCalls($node);
118+
// a child class can add asserting lifecycle methods, that count toward the inherited tests
119+
if ($node->isAbstract()) {
120+
return null;
121+
}
121122

123+
$scope = ScopeFetcher::fetch($node);
124+
125+
// the parent test case asserts in its own integration methods
126+
if ($this->isInTwigIntegrationTestCase($scope)) {
127+
return null;
128+
}
129+
130+
// assertions in lifecycle methods count toward every test of the class
131+
if ($this->lifecycleMethodAssertAnalyzer->hasAssertingLifecycleMethod($node)) {
132+
return null;
133+
}
134+
135+
$hasChanged = false;
136+
137+
foreach ($node->getMethods() as $classMethod) {
138+
if ($this->shouldSkipClassMethod($classMethod)) {
139+
continue;
140+
}
141+
142+
$this->removeAddToAssertionCountCalls($classMethod);
143+
$this->addDoesNotPerformAssertions($classMethod);
144+
145+
$hasChanged = true;
146+
}
147+
148+
if (! $hasChanged) {
149+
return null;
150+
}
151+
152+
return $node;
153+
}
154+
155+
private function addDoesNotPerformAssertions(ClassMethod $classMethod): void
156+
{
122157
// the attribute is available since PHPUnit 10, prefer it over the annotation
123158
if ($this->reflectionProvider->hasClass(PHPUnitAttribute::DOES_NOT_PERFORM_ASSERTIONS)) {
124-
$node->attrGroups[] = new AttributeGroup([
159+
$classMethod->attrGroups[] = new AttributeGroup([
125160
new Attribute(new FullyQualified(PHPUnitAttribute::DOES_NOT_PERFORM_ASSERTIONS)),
126161
]);
127162

128-
return $node;
163+
return;
129164
}
130165

131-
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
166+
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod);
132167
$phpDocInfo->addPhpDocTagNode(new PhpDocTagNode('@doesNotPerformAssertions', new GenericTagValueNode('')));
133168

134-
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
135-
136-
return $node;
169+
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($classMethod);
137170
}
138171

139172
private function shouldSkipClassMethod(ClassMethod $classMethod): bool
140173
{
141-
if (! $this->testsNodeAnalyzer->isInTestClass($classMethod)) {
142-
return true;
143-
}
144-
145174
if (! $this->testsNodeAnalyzer->isTestClassMethod($classMethod)) {
146175
return true;
147176
}
@@ -150,21 +179,6 @@ private function shouldSkipClassMethod(ClassMethod $classMethod): bool
150179
return true;
151180
}
152181

153-
// we have no idea how the trait is used, the using class can assert on its own
154-
if ($this->isInTrait($classMethod)) {
155-
return true;
156-
}
157-
158-
// a child class can add asserting lifecycle methods, that count toward the inherited tests
159-
if ($this->isInAbstractClass($classMethod)) {
160-
return true;
161-
}
162-
163-
// the parent test case asserts in its own integration methods
164-
if ($this->isInTwigIntegrationTestCase($classMethod)) {
165-
return true;
166-
}
167-
168182
if ($this->hasAssertingAnnotationOrAttribute($classMethod)) {
169183
return true;
170184
}
@@ -174,12 +188,6 @@ private function shouldSkipClassMethod(ClassMethod $classMethod): bool
174188
return true;
175189
}
176190

177-
// assertions in lifecycle methods count toward every test of the class
178-
$class = $this->resolveCurrentClass($classMethod);
179-
if ($class instanceof Class_ && $this->lifecycleMethodAssertAnalyzer->hasAssertingLifecycleMethod($class)) {
180-
return true;
181-
}
182-
183191
return $this->mockedVariableAnalyzer->containsMockAsUsedVariable($classMethod);
184192
}
185193

@@ -226,40 +234,9 @@ private function isAddToAssertionCountExpression(Node $node): bool
226234
return $this->isName($methodCall->name, 'addToAssertionCount');
227235
}
228236

229-
private function isInTrait(ClassMethod $classMethod): bool
230-
{
231-
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
232-
if (! $classReflection instanceof ClassReflection) {
233-
return false;
234-
}
235-
236-
return $classReflection->isTrait();
237-
}
238-
239-
private function isInAbstractClass(ClassMethod $classMethod): bool
240-
{
241-
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
242-
if (! $classReflection instanceof ClassReflection) {
243-
return false;
244-
}
245-
246-
return $classReflection->isAbstract();
247-
}
248-
249-
private function resolveCurrentClass(ClassMethod $classMethod): ?Class_
250-
{
251-
$class = $this->betterNodeFinder->findFirst(
252-
$this->getFile()
253-
->getNewStmts(),
254-
fn (Node $node): bool => $node instanceof Class_ && in_array($classMethod, $node->getMethods(), true)
255-
);
256-
257-
return $class instanceof Class_ ? $class : null;
258-
}
259-
260-
private function isInTwigIntegrationTestCase(ClassMethod $classMethod): bool
237+
private function isInTwigIntegrationTestCase(Scope $scope): bool
261238
{
262-
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
239+
$classReflection = $scope->getClassReflection();
263240
if (! $classReflection instanceof ClassReflection) {
264241
return false;
265242
}

‎tests/Issues/PHPUnit10DataProvider/Fixture/some_test.php.inc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ namespace Rector\PHPUnit\Tests\Issues\PHPUnit10DataProvider\Fixture;
2626

2727
final class SomeTest extends \PHPUnit\Framework\TestCase
2828
{
29-
#[\PHPUnit\Framework\Attributes\DataProvider('fooProvider')]
3029
#[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
30+
#[\PHPUnit\Framework\Attributes\DataProvider('fooProvider')]
3131
#[\PHPUnit\Framework\Attributes\Test]
3232
public function it_should_do_stuff(string $foo): void
3333
{

0 commit comments

Comments
 (0)