Skip to content

Commit 40f12c6

Browse files
committed
Add rule to convert phpunit methods to camel case
1 parent 54d6bf4 commit 40f12c6

File tree

4 files changed

+168
-0
lines changed

4 files changed

+168
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\PreferTestsWithCamelCaseRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class RenameTest extends TestCase
8+
{
9+
public function test_me()
10+
{
11+
}
12+
13+
public function test_this_is_a_long_test_case()
14+
{
15+
}
16+
}
17+
18+
?>
19+
-----
20+
<?php
21+
22+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\PreferTestsWithCamelCaseRector\Fixture;
23+
24+
use PHPUnit\Framework\TestCase;
25+
26+
final class RenameTest extends TestCase
27+
{
28+
public function testMe()
29+
{
30+
}
31+
32+
public function testThisIsALongTestCase()
33+
{
34+
}
35+
}
36+
37+
?>
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\ClassMethod\PreferTestsWithCamelCaseRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class PreferTestsWithCamelCaseRectorTest 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\CodeQuality\Rector\ClassMethod\PreferTestsWithCamelCaseRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(PreferTestsWithCamelCaseRector::class);
10+
};
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\CodeQuality\Rector\ClassMethod;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Stmt\ClassMethod;
9+
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
10+
use Rector\Rector\AbstractRector;
11+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
12+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
13+
14+
/**
15+
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\PreferTestsWithCamelCaseRector\PreferTestsWithCamelCaseRectorTest
16+
*/
17+
final class PreferTestsWithCamelCaseRector extends AbstractRector
18+
{
19+
public function __construct(
20+
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
21+
) {
22+
}
23+
24+
public function getRuleDefinition(): RuleDefinition
25+
{
26+
return new RuleDefinition('Changes PHPUnit tests methods to camel case', [
27+
new CodeSample(
28+
<<<'CODE_SAMPLE'
29+
use PHPUnit\Framework\TestCase;
30+
31+
final class SomeClass extends TestCase
32+
{
33+
public function test_something()
34+
{
35+
}
36+
}
37+
CODE_SAMPLE
38+
,
39+
<<<'CODE_SAMPLE'
40+
use PHPUnit\Framework\TestCase;
41+
42+
final class SomeClass extends TestCase
43+
{
44+
public function testSomething()
45+
{
46+
}
47+
}
48+
CODE_SAMPLE
49+
),
50+
]);
51+
}
52+
53+
/**
54+
* @return array<class-string<Node>>
55+
*/
56+
public function getNodeTypes(): array
57+
{
58+
return [ClassMethod::class];
59+
}
60+
61+
/**
62+
* @param ClassMethod $node
63+
*/
64+
public function refactor(Node $node): ?Node
65+
{
66+
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
67+
return null;
68+
}
69+
70+
if (! $this->testsNodeAnalyzer->isTestClassMethod($node)) {
71+
return null;
72+
}
73+
74+
$currentName = $node->name->toString();
75+
$newName = $this->toCamelCase($currentName);
76+
77+
if ($currentName === $newName) {
78+
return null;
79+
}
80+
81+
$node->name = new Node\Identifier($newName);
82+
83+
return $node;
84+
}
85+
86+
public function toCamelCase(string $value): string
87+
{
88+
$words = explode(' ', str_replace(['-', '_'], ' ', $value));
89+
$words = array_map(fn (string $word) => ucfirst($word), $words);
90+
91+
return lcfirst(implode($words));
92+
}
93+
}

0 commit comments

Comments
 (0)