44
55use PhpSchool \PhpWorkshop \Event \Event ;
66use PhpSchool \PhpWorkshop \Event \EventDispatcher ;
7+ use PhpSchool \PhpWorkshop \Event \EventInterface ;
78use PhpSchool \PhpWorkshop \Result \ResultInterface ;
89use PhpSchool \PhpWorkshop \ResultAggregator ;
910use PHPUnit_Framework_TestCase ;
@@ -44,9 +45,15 @@ public function testOnlyAppropriateListenersAreCalledForEvent()
4445 $ mockCallback2 ->expects ($ this ->never ())
4546 ->method ('doNotInvokeMe ' );
4647
47- $ this ->eventDispatcher ->listen ('some-event ' , [$ mockCallback1 , 'callback ' ]);
48- $ this ->eventDispatcher ->listen ('some-event ' , [$ mockCallback1 , 'callback ' ]);
49- $ this ->eventDispatcher ->listen ('different-event ' , [$ mockCallback2 , 'doNotInvokeMe ' ]);
48+ $ cb = function (Event $ e ) use ($ mockCallback1 ) {
49+ $ mockCallback1 ->callback ($ e );
50+ };
51+
52+ $ this ->eventDispatcher ->listen ('some-event ' , $ cb );
53+ $ this ->eventDispatcher ->listen ('some-event ' , $ cb );
54+ $ this ->eventDispatcher ->listen ('different-event ' , function (Event $ e ) use ($ mockCallback2 ) {
55+ $ mockCallback2 ->doNotInvokeMe ($ e );
56+ });
5057 $ this ->eventDispatcher ->dispatch ($ e );
5158 }
5259
@@ -61,8 +68,12 @@ public function testOnlyAppropriateVerifiersAreCalledForEvent()
6168 ->with ($ e )
6269 ->will ($ this ->returnValue ($ result ));
6370
64- $ this ->eventDispatcher ->insertVerifier ('some-event ' , [$ mockCallback1 , 'callback ' ]);
65- $ this ->eventDispatcher ->insertVerifier ('some-event ' , [$ mockCallback1 , 'callback ' ]);
71+ $ cb = function (Event $ e ) use ($ mockCallback1 ) {
72+ return $ mockCallback1 ->callback ($ e );
73+ };
74+
75+ $ this ->eventDispatcher ->insertVerifier ('some-event ' , $ cb );
76+ $ this ->eventDispatcher ->insertVerifier ('some-event ' , $ cb );
6677 $ this ->eventDispatcher ->dispatch ($ e );
6778
6879 $ this ->assertEquals ([$ result , $ result ], iterator_to_array ($ this ->results ));
@@ -77,7 +88,9 @@ public function testVerifyReturnIsSkippedIfNotInstanceOfResult()
7788 ->with ($ e )
7889 ->will ($ this ->returnValue (null ));
7990
80- $ this ->eventDispatcher ->insertVerifier ('some-event ' , [$ mockCallback1 , 'callback ' ]);
91+ $ this ->eventDispatcher ->insertVerifier ('some-event ' , function (Event $ e ) use ($ mockCallback1 ) {
92+ $ mockCallback1 ->callback ($ e );
93+ });
8194 $ this ->eventDispatcher ->dispatch ($ e );
8295
8396 $ this ->assertEquals ([], iterator_to_array ($ this ->results ));
@@ -97,4 +110,23 @@ public function testListenWithMultipleEvents()
97110 $ this ->eventDispatcher ->dispatch ($ e1 );
98111 $ this ->eventDispatcher ->dispatch ($ e2 );
99112 }
113+
114+ public function testListenersAndVerifiersAreCalledInOrderOfAttachment ()
115+ {
116+ $ e1 = new Event ('first-event ' , ['arg1 ' => 1 , 'arg2 ' => 2 ]);
117+
118+
119+ $ counter = 0 ;
120+ $ this ->eventDispatcher ->insertVerifier ('first-event ' , function (Event $ e ) use (&$ counter ) {
121+ $ this ->assertEquals (0 , $ counter );
122+ $ counter ++;
123+ });
124+
125+ $ this ->eventDispatcher ->listen ('first-event ' , function (Event $ e ) use (&$ counter ) {
126+ $ this ->assertEquals (1 , $ counter );
127+ $ counter ++;
128+ });
129+
130+ $ this ->eventDispatcher ->dispatch ($ e1 );
131+ }
100132}
0 commit comments