Skip to content

Commit 6832d24

Browse files
committed
[code-quality] Add AssertThatToDirectAssertRector
1 parent 7ca99bc commit 6832d24

File tree

5 files changed

+206
-0
lines changed

5 files changed

+206
-0
lines changed
Lines changed: 28 additions & 0 deletions
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\CodeQuality\Rector\MethodCall\AssertThatToDirectAssertRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class AssertThatToDirectAssertRectorTest 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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertThatToDirectAssertRector\Fixture;
6+
7+
final class SimpleAssertTthat extends \PHPUnit\Framework\TestCase
8+
{
9+
public function test()
10+
{
11+
$this->assertThat('value', $this->equalTo('expectedValue'));
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
declare(strict_types=1);
20+
21+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertThatToDirectAssertRector\Fixture;
22+
23+
final class SimpleAssertTthat extends \PHPUnit\Framework\TestCase
24+
{
25+
public function test()
26+
{
27+
$this->assertEquals('expectedValue', 'value');
28+
}
29+
}
30+
31+
?>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertThatToDirectAssertRector\Fixture;
6+
7+
final class ThatAssertTrue extends \PHPUnit\Framework\TestCase
8+
{
9+
public function test()
10+
{
11+
$this->assertThat('value', $this->isTrue());
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
declare(strict_types=1);
20+
21+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertThatToDirectAssertRector\Fixture;
22+
23+
final class ThatAssertTrue extends \PHPUnit\Framework\TestCase
24+
{
25+
public function test()
26+
{
27+
$this->assertTrue('value');
28+
}
29+
}
30+
31+
?>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\AssertThatToDirectAssertRector;
7+
8+
return RectorConfig::configure()
9+
->withRules([AssertThatToDirectAssertRector::class]);
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\MethodCall;
9+
use PhpParser\Node\Identifier;
10+
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
11+
use Rector\Rector\AbstractRector;
12+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
13+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
14+
15+
/**
16+
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertThatToDirectAssertRector\AssertThatToDirectAssertRectorTest
17+
*/
18+
final class AssertThatToDirectAssertRector extends AbstractRector
19+
{
20+
public function __construct(
21+
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
22+
) {
23+
}
24+
25+
public function getRuleDefinition(): RuleDefinition
26+
{
27+
return new RuleDefinition(
28+
'Change $this->assertThat($value, $this->*()) to direct $this->assert*() method',
29+
[
30+
new CodeSample(
31+
<<<'CODE_SAMPLE'
32+
use PHPUnit\Framework\TestCase;
33+
34+
final class SomeTest extends TestCase
35+
{
36+
public function test()
37+
{
38+
$this->assertThat($value, $this->isTrue());
39+
}
40+
}
41+
CODE_SAMPLE
42+
43+
,
44+
<<<'CODE_SAMPLE'
45+
use PHPUnit\Framework\TestCase;
46+
47+
final class SomeTest extends TestCase
48+
{
49+
public function test()
50+
{
51+
$this->assertTrue($value);
52+
}
53+
}
54+
CODE_SAMPLE
55+
),
56+
]
57+
);
58+
}
59+
60+
/**
61+
* @return array<class-string<Node>>
62+
*/
63+
public function getNodeTypes(): array
64+
{
65+
return [MethodCall::class];
66+
}
67+
68+
/**
69+
* @param MethodCall $node
70+
*/
71+
public function refactor(Node $node): ?Node
72+
{
73+
if ($node->isFirstClassCallable()) {
74+
return null;
75+
}
76+
77+
if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames($node, ['assertThat'])) {
78+
return null;
79+
}
80+
81+
$secondArg = $node->getArgs()[1];
82+
if (! $secondArg->value instanceof MethodCall) {
83+
return null;
84+
}
85+
86+
$exactAssertMethodCall = $secondArg->value;
87+
$exactAssertName = $this->getName($exactAssertMethodCall->name);
88+
89+
if ($exactAssertName === 'equalTo') {
90+
$node->name = new Identifier('assertEquals');
91+
92+
$node->args[1] = $node->args[0];
93+
$node->args[0] = $exactAssertMethodCall->args[0];
94+
95+
return $node;
96+
}
97+
98+
if ($exactAssertName === 'isTrue') {
99+
$node->name = new Identifier('assertTrue');
100+
unset($node->args[1]);
101+
102+
return $node;
103+
}
104+
105+
return null;
106+
}
107+
}

0 commit comments

Comments
 (0)