1
|
|
|
<?php |
2
|
|
|
namespace FOS\ElasticaBundle\Tests\Persister; |
3
|
|
|
|
4
|
|
|
use FOS\ElasticaBundle\Persister\Event\Events; |
5
|
|
|
use FOS\ElasticaBundle\Persister\InPlacePagerPersister; |
6
|
|
|
use FOS\ElasticaBundle\Persister\ObjectPersisterInterface; |
7
|
|
|
use FOS\ElasticaBundle\Persister\Event\OnExceptionEvent; |
8
|
|
|
use FOS\ElasticaBundle\Persister\PagerPersisterInterface; |
9
|
|
|
use FOS\ElasticaBundle\Persister\PersisterRegistry; |
10
|
|
|
use FOS\ElasticaBundle\Persister\Event\PostInsertObjectsEvent; |
11
|
|
|
use FOS\ElasticaBundle\Persister\Event\PostPersistEvent; |
12
|
|
|
use FOS\ElasticaBundle\Persister\Event\PreFetchObjectsEvent; |
13
|
|
|
use FOS\ElasticaBundle\Persister\Event\PreInsertObjectsEvent; |
14
|
|
|
use FOS\ElasticaBundle\Persister\Event\PrePersistEvent; |
15
|
|
|
use FOS\ElasticaBundle\Provider\PagerfantaPager; |
16
|
|
|
use Pagerfanta\Adapter\ArrayAdapter; |
17
|
|
|
use Pagerfanta\Pagerfanta; |
18
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
19
|
|
|
|
20
|
|
|
class InPlacePagerPersisterTest extends \PHPUnit_Framework_TestCase |
21
|
|
|
{ |
22
|
|
|
public function testShouldImplementPagerPersisterInterface() |
23
|
|
|
{ |
24
|
|
|
$rc = new \ReflectionClass(InPlacePagerPersister::class); |
25
|
|
|
|
26
|
|
|
$this->assertTrue($rc->implementsInterface(PagerPersisterInterface::class)); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testCouldBeConstructedWithPersisterRegistryAndDispatcherAsArguments() |
30
|
|
|
{ |
31
|
|
|
new InPlacePagerPersister($this->createPersisterRegistryMock(), new EventDispatcher()); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testShouldDispatchPrePersistEventWithExpectedArguments() |
35
|
|
|
{ |
36
|
|
|
$objectPersisterMock = $this->createObjectPersisterMock(); |
37
|
|
|
|
38
|
|
|
$options = ['indexName' => 'theIndex', 'typeName' => 'theType', 'batch_size' => 10]; |
39
|
|
|
|
40
|
|
|
$registryMock = $this->createPersisterRegistryStub($objectPersisterMock); |
41
|
|
|
$dispatcher = new EventDispatcher(); |
42
|
|
|
|
43
|
|
|
$persister = new InPlacePagerPersister($registryMock, $dispatcher); |
44
|
|
|
|
45
|
|
|
$pager = $this->createPager([new \stdClass(), new \stdClass()]); |
46
|
|
|
|
47
|
|
|
$called = false; |
48
|
|
|
$dispatcher->addListener(Events::PRE_PERSIST, function($event) use(&$called, $pager, $objectPersisterMock, $options) { |
49
|
|
|
$called = true; |
50
|
|
|
|
51
|
|
|
$this->assertInstanceOf(PrePersistEvent::class, $event); |
52
|
|
|
$this->assertSame($pager, $event->getPager()); |
53
|
|
|
$this->assertSame($objectPersisterMock, $event->getObjectPersister()); |
54
|
|
|
$this->assertSame($options, $event->getOptions()); |
55
|
|
|
}); |
56
|
|
|
|
57
|
|
|
$persister->insert($pager, $options); |
58
|
|
|
|
59
|
|
|
$this->assertTrue($called); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testShouldDispatchPreFetchObjectsEventWithExpectedArguments() |
63
|
|
|
{ |
64
|
|
|
$objectPersisterMock = $this->createObjectPersisterMock(); |
65
|
|
|
|
66
|
|
|
$options = ['indexName' => 'theIndex', 'typeName' => 'theType', 'batch_size' => 10]; |
67
|
|
|
|
68
|
|
|
$registryMock = $this->createPersisterRegistryStub($objectPersisterMock); |
69
|
|
|
$dispatcher = new EventDispatcher(); |
70
|
|
|
|
71
|
|
|
$persister = new InPlacePagerPersister($registryMock, $dispatcher); |
72
|
|
|
|
73
|
|
|
$objects = [new \stdClass(), new \stdClass()]; |
74
|
|
|
|
75
|
|
|
$pager = $this->createPager($objects); |
76
|
|
|
|
77
|
|
|
$called = false; |
78
|
|
|
$dispatcher->addListener(Events::PRE_FETCH_OBJECTS, function($event) use(&$called, $pager, $objectPersisterMock, $options) { |
79
|
|
|
$called = true; |
80
|
|
|
|
81
|
|
|
$this->assertInstanceOf(PreFetchObjectsEvent::class, $event); |
82
|
|
|
$this->assertSame($pager, $event->getPager()); |
83
|
|
|
$this->assertSame($objectPersisterMock, $event->getObjectPersister()); |
84
|
|
|
$this->assertSame($options, $event->getOptions()); |
85
|
|
|
}); |
86
|
|
|
|
87
|
|
|
$persister->insert($pager, $options); |
88
|
|
|
|
89
|
|
|
$this->assertTrue($called); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
View Code Duplication |
public function testShouldDispatchPreInsertObjectsEventWithExpectedArguments() |
93
|
|
|
{ |
94
|
|
|
$objectPersisterMock = $this->createObjectPersisterMock(); |
95
|
|
|
|
96
|
|
|
$options = ['indexName' => 'theIndex', 'typeName' => 'theType', 'batch_size' => 10]; |
97
|
|
|
|
98
|
|
|
$registryMock = $this->createPersisterRegistryStub($objectPersisterMock); |
99
|
|
|
$dispatcher = new EventDispatcher(); |
100
|
|
|
|
101
|
|
|
$persister = new InPlacePagerPersister($registryMock, $dispatcher); |
102
|
|
|
|
103
|
|
|
$objects = [new \stdClass(), new \stdClass()]; |
104
|
|
|
|
105
|
|
|
$pager = $this->createPager($objects); |
106
|
|
|
|
107
|
|
|
$called = false; |
108
|
|
|
$dispatcher->addListener(Events::PRE_INSERT_OBJECTS, function($event) use(&$called, $pager, $objectPersisterMock, $objects, $options) { |
109
|
|
|
$called = true; |
110
|
|
|
|
111
|
|
|
$this->assertInstanceOf(PreInsertObjectsEvent::class, $event); |
112
|
|
|
$this->assertSame($pager, $event->getPager()); |
113
|
|
|
$this->assertSame($objectPersisterMock, $event->getObjectPersister()); |
114
|
|
|
$this->assertSame($options, $event->getOptions()); |
115
|
|
|
$this->assertSame($objects, $event->getObjects()); |
116
|
|
|
}); |
117
|
|
|
|
118
|
|
|
$persister->insert($pager, $options); |
119
|
|
|
|
120
|
|
|
$this->assertTrue($called); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
View Code Duplication |
public function testShouldDispatchPostInsertObjectsEventWithExpectedArguments() |
124
|
|
|
{ |
125
|
|
|
$objectPersisterMock = $this->createObjectPersisterMock(); |
126
|
|
|
|
127
|
|
|
$options = ['indexName' => 'theIndex', 'typeName' => 'theType', 'batch_size' => 10]; |
128
|
|
|
|
129
|
|
|
$registryMock = $this->createPersisterRegistryStub($objectPersisterMock); |
130
|
|
|
$dispatcher = new EventDispatcher(); |
131
|
|
|
|
132
|
|
|
$persister = new InPlacePagerPersister($registryMock, $dispatcher); |
133
|
|
|
|
134
|
|
|
$objects = [new \stdClass(), new \stdClass()]; |
135
|
|
|
|
136
|
|
|
$pager = $this->createPager($objects); |
137
|
|
|
|
138
|
|
|
$called = false; |
139
|
|
|
$dispatcher->addListener(Events::POST_INSERT_OBJECTS, function($event) use(&$called, $pager, $objectPersisterMock, $objects, $options) { |
140
|
|
|
$called = true; |
141
|
|
|
|
142
|
|
|
$this->assertInstanceOf(PostInsertObjectsEvent::class, $event); |
143
|
|
|
$this->assertSame($pager, $event->getPager()); |
144
|
|
|
$this->assertSame($objectPersisterMock, $event->getObjectPersister()); |
145
|
|
|
$this->assertSame($options, $event->getOptions()); |
146
|
|
|
$this->assertSame($objects, $event->getObjects()); |
147
|
|
|
}); |
148
|
|
|
|
149
|
|
|
$persister->insert($pager, $options); |
150
|
|
|
|
151
|
|
|
$this->assertTrue($called); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
View Code Duplication |
public function testShouldDispatchPostPersistEventWithExpectedArguments() |
155
|
|
|
{ |
156
|
|
|
$objectPersisterMock = $this->createObjectPersisterMock(); |
157
|
|
|
|
158
|
|
|
$options = ['indexName' => 'theIndex', 'typeName' => 'theType', 'batch_size' => 10]; |
159
|
|
|
|
160
|
|
|
$registryMock = $this->createPersisterRegistryStub($objectPersisterMock); |
161
|
|
|
$dispatcher = new EventDispatcher(); |
162
|
|
|
|
163
|
|
|
$persister = new InPlacePagerPersister($registryMock, $dispatcher); |
164
|
|
|
|
165
|
|
|
$objects = [new \stdClass(), new \stdClass()]; |
166
|
|
|
|
167
|
|
|
$pager = $this->createPager($objects); |
168
|
|
|
|
169
|
|
|
$called = false; |
170
|
|
|
$dispatcher->addListener(Events::POST_PERSIST, function($event) use(&$called, $pager, $objectPersisterMock, $objects, $options) { |
171
|
|
|
$called = true; |
172
|
|
|
|
173
|
|
|
$this->assertInstanceOf(PostPersistEvent::class, $event); |
174
|
|
|
$this->assertSame($pager, $event->getPager()); |
175
|
|
|
$this->assertSame($objectPersisterMock, $event->getObjectPersister()); |
176
|
|
|
$this->assertSame($options, $event->getOptions()); |
177
|
|
|
}); |
178
|
|
|
|
179
|
|
|
$persister->insert($pager, $options); |
180
|
|
|
|
181
|
|
|
$this->assertTrue($called); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function testShouldCallObjectPersisterInsertManyMethodForEachPage() |
185
|
|
|
{ |
186
|
|
|
$options = ['indexName' => 'theIndex', 'typeName' => 'theType', 'batch_size' => 2]; |
187
|
|
|
|
188
|
|
|
$firstPage = [new \stdClass(), new \stdClass()]; |
189
|
|
|
$secondPage = [new \stdClass(), new \stdClass()]; |
190
|
|
|
$thirdPage = [new \stdClass(), new \stdClass()]; |
191
|
|
|
|
192
|
|
|
$objects = [$firstPage[0], $firstPage[1], $secondPage[0], $secondPage[1], $thirdPage[0], $thirdPage[1]]; |
193
|
|
|
|
194
|
|
|
$objectPersisterMock = $this->createObjectPersisterMock(); |
195
|
|
|
$objectPersisterMock |
196
|
|
|
->expects($this->exactly(3)) |
197
|
|
|
->method('insertMany') |
198
|
|
|
->withConsecutive($this->identicalTo([$firstPage]), $this->identicalTo([$secondPage]), $this->identicalTo([$thirdPage])) |
199
|
|
|
; |
200
|
|
|
|
201
|
|
|
$registryMock = $this->createPersisterRegistryStub($objectPersisterMock); |
202
|
|
|
|
203
|
|
|
$persister = new InPlacePagerPersister($registryMock, new EventDispatcher()); |
204
|
|
|
|
205
|
|
|
$pager = $this->createPager($objects); |
206
|
|
|
|
207
|
|
|
$persister->insert($pager, $options); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function testShouldDispatchOnExceptionEventWithExpectedArgumentsAndReThrowIt() |
211
|
|
|
{ |
212
|
|
|
$exception = new \LogicException(); |
213
|
|
|
|
214
|
|
|
$objectPersisterMock = $this->createObjectPersisterMock(); |
215
|
|
|
$objectPersisterMock |
216
|
|
|
->expects($this->once()) |
217
|
|
|
->method('insertMany') |
218
|
|
|
->willThrowException($exception) |
219
|
|
|
; |
220
|
|
|
|
221
|
|
|
|
222
|
|
|
$options = ['indexName' => 'theIndex', 'typeName' => 'theType', 'batch_size' => 10]; |
223
|
|
|
|
224
|
|
|
$registryMock = $this->createPersisterRegistryStub($objectPersisterMock); |
225
|
|
|
$dispatcher = new EventDispatcher(); |
226
|
|
|
|
227
|
|
|
$persister = new InPlacePagerPersister($registryMock, $dispatcher); |
228
|
|
|
|
229
|
|
|
$objects = [new \stdClass(), new \stdClass()]; |
230
|
|
|
|
231
|
|
|
$pager = $this->createPager($objects); |
232
|
|
|
|
233
|
|
|
$called = false; |
234
|
|
|
$dispatcher->addListener(Events::ON_EXCEPTION, function($event) use(&$called, $pager, $objectPersisterMock, $exception, $options) { |
235
|
|
|
$called = true; |
236
|
|
|
|
237
|
|
|
$this->assertInstanceOf(OnExceptionEvent::class, $event); |
238
|
|
|
$this->assertSame($pager, $event->getPager()); |
239
|
|
|
$this->assertSame($objectPersisterMock, $event->getObjectPersister()); |
240
|
|
|
$this->assertSame($options, $event->getOptions()); |
241
|
|
|
$this->assertSame($exception, $event->getException()); |
242
|
|
|
}); |
243
|
|
|
|
244
|
|
|
try { |
245
|
|
|
$persister->insert($pager, $options); |
246
|
|
|
} catch (\Exception $e) { |
247
|
|
|
$this->assertTrue($called); |
248
|
|
|
$this->assertSame($exception, $e); |
249
|
|
|
|
250
|
|
|
return; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
$this->fail('The exception is expected to be thrown'); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
public function testShouldDispatchOnExceptionEventButNotReThrowIt() |
257
|
|
|
{ |
258
|
|
|
$exception = new \LogicException(); |
259
|
|
|
|
260
|
|
|
$objectPersisterMock = $this->createObjectPersisterMock(); |
261
|
|
|
$objectPersisterMock |
262
|
|
|
->expects($this->once()) |
263
|
|
|
->method('insertMany') |
264
|
|
|
->willThrowException($exception) |
265
|
|
|
; |
266
|
|
|
|
267
|
|
|
|
268
|
|
|
$options = ['indexName' => 'theIndex', 'typeName' => 'theType', 'batch_size' => 10]; |
269
|
|
|
|
270
|
|
|
$registryMock = $this->createPersisterRegistryStub($objectPersisterMock); |
271
|
|
|
$dispatcher = new EventDispatcher(); |
272
|
|
|
|
273
|
|
|
$persister = new InPlacePagerPersister($registryMock, $dispatcher); |
274
|
|
|
|
275
|
|
|
$objects = [new \stdClass(), new \stdClass()]; |
276
|
|
|
|
277
|
|
|
$pager = $this->createPager($objects); |
278
|
|
|
|
279
|
|
|
$called = false; |
280
|
|
|
$dispatcher->addListener(Events::ON_EXCEPTION, function(OnExceptionEvent $event) use(&$called) { |
281
|
|
|
$called = true; |
282
|
|
|
|
283
|
|
|
$event->setIgnore(true); |
284
|
|
|
}); |
285
|
|
|
|
286
|
|
|
$persister->insert($pager, $options); |
287
|
|
|
|
288
|
|
|
$this->assertTrue($called); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
private function createPager(array $objects) |
292
|
|
|
{ |
293
|
|
|
return new PagerfantaPager(new Pagerfanta(new ArrayAdapter($objects))); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @return ObjectPersisterInterface|\PHPUnit_Framework_MockObject_MockObject |
298
|
|
|
*/ |
299
|
|
|
private function createObjectPersisterMock() |
300
|
|
|
{ |
301
|
|
|
return $this->getMock(ObjectPersisterInterface::class, [], [], '', false); |
|
|
|
|
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @return PersisterRegistry|\PHPUnit_Framework_MockObject_MockObject |
306
|
|
|
*/ |
307
|
|
|
private function createPersisterRegistryMock() |
308
|
|
|
{ |
309
|
|
|
return $this->getMock(PersisterRegistry::class, [], [], '', false); |
|
|
|
|
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @return PersisterRegistry|\PHPUnit_Framework_MockObject_MockObject |
314
|
|
|
*/ |
315
|
|
|
private function createPersisterRegistryStub($objectPersister = null) |
316
|
|
|
{ |
317
|
|
|
$registryMock = $this->createPersisterRegistryMock(); |
318
|
|
|
$registryMock |
319
|
|
|
->expects($this->once()) |
320
|
|
|
->method('getPersister') |
321
|
|
|
->with('theIndex', 'theType') |
322
|
|
|
->willReturn($objectPersister) |
323
|
|
|
; |
324
|
|
|
|
325
|
|
|
return $registryMock; |
326
|
|
|
} |
327
|
|
|
} |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.