Completed
Push — master ( fce8e6...b0acfe )
by GBProd
02:14
created

BulkDataProviderTest::testUpdate()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 14

Duplication

Lines 29
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 29
loc 29
rs 8.8571
cc 1
eloc 14
nc 1
nop 0
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
   /**
57
    * @return IndicesNamespace
58
    */
59
   private function newIndicesExpectingRefresh($index)
60
   {
61
      $indices = $this
62
         ->getMockBuilder(IndicesNamespace::class)
63
         ->disableOriginalConstructor()
64
         ->getMock()
65
      ;
66
67
      $indices
68
         ->expects($this->once())
69
         ->method('refresh')
70
         ->with([
71
            'index' => $index,
72
         ])
73
      ;
74
75
      return $indices;
76
   }
77
78
   public function testIndexWithIndexAndType()
79
   {
80
      $client = $this->newClientExpectingBulk(
81
         [
82
            'body' =>
83
            [
84
               [
85
                  'index' => [
86
                      '_index' => 'my_index',
87
                      '_type'  => 'my_type',
88
                      '_id'    => 'my_id',
89
                  ]
90
               ],
91
               [
92
                  'foo' => 'bar',
93
               ]
94
            ]
95
         ]
96
      );
97
98
      $provider = $this->getMockForAbstractClass(BulkDataProvider::class);
99
      $provider
100
         ->expects($this->once())
101
         ->method('populate')
102
         ->will(
103
            $this->returnCallback(
104
               function () use ($provider) {
105
                  $provider->index(
106
                     'my_id',
107
                     ['foo' => 'bar']
108
                  );
109
               }
110
            )
111
         )
112
      ;
113
114
      $provider->run(
115
         $client,
116
         'my_index',
117
         'my_type',
118
         $this->getMock(EventDispatcherInterface::class)
119
      );
120
   }
121
122
   private function newClientExpectingBulk($content)
123
   {
124
      $client = $this->getClient('my_index');
125
126
      $client
127
         ->expects($this->once())
128
         ->method('bulk')
129
         ->with($content)
130
      ;
131
132
      return $client;
133
   }
134
135
   public function testIndexRunBulkTwiceIfMoreThanBatchSize()
136
   {
137
      $provider = $this->getMockForAbstractClass(BulkDataProvider::class);
138
139
      $client = $this->getClient('my_index');
140
      $client
141
         ->expects($this->exactly(3))
142
         ->method('bulk')
143
      ;
144
145
      $provider->changeBulkSize(50);
146
      $provider
147
         ->expects($this->once())
148
         ->method('populate')
149
         ->will(
150
            $this->returnCallback(
151
               function () use ($provider) {
152
                  for($i = 0; $i < 150; $i++) {
153
                     $provider->index(
154
                        'my_id',
155
                        ['foo' => 'bar']
156
                     );
157
                  }
158
               }
159
            )
160
         )
161
      ;
162
163
      $provider->run(
164
         $client,
165
         'my_index',
166
         'my_type',
167
         $this->getMock(EventDispatcherInterface::class)
168
      );
169
   }
170
171
   public function testCountIsNull()
172
   {
173
      $provider = $this->getMockForAbstractClass(BulkDataProvider::class);
174
175
      $this->assertNull($provider->count());
176
   }
177
178
   public function testDelete()
179
   {
180
      $bulk = [
181
         'body' =>
182
         [
183
            [
184
               'delete' => [
185
                   '_index' => 'my_index',
186
                   '_type'  => 'my_type',
187
                   '_id'    => 'my_id',
188
               ]
189
            ]
190
         ]
191
      ];
192
193
      $client = $this->newClientExpectingBulk($bulk);
194
195
      $provider = $this->newProviderForBulk('delete', 'my_id', null);
196
197
      $provider->run(
198
         $client,
199
         'my_index',
200
         'my_type',
201
         $this->getMock(EventDispatcherInterface::class)
202
      );
203
   }
204
205
   private function newProviderForBulk($method, $id, $content)
206
   {
207
      $provider = $this->getMockForAbstractClass(BulkDataProvider::class);
208
      $provider
209
         ->expects($this->once())
210
         ->method('populate')
211
         ->will(
212
            $this->returnCallback(
213
               function () use ($provider, $method, $id, $content) {
214
                  $provider->{$method}($id, $content);
215
               }
216
            )
217
         )
218
      ;
219
220
      return $provider;
221
   }
222
223
   public function testCreate()
224
   {
225
      $client = $this->newClientExpectingBulk(
226
         [
227
            'body' =>
228
            [
229
               [
230
                  'create' => [
231
                      '_index' => 'my_index',
232
                      '_type'  => 'my_type',
233
                      '_id'    => 'my_id',
234
                  ]
235
               ],
236
               [
237
                  'foo' => 'bar',
238
               ]
239
            ]
240
         ]
241
      );
242
243
      $provider = $this->newProviderForBulk('create', 'my_id', ['foo' => 'bar']);
244
245
      $provider->run(
246
         $client,
247
         'my_index',
248
         'my_type',
249
         $this->getMock(EventDispatcherInterface::class)
250
      );
251
   }
252
253
   public function testUpdate()
254
   {
255
      $client = $this->newClientExpectingBulk(
256
         [
257
            'body' =>
258
            [
259
               [
260
                  'update' => [
261
                      '_index' => 'my_index',
262
                      '_type'  => 'my_type',
263
                      '_id'    => 'my_id',
264
                  ]
265
               ],
266
               [
267
                  'foo' => 'bar',
268
               ]
269
            ]
270
         ]
271
      );
272
273
      $provider = $this->newProviderForBulk('update', 'my_id', ['foo' => 'bar']);
274
275
      $provider->run(
276
         $client,
277
         'my_index',
278
         'my_type',
279
         $this->getMock(EventDispatcherInterface::class)
280
      );
281
   }
282
}
283