Completed
Push — master ( ecd220...59c7a5 )
by GBProd
02:21
created

DataProviderTest::testIndexWithIndexAndType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 47
rs 9.0303
cc 1
eloc 27
nc 1
nop 0
1
<?php
2
3
namespace Tests\GBProd\ElasticsearchDataProviderBundle\DataProvider;
4
5
use Elasticsearch\Client;
6
use GBProd\ElasticsearchDataProviderBundle\DataProvider\DataProvider;
7
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
8
9
/**
10
 * Tests for abstract data provider
11
 * 
12
 * @author gbprod <[email protected]>
13
 */
14
class DataProviderTest extends \PHPUnit_Framework_TestCase
15
{
16
   public function testRunExecutePopulate()
17
   {
18
      $provider = $this->getMockForAbstractClass(DataProvider::class);
19
      
20
      $provider
21
         ->expects($this->once())
22
         ->method('populate')
23
      ;
24
      
25
      $provider->run(
26
         $this->getClient(), 
27
         'index', 
28
         'type',
29
         $this->getMock(EventDispatcherInterface::class)
30
      );
31
   }
32
   
33
   private function getClient()
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...
34
   {
35
      return $this
36
         ->getMockBuilder(Client::class)
37
         ->disableOriginalConstructor()
38
         ->getMock()
39
      ;
40
   }
41
   
42
   public function testIndexWithIndexAndType()
43
   {
44
      $provider = $this->getMockForAbstractClass(DataProvider::class);
45
      
46
      $client = $this->getClient();
47
      $client
48
         ->expects($this->once())
49
         ->method('bulk')
50
         ->with([
51
            'body' => 
52
            [
53
               [
54
                  'index' => [
55
                      '_index' => 'my_index',
56
                      '_type'  => 'my_type',
57
                      '_id'    => 'my_id',
58
                  ]
59
               ],
60
               [
61
                  'foo' => 'bar',
62
               ]
63
            ]
64
         ]
65
      );
66
      
67
      $provider
68
         ->expects($this->once())
69
         ->method('populate')
70
         ->will(
71
            $this->returnCallback(
72
               function () use ($provider) {
73
                  $provider->index(
74
                     'my_id', 
75
                     ['foo' => 'bar']
76
                  );   
77
               }
78
            )
79
         )
80
      ;
81
      
82
      $provider->run(
83
         $client, 
84
         'my_index', 
85
         'my_type',
86
         $this->getMock(EventDispatcherInterface::class)
87
      );
88
   }
89
   
90
   public function testIndexRunBulkTwiceIfMoreThanBatchSize()
91
   {
92
      $provider = $this->getMockForAbstractClass(DataProvider::class);
93
      
94
      $client = $this->getClient();
95
      $client
96
         ->expects($this->exactly(2))
97
         ->method('bulk')
98
      ;
99
      
100
      $provider
101
         ->expects($this->once())
102
         ->method('populate')
103
         ->will(
104
            $this->returnCallback(
105
               function () use ($provider) {
106
                  for($i = 0; $i < 1500; $i++) {
107
                     $provider->index(
108
                        'my_id', 
109
                        ['foo' => 'bar']
110
                     );
111
                  }
112
               }
113
            )
114
         )
115
      ;
116
      
117
      $provider->run(
118
         $client, 
119
         'my_index', 
120
         'my_type',
121
         $this->getMock(EventDispatcherInterface::class)
122
      );
123
   }
124
}