Skip to content

Rule to enforce class naming #214

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

Closed
cs278 opened this issue Nov 26, 2024 · 8 comments
Closed

Rule to enforce class naming #214

cs278 opened this issue Nov 26, 2024 · 8 comments

Comments

@cs278
Copy link

cs278 commented Nov 26, 2024

Generally speaking PHPUnit looks for tests in files named *Test.php, which conventionally means the classes should be named *Test. Additionally I believe it is recommended/required (I cannot find docs for this) that "tests" designed to be extended should be named *TestCase.

I propose adding rule to this extension which enforces that classes are named appropriately:

  • If a concrete class extends PHPUnit\Framework\TestCase and it should be named *Test - could also enforce final?
  • If an abstract class extends PHPUnit\Framework\TestCase it should be named *TestCase.

Ideally the rule would also enforce the file is named appropriately as well but that might be outside scope.

If this convention isn't followed and you don't explicitly tell PHPUnit your test files then tests are not run, potentially keeping the build passing because a number of tests are being silently ignored.

@ruudk
Copy link
Contributor

ruudk commented Nov 26, 2024

I like this idea.

@ruudk
Copy link
Contributor

ruudk commented Nov 26, 2024

Another one: all public non-static methods should have a #[Test] attribute or named like test*. It happened a few times we thought we had tests but they were not picked up.

@ruudk
Copy link
Contributor

ruudk commented Nov 26, 2024

This could be a good starting point for the final / abstract part, or as inspiration: https://github.com/ergebnis/phpstan-rules/blob/main/src/Classes/FinalRule.php

@cs278
Copy link
Author

cs278 commented Nov 27, 2024

@ruudk I've thrown a PR together implementing my proposal, your proposal for checking methods as well could be added as an additional rule.

@staabm
Copy link
Contributor

staabm commented Dec 10, 2024

since I also stumbled recently over the "name your abstract test class" with a "TestCase" suffix, I really like this proposal.

@ondrejmirtes
Copy link
Member

Hey, this isn't generally applicable. The way PHPUnit looks for test files is configurable. It's just that it usually looks like this:

<directory suffix="Test.php">

I feel like these suggested rules are too opinionated for the general audience.

@cs278
Copy link
Author

cs278 commented Dec 16, 2024

Damn, I'd completely forgotten this was even configurable.

Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 17, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants