-
-
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
PHPUnit's test runner overwrites custom error handler registered using set_error_handler()
in bootstrap script
#5676
Comments
CC @mvorisek |
The current behaviour is expected - in https://github.com/marmichalski/phpunit-err-handler/blob/e4994c26/tests/TriggerTest.php#L19 you expect an exception beeing thrown because of https://github.com/marmichalski/phpunit-err-handler/blob/e4994c26/bootstrap.php#L7-L13 but the PHPUnit handler (registered since PHPUnit 10.x) prevents that because PHP handlers are not nested. So the behaviour is like when there would be no custom global handler - https://3v4l.org/KZVq4. If you want a custom error handler to be used, use |
I am not arguing it is incorrect. I am saying it changed in a patch release, see that 10.5.3 respected that and all tests passed. |
Although, on the other hand, one could see it as incorrect and a bug instead. You should probably also update the docs if that's the way to go https://docs.phpunit.de/en/10.5/error-handling.html#error-handling (re Other error handlers). |
This is the same when your app would be used as a dependency which register it's own handler as well. It will be masked too. In theory, we can emulate nested PHP error handler support in PHPUnit error handler, but I do not support this idea - PHPUnit should detect "non-execution-breaking warnings", not silence the side effects (like
If you rely on some global specifics in **unit** testing, you should set these specifics using before test hook. |
As
Those global specifics are set globally in a bootstrap file, though, and the same file is included using PHPUnit's own Anyway, it seems that up until 10.5.3 PHPUnit was not overriding |
Stuck with the same problem as @marmichalski What about a global |
@marmichalski as long as you did not use/used
In post above, in the paragraph starting with "In theory, we can emulate nested PHP error handler support in PHPUnit error handler"... I have discussed it there. PHPUnit should probably not propagate this "handled/logged warnings".
@cracksalad Please provide a minimal repro of your usecase.
I do not support global I might mildly support an option to allow to emulate the error handler nesting, which will solve this "app with bootstrapped error handler" usecase. But I still think there are better solutions like setting the own error handler before and after each test using PHPUnit hook. |
|
It is exactly the same as the one described above. I am setting a custom error handler in PHPUnit's bootstrap file to convert all the
Even if that is the better solution in theory, it means a lot of work to put into existing test cases (there are a lot of them) right now - for a patch release! |
I looked into the PHPUnit code: Since PHPUnit 10.0.0 the PHPUnit error handler was always registered in https://github.com/sebastianbergmann/phpunit/blob/10.5.3/src/Runner/ErrorHandler.php#L161 but **only if there was no other error handler**. This was of course not good as as discussed above, PHPUnit was not able to detect any warnings. |
This is at least documented:
|
https://github.com/sebastianbergmann/phpunit/blob/10.5.3/src/Runner/ErrorHandler.php#L158-L164 is not strictly needed for/in #5592 and can be reverted for 10.x. Feel free to submit a PR, add an E2E test and ping me. For 11.x, the current (10.5.4+) behaviour should be default as warnings should be always logged by PHPUnit with possibly a global option with options:
|
@sebastianbergmann can this issue be reopened? |
@mvorisek Then please send a PR for |
@mvorisek This would be the revert for diff --git a/src/Runner/ErrorHandler.php b/src/Runner/ErrorHandler.php
index ea3acfa5d..e1c5b8a72 100644
--- a/src/Runner/ErrorHandler.php
+++ b/src/Runner/ErrorHandler.php
@@ -166,7 +166,13 @@ public function enable(): void
return;
}
- set_error_handler($this);
+ $oldErrorHandler = set_error_handler($this);
+
+ if ($oldErrorHandler !== null) {
+ restore_error_handler();
+
+ return;
+ }
$this->enabled = true;
$this->originalErrorReportingLevel = error_reporting(); @marmichalski @cracksalad Can you please test whether your test suites would work again with this change? Thanks! |
PHPUnit 11.0 will be released in less that two weeks. I think that we should keep the current behaviour of |
@sebastianbergmann #5676 (comment) - yes, and I agree it should be reverted for 10.5.x only. |
Can confirm that this works with the example repo, phpunit 10.5.8 (manually applying the patch). |
Thank you, @mvorisek and @marmichalski. I will take care of |
set_error_handler()
in bootstrap script
I have reverted the code in question in f38fc88 for PHPUnit 10.5 on the I have reverted the revert in 26e8bf4 for PHPUnit 11 on the |
The change of behaviour in PHPUnit 11 (compared to PHPUnit 10) has been reverted via 9c05f71, 7cea991, and 4bbb3ca. I have opened #5686 for tracking how I would like to improve the current situation. |
Summary
There seems to be a change in behaviour starting with PHPUnit 10.5.4 when a custom error handler is used.
I have prepared a minimal repository with the issue here, handler is registered in bootstrap.php, and you can see failing tests here.
This seems to be caused by #5592.
Current behavior
PHPUnit swallows triggered error.
How to reproduce
Run tests in the repo.
Expected behavior
PHPUnit not taking precedence over custom error handler.
The text was updated successfully, but these errors were encountered: