66
77use Antidot \Event \Container \Config \ConfigProvider ;
88use Antidot \Event \Container \EventDispatcherFactory ;
9- use Antidot \Event \Event ;
109use Antidot \Event \EventDispatcher ;
1110use Antidot \Event \ListenerInterface ;
12- use function dump ;
13- use function get_class ;
1411use PHPUnit \Framework \MockObject \MockObject ;
1512use PHPUnit \Framework \TestCase ;
16-
17- use function array_merge ;
1813use Psr \Container \ContainerInterface ;
1914use Psr \EventDispatcher \EventDispatcherInterface ;
2015
16+ use function array_merge ;
17+
2118class EventDispatcherFactoryTest extends TestCase
2219{
2320 /** @var array */
@@ -30,8 +27,6 @@ class EventDispatcherFactoryTest extends TestCase
3027 public function testItShouldCreateNewInstancesOfEventDispatcher (): void
3128 {
3229 $ this ->givenAConfigProvider ();
33- $ this ->havingAContainerWithConfig ();
34- $ this ->havingListenersConfiguredToReceiveAnEvent ();
3530 $ this ->whenEventDispatcherFactoryIsInvoked ();
3631 $ this ->thenItReturnsInstanceOfEventDispatcher ();
3732 $ this ->andThenDispatcherShouldDispatchAnEvent ();
@@ -40,15 +35,13 @@ public function testItShouldCreateNewInstancesOfEventDispatcher(): void
4035 public function testItShouldCreateNewInstancesOfEventDispatcherAlsoWithIncompleteConfiguration (): void
4136 {
4237 $ this ->givenAnIncompleteConfigProvider ();
43- $ this ->havingAContainerWithConfig ();
4438 $ this ->whenEventDispatcherFactoryIsInvoked ();
4539 $ this ->thenItReturnsInstanceOfEventDispatcher ();
4640 }
4741
4842 public function testItShouldCreateNewInstancesOfEventDispatcherAlsoWithMoreIncompleteConfiguration (): void
4943 {
5044 $ this ->givenAMoreIncompleteConfigProvider ();
51- $ this ->havingAContainerWithConfig ();
5245 $ this ->whenEventDispatcherFactoryIsInvoked ();
5346 $ this ->thenItReturnsInstanceOfEventDispatcher ();
5447 }
@@ -57,7 +50,6 @@ public function testItShouldThrowAnExceptionWhenAnInvalidConfigGiven(): void
5750 {
5851 $ this ->expectAnException ();
5952 $ this ->givenInvalidConfiguration ();
60- $ this ->havingAContainerWithConfig ();
6153 $ this ->whenEventDispatcherFactoryIsInvoked ();
6254 }
6355
@@ -74,6 +66,16 @@ private function givenAConfigProvider(): void
7466 ]
7567 ]
7668 ]);
69+ $ this ->container = $ this ->createMock (ContainerInterface::class);
70+ $ this ->container
71+ ->expects ($ this ->exactly (3 ))
72+ ->method ('get ' )
73+ ->withConsecutive (['config ' ], ['Listener1 ' ], ['Listener2 ' ])
74+ ->willReturnOnConsecutiveCalls (
75+ $ this ->config ,
76+ $ this ->createMock (ListenerInterface::class),
77+ $ this ->createMock (ListenerInterface::class)
78+ );
7779 }
7880
7981 private function givenInvalidConfiguration (): void
@@ -88,6 +90,7 @@ private function givenInvalidConfiguration(): void
8890 ]
8991 ]
9092 ];
93+ $ this ->prepareContainer ();
9194 }
9295
9396 private function givenAnIncompleteConfigProvider (): void
@@ -100,6 +103,7 @@ private function givenAnIncompleteConfigProvider(): void
100103 ]
101104 ]
102105 ]);
106+ $ this ->prepareContainer ();
103107 }
104108
105109 private function givenAMoreIncompleteConfigProvider (): void
@@ -110,30 +114,7 @@ private function givenAMoreIncompleteConfigProvider(): void
110114 'event-listeners ' => null
111115 ]
112116 ]);
113- }
114-
115- private function havingAContainerWithConfig (): void
116- {
117- $ this ->container = $ this ->createMock (ContainerInterface::class);
118- $ this ->container
119- ->expects ($ this ->at (0 ))
120- ->method ('get ' )
121- ->with ('config ' )
122- ->willReturn ($ this ->config );
123- }
124-
125- private function havingListenersConfiguredToReceiveAnEvent (): void
126- {
127- $ this ->container
128- ->expects ($ this ->at (1 ))
129- ->method ('get ' )
130- ->with ('Listener1 ' )
131- ->willReturn ($ this ->createMock (ListenerInterface::class));
132- $ this ->container
133- ->expects ($ this ->at (2 ))
134- ->method ('get ' )
135- ->with ('Listener2 ' )
136- ->willReturn ($ this ->createMock (ListenerInterface::class));
117+ $ this ->prepareContainer ();
137118 }
138119
139120 private function whenEventDispatcherFactoryIsInvoked (): void
@@ -158,4 +139,14 @@ private function expectAnException(): void
158139 {
159140 $ this ->expectException (\RuntimeException::class);
160141 }
142+
143+ private function prepareContainer (): void
144+ {
145+ $ this ->container = $ this ->createMock (ContainerInterface::class);
146+ $ this ->container
147+ ->expects ($ this ->once ())
148+ ->method ('get ' )
149+ ->with ('config ' )
150+ ->willReturn ($ this ->config );
151+ }
161152}
0 commit comments