|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lampager\Cake\Test\TestCase; |
|
4
|
|
|
|
|
5
|
|
|
use Cake\I18n\Time; |
|
6
|
|
|
use Cake\ORM\Entity; |
|
7
|
|
|
use Lampager\Cake\PaginationResult; |
|
8
|
|
|
|
|
9
|
|
|
class PaginationResultTest extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
public function testIterator() |
|
12
|
|
|
{ |
|
13
|
|
|
$entities = [ |
|
14
|
|
|
new Entity([ |
|
15
|
|
|
'id' => 1, |
|
16
|
|
|
'modified' => new Time('2017-01-01 10:00:00'), |
|
17
|
|
|
]), |
|
18
|
|
|
new Entity([ |
|
19
|
|
|
'id' => 3, |
|
20
|
|
|
'modified' => new Time('2017-01-01 10:00:00'), |
|
21
|
|
|
]), |
|
22
|
|
|
new Entity([ |
|
23
|
|
|
'id' => 5, |
|
24
|
|
|
'modified' => new Time('2017-01-01 10:00:00'), |
|
25
|
|
|
]), |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
|
|
$actual = new PaginationResult($entities, []); |
|
29
|
|
|
|
|
30
|
|
|
$this->assertTrue($actual->valid()); |
|
31
|
|
|
$this->assertSame(0, $actual->key()); |
|
32
|
|
|
$this->assertSame($entities[0], $actual->current()); |
|
33
|
|
|
|
|
34
|
|
|
$actual->next(); |
|
35
|
|
|
$this->assertTrue($actual->valid()); |
|
36
|
|
|
$this->assertSame(1, $actual->key()); |
|
37
|
|
|
$this->assertSame($entities[1], $actual->current()); |
|
38
|
|
|
|
|
39
|
|
|
$actual->next(); |
|
40
|
|
|
$this->assertTrue($actual->valid()); |
|
41
|
|
|
$this->assertSame(2, $actual->key()); |
|
42
|
|
|
$this->assertSame($entities[2], $actual->current()); |
|
43
|
|
|
|
|
44
|
|
|
$actual->next(); |
|
45
|
|
|
$this->assertFalse($actual->valid()); |
|
46
|
|
|
|
|
47
|
|
|
$actual->rewind(); |
|
48
|
|
|
$this->assertTrue($actual->valid()); |
|
49
|
|
|
$this->assertSame(0, $actual->key()); |
|
50
|
|
|
$this->assertSame($entities[0], $actual->current()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testJsonSerialize() |
|
54
|
|
|
{ |
|
55
|
|
|
$entities = [ |
|
56
|
|
|
new Entity([ |
|
57
|
|
|
'id' => 1, |
|
58
|
|
|
'modified' => new Time('2017-01-01 10:00:00'), |
|
59
|
|
|
]), |
|
60
|
|
|
new Entity([ |
|
61
|
|
|
'id' => 3, |
|
62
|
|
|
'modified' => new Time('2017-01-01 10:00:00'), |
|
63
|
|
|
]), |
|
64
|
|
|
new Entity([ |
|
65
|
|
|
'id' => 5, |
|
66
|
|
|
'modified' => new Time('2017-01-01 10:00:00'), |
|
67
|
|
|
]), |
|
68
|
|
|
]; |
|
69
|
|
|
|
|
70
|
|
|
$meta = [ |
|
71
|
|
|
'hasPrevious' => null, |
|
72
|
|
|
'previousCursor' => null, |
|
73
|
|
|
'hasNext' => true, |
|
74
|
|
|
'nextCursor' => [ |
|
75
|
|
|
'Posts.id' => 2, |
|
76
|
|
|
'Posts.modified' => new Time('2017-01-01 11:00:00'), |
|
77
|
|
|
], |
|
78
|
|
|
]; |
|
79
|
|
|
|
|
80
|
|
|
$actual = json_encode(new PaginationResult($entities, $meta)); |
|
81
|
|
|
$expected = '{ |
|
82
|
|
|
"records": [ |
|
83
|
|
|
{ |
|
84
|
|
|
"id": 1, |
|
85
|
|
|
"modified": "2017-01-01T10:00:00+00:00" |
|
86
|
|
|
}, |
|
87
|
|
|
{ |
|
88
|
|
|
"id": 3, |
|
89
|
|
|
"modified": "2017-01-01T10:00:00+00:00" |
|
90
|
|
|
}, |
|
91
|
|
|
{ |
|
92
|
|
|
"id": 5, |
|
93
|
|
|
"modified": "2017-01-01T10:00:00+00:00" |
|
94
|
|
|
} |
|
95
|
|
|
], |
|
96
|
|
|
"hasPrevious": null, |
|
97
|
|
|
"previousCursor": null, |
|
98
|
|
|
"hasNext": true, |
|
99
|
|
|
"nextCursor": { |
|
100
|
|
|
"Posts.id": 2, |
|
101
|
|
|
"Posts.modified": "2017-01-01T11:00:00+00:00" |
|
102
|
|
|
} |
|
103
|
|
|
}'; |
|
104
|
|
|
|
|
105
|
|
|
$this->assertJsonStringEqualsJsonString($expected, $actual); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function testSerializeAndUnserialize() |
|
109
|
|
|
{ |
|
110
|
|
|
$entities = [ |
|
111
|
|
|
new Entity([ |
|
112
|
|
|
'id' => 1, |
|
113
|
|
|
'modified' => new Time('2017-01-01 10:00:00'), |
|
114
|
|
|
]), |
|
115
|
|
|
new Entity([ |
|
116
|
|
|
'id' => 3, |
|
117
|
|
|
'modified' => new Time('2017-01-01 10:00:00'), |
|
118
|
|
|
]), |
|
119
|
|
|
new Entity([ |
|
120
|
|
|
'id' => 5, |
|
121
|
|
|
'modified' => new Time('2017-01-01 10:00:00'), |
|
122
|
|
|
]), |
|
123
|
|
|
]; |
|
124
|
|
|
|
|
125
|
|
|
$meta = [ |
|
126
|
|
|
'hasPrevious' => null, |
|
127
|
|
|
'previousCursor' => null, |
|
128
|
|
|
'hasNext' => true, |
|
129
|
|
|
'nextCursor' => [ |
|
130
|
|
|
'Posts.id' => 2, |
|
131
|
|
|
'Posts.modified' => new Time('2017-01-01 11:00:00'), |
|
132
|
|
|
], |
|
133
|
|
|
]; |
|
134
|
|
|
|
|
135
|
|
|
$actual = unserialize(serialize(new PaginationResult($entities, $meta))); |
|
136
|
|
|
$expected = new PaginationResult($entities, $meta); |
|
137
|
|
|
$this->assertJsonEquals($expected, $actual); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|