1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lampager\Cake\Test\TestCase; |
4
|
|
|
|
5
|
|
|
use ArrayIterator; |
6
|
|
|
use Cake\I18n\Time; |
7
|
|
|
use Cake\ORM\Entity; |
8
|
|
|
use Lampager\Cake\PaginationResult; |
9
|
|
|
use Traversable; |
10
|
|
|
|
11
|
|
|
class PaginationResultTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @param array|Traversable $entities |
15
|
|
|
* @param mixed[] $meta |
16
|
|
|
* @dataProvider resultProvider |
17
|
|
|
*/ |
18
|
|
|
public function testIteratorCurrent($entities, array $meta) |
19
|
|
|
{ |
20
|
|
|
$actual = new PaginationResult($entities, $meta); |
21
|
|
|
|
22
|
|
|
$this->assertSame($entities[0], $actual->current()); |
23
|
|
|
|
24
|
|
|
$actual->next(); |
25
|
|
|
$this->assertTrue($actual->valid()); |
26
|
|
|
$this->assertSame(1, $actual->key()); |
27
|
|
|
$this->assertSame($entities[1], $actual->current()); |
28
|
|
|
|
29
|
|
|
$actual->next(); |
30
|
|
|
$this->assertTrue($actual->valid()); |
31
|
|
|
$this->assertSame(2, $actual->key()); |
32
|
|
|
$this->assertSame($entities[2], $actual->current()); |
33
|
|
|
|
34
|
|
|
$actual->next(); |
35
|
|
|
$this->assertFalse($actual->valid()); |
36
|
|
|
|
37
|
|
|
$actual->rewind(); |
38
|
|
|
$this->assertTrue($actual->valid()); |
39
|
|
|
$this->assertSame(0, $actual->key()); |
40
|
|
|
$this->assertSame($entities[0], $actual->current()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param array|Traversable $entities |
45
|
|
|
* @param mixed[] $meta |
46
|
|
|
* @dataProvider resultProvider |
47
|
|
|
*/ |
48
|
|
|
public function testIteratorKey($entities, array $meta) |
49
|
|
|
{ |
50
|
|
|
$actual = new PaginationResult($entities, $meta); |
51
|
|
|
|
52
|
|
|
$this->assertSame(0, $actual->key()); |
53
|
|
|
|
54
|
|
|
$actual->next(); |
55
|
|
|
$this->assertTrue($actual->valid()); |
56
|
|
|
$this->assertSame(1, $actual->key()); |
57
|
|
|
$this->assertSame($entities[1], $actual->current()); |
58
|
|
|
|
59
|
|
|
$actual->next(); |
60
|
|
|
$this->assertTrue($actual->valid()); |
61
|
|
|
$this->assertSame(2, $actual->key()); |
62
|
|
|
$this->assertSame($entities[2], $actual->current()); |
63
|
|
|
|
64
|
|
|
$actual->next(); |
65
|
|
|
$this->assertFalse($actual->valid()); |
66
|
|
|
|
67
|
|
|
$actual->rewind(); |
68
|
|
|
$this->assertTrue($actual->valid()); |
69
|
|
|
$this->assertSame(0, $actual->key()); |
70
|
|
|
$this->assertSame($entities[0], $actual->current()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param array|Traversable $entities |
75
|
|
|
* @param mixed[] $meta |
76
|
|
|
* @dataProvider resultProvider |
77
|
|
|
*/ |
78
|
|
|
public function testIteratorNext($entities, array $meta) |
79
|
|
|
{ |
80
|
|
|
$actual = new PaginationResult($entities, $meta); |
81
|
|
|
|
82
|
|
|
$actual->next(); |
83
|
|
|
$this->assertTrue($actual->valid()); |
84
|
|
|
$this->assertSame(1, $actual->key()); |
85
|
|
|
$this->assertSame($entities[1], $actual->current()); |
86
|
|
|
|
87
|
|
|
$actual->next(); |
88
|
|
|
$this->assertTrue($actual->valid()); |
89
|
|
|
$this->assertSame(2, $actual->key()); |
90
|
|
|
$this->assertSame($entities[2], $actual->current()); |
91
|
|
|
|
92
|
|
|
$actual->next(); |
93
|
|
|
$this->assertFalse($actual->valid()); |
94
|
|
|
|
95
|
|
|
$actual->rewind(); |
96
|
|
|
$this->assertTrue($actual->valid()); |
97
|
|
|
$this->assertSame(0, $actual->key()); |
98
|
|
|
$this->assertSame($entities[0], $actual->current()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param array|Traversable $entities |
103
|
|
|
* @param mixed[] $meta |
104
|
|
|
* @dataProvider resultProvider |
105
|
|
|
*/ |
106
|
|
|
public function testIteratorValid($entities, array $meta) |
107
|
|
|
{ |
108
|
|
|
$actual = new PaginationResult($entities, $meta); |
109
|
|
|
|
110
|
|
|
$this->assertTrue($actual->valid()); |
111
|
|
|
$this->assertSame(0, $actual->key()); |
112
|
|
|
$this->assertSame($entities[0], $actual->current()); |
113
|
|
|
|
114
|
|
|
$actual->next(); |
115
|
|
|
$this->assertTrue($actual->valid()); |
116
|
|
|
$this->assertSame(1, $actual->key()); |
117
|
|
|
$this->assertSame($entities[1], $actual->current()); |
118
|
|
|
|
119
|
|
|
$actual->next(); |
120
|
|
|
$this->assertTrue($actual->valid()); |
121
|
|
|
$this->assertSame(2, $actual->key()); |
122
|
|
|
$this->assertSame($entities[2], $actual->current()); |
123
|
|
|
|
124
|
|
|
$actual->next(); |
125
|
|
|
$this->assertFalse($actual->valid()); |
126
|
|
|
|
127
|
|
|
$actual->rewind(); |
128
|
|
|
$this->assertTrue($actual->valid()); |
129
|
|
|
$this->assertSame(0, $actual->key()); |
130
|
|
|
$this->assertSame($entities[0], $actual->current()); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param array|Traversable $entities |
135
|
|
|
* @param mixed[] $meta |
136
|
|
|
* @param string $expected |
137
|
|
|
* @dataProvider resultProvider |
138
|
|
|
*/ |
139
|
|
|
public function testJsonSerialize($entities, array $meta, $expected) |
140
|
|
|
{ |
141
|
|
|
$actual = json_encode(new PaginationResult($entities, $meta)); |
142
|
|
|
$this->assertJsonStringEqualsJsonString($expected, $actual); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param array|Traversable $entities |
147
|
|
|
* @param mixed[] $meta |
148
|
|
|
* @dataProvider resultProvider |
149
|
|
|
*/ |
150
|
|
|
public function testSerializeAndUnserialize($entities, array $meta) |
151
|
|
|
{ |
152
|
|
|
$actual = unserialize(serialize(new PaginationResult($entities, $meta))); |
153
|
|
|
$expected = new PaginationResult($entities, $meta); |
154
|
|
|
$this->assertJsonEquals($expected, $actual); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function resultProvider() |
158
|
|
|
{ |
159
|
|
|
yield 'Array iteration' => [ |
160
|
|
|
[ |
161
|
|
|
new Entity([ |
162
|
|
|
'id' => 1, |
163
|
|
|
'modified' => new Time('2017-01-01 10:00:00'), |
164
|
|
|
]), |
165
|
|
|
new Entity([ |
166
|
|
|
'id' => 3, |
167
|
|
|
'modified' => new Time('2017-01-01 10:00:00'), |
168
|
|
|
]), |
169
|
|
|
new Entity([ |
170
|
|
|
'id' => 5, |
171
|
|
|
'modified' => new Time('2017-01-01 10:00:00'), |
172
|
|
|
]), |
173
|
|
|
], |
174
|
|
|
[ |
175
|
|
|
'hasPrevious' => null, |
176
|
|
|
'previousCursor' => null, |
177
|
|
|
'hasNext' => true, |
178
|
|
|
'nextCursor' => [ |
179
|
|
|
'Posts.id' => 2, |
180
|
|
|
'Posts.modified' => new Time('2017-01-01 11:00:00'), |
181
|
|
|
], |
182
|
|
|
], |
183
|
|
|
'{ |
184
|
|
|
"records": [ |
185
|
|
|
{ |
186
|
|
|
"id": 1, |
187
|
|
|
"modified": "2017-01-01T10:00:00+00:00" |
188
|
|
|
}, |
189
|
|
|
{ |
190
|
|
|
"id": 3, |
191
|
|
|
"modified": "2017-01-01T10:00:00+00:00" |
192
|
|
|
}, |
193
|
|
|
{ |
194
|
|
|
"id": 5, |
195
|
|
|
"modified": "2017-01-01T10:00:00+00:00" |
196
|
|
|
} |
197
|
|
|
], |
198
|
|
|
"hasPrevious": null, |
199
|
|
|
"previousCursor": null, |
200
|
|
|
"hasNext": true, |
201
|
|
|
"nextCursor": { |
202
|
|
|
"Posts.id": 2, |
203
|
|
|
"Posts.modified": "2017-01-01T11:00:00+00:00" |
204
|
|
|
} |
205
|
|
|
}', |
206
|
|
|
]; |
207
|
|
|
|
208
|
|
|
yield 'ArrayIterator iteration' => [ |
209
|
|
|
new ArrayIterator([ |
210
|
|
|
new Entity([ |
211
|
|
|
'id' => 1, |
212
|
|
|
'modified' => new Time('2017-01-01 10:00:00'), |
213
|
|
|
]), |
214
|
|
|
new Entity([ |
215
|
|
|
'id' => 3, |
216
|
|
|
'modified' => new Time('2017-01-01 10:00:00'), |
217
|
|
|
]), |
218
|
|
|
new Entity([ |
219
|
|
|
'id' => 5, |
220
|
|
|
'modified' => new Time('2017-01-01 10:00:00'), |
221
|
|
|
]), |
222
|
|
|
]), |
223
|
|
|
[ |
224
|
|
|
'hasPrevious' => null, |
225
|
|
|
'previousCursor' => null, |
226
|
|
|
'hasNext' => true, |
227
|
|
|
'nextCursor' => [ |
228
|
|
|
'Posts.id' => 2, |
229
|
|
|
'Posts.modified' => new Time('2017-01-01 11:00:00'), |
230
|
|
|
], |
231
|
|
|
], |
232
|
|
|
'{ |
233
|
|
|
"records": [ |
234
|
|
|
{ |
235
|
|
|
"id": 1, |
236
|
|
|
"modified": "2017-01-01T10:00:00+00:00" |
237
|
|
|
}, |
238
|
|
|
{ |
239
|
|
|
"id": 3, |
240
|
|
|
"modified": "2017-01-01T10:00:00+00:00" |
241
|
|
|
}, |
242
|
|
|
{ |
243
|
|
|
"id": 5, |
244
|
|
|
"modified": "2017-01-01T10:00:00+00:00" |
245
|
|
|
} |
246
|
|
|
], |
247
|
|
|
"hasPrevious": null, |
248
|
|
|
"previousCursor": null, |
249
|
|
|
"hasNext": true, |
250
|
|
|
"nextCursor": { |
251
|
|
|
"Posts.id": 2, |
252
|
|
|
"Posts.modified": "2017-01-01T11:00:00+00:00" |
253
|
|
|
} |
254
|
|
|
}', |
255
|
|
|
]; |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|