Skip to content

Commit a84ea8d

Browse files
committed
Fix #859 + unit tests
1 parent 50485d7 commit a84ea8d

File tree

2 files changed

+133
-1
lines changed

2 files changed

+133
-1
lines changed

src/Elasticsearch/Helper/Iterators/SearchHitIterator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public function current()
130130
*/
131131
public function key()
132132
{
133-
return $this->current_hit_index;
133+
return $this->current_key;
134134
}
135135

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

0 commit comments

Comments
 (0)