15
15
use ArchiPro \EventDispatcher \Tests \Fixture \TestEvent ;
16
16
use Exception ;
17
17
use PHPUnit \Framework \TestCase ;
18
- use Revolt \EventLoop ;
19
-
20
18
use Throwable ;
21
19
22
20
/**
@@ -34,18 +32,21 @@ class AsyncEventDispatcherTest extends TestCase
34
32
private ListenerProvider $ listenerProvider ;
35
33
private AsyncEventDispatcher $ dispatcher ;
36
34
35
+ /** @var array<Throwable> */
36
+ private array $ errors = [];
37
+
37
38
/**
38
39
* Sets up the test environment before each test.
39
40
*/
40
41
protected function setUp (): void
41
42
{
42
43
$ 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
+ );
49
50
}
50
51
51
52
/**
@@ -80,6 +81,8 @@ public function testDispatchEventToMultipleListeners(): void
80
81
$ this ->assertCount (2 , $ results );
81
82
$ this ->assertContains ('listener1: test data ' , $ results );
82
83
$ this ->assertContains ('listener2: test data ' , $ results );
84
+
85
+ $ this ->assertCount (0 , $ this ->errors , 'No errors are logged ' );
83
86
}
84
87
85
88
/**
@@ -103,6 +106,8 @@ public function testSynchronousStoppableEvent(): void
103
106
104
107
$ this ->assertCount (1 , $ results );
105
108
$ this ->assertEquals (['listener1 ' ], $ results );
109
+
110
+ $ this ->assertCount (0 , $ this ->errors , 'No errors are logged ' );
106
111
}
107
112
108
113
/**
@@ -114,6 +119,7 @@ public function testNoListenersForEvent(): void
114
119
$ dispatchedEvent = $ this ->dispatcher ->dispatch ($ event );
115
120
116
121
$ this ->assertSame ($ event , $ dispatchedEvent ->await ());
122
+ $ this ->assertCount (0 , $ this ->errors , 'No errors are logged ' );
117
123
}
118
124
119
125
/**
@@ -158,16 +164,18 @@ public function testDispatchesFailureInOneListenerDoesNotAffectOthers(): void
158
164
159
165
$ futureEvent = $ this ->dispatcher ->dispatch ($ event );
160
166
161
- $ futureEvent = $ futureEvent ->await ();
167
+ $ futureEvent ->await ();
162
168
163
169
$ this ->assertTrue (
164
- $ futureEvent ->calledOnce ,
170
+ $ event ->calledOnce ,
165
171
'The first listener should have been called '
166
172
);
167
173
$ this ->assertTrue (
168
- $ futureEvent ->calledTwice ,
174
+ $ event ->calledTwice ,
169
175
'The second listener should have been called despite the failure of the first listener '
170
176
);
177
+
178
+ $ this ->assertCount (2 , $ this ->errors , 'Errors are caught for both listeners ' );
171
179
}
172
180
173
181
public function testCancellationOfStoppableEvent (): void
@@ -187,6 +195,8 @@ public function testCancellationOfStoppableEvent(): void
187
195
$ this ->expectException (CancelledException::class);
188
196
189
197
$ this ->dispatcher ->dispatch ($ event , $ cancellation )->await ();
198
+
199
+ $ this ->assertCount (0 , $ this ->errors , 'No errors are caught ' );
190
200
}
191
201
192
202
public function testCancellationOfNonStoppableEvent (): void
@@ -206,6 +216,7 @@ public function testCancellationOfNonStoppableEvent(): void
206
216
$ this ->expectException (CancelledException::class);
207
217
208
218
$ this ->dispatcher ->dispatch ($ event , $ cancellation )->await ();
209
- }
210
219
220
+ $ this ->assertCount (0 , $ this ->errors , 'No errors are caught ' );
221
+ }
211
222
}
0 commit comments