1<?php
2
3declare(strict_types = 1);
4
5namespace Elasticsearch\Tests\Helper\Iterators;
6
7use Elasticsearch\Helper\Iterators\SearchHitIterator;
8use Elasticsearch\Helper\Iterators\SearchResponseIterator;
9use Mockery;
10
11/**
12 * Class SearchResponseIteratorTest
13 *
14 * @package Elasticsearch\Tests\Helper\Iterators
15 * @author  Enrico Zimuel <enrico.zimuel@elastic.co>
16 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
17 * @link    http://Elasticsearch.org
18 */
19class SearchHitIteratorTest extends \PHPUnit\Framework\TestCase
20{
21
22    public function setUp()
23    {
24        $this->searchResponse = Mockery::mock(SearchResponseIterator::class);
25    }
26
27    public function tearDown()
28    {
29        Mockery::close();
30    }
31
32    public function testWithNoResults()
33    {
34        $searchHit = new SearchHitIterator($this->searchResponse);
35        $this->assertCount(0, $searchHit);
36    }
37
38    public function testWithHits()
39    {
40        $this->searchResponse->shouldReceive('rewind')
41            ->once()
42            ->ordered();
43
44        $this->searchResponse->shouldReceive('current')
45            ->andReturn(
46                [
47                'hits' => [
48                    'hits' => [
49                        [ 'foo' => 'bar0' ],
50                        [ 'foo' => 'bar1' ],
51                        [ 'foo' => 'bar2' ]
52                    ],
53                    'total' => 3
54                ]
55                ],
56                [
57                'hits' => [
58                    'hits' => [
59                        [ 'foo' => 'bar0' ],
60                        [ 'foo' => 'bar1' ],
61                        [ 'foo' => 'bar2' ]
62                    ],
63                    'total' => 3
64                ]
65                ],
66                [
67                'hits' => [
68                    'hits' => [
69                        [ 'foo' => 'bar0' ],
70                        [ 'foo' => 'bar1' ],
71                        [ 'foo' => 'bar2' ]
72                    ],
73                    'total' => 3
74                ]
75                ],
76                [
77                'hits' => [
78                    'hits' => [
79                        [ 'foo' => 'bar0' ],
80                        [ 'foo' => 'bar1' ],
81                        [ 'foo' => 'bar2' ]
82                    ],
83                    'total' => 3
84                ]
85                ],
86                [
87                'hits' => [
88                    'hits' => [
89                        [ 'foo' => 'bar3' ],
90                        [ 'foo' => 'bar4' ]
91                    ],
92                    'total' => 2
93                ]
94                ],
95                [
96                'hits' => [
97                    'hits' => [
98                        [ 'foo' => 'bar3' ],
99                        [ 'foo' => 'bar4' ]
100                    ],
101                    'total' => 2
102                ]
103                ]
104            );
105
106        $this->searchResponse->shouldReceive('valid')
107            ->andReturn(true, true, true, false);
108
109        $this->searchResponse->shouldReceive('next')
110            ->times(2)
111            ->ordered();
112
113        $responses = new SearchHitIterator($this->searchResponse);
114        $i = 0;
115        foreach ($responses as $key => $value) {
116            $this->assertEquals($i, $key);
117            $this->assertEquals("bar$i", $value['foo']);
118            $i++;
119        }
120    }
121}
122