Skip to content

Commit ee4dc09

Browse files
XammieTomasVotruba
andauthored
[CodeQuality] Rule to enforce test method code style (#668)
* Add rule to convert phpunit methods to snake case * Add rule to convert phpunit methods to camel case * address review: use Class_ node type, skip rename on existing method name collision --------- Co-authored-by: Tomas Votruba <tomas.vot@gmail.com>
1 parent f7c61b8 commit ee4dc09

10 files changed

Lines changed: 400 additions & 0 deletions

File tree

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\Class_\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\Class_\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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferTestsWithCamelCaseRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipExistingNameTest extends TestCase
8+
{
9+
public function test_me()
10+
{
11+
}
12+
13+
public function testMe()
14+
{
15+
}
16+
}
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\Class_\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\Class_\PreferTestsWithCamelCaseRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(PreferTestsWithCamelCaseRector::class);
10+
};
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\Class_\PreferTestsWithSnakeCaseRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class RenameTest extends TestCase
8+
{
9+
public function testMe()
10+
{
11+
}
12+
13+
public function testThisIsALongTestCase()
14+
{
15+
}
16+
}
17+
18+
?>
19+
-----
20+
<?php
21+
22+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferTestsWithSnakeCaseRector\Fixture;
23+
24+
use PHPUnit\Framework\TestCase;
25+
26+
final class RenameTest extends TestCase
27+
{
28+
public function test_me()
29+
{
30+
}
31+
32+
public function test_this_is_a_long_test_case()
33+
{
34+
}
35+
}
36+
37+
?>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferTestsWithSnakeCaseRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipExistingNameTest extends TestCase
8+
{
9+
public function testMe()
10+
{
11+
}
12+
13+
public function test_me()
14+
{
15+
}
16+
}
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\Class_\PreferTestsWithSnakeCaseRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class PreferTestsWithSnakeCaseRectorTest 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\Class_\PreferTestsWithSnakeCaseRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(PreferTestsWithSnakeCaseRector::class);
10+
};
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\Class_;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Stmt\Class_;
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\Class_\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 test 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 [Class_::class];
59+
}
60+
61+
/**
62+
* @param Class_ $node
63+
*/
64+
public function refactor(Node $node): ?Node
65+
{
66+
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
67+
return null;
68+
}
69+
70+
$hasChanged = false;
71+
72+
foreach ($node->getMethods() as $classMethod) {
73+
if (! $this->testsNodeAnalyzer->isTestClassMethod($classMethod)) {
74+
continue;
75+
}
76+
77+
$currentName = $classMethod->name->toString();
78+
$newName = $this->toCamelCase($currentName);
79+
80+
if ($currentName === $newName) {
81+
continue;
82+
}
83+
84+
// avoid name collision with an existing method
85+
if ($node->getMethod($newName) instanceof Node) {
86+
continue;
87+
}
88+
89+
$classMethod->name = new Node\Identifier($newName);
90+
$hasChanged = true;
91+
}
92+
93+
if ($hasChanged === false) {
94+
return null;
95+
}
96+
97+
return $node;
98+
}
99+
100+
private function toCamelCase(string $value): string
101+
{
102+
$words = explode(' ', str_replace(['-', '_'], ' ', $value));
103+
$words = array_map(fn (string $word): string => ucfirst($word), $words);
104+
105+
return lcfirst(implode($words));
106+
}
107+
}

0 commit comments

Comments
 (0)