1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Povs\ListerBundle\View; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Povs\ListerBundle\Service\Paginator; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author Povilas Margaiatis <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class PagerViewTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
private $paginator; |
14
|
|
|
|
15
|
|
|
public function setUp() |
16
|
|
|
{ |
17
|
|
|
$this->paginator = $this->createMock(Paginator::class); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function testGetCurrentPage(): void |
21
|
|
|
{ |
22
|
|
|
$view = $this->getPagerView(1, 20); |
23
|
|
|
$this->assertEquals(1, $view->getCurrentPage()); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testGetTotal(): void |
27
|
|
|
{ |
28
|
|
|
$view = $this->getPagerView(1, 20, 1000); |
29
|
|
|
$this->assertEquals(1000, $view->getTotal()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testGetLength(): void |
33
|
|
|
{ |
34
|
|
|
$view = $this->getPagerView(1, 20); |
35
|
|
|
$this->assertEquals(20, $view->getLength()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testGetFirstResultWithPage(): void |
39
|
|
|
{ |
40
|
|
|
$view = $this->getPagerView(1, 20); |
41
|
|
|
$this->assertEquals(81, $view->getFirstResult(5)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testGetFirstResultWithoutPage(): void |
45
|
|
|
{ |
46
|
|
|
$view = $this->getPagerView(2, 20); |
47
|
|
|
$this->assertEquals(21, $view->getFirstResult()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testGetLastResultWithPageWithTotalGreaterThanResult(): void |
51
|
|
|
{ |
52
|
|
|
$view = $this->getPagerView(1, 20, 100); |
53
|
|
|
$this->assertEquals(100, $view->getLastResult(5)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testGetLastResultWithPageWithTotalLessThanResult(): void |
57
|
|
|
{ |
58
|
|
|
$view = $this->getPagerView(1, 20, 50); |
59
|
|
|
$this->assertEquals(50, $view->getLastResult(5)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testGetLastResultWithoutPage(): void |
63
|
|
|
{ |
64
|
|
|
$view = $this->getPagerView(2, 20, 100); |
65
|
|
|
$this->assertEquals(40, $view->getLastResult()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testGetLastPage(): void |
69
|
|
|
{ |
70
|
|
|
$view = $this->getPagerView(2, 40, 100); |
71
|
|
|
$this->assertEquals(3, $view->getLastPage()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testGetPrevPageValid(): void |
75
|
|
|
{ |
76
|
|
|
$view = $this->getPagerView(3, 40, 100); |
77
|
|
|
$this->assertEquals(2, $view->getPrevPage()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testGetPrevPageNotValid(): void |
81
|
|
|
{ |
82
|
|
|
$view = $this->getPagerView(1, 40, null); |
83
|
|
|
$this->assertNull($view->getPrevPage()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testGetNextPageValid(): void |
87
|
|
|
{ |
88
|
|
|
$view = $this->getPagerView(2, 40, 100); |
89
|
|
|
$this->assertEquals(3, $view->getNextPage()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testGetNextPageNotValid(): void |
93
|
|
|
{ |
94
|
|
|
$view = $this->getPagerView(3, 40, 100); |
95
|
|
|
$this->assertNull($view->getNextPage()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testValidatePageValid(): void |
99
|
|
|
{ |
100
|
|
|
$view = $this->getPagerView(3, 40, 100); |
101
|
|
|
$this->assertTrue($view->validatePage(2)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testValidatePageNotValid(): void |
105
|
|
|
{ |
106
|
|
|
$view = $this->getPagerView(3, 40, 100); |
107
|
|
|
$this->assertFalse($view->validatePage(4)); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testIteratePageValid(): void |
111
|
|
|
{ |
112
|
|
|
$this->paginator->expects($this->exactly(2)) |
113
|
|
|
->method('getCount') |
114
|
|
|
->willReturn(100); |
115
|
|
|
$this->paginator->expects($this->exactly(2)) |
116
|
|
|
->method('getData') |
117
|
|
|
->willReturnMap([ |
118
|
|
|
[40, 40, ['foo' => 'bar']], |
119
|
|
|
[80, 40, ['foo1' => 'bar1']] |
120
|
|
|
]); |
121
|
|
|
|
122
|
|
|
$view = $this->getPagerView(2, 40); |
123
|
|
|
$view->iteratePage(3); |
124
|
|
|
$this->assertEquals(3, $view->getCurrentPage()); |
125
|
|
|
$this->assertEquals(['foo1' => 'bar1'], $view->getData()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testIteratePageNotValid(): void |
129
|
|
|
{ |
130
|
|
|
$view = $this->getPagerView(2, 40, 100); |
131
|
|
|
$this->assertFalse($view->iteratePage(4)); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function testIterateNextPageValid(): void |
135
|
|
|
{ |
136
|
|
|
$this->paginator->expects($this->exactly(2)) |
137
|
|
|
->method('getCount') |
138
|
|
|
->willReturn(100); |
139
|
|
|
$this->paginator->expects($this->exactly(2)) |
140
|
|
|
->method('getData') |
141
|
|
|
->willReturnMap([ |
142
|
|
|
[0, 40, ['foo' => 'bar']], |
143
|
|
|
[40, 40, ['foo1' => 'bar1']] |
144
|
|
|
]); |
145
|
|
|
|
146
|
|
|
$view = $this->getPagerView(1, 40); |
147
|
|
|
$view->iterateNextPage(); |
148
|
|
|
$this->assertEquals(2, $view->getCurrentPage()); |
149
|
|
|
$this->assertEquals(['foo1' => 'bar1'], $view->getData()); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function testIterateNextPageNotValid(): void |
153
|
|
|
{ |
154
|
|
|
$view = $this->getPagerView(3, 40, 100); |
155
|
|
|
$this->assertFalse($view->iterateNextPage()); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function testGetData(): void |
159
|
|
|
{ |
160
|
|
|
$this->paginator->expects($this->once()) |
161
|
|
|
->method('getData') |
162
|
|
|
->with(80, 40) |
163
|
|
|
->willReturn(['foo' => 'bar']); |
164
|
|
|
|
165
|
|
|
$view = $this->getPagerView(3, 40, 100); |
166
|
|
|
$this->assertEquals(['foo' => 'bar'], $view->getData()); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @dataProvider pagesProvider |
171
|
|
|
* @param int $page |
172
|
|
|
* @param int $length |
173
|
|
|
* @param int $perPage |
174
|
|
|
* @param int $pagesLength |
175
|
|
|
* @param array $expectedResult |
176
|
|
|
*/ |
177
|
|
|
public function testGetPages(int $page, int $length, int $perPage, int $pagesLength, array $expectedResult): void |
178
|
|
|
{ |
179
|
|
|
$view = $this->getPagerView($page, $length, $perPage); |
180
|
|
|
$res = $view->getPages($pagesLength); |
181
|
|
|
|
182
|
|
|
$this->assertEquals($expectedResult, $res); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function pagesProvider(): array |
186
|
|
|
{ |
187
|
|
|
return [ |
188
|
|
|
[1, 20, 100, 1, $this->getPages([1, 2, 3, 4, 5], 1)], |
189
|
|
|
[5, 20, 1000, 2, $this->getPages([1, 2, 3, 4, 5, 6, 7, null, 50], 5)], |
190
|
|
|
[1, 20, 0, 1, []], |
191
|
|
|
[10, 20, 1000, 2, $this->getPages([1, null, 8, 9, 10, 11, 12, null, 50], 10)], |
192
|
|
|
[10, 20, 1000, 1, $this->getPages([1, null, 9, 10, 11, null, 50], 10)], |
193
|
|
|
[48, 20, 1000, 1, $this->getPages([1, null, 46, 47, 48, 49, 50], 48)], |
194
|
|
|
[50, 20, 1000, 2, $this->getPages([1, null, 44, 45, 46, 47, 48, 49, 50], 50)], |
195
|
|
|
]; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param array $pages |
200
|
|
|
* @param int $activePage |
201
|
|
|
* |
202
|
|
|
* @return array |
203
|
|
|
*/ |
204
|
|
|
private function getPages(array $pages, int $activePage): array |
205
|
|
|
{ |
206
|
|
|
$p = []; |
207
|
|
|
|
208
|
|
|
foreach ($pages as $page) { |
209
|
|
|
$p[] = [ |
210
|
|
|
'page' => $page, |
211
|
|
|
'active' => $activePage === $page |
212
|
|
|
]; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return $p; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param int $currentPage |
220
|
|
|
* @param int $perPage |
221
|
|
|
* @param int|null $total |
222
|
|
|
* @param array|null $data |
223
|
|
|
* |
224
|
|
|
* @return PagerView |
225
|
|
|
*/ |
226
|
|
|
private function getPagerView(int $currentPage, int $perPage, ?int $total = null, ?array $data = null): PagerView |
227
|
|
|
{ |
228
|
|
|
if (null !== $total) { |
229
|
|
|
$this->paginator->expects($this->once()) |
230
|
|
|
->method('getCount') |
231
|
|
|
->willReturn($total); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
if (null !== $data) { |
235
|
|
|
$this->paginator->expects($this->once()) |
236
|
|
|
->method('getData') |
237
|
|
|
->willReturn($data); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return new PagerView($this->paginator, $currentPage, $perPage); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|