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,33 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector\Fixture;

use PHPUnit\Framework\TestCase;

final class AtWithOneCountTest extends TestCase
{
public function test()
{
$mock->expects($this->at(1))
->method('foo')
->willReturn('1');
}
}
?>
-----
<?php

namespace Rector\PHPUnit\Tests\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector\Fixture;

use PHPUnit\Framework\TestCase;

final class AtWithOneCountTest extends TestCase
{
public function test()
{
$mock->expects($this->once())
->method('foo')
->willReturn('1');
}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector\Fixture;

use PHPUnit\Framework\TestCase;

final class AtWithTwoOrMoreCount extends TestCase
{
public function test()
{
$mock->expects($this->at(2))
->method('foo')
->willReturn('1');
}
}
?>
-----
<?php

namespace Rector\PHPUnit\Tests\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector\Fixture;

use PHPUnit\Framework\TestCase;

final class AtWithTwoOrMoreCount extends TestCase
{
public function test()
{
$mock->expects($this->exactly(2))
->method('foo')
->willReturn('1');
}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector\Fixture;

use PHPUnit\Framework\TestCase;

final class AtWithZeroCountTest extends TestCase
{
public function test()
{
$mock->expects($this->at(0))
->method('foo')
->willReturn('1');
}
}
?>
-----
<?php

namespace Rector\PHPUnit\Tests\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector\Fixture;

use PHPUnit\Framework\TestCase;

final class AtWithZeroCountTest extends TestCase
{
public function test()
{
$mock->expects($this->never())
->method('foo')
->willReturn('1');
}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector\Fixture;

use PHPUnit\Framework\TestCase;

final class MultipleAtWithDifferentCount extends TestCase
{
public function test()
{
$mock->expects($this->at(0))
->method('foo')
->willReturn('1');

$mock->expects($this->at(1))
->method('foo')
->willReturn('1');

$mock->expects($this->at(2))
->method('foo')
->willReturn('1');
$mock->expects($this->at(3))
->method('foo')
->willReturn('1');
}
}
?>
-----
<?php

namespace Rector\PHPUnit\Tests\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector\Fixture;

use PHPUnit\Framework\TestCase;

final class MultipleAtWithDifferentCount extends TestCase
{
public function test()
{
$mock->expects($this->never())
->method('foo')
->willReturn('1');

$mock->expects($this->once())
->method('foo')
->willReturn('1');

$mock->expects($this->exactly(2))
->method('foo')
->willReturn('1');
$mock->expects($this->exactly(3))
->method('foo')
->willReturn('1');
}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ReplaceAtMethodWithDesiredMatcherRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(ReplaceAtMethodWithDesiredMatcherRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\PHPUnit90\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\Int_;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\PHPUnit\Tests\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector\ReplaceAtMethodWithDesiredMatcherRectorTest
*/
final class ReplaceAtMethodWithDesiredMatcherRector extends AbstractRector
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Replace at() method call with desired matcher',
[
new CodeSample(
<<<'CODE_SAMPLE'
$mock->expects($this->at(0))
->method('foo')
->willReturn('1');
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$mock->expects($this->never())
->method('foo')
->willReturn('1');
CODE_SAMPLE
),
]
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}

/**
* @param MethodCall $node
*/
public function refactor(Node $node): null|MethodCall
{
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
}

if ($node->var instanceof MethodCall && $arg = $this->findAtMethodCall($node->var)) {
$this->replaceWithDesiredMatcher($arg);
Copy link
Member

Choose a reason for hiding this comment

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

return $node is needed after node changed, the flag like $hasChanged is needed for it.

Copy link
Member

Choose a reason for hiding this comment

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

}

return null;
}

private function findAtMethodCall(MethodCall $methodCall): ?Arg
{
foreach ($methodCall->getArgs() as $arg) {
if ($arg->value instanceof MethodCall &&
$arg->value->name instanceof Identifier &&
$arg->value->name->toString() === 'at'
) {
return $arg;
}
}

if ($methodCall->var instanceof MethodCall) {
$this->findAtMethodCall($methodCall->var);
}

return null;
}

private function replaceWithDesiredMatcher(Arg $arg): void
{
if (! $arg->value instanceof MethodCall) {
return;
}

foreach ($arg->value->getArgs() as $item) {
if ($item->value instanceof Int_) {
$count = $item->value->value;
}
}

if (! isset($count)) {
return;
}

if ($count === 0) {
$arg->value = new MethodCall($arg->value->var, 'never');
} elseif ($count === 1) {
$arg->value = new MethodCall($arg->value->var, 'once');
} elseif ($count > 1) {
$arg->value = new MethodCall($arg->value->var, 'exactly', [new Arg(new Int_($count))]);
}
}
}
Loading