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
@@ -0,0 +1,39 @@
<?php

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

class RemoveAddToAssertionCount extends \PHPUnit\Framework\TestCase
{
public function test()
{
$this->someMethodCall();
$this->addToAssertionCount(1);
}

private function someMethodCall()
{
}
}

?>
-----
<?php

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

class RemoveAddToAssertionCount extends \PHPUnit\Framework\TestCase
{
/**
* @doesNotPerformAssertions
*/
public function test()
{
$this->someMethodCall();
}

private function someMethodCall()
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

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

class RemoveAddToAssertionCountInTryCatch extends \PHPUnit\Framework\TestCase
{
public function test()
{
try {
$this->someMethodCall();
$this->addToAssertionCount(1);
} catch (\Throwable $throwable) {
}
}

private function someMethodCall()
{
}
}

?>
-----
<?php

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

class RemoveAddToAssertionCountInTryCatch extends \PHPUnit\Framework\TestCase
{
/**
* @doesNotPerformAssertions
*/
public function test()
{
try {
$this->someMethodCall();
} catch (\Throwable $throwable) {
}
}

private function someMethodCall()
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

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

class RemoveAddToAssertionCountWithComment extends \PHPUnit\Framework\TestCase
{
public function test()
{
$this->someMethodCall();

$this->addToAssertionCount(1); // Verify that no exception is thrown
}

private function someMethodCall()
{
}
}

?>
-----
<?php

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

class RemoveAddToAssertionCountWithComment extends \PHPUnit\Framework\TestCase
{
/**
* @doesNotPerformAssertions
*/
public function test()
{
$this->someMethodCall();
}

private function someMethodCall()
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
namespace Rector\PHPUnit\PHPUnit60\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
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;
Expand Down Expand Up @@ -104,6 +108,8 @@ public function refactor(Node $node): ?Node
return null;
}

$this->removeAddToAssertionCountCalls($node);

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

Expand Down Expand Up @@ -143,6 +149,49 @@ private function shouldSkipClassMethod(ClassMethod $classMethod): bool
return $this->mockedVariableAnalyzer->containsMockAsUsedVariable($classMethod);
}

/**
* The assertion count fakes an assertion, but the "@doesNotPerformAssertions" annotation makes it obsolete
*/
private function removeAddToAssertionCountCalls(ClassMethod $classMethod): void
{
$hasJustRemovedCall = false;

$this->traverseNodesWithCallable($classMethod, function (Node $node) use (&$hasJustRemovedCall): ?int {
// a comment on the same line as the removed call is parsed as a nop statement right behind it
if ($hasJustRemovedCall && $node instanceof Nop) {
return NodeVisitor::REMOVE_NODE;
}

$hasJustRemovedCall = false;

if (! $this->isAddToAssertionCountExpression($node)) {
return null;
}

$hasJustRemovedCall = true;

return NodeVisitor::REMOVE_NODE;
});
}

private function isAddToAssertionCountExpression(Node $node): bool
{
if (! $node instanceof Expression) {
return false;
}

if (! $node->expr instanceof MethodCall) {
return false;
}

$methodCall = $node->expr;
if (! $this->isName($methodCall->var, 'this')) {
return false;
}

return $this->isName($methodCall->name, 'addToAssertionCount');
}

private function isInTwigIntegrationTestCase(ClassMethod $classMethod): bool
{
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
Expand Down
Loading