1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FOS\ElasticaBundle\Tests\Index; |
4
|
|
|
|
5
|
|
|
use Elastica\Type; |
6
|
|
|
use FOS\ElasticaBundle\Configuration\ConfigManager; |
7
|
|
|
use FOS\ElasticaBundle\Configuration\IndexConfig; |
8
|
|
|
use FOS\ElasticaBundle\Configuration\IndexTemplateConfig; |
9
|
|
|
use FOS\ElasticaBundle\Configuration\TypeConfig; |
10
|
|
|
use FOS\ElasticaBundle\Elastica\Index; |
11
|
|
|
use Elastica\IndexTemplate; |
12
|
|
|
use FOS\ElasticaBundle\Event\IndexResetEvent; |
13
|
|
|
use FOS\ElasticaBundle\Event\TypeResetEvent; |
14
|
|
|
use FOS\ElasticaBundle\Index\AliasProcessor; |
15
|
|
|
use FOS\ElasticaBundle\Index\IndexManager; |
16
|
|
|
use FOS\ElasticaBundle\Index\MappingBuilder; |
17
|
|
|
use FOS\ElasticaBundle\Index\Resetter; |
18
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
19
|
|
|
|
20
|
|
|
class ResetterTest extends \PHPUnit_Framework_TestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var Resetter |
24
|
|
|
*/ |
25
|
|
|
private $resetter; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|AliasProcessor |
29
|
|
|
*/ |
30
|
|
|
private $aliasProcessor; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|ConfigManager |
34
|
|
|
*/ |
35
|
|
|
private $configManager; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|EventDispatcherInterface |
39
|
|
|
*/ |
40
|
|
|
private $dispatcher; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|\Elastica\Client |
44
|
|
|
*/ |
45
|
|
|
private $elasticaClient; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|IndexManager |
49
|
|
|
*/ |
50
|
|
|
private $indexManager; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|MappingBuilder |
54
|
|
|
*/ |
55
|
|
|
private $mappingBuilder; |
56
|
|
|
|
57
|
|
|
public function testResetAllIndexes() |
58
|
|
|
{ |
59
|
|
|
$indexName = 'index1'; |
60
|
|
|
$indexConfig = new IndexConfig($indexName, array(), array()); |
61
|
|
|
$this->mockIndex($indexName, $indexConfig); |
62
|
|
|
|
63
|
|
|
$this->configManager->expects($this->once()) |
64
|
|
|
->method('getIndexNames') |
65
|
|
|
->will($this->returnValue(array($indexName))); |
66
|
|
|
|
67
|
|
|
$this->dispatcherExpects(array( |
68
|
|
|
array(IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')), |
69
|
|
|
array(IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')) |
70
|
|
|
)); |
71
|
|
|
|
72
|
|
|
$this->elasticaClient->expects($this->exactly(2)) |
73
|
|
|
->method('request') |
74
|
|
|
->withConsecutive( |
75
|
|
|
array('index1/', 'DELETE'), |
76
|
|
|
array('index1/', 'PUT', array(), array()) |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
$this->resetter->resetAllIndexes(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testResetAllIndexTemplates() |
83
|
|
|
{ |
84
|
|
|
$indexTemplate = 'index_template1'; |
85
|
|
|
|
86
|
|
|
$config = array( |
87
|
|
|
'template' => 't*' |
88
|
|
|
); |
89
|
|
|
$indexTemplateConfig = new IndexTemplateConfig($indexTemplate, array(), $config); |
90
|
|
|
$this->mockIndexTemplate($indexTemplate, $indexTemplateConfig); |
91
|
|
|
|
92
|
|
|
$this->configManager->expects($this->once()) |
93
|
|
|
->method('getIndexTemplatesNames') |
94
|
|
|
->will($this->returnValue(array($indexTemplate))); |
95
|
|
|
|
96
|
|
|
$this->elasticaClient->expects($this->exactly(1)) |
97
|
|
|
->method('request') |
98
|
|
|
->withConsecutive( |
99
|
|
|
array('/_template/index_template1', 'PUT', array(), array()) |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
$this->resetter->resetAllTemplates(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testResetAllIndexTemplatesAndDeleteIndexes() |
106
|
|
|
{ |
107
|
|
|
$indexTemplate = 'index_template1'; |
108
|
|
|
|
109
|
|
|
$config = array( |
110
|
|
|
'template' => 't*' |
111
|
|
|
); |
112
|
|
|
$indexTemplateConfig = new IndexTemplateConfig($indexTemplate, array(), $config); |
113
|
|
|
$this->mockIndexTemplate($indexTemplate, $indexTemplateConfig); |
114
|
|
|
|
115
|
|
|
$this->configManager->expects($this->once()) |
116
|
|
|
->method('getIndexTemplatesNames') |
117
|
|
|
->will($this->returnValue(array($indexTemplate))); |
118
|
|
|
|
119
|
|
|
$this->elasticaClient->expects($this->at(0)) |
120
|
|
|
->method('request') |
121
|
|
|
->withConsecutive( |
122
|
|
|
array($config['template'] . '/', 'DELETE', array(), array()) |
123
|
|
|
); |
124
|
|
|
$this->elasticaClient->expects($this->at(1)) |
125
|
|
|
->method('request') |
126
|
|
|
->withConsecutive( |
127
|
|
|
array('/_template/index_template1', 'PUT', array(), array()) |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
$this->resetter->resetAllTemplates(true); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testResetIndexTemplate() |
134
|
|
|
{ |
135
|
|
|
$indexTemplate = 'index_template1'; |
136
|
|
|
|
137
|
|
|
$config = array( |
138
|
|
|
'template' => 't*' |
139
|
|
|
); |
140
|
|
|
$indexTemplateConfig = new IndexTemplateConfig($indexTemplate, array(), $config); |
141
|
|
|
$this->mockIndexTemplate($indexTemplate, $indexTemplateConfig); |
142
|
|
|
|
143
|
|
|
$this->elasticaClient->expects($this->exactly(1)) |
144
|
|
|
->method('request') |
145
|
|
|
->withConsecutive( |
146
|
|
|
array('/_template/index_template1', 'PUT', array(), array()) |
147
|
|
|
); |
148
|
|
|
|
149
|
|
|
$this->resetter->resetTemplate('index_template1'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function testResetIndexTemplateAndDeleteIndex() |
153
|
|
|
{ |
154
|
|
|
$indexTemplate = 'index_template1'; |
155
|
|
|
|
156
|
|
|
$config = array( |
157
|
|
|
'template' => 't*' |
158
|
|
|
); |
159
|
|
|
$indexTemplateConfig = new IndexTemplateConfig($indexTemplate, array(), $config); |
160
|
|
|
$this->mockIndexTemplate($indexTemplate, $indexTemplateConfig); |
161
|
|
|
|
162
|
|
|
$this->elasticaClient->expects($this->at(0)) |
163
|
|
|
->method('request') |
164
|
|
|
->withConsecutive( |
165
|
|
|
array($config['template'] . '/', 'DELETE', array(), array()) |
166
|
|
|
); |
167
|
|
|
$this->elasticaClient->expects($this->at(1)) |
168
|
|
|
->method('request') |
169
|
|
|
->withConsecutive( |
170
|
|
|
array('/_template/index_template1', 'PUT', array(), array()) |
171
|
|
|
); |
172
|
|
|
|
173
|
|
|
$this->resetter->resetTemplate('index_template1', true); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function testResetIndex() |
177
|
|
|
{ |
178
|
|
|
$indexConfig = new IndexConfig('index1', array(), array()); |
179
|
|
|
$this->mockIndex('index1', $indexConfig); |
180
|
|
|
|
181
|
|
|
$this->dispatcherExpects(array( |
182
|
|
|
array(IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')), |
183
|
|
|
array(IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')) |
184
|
|
|
)); |
185
|
|
|
|
186
|
|
|
$this->elasticaClient->expects($this->exactly(2)) |
187
|
|
|
->method('request') |
188
|
|
|
->withConsecutive( |
189
|
|
|
array('index1/', 'DELETE'), |
190
|
|
|
array('index1/', 'PUT', array(), array()) |
191
|
|
|
); |
192
|
|
|
|
193
|
|
|
$this->resetter->resetIndex('index1'); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function testResetIndexWithDifferentName() |
197
|
|
|
{ |
198
|
|
|
$indexConfig = new IndexConfig('index1', array(), array( |
199
|
|
|
'elasticSearchName' => 'notIndex1' |
200
|
|
|
)); |
201
|
|
|
$this->mockIndex('index1', $indexConfig); |
202
|
|
|
$this->dispatcherExpects(array( |
203
|
|
|
array(IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')), |
204
|
|
|
array(IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')) |
205
|
|
|
)); |
206
|
|
|
|
207
|
|
|
$this->elasticaClient->expects($this->exactly(2)) |
208
|
|
|
->method('request') |
209
|
|
|
->withConsecutive( |
210
|
|
|
array('index1/', 'DELETE'), |
211
|
|
|
array('index1/', 'PUT', array(), array()) |
212
|
|
|
); |
213
|
|
|
|
214
|
|
|
$this->resetter->resetIndex('index1'); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function testResetIndexWithDifferentNameAndAlias() |
218
|
|
|
{ |
219
|
|
|
$indexConfig = new IndexConfig('index1', array(), array( |
220
|
|
|
'elasticSearchName' => 'notIndex1', |
221
|
|
|
'useAlias' => true |
222
|
|
|
)); |
223
|
|
|
$index = $this->mockIndex('index1', $indexConfig); |
224
|
|
|
$this->dispatcherExpects(array( |
225
|
|
|
array(IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')), |
226
|
|
|
array(IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')) |
227
|
|
|
)); |
228
|
|
|
|
229
|
|
|
$this->aliasProcessor->expects($this->once()) |
230
|
|
|
->method('switchIndexAlias') |
231
|
|
|
->with($indexConfig, $index, false); |
232
|
|
|
|
233
|
|
|
$this->elasticaClient->expects($this->exactly(2)) |
234
|
|
|
->method('request') |
235
|
|
|
->withConsecutive( |
236
|
|
|
array('index1/', 'DELETE'), |
237
|
|
|
array('index1/', 'PUT', array(), array()) |
238
|
|
|
); |
239
|
|
|
|
240
|
|
|
$this->resetter->resetIndex('index1'); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @expectedException \InvalidArgumentException |
245
|
|
|
*/ |
246
|
|
View Code Duplication |
public function testFailureWhenMissingIndexDoesntDispatch() |
|
|
|
|
247
|
|
|
{ |
248
|
|
|
$this->configManager->expects($this->once()) |
249
|
|
|
->method('getIndexConfiguration') |
250
|
|
|
->with('nonExistant') |
251
|
|
|
->will($this->throwException(new \InvalidArgumentException)); |
252
|
|
|
|
253
|
|
|
$this->indexManager->expects($this->never()) |
254
|
|
|
->method('getIndex'); |
255
|
|
|
|
256
|
|
|
$this->resetter->resetIndex('nonExistant'); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public function testResetType() |
260
|
|
|
{ |
261
|
|
|
$typeConfig = new TypeConfig('type', array(), array()); |
262
|
|
|
$indexConfig = new IndexConfig('index', array(), array()); |
263
|
|
|
$this->mockType('type', 'index', $typeConfig, $indexConfig); |
264
|
|
|
|
265
|
|
|
$this->dispatcherExpects(array( |
266
|
|
|
array(TypeResetEvent::PRE_TYPE_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\TypeResetEvent')), |
267
|
|
|
array(TypeResetEvent::POST_TYPE_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\TypeResetEvent')) |
268
|
|
|
)); |
269
|
|
|
|
270
|
|
|
$this->elasticaClient->expects($this->exactly(2)) |
271
|
|
|
->method('request') |
272
|
|
|
->withConsecutive( |
273
|
|
|
array('index/type/', 'DELETE'), |
274
|
|
|
array('index/type/_mapping', 'PUT', array('type' => array()), array()) |
275
|
|
|
); |
276
|
|
|
|
277
|
|
|
$this->resetter->resetIndexType('index', 'type'); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
public function testResetTypeWithChangedSettings() |
281
|
|
|
{ |
282
|
|
|
$settingsValue = array( |
283
|
|
|
'analysis' => array( |
284
|
|
|
'analyzer' => array( |
285
|
|
|
'test_analyzer' => array( |
286
|
|
|
'type' => 'standard', |
287
|
|
|
'tokenizer' => 'standard' |
288
|
|
|
) |
289
|
|
|
) |
290
|
|
|
) |
291
|
|
|
); |
292
|
|
|
$typeConfig = new TypeConfig('type', array(), array()); |
293
|
|
|
$indexConfig = new IndexConfig('index', array(), array('settings' => $settingsValue)); |
294
|
|
|
$this->mockType('type', 'index', $typeConfig, $indexConfig); |
295
|
|
|
|
296
|
|
|
$this->elasticaClient->expects($this->exactly(5)) |
297
|
|
|
->method('request') |
298
|
|
|
->withConsecutive( |
299
|
|
|
array('index/type/', 'DELETE'), |
300
|
|
|
array('index/_close', 'POST'), |
301
|
|
|
array('index/_settings', 'PUT', $settingsValue), |
302
|
|
|
array('index/_open', 'POST'), |
303
|
|
|
array('index/type/_mapping', 'PUT', array('type' => array()), array()) |
304
|
|
|
); |
305
|
|
|
|
306
|
|
|
$this->resetter->resetIndexType('index', 'type'); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @expectedException \InvalidArgumentException |
311
|
|
|
*/ |
312
|
|
View Code Duplication |
public function testNonExistantResetType() |
|
|
|
|
313
|
|
|
{ |
314
|
|
|
$this->configManager->expects($this->once()) |
315
|
|
|
->method('getTypeConfiguration') |
316
|
|
|
->with('index', 'nonExistant') |
317
|
|
|
->will($this->throwException(new \InvalidArgumentException)); |
318
|
|
|
|
319
|
|
|
$this->indexManager->expects($this->never()) |
320
|
|
|
->method('getIndex'); |
321
|
|
|
|
322
|
|
|
$this->resetter->resetIndexType('index', 'nonExistant'); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
public function testPostPopulateWithoutAlias() |
326
|
|
|
{ |
327
|
|
|
$this->mockIndex('index', new IndexConfig('index', array(), array())); |
328
|
|
|
|
329
|
|
|
$this->indexManager->expects($this->never()) |
330
|
|
|
->method('getIndex'); |
331
|
|
|
$this->aliasProcessor->expects($this->never()) |
332
|
|
|
->method('switchIndexAlias'); |
333
|
|
|
|
334
|
|
|
$this->resetter->postPopulate('index'); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
public function testPostPopulate() |
338
|
|
|
{ |
339
|
|
|
$indexConfig = new IndexConfig('index', array(), array( 'useAlias' => true)); |
340
|
|
|
$index = $this->mockIndex('index', $indexConfig); |
341
|
|
|
|
342
|
|
|
$this->aliasProcessor->expects($this->once()) |
343
|
|
|
->method('switchIndexAlias') |
344
|
|
|
->with($indexConfig, $index); |
345
|
|
|
|
346
|
|
|
$this->resetter->postPopulate('index'); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
private function dispatcherExpects(array $events) |
350
|
|
|
{ |
351
|
|
|
$expectation = $this->dispatcher->expects($this->exactly(count($events))) |
352
|
|
|
->method('dispatch'); |
353
|
|
|
|
354
|
|
|
call_user_func_array(array($expectation, 'withConsecutive'), $events); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
View Code Duplication |
private function mockIndex($indexName, IndexConfig $config, $mapping = array()) |
|
|
|
|
358
|
|
|
{ |
359
|
|
|
$this->configManager->expects($this->atLeast(1)) |
360
|
|
|
->method('getIndexConfiguration') |
361
|
|
|
->with($indexName) |
362
|
|
|
->will($this->returnValue($config)); |
363
|
|
|
$index = new Index($this->elasticaClient, $indexName); |
364
|
|
|
$this->indexManager->expects($this->any()) |
365
|
|
|
->method('getIndex') |
366
|
|
|
->with($indexName) |
367
|
|
|
->willReturn($index); |
368
|
|
|
$this->mappingBuilder->expects($this->any()) |
369
|
|
|
->method('buildIndexMapping') |
370
|
|
|
->with($config) |
371
|
|
|
->willReturn($mapping); |
372
|
|
|
|
373
|
|
|
return $index; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
View Code Duplication |
private function mockIndexTemplate($indexTemplateName, IndexTemplateConfig $config, $mapping = array()) |
|
|
|
|
377
|
|
|
{ |
378
|
|
|
$this->configManager->expects($this->atLeast(1)) |
379
|
|
|
->method('getIndexTemplateConfiguration') |
380
|
|
|
->with($indexTemplateName) |
381
|
|
|
->will($this->returnValue($config)); |
382
|
|
|
$index = new IndexTemplate($this->elasticaClient, $indexTemplateName); |
383
|
|
|
$this->indexManager->expects($this->any()) |
384
|
|
|
->method('getIndexTemplate') |
385
|
|
|
->with($indexTemplateName) |
386
|
|
|
->willReturn($index); |
387
|
|
|
$this->mappingBuilder->expects($this->any()) |
388
|
|
|
->method('buildIndexTemplateMapping') |
389
|
|
|
->with($config) |
390
|
|
|
->willReturn($mapping); |
391
|
|
|
|
392
|
|
|
return $index; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
private function mockType($typeName, $indexName, TypeConfig $typeConfig, IndexConfig $indexConfig, $mapping = array()) |
396
|
|
|
{ |
397
|
|
|
$this->configManager->expects($this->atLeast(1)) |
398
|
|
|
->method('getTypeConfiguration') |
399
|
|
|
->with($indexName, $typeName) |
400
|
|
|
->will($this->returnValue($typeConfig)); |
401
|
|
|
$index = new Index($this->elasticaClient, $indexName); |
402
|
|
|
$this->indexManager->expects($this->once()) |
403
|
|
|
->method('getIndex') |
404
|
|
|
->with($indexName) |
405
|
|
|
->willReturn($index); |
406
|
|
|
$this->configManager->expects($this->atLeast(1)) |
407
|
|
|
->method('getIndexConfiguration') |
408
|
|
|
->with($indexName) |
409
|
|
|
->will($this->returnValue($indexConfig)); |
410
|
|
|
$this->mappingBuilder->expects($this->once()) |
411
|
|
|
->method('buildTypeMapping') |
412
|
|
|
->with($typeConfig) |
413
|
|
|
->willReturn($mapping); |
414
|
|
|
|
415
|
|
|
return $index; |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
protected function setUp() |
419
|
|
|
{ |
420
|
|
|
$this->aliasProcessor = $this->getMockBuilder('FOS\\ElasticaBundle\\Index\\AliasProcessor') |
421
|
|
|
->disableOriginalConstructor() |
422
|
|
|
->getMock(); |
423
|
|
|
$this->configManager = $this->getMockBuilder('FOS\\ElasticaBundle\\Configuration\\ConfigManager') |
424
|
|
|
->disableOriginalConstructor() |
425
|
|
|
->getMock(); |
426
|
|
|
$this->dispatcher = $this->getMockBuilder('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface') |
427
|
|
|
->getMock(); |
428
|
|
|
$this->elasticaClient = $this->getMockBuilder('Elastica\\Client') |
429
|
|
|
->disableOriginalConstructor() |
430
|
|
|
->getMock(); |
431
|
|
|
$this->indexManager = $this->getMockBuilder('FOS\\ElasticaBundle\\Index\\IndexManager') |
432
|
|
|
->disableOriginalConstructor() |
433
|
|
|
->getMock(); |
434
|
|
|
$this->mappingBuilder = $this->getMockBuilder('FOS\\ElasticaBundle\\Index\\MappingBuilder') |
435
|
|
|
->disableOriginalConstructor() |
436
|
|
|
->getMock(); |
437
|
|
|
|
438
|
|
|
$this->resetter = new Resetter( |
439
|
|
|
$this->configManager, |
440
|
|
|
$this->indexManager, |
441
|
|
|
$this->aliasProcessor, |
442
|
|
|
$this->mappingBuilder, |
443
|
|
|
$this->dispatcher, |
444
|
|
|
$this->elasticaClient |
445
|
|
|
); |
446
|
|
|
} |
447
|
|
|
} |
448
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.