Skip to content

Commit d557f71

Browse files
nitneukdunglas
authored andcommitted
Fix paginator for elasticsearch 7 (#2919)
* in elasticsearch pagination, calculate total for versions >= 7 * write test for Elasticsearch paginator * refactoring Elasticsearch Paginator test, use ternary operator in Paginator::getTotalItems * Refactor elastic paginator test * Fix properties visibility
1 parent 30d565f commit d557f71

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

src/Bridge/Elasticsearch/DataProvider/Paginator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ public function getLastPage(): float
6868
*/
6969
public function getTotalItems(): float
7070
{
71-
return isset($this->documents['hits']['total']) ? (float) $this->documents['hits']['total'] : 0.;
71+
// for elastic search version > 7.0.0
72+
if (\is_array($this->documents['hits']['total'])) {
73+
return (float) ($this->documents['hits']['total']['value'] ?? 0.);
74+
}
75+
76+
// for elastic search old versions
77+
return (float) ($this->documents['hits']['total'] ?? 0.);
7278
}
7379

7480
/**

tests/Bridge/Elasticsearch/DataProvider/PaginatorTest.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
class PaginatorTest extends TestCase
2525
{
26-
protected const DOCUMENTS = [
26+
private const DOCUMENTS = [
2727
'hits' => [
2828
'total' => 8,
2929
'max_score' => 1,
@@ -76,10 +76,13 @@ class PaginatorTest extends TestCase
7676
],
7777
];
7878

79+
private const OFFSET = 4;
80+
private const LIMIT = 4;
81+
7982
/**
8083
* @var PaginatorInterface
8184
*/
82-
protected $paginator;
85+
private $paginator;
8386

8487
public function testConstruct()
8588
{
@@ -111,6 +114,20 @@ public function testGetTotalItems()
111114
self::assertSame(8., $this->paginator->getTotalItems());
112115
}
113116

117+
public function testGetTotalItemsForElasticSearch7()
118+
{
119+
// the total in elastichsearch >= 7 is object and not integer.
120+
$documents = self::DOCUMENTS;
121+
$documents['hits']['total'] = [
122+
'value' => 8,
123+
'relation' => 'eq',
124+
];
125+
126+
$paginator = $this->getPaginator(static::LIMIT, static::OFFSET, $documents);
127+
128+
self::assertSame(8., $paginator->getTotalItems());
129+
}
130+
114131
public function testGetCurrentPage()
115132
{
116133
self::assertSame(2., $this->paginator->getCurrentPage());
@@ -149,23 +166,23 @@ function (array $document): Foo {
149166

150167
protected function setUp(): void
151168
{
152-
$this->paginator = $this->getPaginator(4, 4);
169+
$this->paginator = $this->getPaginator();
153170
}
154171

155-
protected function getPaginator(int $limit, int $offset)
172+
private function getPaginator(int $limit = self::OFFSET, int $offset = self::OFFSET, array $documents = self::DOCUMENTS)
156173
{
157174
$denormalizerProphecy = $this->prophesize(DenormalizerInterface::class);
158175

159-
foreach (static::DOCUMENTS['hits']['hits'] as $document) {
176+
foreach ($documents['hits']['hits'] as $document) {
160177
$denormalizerProphecy
161178
->denormalize($document, Foo::class, ItemNormalizer::FORMAT, [AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => true])
162179
->willReturn($this->denormalizeFoo($document['_source']));
163180
}
164181

165-
return new Paginator($denormalizerProphecy->reveal(), self::DOCUMENTS, Foo::class, $limit, $offset);
182+
return new Paginator($denormalizerProphecy->reveal(), $documents, Foo::class, $limit, $offset);
166183
}
167184

168-
protected function denormalizeFoo(array $fooDocument): Foo
185+
private function denormalizeFoo(array $fooDocument): Foo
169186
{
170187
$foo = new Foo();
171188
$foo->setName($fooDocument['name']);

0 commit comments

Comments
 (0)