Skip to content

Commit 48bc372

Browse files
committed
[PHPUnit60] Skip tests in classes with asserting parent lifecycle methods in AddDoesNotPerformAssertionToNonAssertingTestRector
1 parent 9545637 commit 48bc372

8 files changed

Lines changed: 237 additions & 28 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
4+
5+
use Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Source\AbstractClassWithAssertingTearDown;
6+
7+
class OverrideAssertingParentTearDown extends AbstractClassWithAssertingTearDown
8+
{
9+
protected function tearDown(): void
10+
{
11+
$this->value = null;
12+
}
13+
14+
public function testSomething()
15+
{
16+
$value = 5;
17+
}
18+
}
19+
20+
?>
21+
-----
22+
<?php
23+
24+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
25+
26+
use Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Source\AbstractClassWithAssertingTearDown;
27+
28+
class OverrideAssertingParentTearDown extends AbstractClassWithAssertingTearDown
29+
{
30+
protected function tearDown(): void
31+
{
32+
$this->value = null;
33+
}
34+
35+
#[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
36+
public function testSomething()
37+
{
38+
$value = 5;
39+
}
40+
}
41+
42+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
abstract class Deep0 extends TestCase { protected function setUp(): void { $this->assertTrue(true); } }
8+
abstract class Deep1 extends Deep0 { protected function setUp(): void { parent::setUp(); } }
9+
abstract class Deep2 extends Deep1 { protected function setUp(): void { parent::setUp(); } }
10+
abstract class Deep3 extends Deep2 { protected function setUp(): void { parent::setUp(); } }
11+
abstract class Deep4 extends Deep3 { protected function setUp(): void { parent::setUp(); } }
12+
abstract class Deep5 extends Deep4 { protected function setUp(): void { parent::setUp(); } }
13+
abstract class Deep6 extends Deep5 { protected function setUp(): void { parent::setUp(); } }
14+
15+
final class DeepTest extends Deep6
16+
{
17+
public function testSomething()
18+
{
19+
$value = 1;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
abstract class DeepC0 extends TestCase { protected function setUp(): void { $this->assertTrue(true); } }
8+
abstract class DeepC1 extends DeepC0 { protected function setUp(): void { parent::setUp(); } }
9+
abstract class DeepC2 extends DeepC1 { protected function setUp(): void { parent::setUp(); } }
10+
abstract class DeepC3 extends DeepC2 { protected function setUp(): void { parent::setUp(); } }
11+
abstract class DeepC4 extends DeepC3 { protected function setUp(): void { parent::setUp(); } }
12+
abstract class DeepC5 extends DeepC4 { protected function setUp(): void { parent::setUp(); } }
13+
abstract class DeepC6 extends DeepC5 { protected function setUp(): void { parent::setUp(); } }
14+
15+
final class DeepCTest extends DeepC6
16+
{
17+
public function testSomething()
18+
{
19+
$value = 1;
20+
}
21+
}
22+
23+
final class DeepCShallowTest extends DeepC2
24+
{
25+
public function testOther()
26+
{
27+
$other = 2;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
4+
5+
use Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Source\AbstractClassWithAssertingTearDown;
6+
7+
class SkipAssertingTearDownInParent extends AbstractClassWithAssertingTearDown
8+
{
9+
public function testSomething()
10+
{
11+
$value = 5;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Source;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
abstract class AbstractClassWithAssertingTearDown extends TestCase
10+
{
11+
protected function tearDown(): void
12+
{
13+
$this->assertTrue(true);
14+
}
15+
}

‎rules/CodeQuality/Rector/ClassMethod/DataProviderArrayItemsNewLinedRector.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function refactor(Node $node): ?Node
107107
// find array in data provider - must contain a return node
108108
/** @var Return_[] $returns */
109109
$returns = $this->betterNodeFinder->findInstanceOf((array) $node->stmts, Return_::class);
110-
$hasChanged = \false;
110+
$hasChanged = false;
111111
foreach ($returns as $return) {
112112
if (! $return->expr instanceof Array_) {
113113
continue;
@@ -123,10 +123,10 @@ public function refactor(Node $node): ?Node
123123
}
124124

125125
// ensure newlined printed
126-
$array->setAttribute(AttributeKey::NEWLINED_ARRAY_PRINT, \true);
126+
$array->setAttribute(AttributeKey::NEWLINED_ARRAY_PRINT, true);
127127
// invoke reprint
128128
$array->setAttribute(AttributeKey::ORIGINAL_NODE, null);
129-
$hasChanged = \true;
129+
$hasChanged = true;
130130
}
131131

132132
if ($hasChanged) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public function refactor(Node $node): ?Node
128128
}
129129

130130
// assertions in lifecycle methods count toward every test of the class
131-
if ($this->lifecycleMethodAssertAnalyzer->hasAssertingLifecycleMethod($node)) {
131+
if ($this->lifecycleMethodAssertAnalyzer->hasAssertingLifecycleMethod($node, $scope)) {
132132
return null;
133133
}
134134

‎src/NodeAnalyzer/LifecycleMethodAssertAnalyzer.php‎

Lines changed: 113 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,26 @@
55
namespace Rector\PHPUnit\NodeAnalyzer;
66

77
use PhpParser\Comment\Doc;
8+
use PhpParser\Node;
9+
use PhpParser\Node\Expr\StaticCall;
810
use PhpParser\Node\Stmt\Class_;
11+
use PhpParser\Node\Stmt\ClassLike;
912
use PhpParser\Node\Stmt\ClassMethod;
13+
use PHPStan\Analyser\Scope;
14+
use PHPStan\Reflection\ClassReflection;
1015
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
16+
use Rector\Enum\ObjectReference;
1117
use Rector\NodeNameResolver\NodeNameResolver;
1218
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
19+
use Rector\PhpParser\AstResolver;
20+
use Rector\PhpParser\Node\BetterNodeFinder;
1321
use Rector\PHPUnit\Enum\PHPUnitAttribute;
14-
use WeakMap;
22+
use Rector\PHPUnit\Enum\PHPUnitClassName;
1523

1624
/**
17-
* PHPUnit counts assertions made in lifecycle methods toward every test of the class
25+
* PHPUnit counts assertions made in lifecycle methods of the class and its parents toward every test of the class
1826
*/
19-
final readonly class LifecycleMethodAssertAnalyzer
27+
final class LifecycleMethodAssertAnalyzer
2028
{
2129
/**
2230
* @var string[]
@@ -39,47 +47,128 @@
3947
private const array HOOK_ANNOTATIONS = ['before', 'after', 'preCondition', 'postCondition'];
4048

4149
/**
42-
* @var WeakMap<Class_, bool>
50+
* @var array<string, bool>
4351
*/
44-
private WeakMap $assertingByClass;
52+
private array $assertingByParentClassAndOverriddenHooks = [];
4553

4654
public function __construct(
47-
private AssertCallAnalyzer $assertCallAnalyzer,
48-
private PhpAttributeAnalyzer $phpAttributeAnalyzer,
49-
private PhpDocInfoFactory $phpDocInfoFactory,
50-
private NodeNameResolver $nodeNameResolver,
55+
private readonly AssertCallAnalyzer $assertCallAnalyzer,
56+
private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer,
57+
private readonly PhpDocInfoFactory $phpDocInfoFactory,
58+
private readonly NodeNameResolver $nodeNameResolver,
59+
private readonly AstResolver $astResolver,
60+
private readonly BetterNodeFinder $betterNodeFinder,
5161
) {
52-
$this->assertingByClass = new WeakMap();
5362
}
5463

55-
public function hasAssertingLifecycleMethod(Class_ $class): bool
64+
public function hasAssertingLifecycleMethod(Class_ $class, Scope $scope): bool
5665
{
57-
// every test method of the class shares the result
58-
return $this->assertingByClass[$class] ??= $this->resolveHasAssertingLifecycleMethod($class);
66+
$classReflection = $scope->getClassReflection();
67+
if (! $classReflection instanceof ClassReflection) {
68+
return false;
69+
}
70+
71+
return $this->isClassLikeAsserting($class, $classReflection, []);
5972
}
6073

61-
private function resolveHasAssertingLifecycleMethod(Class_ $class): bool
62-
{
63-
foreach ($class->getMethods() as $classMethod) {
64-
if (! $this->isLifecycleMethod($classMethod)) {
65-
continue;
74+
/**
75+
* @param string[] $overriddenHookMethodNames
76+
*/
77+
private function isClassLikeAsserting(
78+
?ClassLike $classLike,
79+
ClassReflection $classReflection,
80+
array $overriddenHookMethodNames
81+
): bool {
82+
$classMethods = $classLike instanceof ClassLike ? $classLike->getMethods() : [];
83+
84+
foreach ($classMethods as $classMethod) {
85+
$hookMethodName = $this->resolveHookMethodName($classMethod);
86+
87+
if ($this->isRelevantHook($classMethod, $hookMethodName, $overriddenHookMethodNames)
88+
&& $this->containsAssertCall($classMethod)) {
89+
return true;
6690
}
6791

68-
$this->assertCallAnalyzer->resetNesting();
69-
if ($this->assertCallAnalyzer->containsAssertCall($classMethod)) {
70-
return true;
92+
// a hook overridden here hides the parent one, unless it calls it through "parent::"
93+
if ($hookMethodName !== null && ! $this->hasParentCall($classMethod, $hookMethodName)) {
94+
$overriddenHookMethodNames[] = $hookMethodName;
7195
}
7296
}
7397

74-
return false;
98+
$parentClassReflection = $classReflection->getParentClass();
99+
if (! $parentClassReflection instanceof ClassReflection) {
100+
return false;
101+
}
102+
103+
return $this->isParentClassAsserting($parentClassReflection, $overriddenHookMethodNames);
75104
}
76105

77-
private function isLifecycleMethod(ClassMethod $classMethod): bool
106+
/**
107+
* @param string[] $overriddenHookMethodNames
108+
*/
109+
private function isParentClassAsserting(ClassReflection $classReflection, array $overriddenHookMethodNames): bool
78110
{
79-
if ($this->nodeNameResolver->isNames($classMethod->name, self::HOOK_METHOD_NAMES)) {
111+
// do not analyze PHPUnit itself
112+
if ($classReflection->getName() === PHPUnitClassName::TEST_CASE) {
113+
return false;
114+
}
115+
116+
// the result of a parent only depends on the hooks overridden below it, so test classes share the parent chain
117+
$overriddenHookMethodNames = array_unique($overriddenHookMethodNames);
118+
sort($overriddenHookMethodNames);
119+
$cacheKey = $classReflection->getName() . '|' . implode(',', $overriddenHookMethodNames);
120+
121+
return $this->assertingByParentClassAndOverriddenHooks[$cacheKey] ??= $this->isClassLikeAsserting(
122+
$this->astResolver->resolveClassFromClassReflection($classReflection),
123+
$classReflection,
124+
$overriddenHookMethodNames
125+
);
126+
}
127+
128+
private function containsAssertCall(ClassMethod $classMethod): bool
129+
{
130+
// every hook gets its own nesting budget, a "parent::" hook call is evaluated again in the parent itself
131+
$this->assertCallAnalyzer->resetNesting();
132+
133+
return $this->assertCallAnalyzer->containsAssertCall($classMethod);
134+
}
135+
136+
private function hasParentCall(ClassMethod $classMethod, string $hookMethodName): bool
137+
{
138+
return $this->betterNodeFinder->findFirst(
139+
(array) $classMethod->stmts,
140+
fn (Node $node): bool => $node instanceof StaticCall
141+
&& $this->nodeNameResolver->isName($node->class, ObjectReference::PARENT)
142+
&& $this->nodeNameResolver->isName($node->name, $hookMethodName)
143+
) instanceof StaticCall;
144+
}
145+
146+
private function resolveHookMethodName(ClassMethod $classMethod): ?string
147+
{
148+
$methodName = $classMethod->name->toString();
149+
150+
foreach (self::HOOK_METHOD_NAMES as $hookMethodName) {
151+
if (strcasecmp($hookMethodName, $methodName) === 0) {
152+
return $hookMethodName;
153+
}
154+
}
155+
156+
return null;
157+
}
158+
159+
/**
160+
* @param string[] $overriddenHookMethodNames
161+
*/
162+
private function isRelevantHook(
163+
ClassMethod $classMethod,
164+
?string $hookMethodName,
165+
array $overriddenHookMethodNames
166+
): bool {
167+
if ($hookMethodName !== null && ! in_array($hookMethodName, $overriddenHookMethodNames, true)) {
80168
return true;
81169
}
82170

171+
// methods marked as hooks run too, also when overridden
83172
if ($classMethod->attrGroups !== []
84173
&& $this->phpAttributeAnalyzer->hasPhpAttributes($classMethod, self::HOOK_ATTRIBUTES)) {
85174
return true;

0 commit comments

Comments
 (0)