1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FOS\ElasticaBundle\Tests\Index; |
4
|
|
|
|
5
|
|
|
use Elastica\Exception\ResponseException; |
6
|
|
|
use Elastica\Request; |
7
|
|
|
use Elastica\Response; |
8
|
|
|
use Elastica\Type; |
9
|
|
|
use Elastica\Type\Mapping; |
10
|
|
|
use FOS\ElasticaBundle\Configuration\IndexConfig; |
11
|
|
|
use FOS\ElasticaBundle\Configuration\TypeConfig; |
12
|
|
|
use FOS\ElasticaBundle\Elastica\Index; |
13
|
|
|
use FOS\ElasticaBundle\Event\IndexResetEvent; |
14
|
|
|
use FOS\ElasticaBundle\Event\TypeResetEvent; |
15
|
|
|
use FOS\ElasticaBundle\Index\AliasProcessor; |
16
|
|
|
use FOS\ElasticaBundle\Index\Resetter; |
17
|
|
|
|
18
|
|
|
class ResetterTest extends \PHPUnit_Framework_TestCase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var Resetter |
22
|
|
|
*/ |
23
|
|
|
private $resetter; |
24
|
|
|
|
25
|
|
|
private $aliasProcessor; |
26
|
|
|
private $configManager; |
27
|
|
|
private $dispatcher; |
28
|
|
|
private $elasticaClient; |
29
|
|
|
private $indexManager; |
30
|
|
|
private $mappingBuilder; |
31
|
|
|
|
32
|
|
|
public function testResetAllIndexes() |
33
|
|
|
{ |
34
|
|
|
$indexName = 'index1'; |
35
|
|
|
$indexConfig = new IndexConfig($indexName, array(), array()); |
36
|
|
|
$this->mockIndex($indexName, $indexConfig); |
37
|
|
|
|
38
|
|
|
$this->configManager->expects($this->once()) |
39
|
|
|
->method('getIndexNames') |
40
|
|
|
->will($this->returnValue(array($indexName))); |
41
|
|
|
|
42
|
|
|
$this->dispatcherExpects(array( |
43
|
|
|
array(IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')), |
44
|
|
|
array(IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')) |
45
|
|
|
)); |
46
|
|
|
|
47
|
|
|
$this->elasticaClient->expects($this->exactly(2)) |
48
|
|
|
->method('request') |
49
|
|
|
->withConsecutive( |
50
|
|
|
array('index1/', 'DELETE'), |
51
|
|
|
array('index1/', 'PUT', array(), array()) |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
$this->resetter->resetAllIndexes(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testResetIndex() |
58
|
|
|
{ |
59
|
|
|
$indexConfig = new IndexConfig('index1', array(), array()); |
60
|
|
|
$this->mockIndex('index1', $indexConfig); |
61
|
|
|
|
62
|
|
|
$this->dispatcherExpects(array( |
63
|
|
|
array(IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')), |
64
|
|
|
array(IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')) |
65
|
|
|
)); |
66
|
|
|
|
67
|
|
|
$this->elasticaClient->expects($this->exactly(2)) |
68
|
|
|
->method('request') |
69
|
|
|
->withConsecutive( |
70
|
|
|
array('index1/', 'DELETE'), |
71
|
|
|
array('index1/', 'PUT', array(), array()) |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
$this->resetter->resetIndex('index1'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testResetIndexWithDifferentName() |
78
|
|
|
{ |
79
|
|
|
$indexConfig = new IndexConfig('index1', array(), array( |
80
|
|
|
'elasticSearchName' => 'notIndex1' |
81
|
|
|
)); |
82
|
|
|
$this->mockIndex('index1', $indexConfig); |
83
|
|
|
$this->dispatcherExpects(array( |
84
|
|
|
array(IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')), |
85
|
|
|
array(IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')) |
86
|
|
|
)); |
87
|
|
|
|
88
|
|
|
$this->elasticaClient->expects($this->exactly(2)) |
89
|
|
|
->method('request') |
90
|
|
|
->withConsecutive( |
91
|
|
|
array('index1/', 'DELETE'), |
92
|
|
|
array('index1/', 'PUT', array(), array()) |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
$this->resetter->resetIndex('index1'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testResetIndexWithDifferentNameAndAlias() |
99
|
|
|
{ |
100
|
|
|
$indexConfig = new IndexConfig('index1', array(), array( |
101
|
|
|
'elasticSearchName' => 'notIndex1', |
102
|
|
|
'useAlias' => true |
103
|
|
|
)); |
104
|
|
|
$index = $this->mockIndex('index1', $indexConfig); |
105
|
|
|
$this->dispatcherExpects(array( |
106
|
|
|
array(IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')), |
107
|
|
|
array(IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')) |
108
|
|
|
)); |
109
|
|
|
|
110
|
|
|
$this->aliasProcessor->expects($this->once()) |
111
|
|
|
->method('switchIndexAlias') |
112
|
|
|
->with($indexConfig, $index, false); |
113
|
|
|
|
114
|
|
|
$this->elasticaClient->expects($this->exactly(2)) |
115
|
|
|
->method('request') |
116
|
|
|
->withConsecutive( |
117
|
|
|
array('index1/', 'DELETE'), |
118
|
|
|
array('index1/', 'PUT', array(), array()) |
119
|
|
|
); |
120
|
|
|
|
121
|
|
|
$this->resetter->resetIndex('index1'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @expectedException \InvalidArgumentException |
126
|
|
|
*/ |
127
|
|
View Code Duplication |
public function testFailureWhenMissingIndexDoesntDispatch() |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
$this->configManager->expects($this->once()) |
130
|
|
|
->method('getIndexConfiguration') |
131
|
|
|
->with('nonExistant') |
132
|
|
|
->will($this->throwException(new \InvalidArgumentException)); |
133
|
|
|
|
134
|
|
|
$this->indexManager->expects($this->never()) |
135
|
|
|
->method('getIndex'); |
136
|
|
|
|
137
|
|
|
$this->resetter->resetIndex('nonExistant'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function testResetType() |
141
|
|
|
{ |
142
|
|
|
$typeConfig = new TypeConfig('type', array(), array()); |
143
|
|
|
$indexConfig = new IndexConfig('index', array(), array()); |
144
|
|
|
$this->mockType('type', 'index', $typeConfig, $indexConfig); |
145
|
|
|
|
146
|
|
|
$this->dispatcherExpects(array( |
147
|
|
|
array(IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')), |
148
|
|
|
array(IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')), |
149
|
|
|
array(TypeResetEvent::PRE_TYPE_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\TypeResetEvent')), |
150
|
|
|
array(TypeResetEvent::POST_TYPE_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\TypeResetEvent')) |
151
|
|
|
)); |
152
|
|
|
|
153
|
|
|
$this->elasticaClient->expects($this->exactly(3)) |
154
|
|
|
->method('request') |
155
|
|
|
->withConsecutive( |
156
|
|
|
array('index/', 'DELETE'), |
157
|
|
|
array('index/', 'PUT', array(), array()), |
158
|
|
|
array('index/type/_mapping', 'PUT', array('type' => array()), array()) |
159
|
|
|
); |
160
|
|
|
|
161
|
|
|
$this->resetter->resetIndexType('index', 'type'); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function testResetTypeWithChangedSettings() |
165
|
|
|
{ |
166
|
|
|
$settingsValue = array( |
167
|
|
|
'analysis' => array( |
168
|
|
|
'analyzer' => array( |
169
|
|
|
'test_analyzer' => array( |
170
|
|
|
'type' => 'standard', |
171
|
|
|
'tokenizer' => 'standard' |
172
|
|
|
) |
173
|
|
|
) |
174
|
|
|
) |
175
|
|
|
); |
176
|
|
|
$typeConfig = new TypeConfig('type', array(), array()); |
177
|
|
|
$indexConfig = new IndexConfig('index', array(), array('settings' => $settingsValue)); |
178
|
|
|
$this->mockType('type', 'index', $typeConfig, $indexConfig); |
179
|
|
|
|
180
|
|
|
$this->elasticaClient->expects($this->exactly(3)) |
181
|
|
|
->method('request') |
182
|
|
|
->withConsecutive( |
183
|
|
|
array('index/', 'DELETE'), |
184
|
|
|
array('index/', 'PUT', array(), array()), |
185
|
|
|
array('index/type/_mapping', 'PUT', array('type' => array()), array()) |
186
|
|
|
); |
187
|
|
|
|
188
|
|
|
$this->resetter->resetIndexType('index', 'type'); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @expectedException \InvalidArgumentException |
193
|
|
|
*/ |
194
|
|
View Code Duplication |
public function testNonExistantResetType() |
|
|
|
|
195
|
|
|
{ |
196
|
|
|
$this->configManager->expects($this->once()) |
197
|
|
|
->method('getTypeConfiguration') |
198
|
|
|
->with('index', 'nonExistant') |
199
|
|
|
->will($this->throwException(new \InvalidArgumentException)); |
200
|
|
|
|
201
|
|
|
$this->indexManager->expects($this->never()) |
202
|
|
|
->method('getIndex'); |
203
|
|
|
|
204
|
|
|
$this->resetter->resetIndexType('index', 'nonExistant'); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function testPostPopulateWithoutAlias() |
208
|
|
|
{ |
209
|
|
|
$this->mockIndex('index', new IndexConfig('index', array(), array())); |
210
|
|
|
|
211
|
|
|
$this->indexManager->expects($this->never()) |
212
|
|
|
->method('getIndex'); |
213
|
|
|
$this->aliasProcessor->expects($this->never()) |
214
|
|
|
->method('switchIndexAlias'); |
215
|
|
|
|
216
|
|
|
$this->resetter->postPopulate('index'); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function testPostPopulate() |
220
|
|
|
{ |
221
|
|
|
$indexConfig = new IndexConfig('index', array(), array('useAlias' => true)); |
222
|
|
|
$index = $this->mockIndex('index', $indexConfig); |
223
|
|
|
|
224
|
|
|
$this->aliasProcessor->expects($this->once()) |
225
|
|
|
->method('switchIndexAlias') |
226
|
|
|
->with($indexConfig, $index); |
227
|
|
|
|
228
|
|
|
$this->resetter->postPopulate('index'); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
private function dispatcherExpects(array $events) |
232
|
|
|
{ |
233
|
|
|
$expectation = $this->dispatcher->expects($this->exactly(count($events))) |
234
|
|
|
->method('dispatch'); |
235
|
|
|
|
236
|
|
|
call_user_func_array(array($expectation, 'withConsecutive'), $events); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
private function mockIndex($indexName, IndexConfig $config, $mapping = array()) |
240
|
|
|
{ |
241
|
|
|
$this->configManager->expects($this->atLeast(1)) |
242
|
|
|
->method('getIndexConfiguration') |
243
|
|
|
->with($indexName) |
244
|
|
|
->will($this->returnValue($config)); |
245
|
|
|
$index = new Index($this->elasticaClient, $indexName); |
246
|
|
|
$this->indexManager->expects($this->any()) |
247
|
|
|
->method('getIndex') |
248
|
|
|
->with($indexName) |
249
|
|
|
->willReturn($index); |
250
|
|
|
$this->mappingBuilder->expects($this->any()) |
251
|
|
|
->method('buildIndexMapping') |
252
|
|
|
->with($config) |
253
|
|
|
->willReturn($mapping); |
254
|
|
|
|
255
|
|
|
return $index; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
private function mockType($typeName, $indexName, TypeConfig $typeConfig, IndexConfig $indexConfig, $mapping = array()) |
259
|
|
|
{ |
260
|
|
|
$this->configManager->expects($this->atLeast(1)) |
261
|
|
|
->method('getTypeConfiguration') |
262
|
|
|
->with($indexName, $typeName) |
263
|
|
|
->will($this->returnValue($typeConfig)); |
264
|
|
|
$index = new Index($this->elasticaClient, $indexName); |
265
|
|
|
$this->indexManager->expects($this->atLeast(2)) |
266
|
|
|
->method('getIndex') |
267
|
|
|
->with($indexName) |
268
|
|
|
->willReturn($index); |
269
|
|
|
$this->configManager->expects($this->atLeast(1)) |
270
|
|
|
->method('getIndexConfiguration') |
271
|
|
|
->with($indexName) |
272
|
|
|
->will($this->returnValue($indexConfig)); |
273
|
|
|
$this->mappingBuilder->expects($this->any()) |
274
|
|
|
->method('buildIndexMapping') |
275
|
|
|
->with($indexConfig) |
276
|
|
|
->willReturn($mapping); |
277
|
|
|
$this->mappingBuilder->expects($this->once()) |
278
|
|
|
->method('buildTypeMapping') |
279
|
|
|
->with($typeConfig) |
280
|
|
|
->willReturn($mapping); |
281
|
|
|
|
282
|
|
|
return $index; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
protected function setUp() |
286
|
|
|
{ |
287
|
|
|
$this->aliasProcessor = $this->getMockBuilder('FOS\\ElasticaBundle\\Index\\AliasProcessor') |
288
|
|
|
->disableOriginalConstructor() |
289
|
|
|
->getMock(); |
290
|
|
|
$this->configManager = $this->getMockBuilder('FOS\\ElasticaBundle\\Configuration\\ConfigManager') |
291
|
|
|
->disableOriginalConstructor() |
292
|
|
|
->getMock(); |
293
|
|
|
$this->dispatcher = $this->getMockBuilder('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface') |
294
|
|
|
->getMock(); |
295
|
|
|
$this->elasticaClient = $this->getMockBuilder('Elastica\\Client') |
296
|
|
|
->disableOriginalConstructor() |
297
|
|
|
->getMock(); |
298
|
|
|
$this->indexManager = $this->getMockBuilder('FOS\\ElasticaBundle\\Index\\IndexManager') |
299
|
|
|
->disableOriginalConstructor() |
300
|
|
|
->getMock(); |
301
|
|
|
$this->mappingBuilder = $this->getMockBuilder('FOS\\ElasticaBundle\\Index\\MappingBuilder') |
302
|
|
|
->disableOriginalConstructor() |
303
|
|
|
->getMock(); |
304
|
|
|
|
305
|
|
|
$this->resetter = new Resetter( |
306
|
|
|
$this->configManager, |
307
|
|
|
$this->indexManager, |
308
|
|
|
$this->aliasProcessor, |
309
|
|
|
$this->mappingBuilder, |
310
|
|
|
$this->dispatcher |
311
|
|
|
); |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|
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.