Completed
Push — master ( 3d4a40...992a8b )
by Alexander
25s queued 11s
created

testDataReaderWithoutSortableInterface()

Size

Total Lines 23
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 4
nc 1
nop 0
dl 0
loc 23
c 1
b 1
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A KeysetPaginatorTest.php$1 ➔ read() 0 3 1
A KeysetPaginatorTest.php$1 ➔ withFilter() 0 2 1
A KeysetPaginatorTest.php$1 ➔ withLimit() 0 2 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Yiisoft\Data\Tests\Paginator;
5
6
use Yiisoft\Data\Paginator\KeysetPaginator;
7
use Yiisoft\Data\Reader\IterableDataReader;
8
use Yiisoft\Data\Reader\Filter\FilterInterface;
9
use Yiisoft\Data\Reader\DataReaderInterface;
10
use Yiisoft\Data\Reader\FilterableDataInterface;
11
use Yiisoft\Data\Reader\Sort;
12
use Yiisoft\Data\Tests\TestCase;
13
14
final class KeysetPaginatorTest extends Testcase
15
{
16
    private function getDataSet(): array
17
    {
18
        return [
19
            [
20
                'id' => 1,
21
                'name' => 'Codename Boris',
22
            ],
23
            [
24
                'id' => 2,
25
                'name' => 'Codename Doris',
26
            ],
27
            [
28
                'id' => 3,
29
                'name' => 'Agent K',
30
            ],
31
            [
32
                'id' => 5,
33
                'name' => 'Agent J',
34
            ],
35
            [
36
                'id' => 6,
37
                'name' => '007',
38
            ],
39
        ];
40
    }
41
42
    public function testDataReaderWithoutFilterableInterface(): void
43
    {
44
        $nonFilterableDataReader = new class implements DataReaderInterface
45
        {
46
            public function withLimit(int $limit)
47
            {
48
                // do nothing
49
            }
50
51
            public function read(): iterable
52
            {
53
                return [];
54
            }
55
        };
56
57
        $this->expectException(\InvalidArgumentException::class);
58
59
        new KeysetPaginator($nonFilterableDataReader);
60
    }
61
62
    public function testDataReaderWithoutSortableInterface(): void
63
    {
64
        $nonSortableDataReader = new class implements DataReaderInterface, FilterableDataInterface
65
        {
66
            public function withLimit(int $limit)
67
            {
68
                // do nothing
69
            }
70
71
            public function read(): iterable
72
            {
73
                return [];
74
            }
75
76
            public function withFilter(FilterInterface $filter)
77
            {
78
                // do nothing
79
            }
80
        };
81
82
        $this->expectException(\InvalidArgumentException::class);
83
84
        new KeysetPaginator($nonSortableDataReader);
85
    }
86
87
    public function testPageSizeCannotBeLessThanOne(): void
88
    {
89
        $sort = new Sort(['id', 'name']);
90
        $dataReader = (new IterableDataReader($this->getDataSet()))
91
            ->withSort($sort);
92
        $paginator = new KeysetPaginator($dataReader);
93
94
        $this->expectException(\InvalidArgumentException::class);
95
        $paginator->withPageSize(0);
96
    }
97
98
    public function testThrowsExceptionWhenReaderHasNoSort(): void
99
    {
100
        $dataReader = new IterableDataReader($this->getDataSet());
101
102
        $this->expectException(\RuntimeException::class);
103
104
        new KeysetPaginator($dataReader);
105
    }
106
107
    public function testThrowsExceptionWhenNotSorted(): void
108
    {
109
        $sort = new Sort(['id', 'name']);
110
111
        $dataReader = (new IterableDataReader($this->getDataSet()))
112
            ->withSort($sort);
113
114
        $paginator = (new KeysetPaginator($dataReader))
115
            ->withPageSize(2)
116
            ->withLast(3);
117
118
        $this->expectException(\RuntimeException::class);
119
120
        $this->iterableToArray($paginator->read());
121
    }
122
123
    public function testReadFirstPage(): void
124
    {
125
        $sort = (new Sort(['id', 'name']))->withOrderString('id');
126
127
        $dataReader = (new IterableDataReader($this->getDataSet()))
128
            ->withSort($sort);
129
130
131
        $paginator = (new KeysetPaginator($dataReader))
132
            ->withPageSize(2);
133
134
        $expected = [
135
            [
136
                'id' => 1,
137
                'name' => 'Codename Boris',
138
            ],
139
            [
140
                'id' => 2,
141
                'name' => 'Codename Doris',
142
            ],
143
        ];
144
145
        $this->assertSame($expected, $this->iterableToArray($paginator->read()));
146
        $last = end($expected);
147
        $this->assertSame($last['id'], $paginator->getLast());
148
    }
149
150
    public function testReadSecondPage(): void
151
    {
152
        $sort = (new Sort(['id', 'name']))->withOrderString('id');
153
154
        $dataReader = (new IterableDataReader($this->getDataSet()))
155
            ->withSort($sort);
156
157
        $paginator = (new KeysetPaginator($dataReader))
158
            ->withPageSize(2)
159
            ->withLast(2);
160
161
        $expected = [
162
            [
163
                'id' => 3,
164
                'name' => 'Agent K',
165
            ],
166
            [
167
                'id' => 5,
168
                'name' => 'Agent J',
169
            ],
170
        ];
171
172
        $this->assertSame($expected, $this->iterableToArray($paginator->read()));
173
        $last = end($expected);
174
        $this->assertSame($last['id'], $paginator->getLast());
175
    }
176
177
    public function testReadSecondPageOrderedByName(): void
178
    {
179
        $sort = (new Sort(['id', 'name']))->withOrderString('name');
180
181
        $dataReader = (new IterableDataReader($this->getDataSet()))
182
            ->withSort($sort);
183
184
        $paginator = (new KeysetPaginator($dataReader))
185
            ->withPageSize(2)
186
            ->withLast( 'Agent J');
187
188
        $expected = [
189
            [
190
                'id' => 3,
191
                'name' => 'Agent K',
192
            ],
193
            [
194
                'id' => 1,
195
                'name' => 'Codename Boris',
196
            ],
197
        ];
198
199
        $this->assertSame($expected, $this->iterableToArray($paginator->read()));
200
        $last = end($expected);
201
        $this->assertSame($last['name'], $paginator->getLast());
202
    }
203
204
    public function testBackwardPagination(): void
205
    {
206
        $sort = (new Sort(['id', 'name']))->withOrderString('id');
207
208
        $dataReader = (new IterableDataReader($this->getDataSet()))
209
            ->withSort($sort);
210
211
        $paginator = (new KeysetPaginator($dataReader))
212
            ->withPageSize(2)
213
            ->withFirst(5);
214
215
        $expected = [
216
            [
217
                'id' => 2,
218
                'name' => 'Codename Doris',
219
            ],
220
            [
221
                'id' => 3,
222
                'name' => 'Agent K',
223
            ],
224
        ];
225
        $this->assertSame($expected, $this->iterableToArray($paginator->read()));
226
        $first = reset($expected);
227
        $last = end($expected);
228
        $this->assertSame($last['id'], $paginator->getLast(), 'Last value fail!');
229
        $this->assertSame($first['id'], $paginator->getFirst(), 'First value fail!');
230
    }
231
232
    public function testForwardAndBackwardPagination(): void
233
    {
234
        $sort = (new Sort(['id', 'name']))->withOrderString('id');
235
236
        $dataReader = (new IterableDataReader($this->getDataSet()))
237
            ->withSort($sort);
238
239
        $paginator = (new KeysetPaginator($dataReader))
240
            ->withPageSize(2)
241
            ->withLast(2);
242
243
        $expected = [
244
            [
245
                'id' => 3,
246
                'name' => 'Agent K',
247
            ],
248
            [
249
                'id' => 5,
250
                'name' => 'Agent J',
251
            ],
252
        ];
253
        $this->assertSame($expected, $this->iterableToArray($paginator->read()));
254
        $first = reset($expected);
255
        $last = end($expected);
256
        $this->assertSame($last['id'], $paginator->getLast(), 'Last value fail!');
257
        $this->assertSame($first['id'], $paginator->getFirst(), 'First value fail!');
258
259
        $expected = [
260
            [
261
                'id' => 1,
262
                'name' => 'Codename Boris',
263
            ],
264
            [
265
                'id' => 2,
266
                'name' => 'Codename Doris',
267
            ],
268
        ];
269
270
        $paginator = (new KeysetPaginator($dataReader))
271
            ->withPageSize(2)
272
            ->withFirst($paginator->getFirst());
273
274
        $this->assertSame($expected, $this->iterableToArray($paginator->read()));
275
        $first = reset($expected);
276
        $last = end($expected);
277
        $this->assertSame($last['id'], $paginator->getLast(), 'Last value fail!');
278
        $this->assertSame($first['id'], $paginator->getFirst(), 'First value fail!');
279
    }
280
}
281