Skip to content

Commit 622ff82

Browse files
committed
Refactor to use prophecy
1 parent 4ddf5e4 commit 622ff82

File tree

1 file changed

+80
-144
lines changed

1 file changed

+80
-144
lines changed

test/Factory/EventDispatcherFactoryTest.php

+80-144
Original file line numberDiff line numberDiff line change
@@ -22,187 +22,117 @@ class EventDispatcherFactoryTest extends PHPUnit_Framework_TestCase
2222

2323
public function testCreateWithNoConfig()
2424
{
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);
3128

32-
$dispatcher = (new EventDispatcherFactory)->__invoke($c);
29+
$dispatcher = (new EventDispatcherFactory)->__invoke($c->reveal());
3330
$this->assertInstanceOf(EventDispatcher::class, $dispatcher);
3431
$this->assertSame([], $this->readAttribute($dispatcher, 'listeners'));
3532
}
3633

3734
public function testExceptionIsThrownIfEventListenerGroupsNotArray()
3835
{
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);
5540

5641
$this->expectException(InvalidArgumentException::class);
5742
$this->expectExceptionMessage('Expected: "array" Received: "stdClass"');
5843

59-
(new EventDispatcherFactory)->__invoke($c);
44+
(new EventDispatcherFactory)->__invoke($c->reveal());
6045
}
6146

6247
public function testExceptionIsThrownIfEventsNotArray()
6348
{
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]);
8053

8154
$this->expectException(InvalidArgumentException::class);
8255
$this->expectExceptionMessage('Expected: "array" Received: "stdClass"');
8356

84-
(new EventDispatcherFactory)->__invoke($c);
57+
(new EventDispatcherFactory)->__invoke($c->reveal());
8558
}
8659

8760
public function testExceptionIsThrownIfEventListenersNotArray()
8861
{
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);
10972

11073
$this->expectException(InvalidArgumentException::class);
11174
$this->expectExceptionMessage('Expected: "array" Received: "stdClass"');
11275

113-
(new EventDispatcherFactory)->__invoke($c);
76+
(new EventDispatcherFactory)->__invoke($c->reveal());
11477
}
11578

11679
public function testExceptionIsThrownIfListenerNotCallable()
11780
{
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);
13891

13992
$this->expectException(InvalidArgumentException::class);
14093
$this->expectExceptionMessage('Listener must be a callable or a container entry for a callable service.');
14194

142-
(new EventDispatcherFactory)->__invoke($c);
95+
(new EventDispatcherFactory)->__invoke($c->reveal());
14396
}
14497

14598
public function testExceptionIsThrownIfEventsListenerContainerEntryNotExist()
14699
{
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);
167110

168-
$c->expects($this->at(3))
169-
->method('has')
170-
->with('nonExistingContainerEntry')
171-
->will($this->returnValue(false));
111+
$c->has('nonExistingContainerEntry')->willReturn(false);
172112

173113
$this->expectException(InvalidArgumentException::class);
174114
$this->expectExceptionMessage('Container has no entry named: "nonExistingContainerEntry"');
175115

176-
(new EventDispatcherFactory)->__invoke($c);
116+
(new EventDispatcherFactory)->__invoke($c->reveal());
177117
}
178118

179119
public function testConfigEventListenersWithAnonymousFunction()
180120
{
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-
188121
$callback = function () {
189122
};
190123

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+
];
204129

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());
206136
$this->assertInstanceOf(EventDispatcher::class, $dispatcher);
207137
$this->assertSame(
208138
[
@@ -216,15 +146,17 @@ public function testConfigEventListenersWithAnonymousFunction()
216146

217147
public function testListenerFromContainerIsNotFetchedDuringAttaching()
218148
{
149+
$eventConfig = [
150+
'my-group' => [
151+
'someEvent' => [containerListener('containerEntry')]
152+
]
153+
];
154+
219155
$c = $this->prophesize(ContainerInterface::class);
220156

221157
$c->get(ResultAggregator::class)->willReturn(new ResultAggregator);
222158
$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);
228160
$c->has('containerEntry')->willReturn(true);
229161

230162

@@ -237,15 +169,17 @@ public function testListenerFromContainerIsNotFetchedDuringAttaching()
237169

238170
public function testListenerFromContainerIsFetchedWhenEventDispatched()
239171
{
172+
$eventConfig = [
173+
'my-group' => [
174+
'someEvent' => [containerListener('containerEntry')]
175+
]
176+
];
177+
240178
$c = $this->prophesize(ContainerInterface::class);
241179

242180
$c->get(ResultAggregator::class)->willReturn(new ResultAggregator);
243181
$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);
249183
$c->has('containerEntry')->willReturn(true);
250184
$c->get('containerEntry')->willReturn(function () {
251185
});
@@ -259,15 +193,17 @@ public function testListenerFromContainerIsFetchedWhenEventDispatched()
259193

260194
public function testExceptionIsThrownIfMethodDoesNotExistOnContainerEntry()
261195
{
196+
$eventConfig = [
197+
'my-group' => [
198+
'someEvent' => [containerListener('containerEntry', 'notHere')]
199+
]
200+
];
201+
262202
$c = $this->prophesize(ContainerInterface::class);
263203

264204
$c->get(ResultAggregator::class)->willReturn(new ResultAggregator);
265205
$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);
271207
$c->has('containerEntry')->willReturn(true);
272208
$c->get('containerEntry')->willReturn(new \stdClass);
273209

0 commit comments

Comments
 (0)