Completed
Push — master ( c56780...0a78d5 )
by GBProd
02:21
created

BulkDataProviderTest::testIndexWithIndexAndType()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 23

Duplication

Lines 43
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 43
loc 43
rs 8.8571
cc 1
eloc 23
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
   private function newIndicesExpectingRefresh($index)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
1 ignored issue
show
Bug introduced by
The method expects() does not seem to exist on object<Elasticsearch\Client>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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
1 ignored issue
show
Bug introduced by
The method expects() does not seem to exist on object<Elasticsearch\Client>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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