Skip to content

Commit e991afe

Browse files
authored
Add the new ReplaceAtMethodWithDesiredMatcherRector rule (#454)
* Create ReplaceAtMethodWithDesiredMatcherRector.php * Add tests;
1 parent 61f5874 commit e991afe

File tree

7 files changed

+307
-0
lines changed

7 files changed

+307
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class AtWithOneCountTest extends TestCase
8+
{
9+
public function test()
10+
{
11+
$mock->expects($this->at(1))
12+
->method('foo')
13+
->willReturn('1');
14+
}
15+
}
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\PHPUnit\Tests\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector\Fixture;
21+
22+
use PHPUnit\Framework\TestCase;
23+
24+
final class AtWithOneCountTest extends TestCase
25+
{
26+
public function test()
27+
{
28+
$mock->expects($this->once())
29+
->method('foo')
30+
->willReturn('1');
31+
}
32+
}
33+
?>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class AtWithTwoOrMoreCount extends TestCase
8+
{
9+
public function test()
10+
{
11+
$mock->expects($this->at(2))
12+
->method('foo')
13+
->willReturn('1');
14+
}
15+
}
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\PHPUnit\Tests\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector\Fixture;
21+
22+
use PHPUnit\Framework\TestCase;
23+
24+
final class AtWithTwoOrMoreCount extends TestCase
25+
{
26+
public function test()
27+
{
28+
$mock->expects($this->exactly(2))
29+
->method('foo')
30+
->willReturn('1');
31+
}
32+
}
33+
?>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class AtWithZeroCountTest extends TestCase
8+
{
9+
public function test()
10+
{
11+
$mock->expects($this->at(0))
12+
->method('foo')
13+
->willReturn('1');
14+
}
15+
}
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\PHPUnit\Tests\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector\Fixture;
21+
22+
use PHPUnit\Framework\TestCase;
23+
24+
final class AtWithZeroCountTest extends TestCase
25+
{
26+
public function test()
27+
{
28+
$mock->expects($this->never())
29+
->method('foo')
30+
->willReturn('1');
31+
}
32+
}
33+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class MultipleAtWithDifferentCount extends TestCase
8+
{
9+
public function test()
10+
{
11+
$mock->expects($this->at(0))
12+
->method('foo')
13+
->willReturn('1');
14+
15+
$mock->expects($this->at(1))
16+
->method('foo')
17+
->willReturn('1');
18+
19+
$mock->expects($this->at(2))
20+
->method('foo')
21+
->willReturn('1');
22+
$mock->expects($this->at(3))
23+
->method('foo')
24+
->willReturn('1');
25+
}
26+
}
27+
?>
28+
-----
29+
<?php
30+
31+
namespace Rector\PHPUnit\Tests\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector\Fixture;
32+
33+
use PHPUnit\Framework\TestCase;
34+
35+
final class MultipleAtWithDifferentCount extends TestCase
36+
{
37+
public function test()
38+
{
39+
$mock->expects($this->never())
40+
->method('foo')
41+
->willReturn('1');
42+
43+
$mock->expects($this->once())
44+
->method('foo')
45+
->willReturn('1');
46+
47+
$mock->expects($this->exactly(2))
48+
->method('foo')
49+
->willReturn('1');
50+
$mock->expects($this->exactly(3))
51+
->method('foo')
52+
->willReturn('1');
53+
}
54+
}
55+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class ReplaceAtMethodWithDesiredMatcherRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\PHPUnit\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(ReplaceAtMethodWithDesiredMatcherRector::class);
10+
};
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\PHPUnit90\Rector\MethodCall;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Arg;
9+
use PhpParser\Node\Expr\MethodCall;
10+
use PhpParser\Node\Identifier;
11+
use PhpParser\Node\Scalar\Int_;
12+
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
13+
use Rector\Rector\AbstractRector;
14+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
15+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
16+
17+
/**
18+
* @see \Rector\PHPUnit\Tests\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector\ReplaceAtMethodWithDesiredMatcherRectorTest
19+
*/
20+
final class ReplaceAtMethodWithDesiredMatcherRector extends AbstractRector
21+
{
22+
public function __construct(
23+
private readonly TestsNodeAnalyzer $testsNodeAnalyzer
24+
) {
25+
}
26+
27+
public function getRuleDefinition(): RuleDefinition
28+
{
29+
return new RuleDefinition(
30+
'Replace at() method call with desired matcher',
31+
[
32+
new CodeSample(
33+
<<<'CODE_SAMPLE'
34+
$mock->expects($this->at(0))
35+
->method('foo')
36+
->willReturn('1');
37+
CODE_SAMPLE
38+
,
39+
<<<'CODE_SAMPLE'
40+
$mock->expects($this->never())
41+
->method('foo')
42+
->willReturn('1');
43+
CODE_SAMPLE
44+
),
45+
]
46+
);
47+
}
48+
49+
/**
50+
* @return array<class-string<Node>>
51+
*/
52+
public function getNodeTypes(): array
53+
{
54+
return [MethodCall::class];
55+
}
56+
57+
/**
58+
* @param MethodCall $node
59+
*/
60+
public function refactor(Node $node): null|MethodCall
61+
{
62+
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
63+
return null;
64+
}
65+
66+
if ($node->var instanceof MethodCall && $arg = $this->findAtMethodCall($node->var)) {
67+
$this->replaceWithDesiredMatcher($arg);
68+
}
69+
70+
return null;
71+
}
72+
73+
private function findAtMethodCall(MethodCall $methodCall): ?Arg
74+
{
75+
foreach ($methodCall->getArgs() as $arg) {
76+
if ($arg->value instanceof MethodCall &&
77+
$arg->value->name instanceof Identifier &&
78+
$arg->value->name->toString() === 'at'
79+
) {
80+
return $arg;
81+
}
82+
}
83+
84+
if ($methodCall->var instanceof MethodCall) {
85+
$this->findAtMethodCall($methodCall->var);
86+
}
87+
88+
return null;
89+
}
90+
91+
private function replaceWithDesiredMatcher(Arg $arg): void
92+
{
93+
if (! $arg->value instanceof MethodCall) {
94+
return;
95+
}
96+
97+
foreach ($arg->value->getArgs() as $item) {
98+
if ($item->value instanceof Int_) {
99+
$count = $item->value->value;
100+
}
101+
}
102+
103+
if (! isset($count)) {
104+
return;
105+
}
106+
107+
if ($count === 0) {
108+
$arg->value = new MethodCall($arg->value->var, 'never');
109+
} elseif ($count === 1) {
110+
$arg->value = new MethodCall($arg->value->var, 'once');
111+
} elseif ($count > 1) {
112+
$arg->value = new MethodCall($arg->value->var, 'exactly', [new Arg(new Int_($count))]);
113+
}
114+
}
115+
}

0 commit comments

Comments
 (0)