Skip to content

Commit 282fac4

Browse files
authored
[PHPUnit60] Follow parent::test() calls in AddDoesNotPerformAssertionToNonAssertingTestRector (#826)
1 parent c6c5ab2 commit 282fac4

6 files changed

Lines changed: 203 additions & 10 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
4+
5+
use Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Source\AbstractClassWithNonAssertingMethod;
6+
7+
class ParentTestCallWithoutAssert extends AbstractClassWithNonAssertingMethod
8+
{
9+
public function testSomething()
10+
{
11+
parent::testSomething();
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
20+
21+
use Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Source\AbstractClassWithNonAssertingMethod;
22+
23+
class ParentTestCallWithoutAssert extends AbstractClassWithNonAssertingMethod
24+
{
25+
#[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
26+
public function testSomething()
27+
{
28+
parent::testSomething();
29+
}
30+
}
31+
32+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SiblingHelperNonAsserting extends TestCase
8+
{
9+
public function testHelper()
10+
{
11+
$this->helper();
12+
}
13+
14+
private function helper()
15+
{
16+
$value = 1;
17+
}
18+
}
19+
20+
final class SiblingHelperAsserting extends TestCase
21+
{
22+
public function testHelper()
23+
{
24+
$this->helper();
25+
}
26+
27+
private function helper()
28+
{
29+
$this->assertTrue(true);
30+
}
31+
}
32+
33+
?>
34+
-----
35+
<?php
36+
37+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
38+
39+
use PHPUnit\Framework\TestCase;
40+
41+
final class SiblingHelperNonAsserting extends TestCase
42+
{
43+
#[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
44+
public function testHelper()
45+
{
46+
$this->helper();
47+
}
48+
49+
private function helper()
50+
{
51+
$value = 1;
52+
}
53+
}
54+
55+
final class SiblingHelperAsserting extends TestCase
56+
{
57+
public function testHelper()
58+
{
59+
$this->helper();
60+
}
61+
62+
private function helper()
63+
{
64+
$this->assertTrue(true);
65+
}
66+
}
67+
68+
?>
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\AbstractClassWithAssertingMethod;
6+
7+
class SkipParentTestCallWithAssert extends AbstractClassWithAssertingMethod
8+
{
9+
public function testSomething()
10+
{
11+
parent::testSomething();
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 AbstractClassWithAssertingMethod extends TestCase
10+
{
11+
public function testSomething()
12+
{
13+
$this->assertTrue(true);
14+
}
15+
}
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 AbstractClassWithNonAssertingMethod extends TestCase
10+
{
11+
public function testSomething()
12+
{
13+
$value = 1 + 1;
14+
}
15+
}

‎src/NodeAnalyzer/AssertCallAnalyzer.php‎

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,17 @@
1010
use PhpParser\Node\Expr\StaticCall;
1111
use PhpParser\Node\Identifier;
1212
use PhpParser\Node\Stmt\ClassMethod;
13+
use PHPStan\Analyser\Scope;
14+
use PHPStan\Reflection\ClassReflection;
1315
use PHPStan\Type\TypeWithClassName;
16+
use Rector\Enum\ObjectReference;
1417
use Rector\NodeNameResolver\NodeNameResolver;
18+
use Rector\NodeTypeResolver\Node\AttributeKey;
1519
use Rector\NodeTypeResolver\NodeTypeResolver;
1620
use Rector\PhpParser\AstResolver;
1721
use Rector\PhpParser\Node\BetterNodeFinder;
1822
use Rector\PhpParser\Printer\BetterStandardPrinter;
23+
use Rector\PHPUnit\Enum\PHPUnitClassName;
1924
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
2025

2126
final class AssertCallAnalyzer
@@ -74,7 +79,10 @@ public function containsAssertCall(ClassMethod $classMethod): bool
7479
return false;
7580
}
7681

77-
$cacheHash = md5($this->betterStandardPrinter->prettyPrint([$classMethod]));
82+
// the same body means a different method per class, e.g. "$this->helper()" or "parent::test()"
83+
$cacheHash = md5(
84+
$this->betterStandardPrinter->prettyPrint([$classMethod]) . $this->resolveOwnClassName($classMethod)
85+
);
7886

7987
if (isset($this->containsAssertCallByClassMethod[$cacheHash])) {
8088
return $this->containsAssertCallByClassMethod[$cacheHash];
@@ -83,7 +91,7 @@ public function containsAssertCall(ClassMethod $classMethod): bool
8391
// A. try "->assert" shallow search first for performance
8492
$hasDirectAssertOrMockCall = $this->hasDirectAssertOrMockCall($classMethod);
8593
if ($hasDirectAssertOrMockCall) {
86-
$this->containsAssertCallByClassMethod[$cacheHash] = $hasDirectAssertOrMockCall;
94+
$this->containsAssertCallByClassMethod[$cacheHash] = true;
8795
return true;
8896
}
8997

@@ -203,22 +211,64 @@ private function hasNestedAssertCall(ClassMethod $classMethod): bool
203211

204212
private function resolveClassMethodFromCall(StaticCall|MethodCall $call): ?ClassMethod
205213
{
206-
if ($call instanceof MethodCall) {
207-
$objectType = $this->nodeTypeResolver->getType($call->var);
208-
} else {
209-
// StaticCall
210-
$objectType = $this->nodeTypeResolver->getType($call->class);
214+
$methodName = $this->nodeNameResolver->getName($call->name);
215+
if ($methodName === null) {
216+
return null;
217+
}
218+
219+
$className = $this->resolveClassName($call);
220+
if ($className === null) {
221+
return null;
222+
}
223+
224+
return $this->astResolver->resolveClassMethod($className, $methodName);
225+
}
226+
227+
private function resolveOwnClassName(ClassMethod $classMethod): string
228+
{
229+
$classReflection = $this->resolveClassReflection($classMethod);
230+
if (! $classReflection instanceof ClassReflection) {
231+
return '';
232+
}
233+
234+
return $classReflection->getName();
235+
}
236+
237+
private function resolveClassName(StaticCall|MethodCall $call): ?string
238+
{
239+
if ($call instanceof StaticCall && $this->nodeNameResolver->isName($call->class, ObjectReference::PARENT)) {
240+
// "parent" resolves to a union of all ancestors in type resolver, so use the direct parent class instead
241+
$parentClassReflection = $this->resolveClassReflection($call)?->getParentClass();
242+
if (! $parentClassReflection instanceof ClassReflection) {
243+
return null;
244+
}
245+
246+
// PHPUnit itself has no asserting test methods, no need to parse its huge classes
247+
if ($parentClassReflection->getName() === PHPUnitClassName::TEST_CASE) {
248+
return null;
249+
}
250+
251+
return $parentClassReflection->getName();
211252
}
212253

254+
$objectType = $call instanceof MethodCall
255+
? $this->nodeTypeResolver->getType($call->var)
256+
: $this->nodeTypeResolver->getType($call->class);
257+
213258
if (! $objectType instanceof TypeWithClassName) {
214259
return null;
215260
}
216261

217-
$methodName = $this->nodeNameResolver->getName($call->name);
218-
if ($methodName === null) {
262+
return $objectType->getClassName();
263+
}
264+
265+
private function resolveClassReflection(Node $node): ?ClassReflection
266+
{
267+
$scope = $node->getAttribute(AttributeKey::SCOPE);
268+
if (! $scope instanceof Scope) {
219269
return null;
220270
}
221271

222-
return $this->astResolver->resolveClassMethod($objectType->getClassName(), $methodName);
272+
return $scope->getClassReflection();
223273
}
224274
}

0 commit comments

Comments
 (0)