-
Notifications
You must be signed in to change notification settings - Fork 495
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
RuleTestCase: add analyseFileWithErrorsAsComments #2742
base: 1.10.x
Are you sure you want to change the base?
Conversation
You've opened the pull request against the latest branch 1.11.x. If your code is relevant on 1.10.x and you want it to be released sooner, please rebase your pull request and change its target to 1.10.x. |
@@ -4,7 +4,7 @@ function ( | |||
string $str | |||
) { | |||
(string) $str; | |||
(string) new \stdClass(); | |||
(string) new \stdClass(); // error: Cannot cast stdClass to string. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what comes to mind: maybe we can do it similar to assertType()
and assertVariableCertainty()
(string) new \stdClass(); // error: Cannot cast stdClass to string. | |
assertErrorMessage('Cannot cast stdClass to string.', (string) new \stdClass()); |
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I'd like something like that too. Perhaps something like assertErrorOnNextLine('message');
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is much less flexible. You cannot call method between class methods etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error can be anywhere, in phpdoc, at class level, at trait usage. Comment is the only thing you can place anywhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, the implemented version works even within phpdocs (to be supported in #2807)
I've created a similar package to do help with testing custom rules: https://github.com/DaveLiddament/phpstan-rule-test-helper. This provides This allows you to either hard code the error message in the fixture file (similar to @janedbal's suggestion) . E.g. $item->updateName("world"); // ERROR can not call method If the same error message is repeated multiple times, then you can also override the protected function getErrorFormatter(): string
{
return "Can not call method";
} The fixture simplifies to: $item->updateName("world"); // ERROR Finally you can provide context in the error messages: protected function getErrorFormatter(): string
{
return "Can not call method {0} from {1}";
} The fixture error has additional contextual information, if there is more than 1 bit it is separated by $item->updateName("world"); // ERROR Item::updateName|Foo In the above example the expected error message would be: It be nice to either bring similar functionality into PHPStan, or highlight helper packages that can help with testing. |
Some thoughts after letting this sit for a while:
|
I like I love the fact you'd then not even need to create PHPUnit tests, you can just point PHPStan at the code snippet. |
I'm not sure that the next-line magic is needed here. Imo not. |
I'd like to suggest this feature I'm using for about 4-5 years in my projects. E.g. here.
Benefits:
Downsides:
I believe that at least 90% tests could benefit from this. Other can stick with the old approach.