Skip to content

Commit 9f1ac63

Browse files
committed
[PHPUnit60] Change AddDoesNotPerformAssertionToNonAssertingTestRector node from ClassMethod to Class_
1 parent 282fac4 commit 9f1ac63

2 files changed

Lines changed: 41 additions & 39 deletions

File tree

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

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,26 @@
99
use PhpParser\Node\AttributeGroup;
1010
use PhpParser\Node\Expr\MethodCall;
1111
use PhpParser\Node\Name\FullyQualified;
12+
use PhpParser\Node\Stmt\Class_;
1213
use PhpParser\Node\Stmt\ClassMethod;
1314
use PhpParser\Node\Stmt\Expression;
1415
use PhpParser\Node\Stmt\Nop;
1516
use PhpParser\NodeVisitor;
17+
use PHPStan\Analyser\Scope;
1618
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
1719
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
1820
use PHPStan\Reflection\ClassReflection;
1921
use PHPStan\Reflection\ReflectionProvider;
2022
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
2123
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
2224
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
25+
use Rector\PHPStan\ScopeFetcher;
2326
use Rector\PHPUnit\Enum\PHPUnitAttribute;
2427
use Rector\PHPUnit\Enum\PHPUnitClassName;
2528
use Rector\PHPUnit\NodeAnalyzer\AssertCallAnalyzer;
2629
use Rector\PHPUnit\NodeAnalyzer\MockedVariableAnalyzer;
2730
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
2831
use Rector\Rector\AbstractRector;
29-
use Rector\Reflection\ReflectionResolver;
3032
use Rector\RuleDoc\CodeSample\CodeSample;
3133
use Rector\RuleDoc\RuleDefinition;
3234
use Rector\VersionBonding\Contract\ComposerPackageConstraintInterface;
@@ -55,7 +57,6 @@ public function __construct(
5557
private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer,
5658
private readonly DocBlockUpdater $docBlockUpdater,
5759
private readonly PhpDocInfoFactory $phpDocInfoFactory,
58-
private readonly ReflectionResolver $reflectionResolver,
5960
private readonly ReflectionProvider $reflectionProvider,
6061
) {
6162
}
@@ -100,43 +101,64 @@ public function test()
100101
*/
101102
public function getNodeTypes(): array
102103
{
103-
return [ClassMethod::class];
104+
return [Class_::class];
104105
}
105106

106107
/**
107-
* @param ClassMethod $node
108+
* @param Class_ $node
108109
*/
109110
public function refactor(Node $node): ?Node
110111
{
111-
if ($this->shouldSkipClassMethod($node)) {
112+
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
112113
return null;
113114
}
114115

115-
$this->removeAddToAssertionCountCalls($node);
116+
$scope = ScopeFetcher::fetch($node);
116117

118+
// the parent test case asserts in its own integration methods
119+
if ($this->isInTwigIntegrationTestCase($scope)) {
120+
return null;
121+
}
122+
123+
$hasChanged = false;
124+
125+
foreach ($node->getMethods() as $classMethod) {
126+
if ($this->shouldSkipClassMethod($classMethod)) {
127+
continue;
128+
}
129+
130+
$this->removeAddToAssertionCountCalls($classMethod);
131+
$this->addDoesNotPerformAssertions($classMethod);
132+
133+
$hasChanged = true;
134+
}
135+
136+
if (! $hasChanged) {
137+
return null;
138+
}
139+
140+
return $node;
141+
}
142+
143+
private function addDoesNotPerformAssertions(ClassMethod $classMethod): void
144+
{
117145
// the attribute is available since PHPUnit 10, prefer it over the annotation
118146
if ($this->reflectionProvider->hasClass(PHPUnitAttribute::DOES_NOT_PERFORM_ASSERTIONS)) {
119-
$node->attrGroups[] = new AttributeGroup([
147+
$classMethod->attrGroups[] = new AttributeGroup([
120148
new Attribute(new FullyQualified(PHPUnitAttribute::DOES_NOT_PERFORM_ASSERTIONS)),
121149
]);
122150

123-
return $node;
151+
return;
124152
}
125153

126-
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
154+
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod);
127155
$phpDocInfo->addPhpDocTagNode(new PhpDocTagNode('@doesNotPerformAssertions', new GenericTagValueNode('')));
128156

129-
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
130-
131-
return $node;
157+
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($classMethod);
132158
}
133159

134160
private function shouldSkipClassMethod(ClassMethod $classMethod): bool
135161
{
136-
if (! $this->testsNodeAnalyzer->isInTestClass($classMethod)) {
137-
return true;
138-
}
139-
140162
if (! $this->testsNodeAnalyzer->isTestClassMethod($classMethod)) {
141163
return true;
142164
}
@@ -145,16 +167,6 @@ private function shouldSkipClassMethod(ClassMethod $classMethod): bool
145167
return true;
146168
}
147169

148-
// we have no idea how the trait is used, the using class can assert on its own
149-
if ($this->isInTrait($classMethod)) {
150-
return true;
151-
}
152-
153-
// the parent test case asserts in its own integration methods
154-
if ($this->isInTwigIntegrationTestCase($classMethod)) {
155-
return true;
156-
}
157-
158170
if ($this->hasAssertingAnnotationOrAttribute($classMethod)) {
159171
return true;
160172
}
@@ -210,19 +222,9 @@ private function isAddToAssertionCountExpression(Node $node): bool
210222
return $this->isName($methodCall->name, 'addToAssertionCount');
211223
}
212224

213-
private function isInTrait(ClassMethod $classMethod): bool
214-
{
215-
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
216-
if (! $classReflection instanceof ClassReflection) {
217-
return false;
218-
}
219-
220-
return $classReflection->isTrait();
221-
}
222-
223-
private function isInTwigIntegrationTestCase(ClassMethod $classMethod): bool
225+
private function isInTwigIntegrationTestCase(Scope $scope): bool
224226
{
225-
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
227+
$classReflection = $scope->getClassReflection();
226228
if (! $classReflection instanceof ClassReflection) {
227229
return false;
228230
}

‎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)