1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\GBProd\ElasticsearchDataProviderBundle\DataProvider; |
4
|
|
|
|
5
|
|
|
use Elasticsearch\Client; |
6
|
|
|
use Elasticsearch\Namespaces\IndicesNamespace; |
7
|
|
|
use GBProd\ElasticsearchDataProviderBundle\DataProvider\BulkDataProvider; |
8
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Tests for abstract data provider |
12
|
|
|
* |
13
|
|
|
* @author gbprod <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class BulkDataProviderTest extends \PHPUnit_Framework_TestCase |
16
|
|
|
{ |
17
|
|
|
public function testRunExecutePopulate() |
18
|
|
|
{ |
19
|
|
|
$provider = $this->getMockForAbstractClass(BulkDataProvider::class); |
20
|
|
|
|
21
|
|
|
$provider |
22
|
|
|
->expects($this->once()) |
23
|
|
|
->method('populate') |
24
|
|
|
; |
25
|
|
|
|
26
|
|
|
$provider->run( |
27
|
|
|
$this->getClient('index'), |
28
|
|
|
'index', |
29
|
|
|
'type', |
30
|
|
|
$this->getMock(EventDispatcherInterface::class) |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return Client |
36
|
|
|
*/ |
37
|
|
|
private function getClient($index) |
38
|
|
|
{ |
39
|
|
|
$client = $this |
40
|
|
|
->getMockBuilder(Client::class) |
41
|
|
|
->disableOriginalConstructor() |
42
|
|
|
->getMock() |
43
|
|
|
; |
44
|
|
|
|
45
|
|
|
$client |
46
|
|
|
->expects($this->any()) |
47
|
|
|
->method('indices') |
48
|
|
|
->willReturn( |
49
|
|
|
$this->newIndicesExpectingRefresh($index) |
50
|
|
|
) |
51
|
|
|
; |
52
|
|
|
|
53
|
|
|
return $client; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private function newIndicesExpectingRefresh($index) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$indices = $this |
59
|
|
|
->getMockBuilder(IndicesNamespace::class) |
60
|
|
|
->disableOriginalConstructor() |
61
|
|
|
->getMock() |
62
|
|
|
; |
63
|
|
|
|
64
|
|
|
$indices |
65
|
|
|
->expects($this->once()) |
66
|
|
|
->method('refresh') |
67
|
|
|
->with([ |
68
|
|
|
'index' => $index, |
69
|
|
|
]) |
70
|
|
|
; |
71
|
|
|
|
72
|
|
|
return $indices; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
View Code Duplication |
public function testIndexWithIndexAndType() |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$client = $this->newClientExpectingBulk( |
78
|
|
|
[ |
79
|
|
|
'body' => |
80
|
|
|
[ |
81
|
|
|
[ |
82
|
|
|
'index' => [ |
83
|
|
|
'_index' => 'my_index', |
84
|
|
|
'_type' => 'my_type', |
85
|
|
|
'_id' => 'my_id', |
86
|
|
|
] |
87
|
|
|
], |
88
|
|
|
[ |
89
|
|
|
'foo' => 'bar', |
90
|
|
|
] |
91
|
|
|
] |
92
|
|
|
] |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
$provider = $this->getMockForAbstractClass(BulkDataProvider::class); |
96
|
|
|
$provider |
97
|
|
|
->expects($this->once()) |
98
|
|
|
->method('populate') |
99
|
|
|
->will( |
100
|
|
|
$this->returnCallback( |
101
|
|
|
function () use ($provider) { |
102
|
|
|
$provider->index( |
103
|
|
|
'my_id', |
104
|
|
|
['foo' => 'bar'] |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
) |
108
|
|
|
) |
109
|
|
|
; |
110
|
|
|
|
111
|
|
|
$provider->run( |
112
|
|
|
$client, |
113
|
|
|
'my_index', |
114
|
|
|
'my_type', |
115
|
|
|
$this->getMock(EventDispatcherInterface::class) |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private function newClientExpectingBulk($content) |
120
|
|
|
{ |
121
|
|
|
$client = $this->getClient('my_index'); |
122
|
|
|
|
123
|
|
|
$client |
|
|
|
|
124
|
|
|
->expects($this->once()) |
125
|
|
|
->method('bulk') |
126
|
|
|
->with($content) |
127
|
|
|
; |
128
|
|
|
|
129
|
|
|
return $client; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testIndexRunBulkTwiceIfMoreThanBatchSize() |
133
|
|
|
{ |
134
|
|
|
$provider = $this->getMockForAbstractClass(BulkDataProvider::class); |
135
|
|
|
|
136
|
|
|
$client = $this->getClient('my_index'); |
137
|
|
|
$client |
|
|
|
|
138
|
|
|
->expects($this->exactly(3)) |
139
|
|
|
->method('bulk') |
140
|
|
|
; |
141
|
|
|
|
142
|
|
|
$provider->changeBulkSize(50); |
143
|
|
|
$provider |
144
|
|
|
->expects($this->once()) |
145
|
|
|
->method('populate') |
146
|
|
|
->will( |
147
|
|
|
$this->returnCallback( |
148
|
|
|
function () use ($provider) { |
149
|
|
|
for($i = 0; $i < 150; $i++) { |
150
|
|
|
$provider->index( |
151
|
|
|
'my_id', |
152
|
|
|
['foo' => 'bar'] |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
) |
157
|
|
|
) |
158
|
|
|
; |
159
|
|
|
|
160
|
|
|
$provider->run( |
161
|
|
|
$client, |
162
|
|
|
'my_index', |
163
|
|
|
'my_type', |
164
|
|
|
$this->getMock(EventDispatcherInterface::class) |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function testCountIsNull() |
169
|
|
|
{ |
170
|
|
|
$provider = $this->getMockForAbstractClass(BulkDataProvider::class); |
171
|
|
|
|
172
|
|
|
$this->assertNull($provider->count()); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function testDelete() |
176
|
|
|
{ |
177
|
|
|
$client = $this->newClientExpectingBulk( |
178
|
|
|
[ |
179
|
|
|
'body' => |
180
|
|
|
[ |
181
|
|
|
[ |
182
|
|
|
'delete' => [ |
183
|
|
|
'_index' => 'my_index', |
184
|
|
|
'_type' => 'my_type', |
185
|
|
|
'_id' => 'my_id', |
186
|
|
|
] |
187
|
|
|
] |
188
|
|
|
] |
189
|
|
|
] |
190
|
|
|
); |
191
|
|
|
|
192
|
|
|
$provider = $this->getMockForAbstractClass(BulkDataProvider::class); |
193
|
|
|
$provider |
194
|
|
|
->expects($this->once()) |
195
|
|
|
->method('populate') |
196
|
|
|
->will( |
197
|
|
|
$this->returnCallback( |
198
|
|
|
function () use ($provider) { |
199
|
|
|
$provider->delete('my_id'); |
200
|
|
|
} |
201
|
|
|
) |
202
|
|
|
) |
203
|
|
|
; |
204
|
|
|
|
205
|
|
|
$provider->run( |
206
|
|
|
$client, |
207
|
|
|
'my_index', |
208
|
|
|
'my_type', |
209
|
|
|
$this->getMock(EventDispatcherInterface::class) |
210
|
|
|
); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
View Code Duplication |
public function testCreate() |
|
|
|
|
214
|
|
|
{ |
215
|
|
|
$client = $this->newClientExpectingBulk( |
216
|
|
|
[ |
217
|
|
|
'body' => |
218
|
|
|
[ |
219
|
|
|
[ |
220
|
|
|
'create' => [ |
221
|
|
|
'_index' => 'my_index', |
222
|
|
|
'_type' => 'my_type', |
223
|
|
|
'_id' => 'my_id', |
224
|
|
|
] |
225
|
|
|
], |
226
|
|
|
[ |
227
|
|
|
'foo' => 'bar', |
228
|
|
|
] |
229
|
|
|
] |
230
|
|
|
] |
231
|
|
|
); |
232
|
|
|
|
233
|
|
|
$provider = $this->getMockForAbstractClass(BulkDataProvider::class); |
234
|
|
|
$provider |
235
|
|
|
->expects($this->once()) |
236
|
|
|
->method('populate') |
237
|
|
|
->will( |
238
|
|
|
$this->returnCallback( |
239
|
|
|
function () use ($provider) { |
240
|
|
|
$provider->create('my_id', ['foo' => 'bar']); |
241
|
|
|
} |
242
|
|
|
) |
243
|
|
|
) |
244
|
|
|
; |
245
|
|
|
|
246
|
|
|
$provider->run( |
247
|
|
|
$client, |
248
|
|
|
'my_index', |
249
|
|
|
'my_type', |
250
|
|
|
$this->getMock(EventDispatcherInterface::class) |
251
|
|
|
); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
View Code Duplication |
public function testUpdate() |
|
|
|
|
255
|
|
|
{ |
256
|
|
|
$client = $this->newClientExpectingBulk( |
257
|
|
|
[ |
258
|
|
|
'body' => |
259
|
|
|
[ |
260
|
|
|
[ |
261
|
|
|
'update' => [ |
262
|
|
|
'_index' => 'my_index', |
263
|
|
|
'_type' => 'my_type', |
264
|
|
|
'_id' => 'my_id', |
265
|
|
|
] |
266
|
|
|
], |
267
|
|
|
[ |
268
|
|
|
'foo' => 'bar', |
269
|
|
|
] |
270
|
|
|
] |
271
|
|
|
] |
272
|
|
|
); |
273
|
|
|
|
274
|
|
|
$provider = $this->getMockForAbstractClass(BulkDataProvider::class); |
275
|
|
|
$provider |
276
|
|
|
->expects($this->once()) |
277
|
|
|
->method('populate') |
278
|
|
|
->will( |
279
|
|
|
$this->returnCallback( |
280
|
|
|
function () use ($provider) { |
281
|
|
|
$provider->update('my_id', ['foo' => 'bar']); |
282
|
|
|
} |
283
|
|
|
) |
284
|
|
|
) |
285
|
|
|
; |
286
|
|
|
|
287
|
|
|
$provider->run( |
288
|
|
|
$client, |
289
|
|
|
'my_index', |
290
|
|
|
'my_type', |
291
|
|
|
$this->getMock(EventDispatcherInterface::class) |
292
|
|
|
); |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.