1
|
|
|
<?php |
2
|
|
|
namespace FOS\ElasticaBundle\Tests\Doctrine; |
3
|
|
|
|
4
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
use Doctrine\ORM\Configuration; |
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
8
|
|
|
use FOS\ElasticaBundle\Doctrine\RegisterListenersService; |
9
|
|
|
use FOS\ElasticaBundle\Persister\Event\Events; |
10
|
|
|
use FOS\ElasticaBundle\Persister\Event\PostInsertObjectsEvent; |
11
|
|
|
use FOS\ElasticaBundle\Persister\ObjectPersisterInterface; |
12
|
|
|
use FOS\ElasticaBundle\Provider\PagerInterface; |
13
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
14
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
15
|
|
|
|
16
|
|
|
class RegisterListenersServiceTest extends \PHPUnit_Framework_TestCase |
17
|
|
|
{ |
18
|
|
|
public function testCouldBeConstructedWithDispatcherArgument() |
19
|
|
|
{ |
20
|
|
|
new RegisterListenersService($this->createDispatcherMock()); |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
View Code Duplication |
public function testShouldRegisterClearObjectManagerListenerByDefaultAndDispatchOnPostPersistEvent() |
24
|
|
|
{ |
25
|
|
|
$dispatcher = $this->createDispatcher(); |
26
|
|
|
|
27
|
|
|
$service = new RegisterListenersService($dispatcher); |
28
|
|
|
|
29
|
|
|
$manager = $this->createObjectManagerMock(); |
30
|
|
|
$manager |
31
|
|
|
->expects($this->once()) |
32
|
|
|
->method('clear') |
33
|
|
|
; |
34
|
|
|
|
35
|
|
|
$pager = $this->createPagerMock(); |
36
|
|
|
|
37
|
|
|
$service->register($manager, $pager, []); |
38
|
|
|
|
39
|
|
|
$dispatcher->dispatch( |
40
|
|
|
Events::POST_INSERT_OBJECTS, |
41
|
|
|
new PostInsertObjectsEvent($pager, $this->createObjectPersisterMock(), [], []) |
|
|
|
|
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
View Code Duplication |
public function testShouldNotRegisterClearObjectManagerListenerIfOptionFalse() |
46
|
|
|
{ |
47
|
|
|
$dispatcher = $this->createDispatcher(); |
48
|
|
|
|
49
|
|
|
$service = new RegisterListenersService($dispatcher); |
50
|
|
|
|
51
|
|
|
$manager = $this->createObjectManagerMock(); |
52
|
|
|
$manager |
53
|
|
|
->expects($this->never()) |
54
|
|
|
->method('clear') |
55
|
|
|
; |
56
|
|
|
|
57
|
|
|
$pager = $this->createPagerMock(); |
58
|
|
|
|
59
|
|
|
$service->register($manager, $pager, [ |
60
|
|
|
'clear_object_manager' => false |
61
|
|
|
]); |
62
|
|
|
|
63
|
|
|
$dispatcher->dispatch( |
64
|
|
|
Events::POST_INSERT_OBJECTS, |
65
|
|
|
new PostInsertObjectsEvent($pager, $this->createObjectPersisterMock(), [], []) |
|
|
|
|
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
public function testShouldNotCallClearObjectManagerListenerForAnotherPagers() |
70
|
|
|
{ |
71
|
|
|
$dispatcher = $this->createDispatcher(); |
72
|
|
|
|
73
|
|
|
$service = new RegisterListenersService($dispatcher); |
74
|
|
|
|
75
|
|
|
$manager = $this->createObjectManagerMock(); |
76
|
|
|
$manager |
77
|
|
|
->expects($this->never()) |
78
|
|
|
->method('clear') |
79
|
|
|
; |
80
|
|
|
|
81
|
|
|
$pager = $this->createPagerMock(); |
82
|
|
|
$anotherPager = $this->createPagerMock(); |
83
|
|
|
|
84
|
|
|
$service->register($manager, $pager, [ |
85
|
|
|
'clear_object_manager' => true |
86
|
|
|
]); |
87
|
|
|
|
88
|
|
|
$dispatcher->dispatch( |
89
|
|
|
Events::POST_INSERT_OBJECTS, |
90
|
|
|
new PostInsertObjectsEvent($anotherPager, $this->createObjectPersisterMock(), [], []) |
|
|
|
|
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testShouldNotRegisterSleepListenerByDefault() |
95
|
|
|
{ |
96
|
|
|
$dispatcher = $this->createDispatcherMock(); |
97
|
|
|
$dispatcher |
98
|
|
|
->expects($this->never()) |
99
|
|
|
->method('addListener') |
100
|
|
|
->with(Events::POST_INSERT_OBJECTS, $this->isInstanceOf(\Closure::class)) |
101
|
|
|
; |
102
|
|
|
|
103
|
|
|
$service = new RegisterListenersService($dispatcher); |
104
|
|
|
|
105
|
|
|
$manager = $this->createObjectManagerMock(); |
106
|
|
|
|
107
|
|
|
$pager = $this->createPagerMock(); |
108
|
|
|
|
109
|
|
|
$service->register($manager, $pager, [ |
110
|
|
|
'clear_object_manager' => false, |
111
|
|
|
]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
View Code Duplication |
public function testShouldRegisterSleepListenerIfOptionNotZero() |
115
|
|
|
{ |
116
|
|
|
$dispatcher = $this->createDispatcher(); |
117
|
|
|
|
118
|
|
|
$service = new RegisterListenersService($dispatcher); |
119
|
|
|
|
120
|
|
|
$manager = $this->createObjectManagerMock(); |
121
|
|
|
|
122
|
|
|
$pager = $this->createPagerMock(); |
123
|
|
|
|
124
|
|
|
$service->register($manager, $pager, [ |
125
|
|
|
'clear_object_manager' => false, |
126
|
|
|
'sleep' => 2000000, |
127
|
|
|
]); |
128
|
|
|
|
129
|
|
|
$time = microtime(true); |
130
|
|
|
$dispatcher->dispatch( |
131
|
|
|
Events::POST_INSERT_OBJECTS, |
132
|
|
|
new PostInsertObjectsEvent($pager, $this->createObjectPersisterMock(), [], []) |
|
|
|
|
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
$this->assertGreaterThan(1.5, microtime(true) - $time); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
View Code Duplication |
public function testShouldNotCallSleepListenerForAnotherPagers() |
139
|
|
|
{ |
140
|
|
|
$dispatcher = $this->createDispatcher(); |
141
|
|
|
|
142
|
|
|
$service = new RegisterListenersService($dispatcher); |
143
|
|
|
|
144
|
|
|
$manager = $this->createObjectManagerMock(); |
145
|
|
|
|
146
|
|
|
$pager = $this->createPagerMock(); |
147
|
|
|
$anotherPager = $this->createPagerMock(); |
148
|
|
|
|
149
|
|
|
$service->register($manager, $pager, [ |
150
|
|
|
'clear_object_manager' => false, |
151
|
|
|
'sleep' => 2000000, |
152
|
|
|
]); |
153
|
|
|
|
154
|
|
|
$time = microtime(true); |
155
|
|
|
$dispatcher->dispatch( |
156
|
|
|
Events::POST_INSERT_OBJECTS, |
157
|
|
|
new PostInsertObjectsEvent($anotherPager, $this->createObjectPersisterMock(), [], []) |
|
|
|
|
158
|
|
|
); |
159
|
|
|
|
160
|
|
|
$this->assertLessThan(1, microtime(true) - $time); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function testShouldRegisterDisableDebugLoggingByDefaultForEntityManager() |
164
|
|
|
{ |
165
|
|
|
$dispatcher = $this->createDispatcherMock(); |
166
|
|
|
$dispatcher |
167
|
|
|
->expects($this->at(0)) |
168
|
|
|
->method('addListener') |
169
|
|
|
->with(Events::PRE_FETCH_OBJECTS, $this->isInstanceOf(\Closure::class)); |
170
|
|
|
$dispatcher |
171
|
|
|
->expects($this->at(1)) |
172
|
|
|
->method('addListener') |
173
|
|
|
->with(Events::PRE_INSERT_OBJECTS, $this->isInstanceOf(\Closure::class)); |
174
|
|
|
|
175
|
|
|
$service = new RegisterListenersService($dispatcher); |
176
|
|
|
|
177
|
|
|
$configuration = $this->getMock(Configuration::class); |
|
|
|
|
178
|
|
|
$connection = $this->getMock(Connection::class, [], [], '', false); |
|
|
|
|
179
|
|
|
$connection |
180
|
|
|
->expects($this->once()) |
181
|
|
|
->method('getConfiguration') |
182
|
|
|
->willReturn($configuration) |
183
|
|
|
; |
184
|
|
|
|
185
|
|
|
$manager = $this->getMock(EntityManagerInterface::class); |
|
|
|
|
186
|
|
|
$manager |
187
|
|
|
->expects($this->once()) |
188
|
|
|
->method('getConnection') |
189
|
|
|
->willReturn($connection) |
190
|
|
|
; |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
$pager = $this->createPagerMock(); |
194
|
|
|
|
195
|
|
|
$service->register($manager, $pager, [ |
196
|
|
|
'clear_object_manager' => false, |
197
|
|
|
'sleep' => 0, |
198
|
|
|
]); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function testShouldNotRegisterDisableDebugLoggingIfOptionTrueForEntityManager() |
202
|
|
|
{ |
203
|
|
|
$dispatcher = $this->createDispatcherMock(); |
204
|
|
|
$dispatcher |
205
|
|
|
->expects($this->never()) |
206
|
|
|
->method('addListener') |
207
|
|
|
; |
208
|
|
|
|
209
|
|
|
$service = new RegisterListenersService($dispatcher); |
210
|
|
|
|
211
|
|
|
$manager = $this->getMock(EntityManagerInterface::class); |
|
|
|
|
212
|
|
|
$manager |
213
|
|
|
->expects($this->never()) |
214
|
|
|
->method('getConnection') |
215
|
|
|
; |
216
|
|
|
|
217
|
|
|
|
218
|
|
|
$pager = $this->createPagerMock(); |
219
|
|
|
|
220
|
|
|
$service->register($manager, $pager, [ |
221
|
|
|
'clear_object_manager' => false, |
222
|
|
|
'sleep' => 0, |
223
|
|
|
'debug_logging' => true, |
224
|
|
|
]); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public function testShouldRegisterDisableDebugLoggingByDefaultForMongoDBDocumentManager() |
228
|
|
|
{ |
229
|
|
|
if (!class_exists(\Doctrine\ODM\MongoDB\DocumentManager::class)) { |
230
|
|
|
$this->markTestSkipped('Doctrine MongoDB ODM is not available.'); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$dispatcher = $this->createDispatcherMock(); |
234
|
|
|
$dispatcher |
235
|
|
|
->expects($this->at(0)) |
236
|
|
|
->method('addListener') |
237
|
|
|
->with(Events::PRE_FETCH_OBJECTS, $this->isInstanceOf(\Closure::class)); |
238
|
|
|
$dispatcher |
239
|
|
|
->expects($this->at(1)) |
240
|
|
|
->method('addListener') |
241
|
|
|
->with(Events::PRE_INSERT_OBJECTS, $this->isInstanceOf(\Closure::class)); |
242
|
|
|
|
243
|
|
|
$service = new RegisterListenersService($dispatcher); |
244
|
|
|
|
245
|
|
|
$configuration = $this->getMock(Configuration::class); |
|
|
|
|
246
|
|
|
$connection = $this->getMock(Connection::class, [], [], '', false); |
|
|
|
|
247
|
|
|
$connection |
248
|
|
|
->expects($this->once()) |
249
|
|
|
->method('getConfiguration') |
250
|
|
|
->willReturn($configuration) |
251
|
|
|
; |
252
|
|
|
|
253
|
|
|
$manager = $this->getMock(Doctrine\ODM\MongoDB\DocumentManager::class, [], [], '', false); |
|
|
|
|
254
|
|
|
$manager |
255
|
|
|
->expects($this->once()) |
256
|
|
|
->method('getConnection') |
257
|
|
|
->willReturn($connection) |
258
|
|
|
; |
259
|
|
|
|
260
|
|
|
|
261
|
|
|
$pager = $this->createPagerMock(); |
262
|
|
|
|
263
|
|
|
$service->register($manager, $pager, [ |
264
|
|
|
'clear_object_manager' => false, |
265
|
|
|
'sleep' => 0, |
266
|
|
|
]); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public function testShouldNotRegisterDisableDebugLoggingIfOptionTrueForMongoDBDocumentManager() |
270
|
|
|
{ |
271
|
|
|
if (!class_exists(\Doctrine\ODM\MongoDB\DocumentManager::class)) { |
272
|
|
|
$this->markTestSkipped('Doctrine MongoDB ODM is not available.'); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
$dispatcher = $this->createDispatcherMock(); |
276
|
|
|
$dispatcher |
277
|
|
|
->expects($this->never()) |
278
|
|
|
->method('addListener') |
279
|
|
|
; |
280
|
|
|
|
281
|
|
|
$service = new RegisterListenersService($dispatcher); |
282
|
|
|
|
283
|
|
|
$manager = $this->getMock(Doctrine\ODM\MongoDB\DocumentManager::class, [], [], '', false); |
|
|
|
|
284
|
|
|
$manager |
285
|
|
|
->expects($this->never()) |
286
|
|
|
->method('getConnection') |
287
|
|
|
; |
288
|
|
|
|
289
|
|
|
|
290
|
|
|
$pager = $this->createPagerMock(); |
291
|
|
|
|
292
|
|
|
$service->register($manager, $pager, [ |
293
|
|
|
'clear_object_manager' => false, |
294
|
|
|
'sleep' => 0, |
295
|
|
|
'debug_logging' => true, |
296
|
|
|
]); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
public function testShouldIgnoreDebugLoggingOptionForPHPCRManager() |
300
|
|
|
{ |
301
|
|
|
if (!class_exists(\Doctrine\ODM\PHPCR\DocumentManagerInterface::class)) { |
302
|
|
|
$this->markTestSkipped('Doctrine PHPCR is not present'); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
$dispatcher = $this->createDispatcherMock(); |
306
|
|
|
$dispatcher |
307
|
|
|
->expects($this->never()) |
308
|
|
|
->method('addListener') |
309
|
|
|
; |
310
|
|
|
|
311
|
|
|
$service = new RegisterListenersService($dispatcher); |
312
|
|
|
|
313
|
|
|
$manager = $this->getMock(\Doctrine\ODM\PHPCR\DocumentManagerInterface::class); |
|
|
|
|
314
|
|
|
|
315
|
|
|
$pager = $this->createPagerMock(); |
316
|
|
|
|
317
|
|
|
$service->register($manager, $pager, [ |
318
|
|
|
'clear_object_manager' => false, |
319
|
|
|
'sleep' => 0, |
320
|
|
|
]); |
321
|
|
|
|
322
|
|
|
$service->register($manager, $pager, [ |
323
|
|
|
'clear_object_manager' => false, |
324
|
|
|
'sleep' => 0, |
325
|
|
|
'debug_logging' => false, |
326
|
|
|
]); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
private function createPagerMock() |
330
|
|
|
{ |
331
|
|
|
return $this->getMock(PagerInterface::class, [], [], '', false); |
|
|
|
|
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* @return ObjectPersisterInterface|\PHPUnit_Framework_MockObject_MockObject |
336
|
|
|
*/ |
337
|
|
|
private function createObjectPersisterMock() |
338
|
|
|
{ |
339
|
|
|
return $this->getMock(ObjectPersisterInterface::class, [], [], '', false); |
|
|
|
|
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* @return ObjectManager|\PHPUnit_Framework_MockObject_MockObject |
344
|
|
|
*/ |
345
|
|
|
private function createObjectManagerMock() |
346
|
|
|
{ |
347
|
|
|
return $this->getMock(ObjectManager::class, [], [], '', false); |
|
|
|
|
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* @return EventDispatcher |
352
|
|
|
*/ |
353
|
|
|
private function createDispatcher() |
354
|
|
|
{ |
355
|
|
|
return new EventDispatcher(); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|EventDispatcherInterface |
360
|
|
|
*/ |
361
|
|
|
private function createDispatcherMock() |
362
|
|
|
{ |
363
|
|
|
return $this->getMock(EventDispatcherInterface::class, [], [], '', false); |
|
|
|
|
364
|
|
|
} |
365
|
|
|
} |
366
|
|
|
|
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.