1515use ArchiPro \EventDispatcher \Tests \Fixture \TestEvent ;
1616use Exception ;
1717use PHPUnit \Framework \TestCase ;
18- use Revolt \EventLoop ;
19-
2018use Throwable ;
2119
2220/**
@@ -34,18 +32,21 @@ class AsyncEventDispatcherTest extends TestCase
3432 private ListenerProvider $ listenerProvider ;
3533 private AsyncEventDispatcher $ dispatcher ;
3634
35+ /** @var array<Throwable> */
36+ private array $ errors = [];
37+
3738 /**
3839 * Sets up the test environment before each test.
3940 */
4041 protected function setUp (): void
4142 {
4243 $ this ->listenerProvider = new ListenerProvider ();
43- $ this ->dispatcher = new AsyncEventDispatcher ($ this -> listenerProvider );
44-
45- EventLoop:: setErrorHandler ( function (Throwable $ err ) {
46- throw $ err ;
47- });
48-
44+ $ this ->dispatcher = new AsyncEventDispatcher (
45+ $ this -> listenerProvider ,
46+ function (Throwable $ exception ) {
47+ $ this -> errors [] = $ exception ;
48+ }
49+ );
4950 }
5051
5152 /**
@@ -80,6 +81,8 @@ public function testDispatchEventToMultipleListeners(): void
8081 $ this ->assertCount (2 , $ results );
8182 $ this ->assertContains ('listener1: test data ' , $ results );
8283 $ this ->assertContains ('listener2: test data ' , $ results );
84+
85+ $ this ->assertCount (0 , $ this ->errors , 'No errors are logged ' );
8386 }
8487
8588 /**
@@ -103,6 +106,8 @@ public function testSynchronousStoppableEvent(): void
103106
104107 $ this ->assertCount (1 , $ results );
105108 $ this ->assertEquals (['listener1 ' ], $ results );
109+
110+ $ this ->assertCount (0 , $ this ->errors , 'No errors are logged ' );
106111 }
107112
108113 /**
@@ -114,6 +119,7 @@ public function testNoListenersForEvent(): void
114119 $ dispatchedEvent = $ this ->dispatcher ->dispatch ($ event );
115120
116121 $ this ->assertSame ($ event , $ dispatchedEvent ->await ());
122+ $ this ->assertCount (0 , $ this ->errors , 'No errors are logged ' );
117123 }
118124
119125 /**
@@ -158,16 +164,18 @@ public function testDispatchesFailureInOneListenerDoesNotAffectOthers(): void
158164
159165 $ futureEvent = $ this ->dispatcher ->dispatch ($ event );
160166
161- $ futureEvent = $ futureEvent ->await ();
167+ $ futureEvent ->await ();
162168
163169 $ this ->assertTrue (
164- $ futureEvent ->calledOnce ,
170+ $ event ->calledOnce ,
165171 'The first listener should have been called '
166172 );
167173 $ this ->assertTrue (
168- $ futureEvent ->calledTwice ,
174+ $ event ->calledTwice ,
169175 'The second listener should have been called despite the failure of the first listener '
170176 );
177+
178+ $ this ->assertCount (2 , $ this ->errors , 'Errors are caught for both listeners ' );
171179 }
172180
173181 public function testCancellationOfStoppableEvent (): void
@@ -187,6 +195,8 @@ public function testCancellationOfStoppableEvent(): void
187195 $ this ->expectException (CancelledException::class);
188196
189197 $ this ->dispatcher ->dispatch ($ event , $ cancellation )->await ();
198+
199+ $ this ->assertCount (0 , $ this ->errors , 'No errors are caught ' );
190200 }
191201
192202 public function testCancellationOfNonStoppableEvent (): void
@@ -206,6 +216,7 @@ public function testCancellationOfNonStoppableEvent(): void
206216 $ this ->expectException (CancelledException::class);
207217
208218 $ this ->dispatcher ->dispatch ($ event , $ cancellation )->await ();
209- }
210219
220+ $ this ->assertCount (0 , $ this ->errors , 'No errors are caught ' );
221+ }
211222}
0 commit comments