Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAss

class SomeClass extends \PHPUnit\Framework\TestCase
{
/**
* @doesNotPerformAssertions
*/
#[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
public function test()
{
$nothing = 5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAss

class RemoveAddToAssertionCount extends \PHPUnit\Framework\TestCase
{
/**
* @doesNotPerformAssertions
*/
#[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
public function test()
{
$this->someMethodCall();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAss

class RemoveAddToAssertionCountInTryCatch extends \PHPUnit\Framework\TestCase
{
/**
* @doesNotPerformAssertions
*/
#[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
public function test()
{
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAss

class RemoveAddToAssertionCountWithComment extends \PHPUnit\Framework\TestCase
{
/**
* @doesNotPerformAssertions
*/
#[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
public function test()
{
$this->someMethodCall();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;

trait SomeNonAssertingTrait
{
public function testSomething()
{
$nothing = 5;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class TestInAnnotation extends \PHPUnit\Framework\TestCase
{
/**
* @test
* @doesNotPerformAssertions
*/
#[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
public function thisIsTest()
{
$nothing = 5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAss

class TestInAnnotationWithTestAttribute extends \PHPUnit\Framework\TestCase
{
/**
* @doesNotPerformAssertions
*/
#[\PHPUnit\Framework\Attributes\Test]
#[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
public function thisIsTest()
{
$nothing = 5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@
namespace Rector\PHPUnit\PHPUnit60\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Nop;
use PhpParser\NodeVisitor;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Rector\PHPUnit\Enum\PHPUnitAttribute;
use Rector\PHPUnit\Enum\PHPUnitClassName;
use Rector\PHPUnit\NodeAnalyzer\AssertCallAnalyzer;
use Rector\PHPUnit\NodeAnalyzer\MockedVariableAnalyzer;
Expand Down Expand Up @@ -51,13 +56,14 @@ public function __construct(
private readonly DocBlockUpdater $docBlockUpdater,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly ReflectionResolver $reflectionResolver,
private readonly ReflectionProvider $reflectionProvider,
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Tests without assertion will have @doesNotPerformAssertion',
'Tests without assertion will have #[DoesNotPerformAssertions] attribute, or @doesNotPerformAssertions annotation on PHPUnit below 10',
[
new CodeSample(
<<<'CODE_SAMPLE'
Expand All @@ -77,9 +83,7 @@ public function test()

class SomeClass extends TestCase
{
/**
* @doesNotPerformAssertions
*/
#[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
public function test()
{
$nothing = 5;
Expand Down Expand Up @@ -110,6 +114,15 @@ public function refactor(Node $node): ?Node

$this->removeAddToAssertionCountCalls($node);

// the attribute is available since PHPUnit 10, prefer it over the annotation
if ($this->reflectionProvider->hasClass(PHPUnitAttribute::DOES_NOT_PERFORM_ASSERTIONS)) {
$node->attrGroups[] = new AttributeGroup([
new Attribute(new FullyQualified(PHPUnitAttribute::DOES_NOT_PERFORM_ASSERTIONS)),
]);

return $node;
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
$phpDocInfo->addPhpDocTagNode(new PhpDocTagNode('@doesNotPerformAssertions', new GenericTagValueNode('')));

Expand All @@ -132,6 +145,11 @@ private function shouldSkipClassMethod(ClassMethod $classMethod): bool
return true;
}

// we have no idea how the trait is used, the using class can assert on its own
if ($this->isInTrait($classMethod)) {
return true;
}

// the parent test case asserts in its own integration methods
if ($this->isInTwigIntegrationTestCase($classMethod)) {
return true;
Expand Down Expand Up @@ -192,6 +210,16 @@ private function isAddToAssertionCountExpression(Node $node): bool
return $this->isName($methodCall->name, 'addToAssertionCount');
}

private function isInTrait(ClassMethod $classMethod): bool
{
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
if (! $classReflection instanceof ClassReflection) {
return false;
}

return $classReflection->isTrait();
}

private function isInTwigIntegrationTestCase(ClassMethod $classMethod): bool
{
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
Expand All @@ -212,7 +240,7 @@ private function hasAssertingAnnotationOrAttribute(ClassMethod $classMethod): bo

return $this->phpAttributeAnalyzer->hasPhpAttribute(
$classMethod,
'PHPUnit\Framework\Attributes\DoesNotPerformAssertions'
PHPUnitAttribute::DOES_NOT_PERFORM_ASSERTIONS
);
}
}
2 changes: 2 additions & 0 deletions src/Enum/PHPUnitAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ final class PHPUnitAttribute

public const string TEST = 'PHPUnit\Framework\Attributes\Test';

public const string DOES_NOT_PERFORM_ASSERTIONS = 'PHPUnit\Framework\Attributes\DoesNotPerformAssertions';

/**
* Since PHPUnit 12.5.2
* @see https://github.com/sebastianbergmann/phpunit/commit/24c208d6a340c3071f28a9b5cce02b9377adfd43
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ namespace Rector\PHPUnit\Tests\Issues\PHPUnit10DataProvider\Fixture;
final class SomeTest extends \PHPUnit\Framework\TestCase
{
#[\PHPUnit\Framework\Attributes\DataProvider('fooProvider')]
#[\PHPUnit\Framework\Attributes\Test]
#[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
#[\PHPUnit\Framework\Attributes\Test]
public function it_should_do_stuff(string $foo): void
{
}
Expand Down
Loading