@@ -22,187 +22,117 @@ class EventDispatcherFactoryTest extends PHPUnit_Framework_TestCase
22
22
23
23
public function testCreateWithNoConfig ()
24
24
{
25
- $ c = $ this ->createMock (ContainerInterface::class);
26
-
27
- $ c ->expects ($ this ->at (0 ))
28
- ->method ('get ' )
29
- ->with (ResultAggregator::class)
30
- ->will ($ this ->returnValue (new ResultAggregator ));
25
+ $ c = $ this ->prophesize (ContainerInterface::class);
26
+ $ c ->get (ResultAggregator::class)->willReturn (new ResultAggregator );
27
+ $ c ->has ('eventListeners ' )->willReturn (false );
31
28
32
- $ dispatcher = (new EventDispatcherFactory )->__invoke ($ c );
29
+ $ dispatcher = (new EventDispatcherFactory )->__invoke ($ c-> reveal () );
33
30
$ this ->assertInstanceOf (EventDispatcher::class, $ dispatcher );
34
31
$ this ->assertSame ([], $ this ->readAttribute ($ dispatcher , 'listeners ' ));
35
32
}
36
33
37
34
public function testExceptionIsThrownIfEventListenerGroupsNotArray ()
38
35
{
39
- $ c = $ this ->createMock (ContainerInterface::class);
40
-
41
- $ c ->expects ($ this ->at (0 ))
42
- ->method ('get ' )
43
- ->with (ResultAggregator::class)
44
- ->will ($ this ->returnValue (new ResultAggregator ));
45
-
46
- $ c ->expects ($ this ->at (1 ))
47
- ->method ('has ' )
48
- ->with ('eventListeners ' )
49
- ->willReturn (true );
50
-
51
- $ c ->expects ($ this ->at (2 ))
52
- ->method ('get ' )
53
- ->with ('eventListeners ' )
54
- ->will ($ this ->returnValue (new \stdClass ));
36
+ $ c = $ this ->prophesize (ContainerInterface::class);
37
+ $ c ->get (ResultAggregator::class)->willReturn (new ResultAggregator );
38
+ $ c ->has ('eventListeners ' )->willReturn (true );
39
+ $ c ->get ('eventListeners ' )->willReturn (new \stdClass );
55
40
56
41
$ this ->expectException (InvalidArgumentException::class);
57
42
$ this ->expectExceptionMessage ('Expected: "array" Received: "stdClass" ' );
58
43
59
- (new EventDispatcherFactory )->__invoke ($ c );
44
+ (new EventDispatcherFactory )->__invoke ($ c-> reveal () );
60
45
}
61
46
62
47
public function testExceptionIsThrownIfEventsNotArray ()
63
48
{
64
- $ c = $ this ->createMock (ContainerInterface::class);
65
-
66
- $ c ->expects ($ this ->at (0 ))
67
- ->method ('get ' )
68
- ->with (ResultAggregator::class)
69
- ->will ($ this ->returnValue (new ResultAggregator ));
70
-
71
- $ c ->expects ($ this ->at (1 ))
72
- ->method ('has ' )
73
- ->with ('eventListeners ' )
74
- ->willReturn (true );
75
-
76
- $ c ->expects ($ this ->at (2 ))
77
- ->method ('get ' )
78
- ->with ('eventListeners ' )
79
- ->will ($ this ->returnValue (['my-group ' => new \stdClass ]));
49
+ $ c = $ this ->prophesize (ContainerInterface::class);
50
+ $ c ->get (ResultAggregator::class)->willReturn (new ResultAggregator );
51
+ $ c ->has ('eventListeners ' )->willReturn (true );
52
+ $ c ->get ('eventListeners ' )->willReturn (['my-group ' => new \stdClass ]);
80
53
81
54
$ this ->expectException (InvalidArgumentException::class);
82
55
$ this ->expectExceptionMessage ('Expected: "array" Received: "stdClass" ' );
83
56
84
- (new EventDispatcherFactory )->__invoke ($ c );
57
+ (new EventDispatcherFactory )->__invoke ($ c-> reveal () );
85
58
}
86
59
87
60
public function testExceptionIsThrownIfEventListenersNotArray ()
88
61
{
89
- $ c = $ this ->createMock (ContainerInterface::class);
90
-
91
- $ c ->expects ($ this ->at (0 ))
92
- ->method ('get ' )
93
- ->with (ResultAggregator::class)
94
- ->will ($ this ->returnValue (new ResultAggregator ));
95
-
96
- $ c ->expects ($ this ->at (1 ))
97
- ->method ('has ' )
98
- ->with ('eventListeners ' )
99
- ->willReturn (true );
100
-
101
- $ c ->expects ($ this ->at (2 ))
102
- ->method ('get ' )
103
- ->with ('eventListeners ' )
104
- ->will ($ this ->returnValue ([
105
- 'my-group ' => [
106
- 'someEvent ' => new \stdClass
107
- ]
108
- ]));
62
+ $ eventConfig = [
63
+ 'my-group ' => [
64
+ 'someEvent ' => new \stdClass
65
+ ]
66
+ ];
67
+
68
+ $ c = $ this ->prophesize (ContainerInterface::class);
69
+ $ c ->get (ResultAggregator::class)->willReturn (new ResultAggregator );
70
+ $ c ->has ('eventListeners ' )->willReturn (true );
71
+ $ c ->get ('eventListeners ' )->willReturn ($ eventConfig );
109
72
110
73
$ this ->expectException (InvalidArgumentException::class);
111
74
$ this ->expectExceptionMessage ('Expected: "array" Received: "stdClass" ' );
112
75
113
- (new EventDispatcherFactory )->__invoke ($ c );
76
+ (new EventDispatcherFactory )->__invoke ($ c-> reveal () );
114
77
}
115
78
116
79
public function testExceptionIsThrownIfListenerNotCallable ()
117
80
{
118
- $ c = $ this ->createMock (ContainerInterface::class);
119
-
120
- $ c ->expects ($ this ->at (0 ))
121
- ->method ('get ' )
122
- ->with (ResultAggregator::class)
123
- ->will ($ this ->returnValue (new ResultAggregator ));
124
-
125
- $ c ->expects ($ this ->at (1 ))
126
- ->method ('has ' )
127
- ->with ('eventListeners ' )
128
- ->willReturn (true );
129
-
130
- $ c ->expects ($ this ->at (2 ))
131
- ->method ('get ' )
132
- ->with ('eventListeners ' )
133
- ->will ($ this ->returnValue ([
134
- 'my-group ' => [
135
- 'someEvent ' => [new \stdClass ]
136
- ]
137
- ]));
81
+ $ eventConfig = [
82
+ 'my-group ' => [
83
+ 'someEvent ' => [new \stdClass ]
84
+ ]
85
+ ];
86
+
87
+ $ c = $ this ->prophesize (ContainerInterface::class);
88
+ $ c ->get (ResultAggregator::class)->willReturn (new ResultAggregator );
89
+ $ c ->has ('eventListeners ' )->willReturn (true );
90
+ $ c ->get ('eventListeners ' )->willReturn ($ eventConfig );
138
91
139
92
$ this ->expectException (InvalidArgumentException::class);
140
93
$ this ->expectExceptionMessage ('Listener must be a callable or a container entry for a callable service. ' );
141
94
142
- (new EventDispatcherFactory )->__invoke ($ c );
95
+ (new EventDispatcherFactory )->__invoke ($ c-> reveal () );
143
96
}
144
97
145
98
public function testExceptionIsThrownIfEventsListenerContainerEntryNotExist ()
146
99
{
147
- $ c = $ this ->createMock (ContainerInterface::class);
148
-
149
- $ c ->expects ($ this ->at (0 ))
150
- ->method ('get ' )
151
- ->with (ResultAggregator::class)
152
- ->will ($ this ->returnValue (new ResultAggregator ));
153
-
154
- $ c ->expects ($ this ->at (1 ))
155
- ->method ('has ' )
156
- ->with ('eventListeners ' )
157
- ->willReturn (true );
158
-
159
- $ c ->expects ($ this ->at (2 ))
160
- ->method ('get ' )
161
- ->with ('eventListeners ' )
162
- ->will ($ this ->returnValue ([
163
- 'my-group ' => [
164
- 'someEvent ' => [containerListener ('nonExistingContainerEntry ' )]
165
- ]
166
- ]));
100
+ $ eventConfig = [
101
+ 'my-group ' => [
102
+ 'someEvent ' => [containerListener ('nonExistingContainerEntry ' )]
103
+ ]
104
+ ];
105
+
106
+ $ c = $ this ->prophesize (ContainerInterface::class);
107
+ $ c ->get (ResultAggregator::class)->willReturn (new ResultAggregator );
108
+ $ c ->has ('eventListeners ' )->willReturn (true );
109
+ $ c ->get ('eventListeners ' )->willReturn ($ eventConfig );
167
110
168
- $ c ->expects ($ this ->at (3 ))
169
- ->method ('has ' )
170
- ->with ('nonExistingContainerEntry ' )
171
- ->will ($ this ->returnValue (false ));
111
+ $ c ->has ('nonExistingContainerEntry ' )->willReturn (false );
172
112
173
113
$ this ->expectException (InvalidArgumentException::class);
174
114
$ this ->expectExceptionMessage ('Container has no entry named: "nonExistingContainerEntry" ' );
175
115
176
- (new EventDispatcherFactory )->__invoke ($ c );
116
+ (new EventDispatcherFactory )->__invoke ($ c-> reveal () );
177
117
}
178
118
179
119
public function testConfigEventListenersWithAnonymousFunction ()
180
120
{
181
- $ c = $ this ->createMock (ContainerInterface::class);
182
-
183
- $ c ->expects ($ this ->at (0 ))
184
- ->method ('get ' )
185
- ->with (ResultAggregator::class)
186
- ->will ($ this ->returnValue (new ResultAggregator ));
187
-
188
121
$ callback = function () {
189
122
};
190
123
191
- $ c ->expects ($ this ->at (1 ))
192
- ->method ('has ' )
193
- ->with ('eventListeners ' )
194
- ->willReturn (true );
195
-
196
- $ c ->expects ($ this ->at (2 ))
197
- ->method ('get ' )
198
- ->with ('eventListeners ' )
199
- ->will ($ this ->returnValue ([
200
- 'my-group ' => [
201
- 'someEvent ' => [$ callback ]
202
- ]
203
- ]));
124
+ $ eventConfig = [
125
+ 'my-group ' => [
126
+ 'someEvent ' => [$ callback ]
127
+ ]
128
+ ];
204
129
205
- $ dispatcher = (new EventDispatcherFactory )->__invoke ($ c );
130
+ $ c = $ this ->prophesize (ContainerInterface::class);
131
+ $ c ->get (ResultAggregator::class)->willReturn (new ResultAggregator );
132
+ $ c ->has ('eventListeners ' )->willReturn (true );
133
+ $ c ->get ('eventListeners ' )->willReturn ($ eventConfig );
134
+
135
+ $ dispatcher = (new EventDispatcherFactory )->__invoke ($ c ->reveal ());
206
136
$ this ->assertInstanceOf (EventDispatcher::class, $ dispatcher );
207
137
$ this ->assertSame (
208
138
[
@@ -216,15 +146,17 @@ public function testConfigEventListenersWithAnonymousFunction()
216
146
217
147
public function testListenerFromContainerIsNotFetchedDuringAttaching ()
218
148
{
149
+ $ eventConfig = [
150
+ 'my-group ' => [
151
+ 'someEvent ' => [containerListener ('containerEntry ' )]
152
+ ]
153
+ ];
154
+
219
155
$ c = $ this ->prophesize (ContainerInterface::class);
220
156
221
157
$ c ->get (ResultAggregator::class)->willReturn (new ResultAggregator );
222
158
$ c ->has ('eventListeners ' )->willReturn (true );
223
- $ c ->get ('eventListeners ' )->willReturn ([
224
- 'my-group ' => [
225
- 'someEvent ' => [containerListener ('containerEntry ' )]
226
- ]
227
- ]);
159
+ $ c ->get ('eventListeners ' )->willReturn ($ eventConfig );
228
160
$ c ->has ('containerEntry ' )->willReturn (true );
229
161
230
162
@@ -237,15 +169,17 @@ public function testListenerFromContainerIsNotFetchedDuringAttaching()
237
169
238
170
public function testListenerFromContainerIsFetchedWhenEventDispatched ()
239
171
{
172
+ $ eventConfig = [
173
+ 'my-group ' => [
174
+ 'someEvent ' => [containerListener ('containerEntry ' )]
175
+ ]
176
+ ];
177
+
240
178
$ c = $ this ->prophesize (ContainerInterface::class);
241
179
242
180
$ c ->get (ResultAggregator::class)->willReturn (new ResultAggregator );
243
181
$ c ->has ('eventListeners ' )->willReturn (true );
244
- $ c ->get ('eventListeners ' )->willReturn ([
245
- 'my-group ' => [
246
- 'someEvent ' => [containerListener ('containerEntry ' )]
247
- ]
248
- ]);
182
+ $ c ->get ('eventListeners ' )->willReturn ($ eventConfig );
249
183
$ c ->has ('containerEntry ' )->willReturn (true );
250
184
$ c ->get ('containerEntry ' )->willReturn (function () {
251
185
});
@@ -259,15 +193,17 @@ public function testListenerFromContainerIsFetchedWhenEventDispatched()
259
193
260
194
public function testExceptionIsThrownIfMethodDoesNotExistOnContainerEntry ()
261
195
{
196
+ $ eventConfig = [
197
+ 'my-group ' => [
198
+ 'someEvent ' => [containerListener ('containerEntry ' , 'notHere ' )]
199
+ ]
200
+ ];
201
+
262
202
$ c = $ this ->prophesize (ContainerInterface::class);
263
203
264
204
$ c ->get (ResultAggregator::class)->willReturn (new ResultAggregator );
265
205
$ c ->has ('eventListeners ' )->willReturn (true );
266
- $ c ->get ('eventListeners ' )->willReturn ([
267
- 'my-group ' => [
268
- 'someEvent ' => [containerListener ('containerEntry ' , 'notHere ' )]
269
- ]
270
- ]);
206
+ $ c ->get ('eventListeners ' )->willReturn ($ eventConfig );
271
207
$ c ->has ('containerEntry ' )->willReturn (true );
272
208
$ c ->get ('containerEntry ' )->willReturn (new \stdClass );
273
209
0 commit comments