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
22 changes: 22 additions & 0 deletions src/Rector/AbstractPHPUnitRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);

namespace Rector\Rector;

use Nette\Utils\Strings;
use PhpParser\Node;
use Rector\Node\Attribute;

abstract class AbstractPHPUnitRector extends AbstractRector
{
/**
* Check if the file is a PHPUnit TestCase. By default, it should end with "Test", as it is the standard.
*
* @see https://phpunit.de/getting-started-with-phpunit.html
*/
protected function isInTestClass(Node $node): bool
{
$className = $node->getAttribute(Attribute::CLASS_NAME);

return Strings::endsWith((string) $className, 'Test');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add comment there, that this is only approximate assumption?
It might be important in some edge cases, where these Rectorswould fail, so it's easier to debug.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also the note that from PR would be great: https://phpunit.de/getting-started-with-phpunit.html

}
}
24 changes: 7 additions & 17 deletions src/Rector/Contrib/PHPUnit/DelegateExceptionArgumentsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use PhpParser\Node\Identifier;
use Rector\Node\MethodCallNodeFactory;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\Rector\AbstractPHPUnitRector;

/**
* Before:
Expand All @@ -19,7 +19,7 @@
* - $this->expectExceptionMessage('Message');
* - $this->expectExceptionCode('CODE');
*/
final class DelegateExceptionArgumentsRector extends AbstractRector
final class DelegateExceptionArgumentsRector extends AbstractPHPUnitRector
{
/**
* @var string[]
Expand All @@ -29,16 +29,6 @@ final class DelegateExceptionArgumentsRector extends AbstractRector
'setExpectedExceptionRegExp' => 'expectExceptionMessageRegExp',
];

/**
* @var string[]
*/
private $phpUnitTestCaseClasses = [
// PHPUnit 5-
'PHPUnit_Framework_TestCase',
// PHPUnit 6+
'PHPUnit\Framework\TestCase',
];

/**
* @var MethodCallAnalyzer
*/
Expand All @@ -57,11 +47,11 @@ public function __construct(MethodCallAnalyzer $methodCallAnalyzer, MethodCallNo

public function isCandidate(Node $node): bool
{
if (! $this->methodCallAnalyzer->isTypesAndMethods(
$node,
$this->phpUnitTestCaseClasses,
array_keys($this->oldToNewMethod)
)) {
if (! $this->isInTestClass($node)) {
return false;
}

if (! $this->methodCallAnalyzer->isMethods($node, array_keys($this->oldToNewMethod))) {
return false;
}

Expand Down
8 changes: 6 additions & 2 deletions src/Rector/Contrib/PHPUnit/ExceptionAnnotationRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use Rector\Node\MethodCallNodeFactory;
use Rector\Rector\AbstractRector;
use Rector\Rector\AbstractPHPUnitRector;
use Rector\ReflectionDocBlock\NodeAnalyzer\DocBlockAnalyzer;

/**
Expand All @@ -19,7 +19,7 @@
* - $this->expectException('Exception');
* - $this->expectExceptionMessage('Message');
*/
final class ExceptionAnnotationRector extends AbstractRector
final class ExceptionAnnotationRector extends AbstractPHPUnitRector
{
/**
* In reversed order, which they should be called in code.
Expand Down Expand Up @@ -51,6 +51,10 @@ public function __construct(MethodCallNodeFactory $methodCallNodeFactory, DocBlo

public function isCandidate(Node $node): bool
{
if (! $this->isInTestClass($node)) {
return false;
}

if (! $node instanceof ClassMethod) {
return false;
}
Expand Down
13 changes: 2 additions & 11 deletions src/Rector/Contrib/PHPUnit/GetMockRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

namespace Rector\Rector\Contrib\PHPUnit;

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use Rector\Node\Attribute;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeChanger\IdentifierRenamer;
use Rector\Rector\AbstractRector;
use Rector\Rector\AbstractPHPUnitRector;

/**
* Before:
Expand All @@ -18,7 +16,7 @@
* After:
* - $this->createMock('Class')
*/
final class GetMockRector extends AbstractRector
final class GetMockRector extends AbstractPHPUnitRector
{
/**
* @var MethodCallAnalyzer
Expand Down Expand Up @@ -62,11 +60,4 @@ public function refactor(Node $methodCallNode): ?Node

return $methodCallNode;
}

private function isInTestClass(Node $node): bool
{
$className = $node->getAttribute(Attribute::CLASS_NAME);

return Strings::endsWith((string) $className, 'Test');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use PhpParser\Node\Scalar\String_;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeChanger\IdentifierRenamer;
use Rector\Rector\AbstractRector;
use Rector\Rector\AbstractPHPUnitRector;

/**
* Before:
Expand All @@ -28,7 +28,7 @@
* - $this->assert{function}($value, $anything, 'message');
* - $this->assertNot{function}($value, $anything, 'message');
*/
final class AssertCompareToSpecificMethodRector extends AbstractRector
final class AssertCompareToSpecificMethodRector extends AbstractPHPUnitRector
{
/**
* @var string[][]|false[][]
Expand Down Expand Up @@ -63,9 +63,12 @@ public function __construct(MethodCallAnalyzer $methodCallAnalyzer, IdentifierRe

public function isCandidate(Node $node): bool
{
if (! $this->methodCallAnalyzer->isTypesAndMethods(
if (! $this->isInTestClass($node)) {
return false;
}

if (! $this->methodCallAnalyzer->isMethods(
$node,
['PHPUnit\Framework\TestCase', 'PHPUnit_Framework_TestCase'],
['assertSame', 'assertNotSame', 'assertEquals', 'assertNotEquals']
)) {
return false;
Expand All @@ -90,13 +93,9 @@ public function isCandidate(Node $node): bool
}

$methodName = $secondArgumentValue->name->toString();
if (! isset($this->defaultOldToNewMethods[$methodName])) {
return false;
}

$this->activeFuncCallName = $methodName;

return true;
return isset($this->defaultOldToNewMethods[$methodName]);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use PhpParser\Node\Identifier;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeChanger\IdentifierRenamer;
use Rector\Rector\AbstractRector;
use Rector\Rector\AbstractPHPUnitRector;

/**
* - Before:
Expand All @@ -20,7 +20,7 @@
* - $this->assertSame($bar, $foo, 'message');
* - $this->assertLessThanOrEqual($bar, $foo, 'message');
*/
final class AssertComparisonToSpecificMethodRector extends AbstractRector
final class AssertComparisonToSpecificMethodRector extends AbstractPHPUnitRector
{
/**
* @var string[][]|false[][]
Expand Down Expand Up @@ -60,11 +60,11 @@ public function __construct(MethodCallAnalyzer $methodCallAnalyzer, IdentifierRe

public function isCandidate(Node $node): bool
{
if (! $this->methodCallAnalyzer->isTypesAndMethods(
$node,
['PHPUnit\Framework\TestCase', 'PHPUnit_Framework_TestCase'],
['assertTrue', 'assertFalse']
)) {
if (! $this->isInTestClass($node)) {
return false;
}

if (! $this->methodCallAnalyzer->isMethods($node, ['assertTrue', 'assertFalse'])) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace Rector\Rector\Contrib\PHPUnit\SpecificMethod;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeChanger\IdentifierRenamer;
use Rector\Rector\AbstractRector;
use Rector\Rector\AbstractPHPUnitRector;

/**
* Before:
Expand All @@ -19,7 +21,7 @@
* - $this->assertNotContains('foo', $anything, 'message');
* - $this->assertContains('foo', $anything, 'message');
*/
final class AssertFalseStrposToContainsRector extends AbstractRector
final class AssertFalseStrposToContainsRector extends AbstractPHPUnitRector
{
/**
* @var MethodCallAnalyzer
Expand All @@ -39,16 +41,16 @@ public function __construct(MethodCallAnalyzer $methodCallAnalyzer, IdentifierRe

public function isCandidate(Node $node): bool
{
if (! $this->methodCallAnalyzer->isTypesAndMethods(
$node,
['PHPUnit\Framework\TestCase', 'PHPUnit_Framework_TestCase'],
['assertFalse', 'assertNotFalse']
)) {
if (! $this->isInTestClass($node)) {
return false;
}

if (! $this->methodCallAnalyzer->isMethods($node, ['assertFalse', 'assertNotFalse'])) {
return false;
}

$firstArgumentValue = $node->args[0]->value;
if (! $firstArgumentValue instanceof FuncCall) {
if (! $this->isNamedFunction($firstArgumentValue)) {
return false;
}

Expand Down Expand Up @@ -94,4 +96,14 @@ private function renameMethod(MethodCall $methodCallNode): void
$this->identifierRenamer->renameNode($methodCallNode, 'assertContains');
}
}

private function isNamedFunction(Expr $node): bool
{
if (! $node instanceof FuncCall) {
return false;
}

$functionName = $node->name;
return $functionName instanceof Name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Rector\Node\NodeFactory;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeChanger\IdentifierRenamer;
use Rector\Rector\AbstractRector;
use Rector\Rector\AbstractPHPUnitRector;

/**
* - Before:
Expand All @@ -20,7 +20,7 @@
* - $this->assertInstanceOf(Foo::class, $foo, 'message');
* - $this->assertNotInstanceOf(Foo::class, $foo, 'message');
*/
final class AssertInstanceOfComparisonRector extends AbstractRector
final class AssertInstanceOfComparisonRector extends AbstractPHPUnitRector
{
/**
* @var MethodCallAnalyzer
Expand Down Expand Up @@ -49,11 +49,11 @@ public function __construct(

public function isCandidate(Node $node): bool
{
if (! $this->methodCallAnalyzer->isTypesAndMethods(
$node,
['PHPUnit\Framework\TestCase', 'PHPUnit_Framework_TestCase'],
['assertTrue', 'assertFalse']
)) {
if (! $this->isInTestClass($node)) {
return false;
}

if (! $this->methodCallAnalyzer->isMethods($node, ['assertTrue', 'assertFalse'])) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Rector\Node\NodeFactory;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeChanger\IdentifierRenamer;
use Rector\Rector\AbstractRector;
use Rector\Rector\AbstractPHPUnitRector;

/**
* Before:
Expand All @@ -21,7 +21,7 @@
* - $this->assertObjectHasAttribute('foo', $anything);
* - $this->assertArrayNotHasKey('foo', $anything, 'message');
*/
final class AssertIssetToSpecificMethodRector extends AbstractRector
final class AssertIssetToSpecificMethodRector extends AbstractPHPUnitRector
{
/**
* @var MethodCallAnalyzer
Expand Down Expand Up @@ -50,11 +50,11 @@ public function __construct(

public function isCandidate(Node $node): bool
{
if (! $this->methodCallAnalyzer->isTypesAndMethods(
$node,
['PHPUnit\Framework\TestCase', 'PHPUnit_Framework_TestCase'],
['assertTrue', 'assertFalse']
)) {
if (! $this->isInTestClass($node)) {
return false;
}

if (! $this->methodCallAnalyzer->isMethods($node, ['assertTrue', 'assertFalse'])) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use PhpParser\Node\Identifier;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeChanger\IdentifierRenamer;
use Rector\Rector\AbstractRector;
use Rector\Rector\AbstractPHPUnitRector;

/**
* Before:
Expand All @@ -19,7 +19,7 @@
* - $this->assertNull($anything);
* - $this->assertNotFalse($anything);
*/
final class AssertSameBoolNullToSpecificMethodRector extends AbstractRector
final class AssertSameBoolNullToSpecificMethodRector extends AbstractPHPUnitRector
{
/**
* @var string[][]|false[][]
Expand Down Expand Up @@ -53,11 +53,11 @@ public function __construct(MethodCallAnalyzer $methodCallAnalyzer, IdentifierRe

public function isCandidate(Node $node): bool
{
if (! $this->methodCallAnalyzer->isTypesAndMethods(
$node,
['PHPUnit\Framework\TestCase', 'PHPUnit_Framework_TestCase'],
['assertSame', 'assertNotSame']
)) {
if (! $this->isInTestClass($node)) {
return false;
}

if (! $this->methodCallAnalyzer->isMethods($node, ['assertSame', 'assertNotSame'])) {
return false;
}

Expand Down
Loading