Completed
Push — master ( b8b393...c56780 )
by GBProd
02:34
created

DataProviderTest::testCountIsNull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
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
   /**
34
    * @return Client
35
    */
36
   private function getClient()
37
   {
38
      return $this
39
         ->getMockBuilder(Client::class)
40
         ->disableOriginalConstructor()
41
         ->getMock()
42
      ;
43
   }
44
45
   public function testIndexWithIndexAndType()
46
   {
47
      $provider = $this->getMockForAbstractClass(DataProvider::class);
48
49
      $client = $this->getClient();
50
      $client
51
         ->expects($this->once())
52
         ->method('bulk')
53
         ->with([
54
            'body' =>
55
            [
56
               [
57
                  'index' => [
58
                      '_index' => 'my_index',
59
                      '_type'  => 'my_type',
60
                      '_id'    => 'my_id',
61
                  ]
62
               ],
63
               [
64
                  'foo' => 'bar',
65
               ]
66
            ]
67
         ]
68
      );
69
70
      $provider
71
         ->expects($this->once())
72
         ->method('populate')
73
         ->will(
74
            $this->returnCallback(
75
               function () use ($provider) {
76
                  $provider->index(
77
                     'my_id',
78
                     ['foo' => 'bar']
79
                  );
80
               }
81
            )
82
         )
83
      ;
84
85
      $provider->run(
86
         $client,
87
         'my_index',
88
         'my_type',
89
         $this->getMock(EventDispatcherInterface::class)
90
      );
91
   }
92
93
   public function testIndexRunBulkTwiceIfMoreThanBatchSize()
94
   {
95
      $provider = $this->getMockForAbstractClass(DataProvider::class);
96
97
      $client = $this->getClient();
98
      $client
99
         ->expects($this->exactly(2))
100
         ->method('bulk')
101
      ;
102
103
      $provider
104
         ->expects($this->once())
105
         ->method('populate')
106
         ->will(
107
            $this->returnCallback(
108
               function () use ($provider) {
109
                  for($i = 0; $i < 1500; $i++) {
110
                     $provider->index(
111
                        'my_id',
112
                        ['foo' => 'bar']
113
                     );
114
                  }
115
               }
116
            )
117
         )
118
      ;
119
120
      $provider->run(
121
         $client,
122
         'my_index',
123
         'my_type',
124
         $this->getMock(EventDispatcherInterface::class)
125
      );
126
   }
127
128
   public function testCountIsNull()
129
   {
130
      $provider = $this->getMockForAbstractClass(DataProvider::class);
131
132
      $this->assertNull($provider->count());
133
   }
134
}
135