-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
[FEATURE] Expect warning / notice #3758
Comments
Disagree on this:
Overall, code that raises notices/warnings is faulty, and the fault should be removed, not tested. |
PHPUnit 8.4 has #3775. |
@Ocramius The way that PHP uses warning and notices; you're right because those are actually errors. But there are valid reasons to use them. Almost always as runtime warnings; Examples;
@sebastianbergmann This still ends the execution on a warning or notice 😞 Silencing with |
Discussed it yesterday with @sebastianbergmann, and it is possible to test these edge cases by using a custom error handler that collects warnings/notices, and then assert on what was collected. |
@Ocramius Let's switch the argument around. What's the point of the current implementation of If you expect the code to run and a warning is triggered unexpectedly PHPUnit correctly makes the test fail. If there is an assertion where the code should stop executing if it fails, you'll use an exception and not a warning. So the only reason to test for a warning in a unit test if when you explicitly wanted to use a warning. The only reason to choose a warning over an not an exception is that you do not want the code break at that point. So while I agree that warnings and notices should be used sparsely, when they are used you always want to check that the code continues executing correctly and literally never should assume it does so. PHPUnit already creates a 'custom' error handler. It should support capturing expected warnings and notices correctly or not at all. TL;DRRegarding the need to test if the code continues correctly after an expected warning as an "edge case" is not correct. You should either not use/expect warnings at all OR you should always test that the warning doesn't influence the purpose of the function. |
@sebastianbergmann Is it for an option for me to create a PR that implements this feature, or will it be rejected regardlessly? |
Such a feature would be rejected. |
Just figured and I'd chime in and say it's really disappointing to see that attitude taken to a feature like this. @jasny explained the use case pretty well so I don't have much to add on that front. |
@charrondev Feel free to use my custom implementation to assert warnings https://github.com/jasny/phpunit-extension |
For those landing here for some reason (like me) → it was implemented in #3775 |
Throwing an exception for warnings and notices makes it difficult to test these cases. This could be improved by the addition of
expectWarning
andexpectNotice
.Why?
In PHP code will continue to run after a warning or notice. Throwing an exception makes it difficult to test if code behaves correctly after the warning is triggered.
This workaround isn't great. It requires two tests, one for the warning and one for the result. The second test will suppress all warnings and notices. This includes possible issues that happen after the initial warning.
Feature request
Please add the methods
expectWarning
,expectWarningRegex
,expectNotice
andexpectNoticeRegex
. These methods can be called multiple times if multiple warnings and/or notices are expected.Expected warnings and notices aren't converted to exceptions, but other warnings/notices still are.
The text was updated successfully, but these errors were encountered: