/
PaginatorTest.php
196 lines (167 loc) · 5.62 KB
/
PaginatorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Core\Tests\Bridge\Elasticsearch\DataProvider;
use ApiPlatform\Core\Bridge\Elasticsearch\DataProvider\Paginator;
use ApiPlatform\Core\Bridge\Elasticsearch\Serializer\ItemNormalizer;
use ApiPlatform\Core\DataProvider\PaginatorInterface;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Foo;
use ApiPlatform\Core\Tests\ProphecyTrait;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
class PaginatorTest extends TestCase
{
use ProphecyTrait;
private const DOCUMENTS = [
'hits' => [
'total' => 8,
'max_score' => 1,
'hits' => [
[
'_index' => 'foo',
'_type' => '_doc',
'_id' => '5',
'_score' => 1,
'_source' => [
'id' => 5,
'name' => 'Fribourg',
'bar' => 'gruobirf',
],
],
[
'_index' => 'foo',
'_type' => '_doc',
'_id' => '6',
'_score' => 1,
'_source' => [
'id' => 6,
'name' => 'Lausanne',
'bar' => 'ennasual',
],
],
[
'_index' => 'foo',
'_type' => '_doc',
'_id' => '7',
'_score' => 1,
'_source' => [
'id' => 7,
'name' => 'Vallorbe',
'bar' => 'ebrollav',
],
],
[
'_index' => 'foo',
'_type' => '_doc',
'_id' => '8',
'_score' => 1,
'_source' => [
'id' => 8,
'name' => 'Lugano',
'bar' => 'onagul',
],
],
],
],
];
private const OFFSET = 4;
private const LIMIT = 4;
/**
* @var PaginatorInterface
*/
private $paginator;
public function testConstruct()
{
self::assertInstanceOf(PaginatorInterface::class, $this->paginator);
}
public function testCount()
{
self::assertCount(4, $this->paginator);
}
public function testGetLastPage()
{
self::assertSame(2., $this->paginator->getLastPage());
}
public function testGetLastPageWithZeroAsLimit()
{
self::assertSame(1., $this->getPaginator(0, 0)->getLastPage());
}
public function testGetLastPageWithNegativeLimit()
{
self::assertSame(1., $this->getPaginator(-1, 0)->getLastPage());
}
public function testGetTotalItems()
{
self::assertSame(8., $this->paginator->getTotalItems());
}
public function testGetTotalItemsForElasticSearch7()
{
// the total in elastichsearch >= 7 is object and not integer.
$documents = self::DOCUMENTS;
$documents['hits']['total'] = [
'value' => 8,
'relation' => 'eq',
];
$paginator = $this->getPaginator(static::LIMIT, static::OFFSET, $documents);
self::assertSame(8., $paginator->getTotalItems());
}
public function testGetCurrentPage()
{
self::assertSame(2., $this->paginator->getCurrentPage());
}
public function testGetCurrentPageWithZeroAsLimit()
{
self::assertSame(1., $this->getPaginator(0, 0)->getCurrentPage());
}
public function testGetCurrentPageWithNegativeLimit()
{
self::assertSame(1., $this->getPaginator(-1, 0)->getCurrentPage());
}
public function testGetItemsPerPage()
{
self::assertSame(4., $this->paginator->getItemsPerPage());
}
public function testGetIterator()
{
// set local cache
iterator_to_array($this->paginator);
self::assertEquals(
array_map(
function (array $document): Foo {
return $this->denormalizeFoo($document['_source']);
},
static::DOCUMENTS['hits']['hits']
),
iterator_to_array($this->paginator)
);
}
protected function setUp(): void
{
$this->paginator = $this->getPaginator();
}
private function getPaginator(int $limit = self::OFFSET, int $offset = self::OFFSET, array $documents = self::DOCUMENTS)
{
$denormalizerProphecy = $this->prophesize(DenormalizerInterface::class);
foreach ($documents['hits']['hits'] as $document) {
$denormalizerProphecy
->denormalize($document, Foo::class, ItemNormalizer::FORMAT, [AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => true, 'groups' => ['custom']])
->willReturn($this->denormalizeFoo($document['_source']));
}
return new Paginator($denormalizerProphecy->reveal(), $documents, Foo::class, $limit, $offset, ['groups' => ['custom']]);
}
private function denormalizeFoo(array $fooDocument): Foo
{
$foo = new Foo();
$foo->setName($fooDocument['name']);
$foo->setBar($fooDocument['bar']);
return $foo;
}
}