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.
Right now there isn’t a better way to write tests for custom rules. Some points to consider: