Skip to content

[CodeQuality] Rule to enforce test method code style#668

Open
Xammie wants to merge 2 commits intorectorphp:mainfrom
Xammie:add-test-case-style-rules
Open

[CodeQuality] Rule to enforce test method code style#668
Xammie wants to merge 2 commits intorectorphp:mainfrom
Xammie:add-test-case-style-rules

Conversation

@Xammie
Copy link
Copy Markdown

@Xammie Xammie commented Apr 3, 2026

This adds two rules to convert the name of test methods to either CamelCase or snake_case.

// When using
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\PreferTestsWithSnakeCaseRector
$rectorConfig->rule(PreferTestsWithSnakeCaseRector::class);

// From
public function testItWillCreateNewProduct() {}
// To
public function test_it_will_create_new_product() {}

@Xammie Xammie changed the title [CodeQuality] Rule to enfore test method style [CodeQuality] Rule to enfore test method code style Apr 3, 2026
@Xammie Xammie changed the title [CodeQuality] Rule to enfore test method code style [CodeQuality] Rule to enforce test method code style Apr 3, 2026
@Xammie
Copy link
Copy Markdown
Author

Xammie commented Apr 30, 2026

@TomasVotruba this is ready to review :)

if (! $this->testsNodeAnalyzer->isTestClassMethod($node)) {
return null;
}

Copy link
Copy Markdown
Member

@samsonasik samsonasik Apr 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here ensure name is exists first before changing it, you can use Class_ instead of ClassMethod, and verify from $class->getMethod() or classReflection->hasMethod()

@samsonasik
Copy link
Copy Markdown
Member

for additional note: I think this should be your own custom rule for your specific use case :)

@Xammie
Copy link
Copy Markdown
Author

Xammie commented Apr 30, 2026

@samsonasik thank you for the review.

Regarding your additional note, do you mean this should not be part of rector php and instead be part of a separate package?

@samsonasik
Copy link
Copy Markdown
Member

samsonasik commented Apr 30, 2026

yeah, imo, this should be not part of rector repo, you can create your own custom rector rule for it as this is by preference

@Xammie
Copy link
Copy Markdown
Author

Xammie commented Apr 30, 2026

The reason I thought it would fit well here is because this is similar to PreferPHPUnitSelfCallRector and PreferPHPUnitThisCallRector in a way, because that is mostly a preference in code style.

Ofcourse if you still don't think this should be part of rector-phpunit then I completely understand and I will put it into a new package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants