1212use Tempest \EventBus \EventBusConfig ;
1313use Tempest \EventBus \EventHandler ;
1414use Tempest \EventBus \GenericEventBus ;
15+ use Tempest \EventBus \Tests \Fixtures \EventInterface ;
16+ use Tempest \EventBus \Tests \Fixtures \EventInterfaceHandler ;
17+ use Tempest \EventBus \Tests \Fixtures \EventInterfaceImplementation ;
1518use Tempest \EventBus \Tests \Fixtures \ItHappened ;
1619use Tempest \EventBus \Tests \Fixtures \MyEventBusMiddleware ;
1720use Tempest \EventBus \Tests \Fixtures \MyEventHandler ;
@@ -46,12 +49,40 @@ public function test_class_based_handlers(): void
4649 $ eventBus = new GenericEventBus ($ container , $ config );
4750
4851 MyEventHandler::$ itHappened = false ;
49- MyEventBusMiddleware::$ hit = false ;
52+ MyEventBusMiddleware::$ hits = 0 ;
5053
5154 $ eventBus ->dispatch (new ItHappened ());
5255
5356 $ this ->assertTrue (MyEventHandler::$ itHappened );
54- $ this ->assertTrue (MyEventBusMiddleware::$ hit );
57+ $ this ->assertSame (1 , MyEventBusMiddleware::$ hits );
58+ }
59+
60+ public function test_middleware_is_only_triggered_once_per_event_dispatch (): void
61+ {
62+ $ container = new GenericContainer ();
63+
64+ $ handler = new EventHandler ();
65+ $ handler ->setHandler (new MethodReflector (new ReflectionMethod (MyEventHandler::class, 'handleItHappened ' )));
66+
67+ $ config = new EventBusConfig (
68+ handlers: [
69+ ItHappened::class => [
70+ new CallableEventHandler (ItHappened::class, $ handler ),
71+ new CallableEventHandler (ItHappened::class, $ handler ),
72+ ],
73+ ],
74+ middleware: [
75+ MyEventBusMiddleware::class,
76+ ]
77+ );
78+
79+ $ eventBus = new GenericEventBus ($ container , $ config );
80+
81+ MyEventBusMiddleware::$ hits = 0 ;
82+
83+ $ eventBus ->dispatch (new ItHappened ());
84+
85+ $ this ->assertSame (1 , MyEventBusMiddleware::$ hits );
5586 }
5687
5788 public function test_closure_based_handlers (): void
@@ -76,12 +107,12 @@ public function test_closure_based_handlers(): void
76107
77108 $ eventBus = new GenericEventBus ($ container , $ config );
78109
79- MyEventBusMiddleware::$ hit = false ;
110+ MyEventBusMiddleware::$ hits = 0 ;
80111
81112 $ eventBus ->dispatch (new ItHappened ());
82113
83114 $ this ->assertSame ('bar ' , $ called );
84- $ this ->assertTrue ( MyEventBusMiddleware::$ hit );
115+ $ this ->assertSame ( 1 , MyEventBusMiddleware::$ hits );
85116 }
86117
87118 public function test_closure_based_handlers_using_listen_method (): void
@@ -117,4 +148,28 @@ public function test_closure_based_handlers_using_function(): void
117148
118149 $ this ->assertTrue ($ hasHappened );
119150 }
151+
152+ public function test_interface_handlers (): void
153+ {
154+ $ container = new GenericContainer ();
155+
156+ $ handler = new EventHandler ();
157+ $ handler ->setHandler (new MethodReflector (new ReflectionMethod (EventInterfaceHandler::class, 'handleItHappened ' )));
158+
159+ $ config = new EventBusConfig (
160+ handlers: [
161+ EventInterface::class => [
162+ new CallableEventHandler (EventInterface::class, $ handler ),
163+ ],
164+ ]
165+ );
166+
167+ $ eventBus = new GenericEventBus ($ container , $ config );
168+
169+ EventInterfaceHandler::$ itHappened = false ;
170+
171+ $ eventBus ->dispatch (new EventInterfaceImplementation ());
172+
173+ $ this ->assertTrue (EventInterfaceHandler::$ itHappened );
174+ }
120175}
0 commit comments