6
6
7
7
use Antidot \Event \Container \Config \ConfigProvider ;
8
8
use Antidot \Event \Container \EventDispatcherFactory ;
9
- use Antidot \Event \Event ;
10
9
use Antidot \Event \EventDispatcher ;
11
10
use Antidot \Event \ListenerInterface ;
12
- use function dump ;
13
- use function get_class ;
14
11
use PHPUnit \Framework \MockObject \MockObject ;
15
12
use PHPUnit \Framework \TestCase ;
16
-
17
- use function array_merge ;
18
13
use Psr \Container \ContainerInterface ;
19
14
use Psr \EventDispatcher \EventDispatcherInterface ;
20
15
16
+ use function array_merge ;
17
+
21
18
class EventDispatcherFactoryTest extends TestCase
22
19
{
23
20
/** @var array */
@@ -30,8 +27,6 @@ class EventDispatcherFactoryTest extends TestCase
30
27
public function testItShouldCreateNewInstancesOfEventDispatcher (): void
31
28
{
32
29
$ this ->givenAConfigProvider ();
33
- $ this ->havingAContainerWithConfig ();
34
- $ this ->havingListenersConfiguredToReceiveAnEvent ();
35
30
$ this ->whenEventDispatcherFactoryIsInvoked ();
36
31
$ this ->thenItReturnsInstanceOfEventDispatcher ();
37
32
$ this ->andThenDispatcherShouldDispatchAnEvent ();
@@ -40,15 +35,13 @@ public function testItShouldCreateNewInstancesOfEventDispatcher(): void
40
35
public function testItShouldCreateNewInstancesOfEventDispatcherAlsoWithIncompleteConfiguration (): void
41
36
{
42
37
$ this ->givenAnIncompleteConfigProvider ();
43
- $ this ->havingAContainerWithConfig ();
44
38
$ this ->whenEventDispatcherFactoryIsInvoked ();
45
39
$ this ->thenItReturnsInstanceOfEventDispatcher ();
46
40
}
47
41
48
42
public function testItShouldCreateNewInstancesOfEventDispatcherAlsoWithMoreIncompleteConfiguration (): void
49
43
{
50
44
$ this ->givenAMoreIncompleteConfigProvider ();
51
- $ this ->havingAContainerWithConfig ();
52
45
$ this ->whenEventDispatcherFactoryIsInvoked ();
53
46
$ this ->thenItReturnsInstanceOfEventDispatcher ();
54
47
}
@@ -57,7 +50,6 @@ public function testItShouldThrowAnExceptionWhenAnInvalidConfigGiven(): void
57
50
{
58
51
$ this ->expectAnException ();
59
52
$ this ->givenInvalidConfiguration ();
60
- $ this ->havingAContainerWithConfig ();
61
53
$ this ->whenEventDispatcherFactoryIsInvoked ();
62
54
}
63
55
@@ -74,6 +66,16 @@ private function givenAConfigProvider(): void
74
66
]
75
67
]
76
68
]);
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
+ );
77
79
}
78
80
79
81
private function givenInvalidConfiguration (): void
@@ -88,6 +90,7 @@ private function givenInvalidConfiguration(): void
88
90
]
89
91
]
90
92
];
93
+ $ this ->prepareContainer ();
91
94
}
92
95
93
96
private function givenAnIncompleteConfigProvider (): void
@@ -100,6 +103,7 @@ private function givenAnIncompleteConfigProvider(): void
100
103
]
101
104
]
102
105
]);
106
+ $ this ->prepareContainer ();
103
107
}
104
108
105
109
private function givenAMoreIncompleteConfigProvider (): void
@@ -110,30 +114,7 @@ private function givenAMoreIncompleteConfigProvider(): void
110
114
'event-listeners ' => null
111
115
]
112
116
]);
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 ();
137
118
}
138
119
139
120
private function whenEventDispatcherFactoryIsInvoked (): void
@@ -158,4 +139,14 @@ private function expectAnException(): void
158
139
{
159
140
$ this ->expectException (\RuntimeException::class);
160
141
}
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
+ }
161
152
}
0 commit comments