Search code examples
phptestingphpstan

Writing a less brittle test for a custom PHPStan rule


I wrote a custom PHPStan rule, and a test for it. The test analyses a sample PHP class:

class CustomRuleTestData
{
    public function hasViolations() {
        // 
    }
    
    public function hasOtherViolations() {
        //
    }
    public function hasNoViolations() {
        //
    }
    public function hasNoViolationsInAnotherWay()
    {
        //
    }
}

The issue is, according to the documentation, I have to specify the exact lines of code and the message for each line where there should be an error.

This makes things difficult because even if I change the formatting like add an empty line to the test data class, I would have to change the line numbers in the test too. If I add some more coverage or refactor something, I'd have to change the test again, which is annoying.

Is there a way to assert that methods hasViolations() and hasOtherViolations() should error, while hasNoViolations() and hasNoViolationsInAnotherWay() should not, without having to specify the actual line numbers?

Thanks in advance.


Solution

  • Right now there isn’t a better way to write tests for custom rules. Some points to consider:

    1. This isn’t a problem in practice. There is very little reason to change existing test files. You can always add new code to test at the end and you should exclude the test files from coding standard tools.
    2. I’m saying this as a maintainer of the largest suite of these test files, PHPStan itself, so I’m saying this from my own experience 😊
    3. There is an open pull requesth with a different way of writing tests and some discussion: https://github.com/phpstan/phpstan-src/pull/2742