1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSElasticaBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\ElasticaBundle\Tests\Index; |
13
|
|
|
|
14
|
|
|
use Elastica\Client; |
15
|
|
|
use FOS\ElasticaBundle\Configuration\ConfigManager; |
16
|
|
|
use FOS\ElasticaBundle\Configuration\IndexConfig; |
17
|
|
|
use FOS\ElasticaBundle\Configuration\TypeConfig; |
18
|
|
|
use FOS\ElasticaBundle\Elastica\Index; |
19
|
|
|
use FOS\ElasticaBundle\Event\IndexResetEvent; |
20
|
|
|
use FOS\ElasticaBundle\Event\TypeResetEvent; |
21
|
|
|
use FOS\ElasticaBundle\Index\AliasProcessor; |
22
|
|
|
use FOS\ElasticaBundle\Index\IndexManager; |
23
|
|
|
use FOS\ElasticaBundle\Index\MappingBuilder; |
24
|
|
|
use FOS\ElasticaBundle\Index\Resetter; |
25
|
|
|
use PHPUnit\Framework\TestCase; |
26
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
27
|
|
|
|
28
|
|
|
class ResetterTest extends TestCase |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var Resetter |
32
|
|
|
*/ |
33
|
|
|
private $resetter; |
34
|
|
|
|
35
|
|
|
private $aliasProcessor; |
36
|
|
|
private $configManager; |
37
|
|
|
private $dispatcher; |
38
|
|
|
private $elasticaClient; |
39
|
|
|
private $indexManager; |
40
|
|
|
private $mappingBuilder; |
41
|
|
|
|
42
|
|
|
protected function setUp() |
43
|
|
|
{ |
44
|
|
|
$this->aliasProcessor = $this->createMock(AliasProcessor::class); |
45
|
|
|
$this->configManager = $this->createMock(ConfigManager::class); |
46
|
|
|
$this->dispatcher = $this->createMock(EventDispatcherInterface::class); |
47
|
|
|
$this->elasticaClient = $this->createMock(Client::class); |
48
|
|
|
$this->indexManager = $this->createMock(IndexManager::class); |
49
|
|
|
$this->mappingBuilder = $this->createMock(MappingBuilder::class); |
50
|
|
|
|
51
|
|
|
$this->resetter = new Resetter( |
52
|
|
|
$this->configManager, |
|
|
|
|
53
|
|
|
$this->indexManager, |
|
|
|
|
54
|
|
|
$this->aliasProcessor, |
|
|
|
|
55
|
|
|
$this->mappingBuilder, |
|
|
|
|
56
|
|
|
$this->dispatcher |
|
|
|
|
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testResetAllIndexes() |
61
|
|
|
{ |
62
|
|
|
$indexName = 'index1'; |
63
|
|
|
$indexConfig = new IndexConfig($indexName, [], []); |
64
|
|
|
$this->mockIndex($indexName, $indexConfig); |
65
|
|
|
|
66
|
|
|
$this->configManager->expects($this->once()) |
67
|
|
|
->method('getIndexNames') |
68
|
|
|
->will($this->returnValue([$indexName])); |
69
|
|
|
|
70
|
|
|
$this->dispatcherExpects([ |
71
|
|
|
[IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf(IndexResetEvent::class)], |
72
|
|
|
[IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf(IndexResetEvent::class)], |
73
|
|
|
]); |
74
|
|
|
|
75
|
|
|
$this->elasticaClient->expects($this->exactly(2)) |
76
|
|
|
->method('requestEndpoint'); |
77
|
|
|
|
78
|
|
|
$this->resetter->resetAllIndexes(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
View Code Duplication |
public function testResetIndex() |
82
|
|
|
{ |
83
|
|
|
$indexConfig = new IndexConfig('index1', [], []); |
84
|
|
|
$this->mockIndex('index1', $indexConfig); |
85
|
|
|
|
86
|
|
|
$this->dispatcherExpects([ |
87
|
|
|
[IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf(IndexResetEvent::class)], |
88
|
|
|
[IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf(IndexResetEvent::class)], |
89
|
|
|
]); |
90
|
|
|
|
91
|
|
|
$this->elasticaClient->expects($this->exactly(2)) |
92
|
|
|
->method('requestEndpoint'); |
93
|
|
|
|
94
|
|
|
$this->resetter->resetIndex('index1'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
View Code Duplication |
public function testResetIndexWithDifferentName() |
98
|
|
|
{ |
99
|
|
|
$indexConfig = new IndexConfig('index1', [], [ |
100
|
|
|
'elasticSearchName' => 'notIndex1', |
101
|
|
|
]); |
102
|
|
|
$this->mockIndex('index1', $indexConfig); |
103
|
|
|
$this->dispatcherExpects([ |
104
|
|
|
[IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf(IndexResetEvent::class)], |
105
|
|
|
[IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf(IndexResetEvent::class)], |
106
|
|
|
]); |
107
|
|
|
|
108
|
|
|
$this->elasticaClient->expects($this->exactly(2)) |
109
|
|
|
->method('requestEndpoint'); |
110
|
|
|
|
111
|
|
|
$this->resetter->resetIndex('index1'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testResetIndexWithDifferentNameAndAlias() |
115
|
|
|
{ |
116
|
|
|
$indexConfig = new IndexConfig('index1', [], [ |
117
|
|
|
'elasticSearchName' => 'notIndex1', |
118
|
|
|
'useAlias' => true, |
119
|
|
|
]); |
120
|
|
|
$index = $this->mockIndex('index1', $indexConfig); |
121
|
|
|
$this->dispatcherExpects([ |
122
|
|
|
[IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf(IndexResetEvent::class)], |
123
|
|
|
[IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf(IndexResetEvent::class)], |
124
|
|
|
]); |
125
|
|
|
|
126
|
|
|
$this->aliasProcessor->expects($this->once()) |
127
|
|
|
->method('switchIndexAlias') |
128
|
|
|
->with($indexConfig, $index, false); |
129
|
|
|
|
130
|
|
|
$this->elasticaClient->expects($this->exactly(2)) |
131
|
|
|
->method('requestEndpoint'); |
132
|
|
|
|
133
|
|
|
$this->resetter->resetIndex('index1'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @expectedException \InvalidArgumentException |
138
|
|
|
*/ |
139
|
|
View Code Duplication |
public function testFailureWhenMissingIndexDoesntDispatch() |
140
|
|
|
{ |
141
|
|
|
$this->configManager->expects($this->once()) |
142
|
|
|
->method('getIndexConfiguration') |
143
|
|
|
->with('nonExistant') |
144
|
|
|
->will($this->throwException(new \InvalidArgumentException())); |
|
|
|
|
145
|
|
|
|
146
|
|
|
$this->indexManager->expects($this->never()) |
147
|
|
|
->method('getIndex'); |
148
|
|
|
|
149
|
|
|
$this->resetter->resetIndex('nonExistant'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function testResetType() |
153
|
|
|
{ |
154
|
|
|
$typeConfig = new TypeConfig('type', [], []); |
155
|
|
|
$indexConfig = new IndexConfig('index', [], []); |
156
|
|
|
$this->mockType('type', 'index', $typeConfig, $indexConfig); |
157
|
|
|
|
158
|
|
|
$this->dispatcherExpects([ |
159
|
|
|
[IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf(IndexResetEvent::class)], |
160
|
|
|
[IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf(IndexResetEvent::class)], |
161
|
|
|
[TypeResetEvent::PRE_TYPE_RESET, $this->isInstanceOf(TypeResetEvent::class)], |
162
|
|
|
[TypeResetEvent::POST_TYPE_RESET, $this->isInstanceOf(TypeResetEvent::class)], |
163
|
|
|
]); |
164
|
|
|
|
165
|
|
|
$this->elasticaClient->expects($this->exactly(3)) |
166
|
|
|
->method('requestEndpoint'); |
167
|
|
|
|
168
|
|
|
$this->resetter->resetIndexType('index', 'type'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testResetTypeWithChangedSettings() |
172
|
|
|
{ |
173
|
|
|
$settingsValue = [ |
174
|
|
|
'analysis' => [ |
175
|
|
|
'analyzer' => [ |
176
|
|
|
'test_analyzer' => [ |
177
|
|
|
'type' => 'standard', |
178
|
|
|
'tokenizer' => 'standard', |
179
|
|
|
], |
180
|
|
|
], |
181
|
|
|
], |
182
|
|
|
]; |
183
|
|
|
$typeConfig = new TypeConfig('type', [], []); |
184
|
|
|
$indexConfig = new IndexConfig('index', [], ['settings' => $settingsValue]); |
185
|
|
|
$this->mockType('type', 'index', $typeConfig, $indexConfig); |
186
|
|
|
|
187
|
|
|
$this->elasticaClient->expects($this->exactly(3)) |
188
|
|
|
->method('requestEndpoint'); |
189
|
|
|
|
190
|
|
|
$this->resetter->resetIndexType('index', 'type'); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @expectedException \InvalidArgumentException |
195
|
|
|
*/ |
196
|
|
View Code Duplication |
public function testNonExistantResetType() |
197
|
|
|
{ |
198
|
|
|
$this->configManager->expects($this->once()) |
199
|
|
|
->method('getTypeConfiguration') |
200
|
|
|
->with('index', 'nonExistant') |
201
|
|
|
->will($this->throwException(new \InvalidArgumentException())); |
|
|
|
|
202
|
|
|
|
203
|
|
|
$this->indexManager->expects($this->never()) |
204
|
|
|
->method('getIndex'); |
205
|
|
|
|
206
|
|
|
$this->resetter->resetIndexType('index', 'nonExistant'); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function testPostPopulateWithoutAlias() |
210
|
|
|
{ |
211
|
|
|
$this->mockIndex('index', new IndexConfig('index', [], [])); |
212
|
|
|
|
213
|
|
|
$this->indexManager->expects($this->never()) |
214
|
|
|
->method('getIndex'); |
215
|
|
|
$this->aliasProcessor->expects($this->never()) |
216
|
|
|
->method('switchIndexAlias'); |
217
|
|
|
|
218
|
|
|
$this->resetter->switchIndexAlias('index'); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function testPostPopulate() |
222
|
|
|
{ |
223
|
|
|
$indexConfig = new IndexConfig('index', [], ['useAlias' => true]); |
224
|
|
|
$index = $this->mockIndex('index', $indexConfig); |
225
|
|
|
|
226
|
|
|
$this->aliasProcessor->expects($this->once()) |
227
|
|
|
->method('switchIndexAlias') |
228
|
|
|
->with($indexConfig, $index); |
229
|
|
|
|
230
|
|
|
$this->resetter->switchIndexAlias('index'); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
private function dispatcherExpects(array $events) |
234
|
|
|
{ |
235
|
|
|
$expectation = $this->dispatcher->expects($this->exactly(count($events))) |
236
|
|
|
->method('dispatch'); |
237
|
|
|
|
238
|
|
|
call_user_func_array([$expectation, 'withConsecutive'], $events); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
private function mockIndex($indexName, IndexConfig $config, $mapping = []) |
242
|
|
|
{ |
243
|
|
|
$this->configManager->expects($this->atLeast(1)) |
244
|
|
|
->method('getIndexConfiguration') |
245
|
|
|
->with($indexName) |
246
|
|
|
->will($this->returnValue($config)); |
247
|
|
|
$index = new Index($this->elasticaClient, $indexName); |
|
|
|
|
248
|
|
|
$this->indexManager->expects($this->any()) |
249
|
|
|
->method('getIndex') |
250
|
|
|
->with($indexName) |
251
|
|
|
->willReturn($index); |
252
|
|
|
$this->mappingBuilder->expects($this->any()) |
253
|
|
|
->method('buildIndexMapping') |
254
|
|
|
->with($config) |
255
|
|
|
->willReturn($mapping); |
256
|
|
|
|
257
|
|
|
return $index; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
private function mockType($typeName, $indexName, TypeConfig $typeConfig, IndexConfig $indexConfig, $mapping = []) |
261
|
|
|
{ |
262
|
|
|
$this->configManager->expects($this->atLeast(1)) |
263
|
|
|
->method('getTypeConfiguration') |
264
|
|
|
->with($indexName, $typeName) |
265
|
|
|
->will($this->returnValue($typeConfig)); |
266
|
|
|
$index = new Index($this->elasticaClient, $indexName); |
|
|
|
|
267
|
|
|
$this->indexManager->expects($this->atLeast(2)) |
268
|
|
|
->method('getIndex') |
269
|
|
|
->with($indexName) |
270
|
|
|
->willReturn($index); |
271
|
|
|
$this->configManager->expects($this->atLeast(1)) |
272
|
|
|
->method('getIndexConfiguration') |
273
|
|
|
->with($indexName) |
274
|
|
|
->will($this->returnValue($indexConfig)); |
275
|
|
|
$this->mappingBuilder->expects($this->any()) |
276
|
|
|
->method('buildIndexMapping') |
277
|
|
|
->with($indexConfig) |
278
|
|
|
->willReturn($mapping); |
279
|
|
|
$this->mappingBuilder->expects($this->once()) |
280
|
|
|
->method('buildTypeMapping') |
281
|
|
|
->with($typeConfig) |
282
|
|
|
->willReturn($mapping); |
283
|
|
|
|
284
|
|
|
return $index; |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: