-
Notifications
You must be signed in to change notification settings - Fork 67
[CodeQuality] Rule to enforce test method code style #668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TomasVotruba
merged 3 commits into
rectorphp:main
from
Xammie:add-test-case-style-rules
Jun 26, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
rules-tests/CodeQuality/Rector/Class_/PreferTestsWithCamelCaseRector/Fixture/rename.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferTestsWithCamelCaseRector\Fixture; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class RenameTest extends TestCase | ||
| { | ||
| public function test_me() | ||
| { | ||
| } | ||
|
|
||
| public function test_this_is_a_long_test_case() | ||
| { | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferTestsWithCamelCaseRector\Fixture; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class RenameTest extends TestCase | ||
| { | ||
| public function testMe() | ||
| { | ||
| } | ||
|
|
||
| public function testThisIsALongTestCase() | ||
| { | ||
| } | ||
| } | ||
|
|
||
| ?> |
16 changes: 16 additions & 0 deletions
16
...deQuality/Rector/Class_/PreferTestsWithCamelCaseRector/Fixture/skip_existing_name.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferTestsWithCamelCaseRector\Fixture; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class SkipExistingNameTest extends TestCase | ||
| { | ||
| public function test_me() | ||
| { | ||
| } | ||
|
|
||
| public function testMe() | ||
| { | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
...ality/Rector/Class_/PreferTestsWithCamelCaseRector/PreferTestsWithCamelCaseRectorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferTestsWithCamelCaseRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class PreferTestsWithCamelCaseRectorTest 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'; | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
...tests/CodeQuality/Rector/Class_/PreferTestsWithCamelCaseRector/config/configured_rule.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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\CodeQuality\Rector\Class_\PreferTestsWithCamelCaseRector; | ||
|
|
||
| return static function (RectorConfig $rectorConfig): void { | ||
| $rectorConfig->rule(PreferTestsWithCamelCaseRector::class); | ||
| }; |
37 changes: 37 additions & 0 deletions
37
rules-tests/CodeQuality/Rector/Class_/PreferTestsWithSnakeCaseRector/Fixture/rename.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferTestsWithSnakeCaseRector\Fixture; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class RenameTest extends TestCase | ||
| { | ||
| public function testMe() | ||
| { | ||
| } | ||
|
|
||
| public function testThisIsALongTestCase() | ||
| { | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferTestsWithSnakeCaseRector\Fixture; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class RenameTest extends TestCase | ||
| { | ||
| public function test_me() | ||
| { | ||
| } | ||
|
|
||
| public function test_this_is_a_long_test_case() | ||
| { | ||
| } | ||
| } | ||
|
|
||
| ?> |
16 changes: 16 additions & 0 deletions
16
...deQuality/Rector/Class_/PreferTestsWithSnakeCaseRector/Fixture/skip_existing_name.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferTestsWithSnakeCaseRector\Fixture; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class SkipExistingNameTest extends TestCase | ||
| { | ||
| public function testMe() | ||
| { | ||
| } | ||
|
|
||
| public function test_me() | ||
| { | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
...ality/Rector/Class_/PreferTestsWithSnakeCaseRector/PreferTestsWithSnakeCaseRectorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferTestsWithSnakeCaseRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class PreferTestsWithSnakeCaseRectorTest 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'; | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
...tests/CodeQuality/Rector/Class_/PreferTestsWithSnakeCaseRector/config/configured_rule.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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\CodeQuality\Rector\Class_\PreferTestsWithSnakeCaseRector; | ||
|
|
||
| return static function (RectorConfig $rectorConfig): void { | ||
| $rectorConfig->rule(PreferTestsWithSnakeCaseRector::class); | ||
| }; |
107 changes: 107 additions & 0 deletions
107
rules/CodeQuality/Rector/Class_/PreferTestsWithCamelCaseRector.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\PHPUnit\CodeQuality\Rector\Class_; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Stmt\Class_; | ||
| use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; | ||
| use Rector\Rector\AbstractRector; | ||
| use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
| use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
|
||
| /** | ||
| * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferTestsWithCamelCaseRector\PreferTestsWithCamelCaseRectorTest | ||
| */ | ||
| final class PreferTestsWithCamelCaseRector extends AbstractRector | ||
| { | ||
| public function __construct( | ||
| private readonly TestsNodeAnalyzer $testsNodeAnalyzer, | ||
| ) { | ||
| } | ||
|
|
||
| public function getRuleDefinition(): RuleDefinition | ||
| { | ||
| return new RuleDefinition('Changes PHPUnit test methods to camel case', [ | ||
| new CodeSample( | ||
| <<<'CODE_SAMPLE' | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class SomeClass extends TestCase | ||
| { | ||
| public function test_something() | ||
| { | ||
| } | ||
| } | ||
| CODE_SAMPLE | ||
| , | ||
| <<<'CODE_SAMPLE' | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class SomeClass extends TestCase | ||
| { | ||
| public function testSomething() | ||
| { | ||
| } | ||
| } | ||
| CODE_SAMPLE | ||
| ), | ||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<class-string<Node>> | ||
| */ | ||
| public function getNodeTypes(): array | ||
| { | ||
| return [Class_::class]; | ||
| } | ||
|
|
||
| /** | ||
| * @param Class_ $node | ||
| */ | ||
| public function refactor(Node $node): ?Node | ||
| { | ||
| if (! $this->testsNodeAnalyzer->isInTestClass($node)) { | ||
| return null; | ||
| } | ||
|
|
||
| $hasChanged = false; | ||
|
|
||
| foreach ($node->getMethods() as $classMethod) { | ||
| if (! $this->testsNodeAnalyzer->isTestClassMethod($classMethod)) { | ||
| continue; | ||
| } | ||
|
|
||
| $currentName = $classMethod->name->toString(); | ||
| $newName = $this->toCamelCase($currentName); | ||
|
|
||
| if ($currentName === $newName) { | ||
| continue; | ||
| } | ||
|
|
||
| // avoid name collision with an existing method | ||
| if ($node->getMethod($newName) instanceof Node) { | ||
| continue; | ||
| } | ||
|
|
||
| $classMethod->name = new Node\Identifier($newName); | ||
| $hasChanged = true; | ||
| } | ||
|
|
||
| if ($hasChanged === false) { | ||
| return null; | ||
| } | ||
|
|
||
| return $node; | ||
| } | ||
|
|
||
| private function toCamelCase(string $value): string | ||
| { | ||
| $words = explode(' ', str_replace(['-', '_'], ' ', $value)); | ||
| $words = array_map(fn (string $word): string => ucfirst($word), $words); | ||
|
|
||
| return lcfirst(implode($words)); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.