@@ -182,6 +182,7 @@ public function testTriggerUntilShouldMarkResponseCollectionStoppedWhenCondition
182
182
$ this ->events ->attach ('foo.bar ' , function () { return 'found ' ; }, 2 );
183
183
$ this ->events ->attach ('foo.bar ' , function () { return 'zero ' ; }, 1 );
184
184
// @codingStandardsIgnoreEnd
185
+
185
186
$ responses = $ this ->events ->triggerUntil (function ($ result ) {
186
187
return ($ result === 'found ' );
187
188
}, 'foo.bar ' , $ this );
@@ -200,6 +201,7 @@ public function testTriggerUntilShouldMarkResponseCollectionStoppedWhenCondition
200
201
$ this ->events ->attach ('foo.bar ' , function () { return 'zero ' ; });
201
202
$ this ->events ->attach ('foo.bar ' , function () { return 'found ' ; });
202
203
// @codingStandardsIgnoreEnd
204
+
203
205
$ responses = $ this ->events ->triggerUntil (function ($ result ) {
204
206
return ($ result === 'found ' );
205
207
}, 'foo.bar ' , $ this );
@@ -216,6 +218,7 @@ public function testResponseCollectionIsNotStoppedWhenNoCallbackMatchedByTrigger
216
218
$ this ->events ->attach ('foo.bar ' , function () { return 'found ' ; }, 2 );
217
219
$ this ->events ->attach ('foo.bar ' , function () { return 'zero ' ; }, 1 );
218
220
// @codingStandardsIgnoreEnd
221
+
219
222
$ responses = $ this ->events ->triggerUntil (function ($ result ) {
220
223
return ($ result === 'never found ' );
221
224
}, 'foo.bar ' , $ this );
@@ -232,6 +235,7 @@ public function testCallingEventsStopPropagationMethodHaltsEventEmission()
232
235
$ this ->events ->attach ('foo.bar ' , function ($ e ) { return 'found ' ; }, 2 );
233
236
$ this ->events ->attach ('foo.bar ' , function ($ e ) { return 'zero ' ; }, 1 );
234
237
// @codingStandardsIgnoreEnd
238
+
235
239
$ responses = $ this ->events ->trigger ('foo.bar ' );
236
240
$ this ->assertInstanceOf ('Zend\EventManager\ResponseCollection ' , $ responses );
237
241
$ this ->assertTrue ($ responses ->stopped ());
@@ -252,6 +256,7 @@ public function testCanAlterParametersWithinAEvent()
252
256
$ bar = $ e ->getParam ('bar ' , '__NO_BAR__ ' );
253
257
return $ foo . ": " . $ bar ;
254
258
});
259
+
255
260
$ responses = $ this ->events ->trigger ('foo.bar ' );
256
261
$ this ->assertEquals ('bar:baz ' , $ responses ->last ());
257
262
}
@@ -260,10 +265,12 @@ public function testParametersArePassedToEventByReference()
260
265
{
261
266
$ params = [ 'foo ' => 'bar ' , 'bar ' => 'baz ' ];
262
267
$ args = $ this ->events ->prepareArgs ($ params );
268
+
263
269
// @codingStandardsIgnoreStart
264
270
$ this ->events ->attach ('foo.bar ' , function ($ e ) { $ e ->setParam ('foo ' , 'FOO ' ); });
265
271
$ this ->events ->attach ('foo.bar ' , function ($ e ) { $ e ->setParam ('bar ' , 'BAR ' ); });
266
272
// @codingStandardsIgnoreEnd
273
+
267
274
$ responses = $ this ->events ->trigger ('foo.bar ' , $ this , $ args );
268
275
$ this ->assertEquals ('FOO ' , $ args ['foo ' ]);
269
276
$ this ->assertEquals ('BAR ' , $ args ['bar ' ]);
@@ -276,6 +283,7 @@ public function testCanPassObjectForEventParameters()
276
283
$ this ->events ->attach ('foo.bar ' , function ($ e ) { $ e ->setParam ('foo ' , 'FOO ' ); });
277
284
$ this ->events ->attach ('foo.bar ' , function ($ e ) { $ e ->setParam ('bar ' , 'BAR ' ); });
278
285
// @codingStandardsIgnoreEnd
286
+
279
287
$ responses = $ this ->events ->trigger ('foo.bar ' , $ this , $ params );
280
288
$ this ->assertEquals ('FOO ' , $ params ->foo );
281
289
$ this ->assertEquals ('BAR ' , $ params ->bar );
@@ -744,4 +752,54 @@ public function testTriggeringAnEventWithAnEmptyNameRaisesAnException($event, $m
744
752
$ this ->events ->$ method ($ event );
745
753
}
746
754
}
755
+
756
+ public function testTriggerEventAcceptsEventInstanceAndTriggersListeners ()
757
+ {
758
+ $ event = $ this ->prophesize (EventInterface::class);
759
+ $ event ->getName ()->willReturn ('test ' );
760
+ $ event ->stopPropagation (false )->shouldBeCalled ();
761
+ $ event ->propagationIsStopped ()->willReturn (false );
762
+
763
+ $ triggered = false ;
764
+ $ this ->events ->attach ('test ' , function ($ e ) use ($ event , &$ triggered ) {
765
+ $ this ->assertSame ($ event ->reveal (), $ e );
766
+ $ triggered = true ;
767
+ });
768
+
769
+ $ this ->events ->triggerEvent ($ event ->reveal ());
770
+ $ this ->assertTrue ($ triggered , 'Listener for event was not triggered ' );
771
+ }
772
+
773
+ public function testTriggerEventUntilAcceptsEventInstanceAndTriggersListenersUntilCallbackEvaluatesTrue ()
774
+ {
775
+ $ event = $ this ->prophesize (EventInterface::class);
776
+ $ event ->getName ()->willReturn ('test ' );
777
+ $ event ->stopPropagation (false )->shouldBeCalled ();
778
+ $ event ->propagationIsStopped ()->willReturn (false );
779
+
780
+ $ callback = function ($ result ) {
781
+ return ($ result === true );
782
+ };
783
+
784
+ $ triggeredOne = false ;
785
+ $ this ->events ->attach ('test ' , function ($ e ) use ($ event , &$ triggeredOne ) {
786
+ $ this ->assertSame ($ event ->reveal (), $ e );
787
+ $ triggeredOne = true ;
788
+ });
789
+
790
+ $ triggeredTwo = false ;
791
+ $ this ->events ->attach ('test ' , function ($ e ) use ($ event , &$ triggeredTwo ) {
792
+ $ this ->assertSame ($ event ->reveal (), $ e );
793
+ $ triggeredTwo = true ;
794
+ return true ;
795
+ });
796
+
797
+ $ this ->events ->attach ('test ' , function ($ e ) {
798
+ $ this ->fail ('Third listener was triggered and should not have been ' );
799
+ });
800
+
801
+ $ this ->events ->triggerEventUntil ($ callback , $ event ->reveal ());
802
+ $ this ->assertTrue ($ triggeredOne , 'First Listener for event was not triggered ' );
803
+ $ this ->assertTrue ($ triggeredTwo , 'First Listener for event was not triggered ' );
804
+ }
747
805
}
0 commit comments