|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\Common\Paginator\Util; |
|
5
|
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
use Shlinkio\Shlink\Common\Paginator\Util\PaginatorUtilsTrait; |
|
8
|
|
|
use Zend\Paginator\Adapter\ArrayAdapter; |
|
9
|
|
|
use Zend\Paginator\Paginator; |
|
10
|
|
|
|
|
11
|
|
|
use function range; |
|
12
|
|
|
|
|
13
|
|
|
class PaginatorUtilsTraitTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
use PaginatorUtilsTrait; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @test |
|
19
|
|
|
* @dataProvider providePaginatorAdapters |
|
20
|
|
|
*/ |
|
21
|
|
|
public function paginatorIsSerializedAsExpected(array $expectedSerialization, Paginator $paginator): void |
|
22
|
|
|
{ |
|
23
|
|
|
$result = $this->serializePaginator($paginator); |
|
24
|
|
|
$this->assertEquals($expectedSerialization, $result); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function providePaginatorAdapters(): iterable |
|
28
|
|
|
{ |
|
29
|
|
|
yield 'empty' => [ |
|
30
|
|
|
[ |
|
31
|
|
|
'data' => [], |
|
32
|
|
|
'pagination' => [ |
|
33
|
|
|
'currentPage' => 1, |
|
34
|
|
|
'pagesCount' => 0, |
|
35
|
|
|
'itemsPerPage' => 10, |
|
36
|
|
|
'itemsInCurrentPage' => 0, |
|
37
|
|
|
'totalItems' => 0, |
|
38
|
|
|
], |
|
39
|
|
|
], |
|
40
|
|
|
new Paginator(new ArrayAdapter([])), |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
yield 'with two pages' => [ |
|
44
|
|
|
[ |
|
45
|
|
|
'data' => [1, 2], |
|
46
|
|
|
'pagination' => [ |
|
47
|
|
|
'currentPage' => 1, |
|
48
|
|
|
'pagesCount' => 2, |
|
49
|
|
|
'itemsPerPage' => 2, |
|
50
|
|
|
'itemsInCurrentPage' => 2, |
|
51
|
|
|
'totalItems' => 3, |
|
52
|
|
|
], |
|
53
|
|
|
], |
|
54
|
|
|
(new Paginator(new ArrayAdapter(range(1, 3))))->setItemCountPerPage(2), |
|
55
|
|
|
]; |
|
56
|
|
|
|
|
57
|
|
|
yield 'not in first page' => [ |
|
58
|
|
|
[ |
|
59
|
|
|
'data' => [7, 8, 9], |
|
60
|
|
|
'pagination' => [ |
|
61
|
|
|
'currentPage' => 3, |
|
62
|
|
|
'pagesCount' => 5, |
|
63
|
|
|
'itemsPerPage' => 3, |
|
64
|
|
|
'itemsInCurrentPage' => 3, |
|
65
|
|
|
'totalItems' => 15, |
|
66
|
|
|
], |
|
67
|
|
|
], |
|
68
|
|
|
(new Paginator(new ArrayAdapter(range(1, 15))))->setItemCountPerPage(3)->setCurrentPageNumber(3), |
|
69
|
|
|
]; |
|
70
|
|
|
|
|
71
|
|
|
yield 'last incomplete page' => [ |
|
72
|
|
|
[ |
|
73
|
|
|
'data' => [13], |
|
74
|
|
|
'pagination' => [ |
|
75
|
|
|
'currentPage' => 5, |
|
76
|
|
|
'pagesCount' => 5, |
|
77
|
|
|
'itemsPerPage' => 3, |
|
78
|
|
|
'itemsInCurrentPage' => 1, |
|
79
|
|
|
'totalItems' => 13, |
|
80
|
|
|
], |
|
81
|
|
|
], |
|
82
|
|
|
(new Paginator(new ArrayAdapter(range(1, 13))))->setItemCountPerPage(3)->setCurrentPageNumber(5), |
|
83
|
|
|
]; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @test |
|
88
|
|
|
* @dataProvider providePaginatorsInPage |
|
89
|
|
|
*/ |
|
90
|
|
|
public function properlyTellsIfInLastPage(bool $expectedInLastPage, Paginator $paginator): void |
|
91
|
|
|
{ |
|
92
|
|
|
$this->assertEquals($expectedInLastPage, $this->isLastPage($paginator)); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function providePaginatorsInPage(): iterable |
|
96
|
|
|
{ |
|
97
|
|
|
yield 'empty in last page' => [true, new Paginator(new ArrayAdapter([]))]; |
|
98
|
|
|
yield 'not empty in last page' => [ |
|
99
|
|
|
true, |
|
100
|
|
|
(new Paginator(new ArrayAdapter(range(1, 15))))->setCurrentPageNumber(5), |
|
101
|
|
|
]; |
|
102
|
|
|
yield 'not in last page' => [false, new Paginator(new ArrayAdapter(range(1, 15)))]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @test |
|
107
|
|
|
* @dataProvider providePaginatorsToFormat |
|
108
|
|
|
*/ |
|
109
|
|
|
public function pageMessageIsProperlyFormatted(string $expectedMessage, string $pattern, Paginator $paginator): void |
|
110
|
|
|
{ |
|
111
|
|
|
$this->assertEquals($expectedMessage, $this->formatCurrentPageMessage($paginator, $pattern)); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function providePaginatorsToFormat(): iterable |
|
115
|
|
|
{ |
|
116
|
|
|
yield [ |
|
117
|
|
|
'Page "5" out of "10"', |
|
118
|
|
|
'Page "%s" out of "%s"', |
|
119
|
|
|
(new Paginator(new ArrayAdapter(range(1, 20))))->setCurrentPageNumber(5)->setItemCountPerPage(2), |
|
120
|
|
|
]; |
|
121
|
|
|
yield [ |
|
122
|
|
|
'Current: 1. Total: 1', |
|
123
|
|
|
'Current: %s. Total: %s', |
|
124
|
|
|
new Paginator(new ArrayAdapter([1])), |
|
125
|
|
|
]; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|