1<?php
2
3declare(strict_types = 1);
4
5namespace Elasticsearch\Tests\Helper\Iterators;
6
7use Elasticsearch\Client;
8use Elasticsearch\Helper\Iterators\SearchResponseIterator;
9use Mockery as m;
10
11/**
12 * Class SearchResponseIteratorTest
13 *
14 * @package Elasticsearch\Tests\Helper\Iterators
15 * @author  Arturo Mejia <arturo.mejia@kreatetechnology.com>
16 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
17 * @link    http://Elasticsearch.org
18 */
19class SearchResponseIteratorTest extends \PHPUnit\Framework\TestCase
20{
21
22    public function tearDown()
23    {
24        m::close();
25    }
26
27    public function testWithNoResults()
28    {
29        $search_params = [
30            'scroll'      => '5m',
31            'index'       => 'twitter',
32            'size'        => 1000,
33            'body'        => [
34                'query' => [
35                    'match_all' => new \stdClass
36                ]
37            ]
38        ];
39
40        $mock_client = m::mock(Client::class);
41
42        $mock_client->shouldReceive('search')
43            ->twice()
44            ->with($search_params)
45            ->andReturn(['_scroll_id' => 'scroll_id_01']);
46
47        $mock_client->shouldReceive('clearScroll')
48            ->twice()
49            ->withAnyArgs();
50
51        $responses = new SearchResponseIterator($mock_client, $search_params);
52
53        $this->assertCount(0, $responses);
54    }
55
56    public function testWithHits()
57    {
58        $search_params = [
59            'scroll'      => '5m',
60            'index'       => 'twitter',
61            'size'        => 1000,
62            'body'        => [
63                'query' => [
64                    'match_all' => new \stdClass
65                ]
66            ]
67        ];
68
69        $mock_client = m::mock(Client::class);
70
71        $mock_client->shouldReceive('search')
72            ->once()
73            ->ordered()
74            ->with($search_params)
75            ->andReturn(
76                [
77                '_scroll_id' => 'scroll_id_01',
78                'hits' => [
79                    'hits' => [
80                        [
81                            'foo' => 'bar'
82                        ]
83                    ]
84                ]
85                ]
86            );
87
88        $mock_client->shouldReceive('scroll')
89            ->once()
90            ->ordered()
91            ->with(
92                [
93                    'scroll_id'  => 'scroll_id_01',
94                    'scroll' => '5m'
95                ]
96            )
97            ->andReturn(
98                [
99                    '_scroll_id' => 'scroll_id_02',
100                    'hits' => [
101                        'hits' => [
102                            [
103                                'foo' => 'bar'
104                            ]
105                        ]
106                    ]
107                ]
108            );
109
110        $mock_client->shouldReceive('scroll')
111            ->once()
112            ->ordered()
113            ->with(
114                [
115                    'scroll_id'  => 'scroll_id_02',
116                    'scroll' => '5m'
117                ]
118            )
119            ->andReturn(
120                [
121                    '_scroll_id' => 'scroll_id_03',
122                    'hits' => [
123                        'hits' => [
124                            [
125                                'foo' => 'bar'
126                            ]
127                        ]
128                    ]
129                ]
130            );
131
132        $mock_client->shouldReceive('scroll')
133            ->once()
134            ->ordered()
135            ->with(
136                [
137                    'scroll_id'  => 'scroll_id_03',
138                    'scroll' => '5m'
139                ]
140            )
141            ->andReturn(
142                [
143                    '_scroll_id' => 'scroll_id_04',
144                    'hits' => [
145                        'hits' => []
146                    ]
147                ]
148            );
149
150        $mock_client->shouldReceive('scroll')
151            ->never()
152            ->with(
153                [
154                    'scroll_id'  => 'scroll_id_04',
155                    'scroll' => '5m'
156                ]
157            );
158
159        $mock_client->shouldReceive('clearScroll')
160            ->once()
161            ->ordered()
162            ->withAnyArgs();
163
164        $responses = new SearchResponseIterator($mock_client, $search_params);
165        $count = 0;
166        $i = 0;
167        foreach ($responses as $response) {
168            $count += count($response['hits']['hits']);
169            $this->assertEquals($response['_scroll_id'], sprintf("scroll_id_%02d", ++$i));
170        }
171        $this->assertEquals(3, $count);
172    }
173}
174