-
-
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
Exceptions thrown in event subscribers affect how tests are run and/or how their outcome is evaluated #5219
Comments
An exception thrown in a subscriber must not have any effect on how tests are run. |
Here is what I get:
|
I think this is what we are missing: diff --git a/src/Event/Dispatcher/DirectDispatcher.php b/src/Event/Dispatcher/DirectDispatcher.php
index ca430ec71..a7169b9ac 100644
--- a/src/Event/Dispatcher/DirectDispatcher.php
+++ b/src/Event/Dispatcher/DirectDispatcher.php
@@ -11,6 +11,7 @@
use function array_key_exists;
use function sprintf;
+use Throwable;
/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
@@ -80,7 +81,10 @@ public function dispatch(Event $event): void
}
foreach ($this->tracers as $tracer) {
- $tracer->trace($event);
+ try {
+ $tracer->trace($event);
+ } catch (Throwable) {
+ }
}
if (!array_key_exists($eventClassName, $this->subscribers)) {
@@ -88,7 +92,10 @@ public function dispatch(Event $event): void
}
foreach ($this->subscribers[$eventClassName] as $subscriber) {
- $subscriber->notify($event);
+ try {
+ $subscriber->notify($event);
+ } catch (Throwable) {
+ }
}
}
} This would ignore all exceptions thrown in subscribers. |
I just verified that the patch shown in #5219 (comment) has the desired effect for your use case, too. |
@cspray Thank you for reporting this issue! |
@sebastianbergmann I noticed in the example you replicated, the event is the
The changes I made to your
|
I am sorry, but I do not understand what you are trying to tell me. As of 74ce5b6, all exceptions thrown in third-party subscribers are ignored (as they should be). For this, it does not matter which event a subscriber subscribes to. |
Summary
A
PHPUnit\Event\Test\PreparationStartedSubscriber
throws an Exception.Current behavior
The test that has had preparation started is silently discarded. There are no indications that the test failed.
How to reproduce
I have also reproduced this in an isolated repo: https://github.com/cspray/phpunit10-bugs under the
PreparationStartedException
directory.Expected behavior
I would expect a test that has an exception thrown in a
PreparationStartedSubscriber
to be properly marked as a failure/error with information on the Exception that was thrown.Composer Info Output
The text was updated successfully, but these errors were encountered: