phpstan-src icon indicating copy to clipboard operation
phpstan-src copied to clipboard

RuleTestCase: enable gathering analyser errors without assert

Open schlndh opened this issue 3 years ago • 2 comments

I want to propose extracting the part of RuleTestCase::analyse that gathers the actual errors into its own public method so that the child test can do the comparison in a different way when necessary.

The use-case I have for this is that I want to use golden tests (i.e. gather the errors once, save them into a file, check them manually and then test against this saved sample in the future) instead of writing down the expected errors manually. This should make it possible to change the error messages easily, as well as edit the analysed file without having to worry about line numbers etc.

Unfortunately, the current interface of the RuleTestCase does not allow me to do this easily. For now, I'm using this workaround but as I'm relying on things not covered by BC promise, it could break easily in the future.

schlndh avatar Sep 18 '22 11:09 schlndh

Interesting, fyi @matthiasnoback also recently started a discussion related to RuleTestCase: https://github.com/phpstan/phpstan/discussions/8006

herndlm avatar Sep 18 '22 11:09 herndlm

@herndlm Thanks for the context. In fact I used something quite similar (at least as far as I can tell from having a brief look) before this. Here is my previous test case and an input file.

However, both of these approaches only solve the issue with line numbers (and only partially). I'd also want to avoid writing down the error messages myself. At the very least I'd want to call a parametrized method to generate the error message (so I can at least fix typos etc easily). However, generating the whole thing automatically and then just checking it seems even better (even if some hacks are required).

schlndh avatar Sep 18 '22 12:09 schlndh

Thank you.

ondrejmirtes avatar Oct 17 '22 12:10 ondrejmirtes