20
20
use PHPUnit \Framework \Attributes \PreserveGlobalState ;
21
21
use PHPUnit \Framework \Attributes \RunInSeparateProcess ;
22
22
use PHPUnit \Framework \Attributes \WithoutErrorHandler ;
23
+ use stdClass ;
23
24
24
25
/**
25
26
* @internal
@@ -29,6 +30,8 @@ final class EventsTest extends CIUnitTestCase
29
30
{
30
31
/**
31
32
* Accessible event manager instance
33
+ *
34
+ * @var MockEvents
32
35
*/
33
36
private Events $ manager ;
34
37
@@ -181,63 +184,68 @@ public function testPriorityWithMultiple(): void
181
184
182
185
public function testRemoveListener (): void
183
186
{
184
- $ result = false ;
187
+ $ user = $ this -> getEditableObject () ;
185
188
186
- $ callback = static function () use (&$ result ): void {
187
- $ result = true ;
189
+ $ callback = static function () use (&$ user ): void {
190
+ $ user ->name = 'Boris ' ;
191
+ $ user ->age = 40 ;
188
192
};
189
193
190
194
Events::on ('foo ' , $ callback );
191
195
192
- Events::trigger ('foo ' );
193
- $ this ->assertTrue ($ result );
196
+ $ this ->assertTrue (Events::trigger ('foo ' ));
197
+ $ this ->assertSame ('Boris ' , $ user ->name );
198
+
199
+ $ user = $ this ->getEditableObject ();
194
200
195
- $ result = false ;
196
201
$ this ->assertTrue (Events::removeListener ('foo ' , $ callback ));
197
202
198
- Events::trigger ('foo ' );
199
- $ this ->assertFalse ( $ result );
203
+ $ this -> assertTrue ( Events::trigger ('foo ' ) );
204
+ $ this ->assertSame ( ' Ivan ' , $ user -> name );
200
205
}
201
206
202
207
public function testRemoveListenerTwice (): void
203
208
{
204
- $ result = false ;
209
+ $ user = $ this -> getEditableObject () ;
205
210
206
- $ callback = static function () use (&$ result ): void {
207
- $ result = true ;
211
+ $ callback = static function () use (&$ user ): void {
212
+ $ user ->name = 'Boris ' ;
213
+ $ user ->age = 40 ;
208
214
};
209
215
210
216
Events::on ('foo ' , $ callback );
211
217
212
- Events::trigger ('foo ' );
213
- $ this ->assertTrue ($ result );
218
+ $ this ->assertTrue (Events::trigger ('foo ' ));
219
+ $ this ->assertSame ('Boris ' , $ user ->name );
220
+
221
+ $ user = $ this ->getEditableObject ();
214
222
215
- $ result = false ;
216
223
$ this ->assertTrue (Events::removeListener ('foo ' , $ callback ));
217
224
$ this ->assertFalse (Events::removeListener ('foo ' , $ callback ));
218
225
219
- Events::trigger ('foo ' );
220
- $ this ->assertFalse ( $ result );
226
+ $ this -> assertTrue ( Events::trigger ('foo ' ) );
227
+ $ this ->assertSame ( ' Ivan ' , $ user -> name );
221
228
}
222
229
223
230
public function testRemoveUnknownListener (): void
224
231
{
225
- $ result = false ;
232
+ $ user = $ this -> getEditableObject () ;
226
233
227
- $ callback = static function () use (&$ result ): void {
228
- $ result = true ;
234
+ $ callback = static function () use (&$ user ): void {
235
+ $ user ->name = 'Boris ' ;
236
+ $ user ->age = 40 ;
229
237
};
230
238
231
239
Events::on ('foo ' , $ callback );
232
240
233
- Events::trigger ('foo ' );
234
- $ this ->assertTrue ( $ result );
241
+ $ this -> assertTrue ( Events::trigger ('foo ' ) );
242
+ $ this ->assertSame ( ' Boris ' , $ user -> name );
235
243
236
- $ result = false ;
237
- $ this ->assertFalse (Events::removeListener ('bar ' , $ callback ));
244
+ $ user = $ this ->getEditableObject ();
238
245
239
- Events::trigger ('foo ' );
240
- $ this ->assertTrue ($ result );
246
+ $ this ->assertFalse (Events::removeListener ('bar ' , $ callback ));
247
+ $ this ->assertTrue (Events::trigger ('foo ' ));
248
+ $ this ->assertSame ('Boris ' , $ user ->name );
241
249
}
242
250
243
251
public function testRemoveAllListenersWithSingleEvent (): void
@@ -315,4 +323,13 @@ public function testSimulate(): void
315
323
316
324
$ this ->assertSame (0 , $ result );
317
325
}
326
+
327
+ private function getEditableObject (): stdClass
328
+ {
329
+ $ user = new stdClass ();
330
+ $ user ->name = 'Ivan ' ;
331
+ $ user ->age = 30 ;
332
+
333
+ return clone $ user ;
334
+ }
318
335
}
0 commit comments