Rule to enforce class naming · Issue #214 · phpstan/phpstan-phpunit · GitHub
Skip to content

Rule to enforce class naming #214

Closed
Closed

Description

@cs278

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.

Activity

  1. ruudk commented on Nov 26, 2024

    @ruudk
    Contributor

    I like this idea.

  2. ruudk commented on Nov 26, 2024

    @ruudk
    Contributor

    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.

  3. ruudk commented on Nov 26, 2024

    @ruudk
    Contributor

    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

  4. cs278 commented on Nov 27, 2024

    @cs278
    Author

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

  5. staabm commented on Dec 10, 2024

    @staabm
    Contributor

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

  6. ondrejmirtes commented on Dec 10, 2024

    @ondrejmirtes
    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.

  7. cs278 commented on Dec 16, 2024

    @cs278
    Author

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

  8. github-actions commented on Jan 17, 2025

    @github-actions

    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.

  9. locked as resolved and limited conversation to collaborators on Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions