1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overblog\GraphQLBundle\Tests\Relay\Connection; |
13
|
|
|
|
14
|
|
|
use Overblog\GraphQLBundle\Definition\Argument; |
15
|
|
|
use Overblog\GraphQLBundle\Relay\Connection\Output\Connection; |
16
|
|
|
use Overblog\GraphQLBundle\Relay\Connection\Paginator; |
17
|
|
|
|
18
|
|
|
class PaginatorTest extends \PHPUnit_Framework_TestCase |
19
|
|
|
{ |
20
|
|
|
protected $data = ['A', 'B', 'C', 'D', 'E']; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Generates an alphabet array starting at 'A' + $offset, ending always at 'E'. |
24
|
|
|
* |
25
|
|
|
* @param int $offset |
26
|
|
|
* |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
|
public function getData($offset = 0) |
30
|
|
|
{ |
31
|
|
|
return array_slice($this->data, $offset); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testGetData() |
35
|
|
|
{ |
36
|
|
|
$this->assertSame($this->data, $this->getData()); |
37
|
|
|
$this->assertSame(['C', 'D', 'E'], $this->getData(2)); |
38
|
|
|
$this->assertSame(['E'], $this->getData(4)); |
39
|
|
|
$this->assertSame([], $this->getData(5)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Compares node values with an array of expected values. |
44
|
|
|
* |
45
|
|
|
* @param $expected |
46
|
|
|
* @param $result |
47
|
|
|
*/ |
48
|
|
|
protected function assertSameEdgeNodeValue($expected, Connection $result) |
49
|
|
|
{ |
50
|
|
|
$this->assertEquals(count($expected), count($result->edges)); |
51
|
|
|
foreach ($expected as $key => $value) { |
52
|
|
|
$this->assertEquals($value, $result->edges[$key]->node); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testForward() |
57
|
|
|
{ |
58
|
|
|
$paginator = new Paginator(function ($offset, $limit) { |
59
|
|
|
$this->assertSame(0, $offset); |
60
|
|
|
$this->assertSame(5, $limit); // Includes the extra element to check if next page is available |
61
|
|
|
|
62
|
|
|
return $this->getData($offset); |
63
|
|
|
}); |
64
|
|
|
|
65
|
|
|
$result = $paginator->forward(new Argument(['first' => 4])); |
66
|
|
|
|
67
|
|
|
$this->assertCount(4, $result->edges); |
68
|
|
|
$this->assertSameEdgeNodeValue(['A', 'B', 'C', 'D'], $result); |
69
|
|
|
$this->assertTrue($result->pageInfo->hasNextPage); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
View Code Duplication |
public function testForwardAfterInMiddle() |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$paginator = new Paginator(function ($offset, $limit) { |
75
|
|
|
$this->assertSame(2, $offset); |
76
|
|
|
$this->assertSame(3, $limit); // Includes the extra element to check if next page is available |
77
|
|
|
|
78
|
|
|
return $this->getData($offset); |
79
|
|
|
}); |
80
|
|
|
|
81
|
|
|
$result = $paginator->forward(new Argument(['first' => 1, 'after' => base64_encode('arrayconnection:2')])); |
82
|
|
|
|
83
|
|
|
$this->assertCount(1, $result->edges); |
84
|
|
|
$this->assertSameEdgeNodeValue(['D'], $result); |
85
|
|
|
$this->assertTrue($result->pageInfo->hasNextPage); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
View Code Duplication |
public function testForwardAfterAtTheEnd() |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$paginator = new Paginator(function ($offset, $limit) { |
91
|
|
|
$this->assertSame(2, $offset); |
92
|
|
|
$this->assertSame(4, $limit); // Includes the extra element to check if next page is available |
93
|
|
|
|
94
|
|
|
return $this->getData($offset); |
95
|
|
|
}); |
96
|
|
|
|
97
|
|
|
$result = $paginator->forward(new Argument(['first' => 2, 'after' => base64_encode('arrayconnection:2')])); |
98
|
|
|
|
99
|
|
|
$this->assertCount(2, $result->edges); |
100
|
|
|
$this->assertSameEdgeNodeValue(['D', 'E'], $result); |
101
|
|
|
$this->assertFalse($result->pageInfo->hasNextPage); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
View Code Duplication |
public function testForwardAfterLast() |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$paginator = new Paginator(function ($offset, $limit) { |
107
|
|
|
$this->assertSame(4, $offset); |
108
|
|
|
$this->assertSame(7, $limit); // Includes the extra element to check if next page is available |
109
|
|
|
|
110
|
|
|
return $this->getData($offset); |
111
|
|
|
}); |
112
|
|
|
|
113
|
|
|
$result = $paginator->forward(new Argument(['first' => 5, 'after' => base64_encode('arrayconnection:4')])); |
114
|
|
|
|
115
|
|
|
$this->assertCount(0, $result->edges); |
116
|
|
|
$this->assertSameEdgeNodeValue([], $result); |
117
|
|
|
$this->assertFalse($result->pageInfo->hasNextPage); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
View Code Duplication |
public function testForwardAfterWithUnvalidCursorAndSlice() |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
$paginator = new Paginator(function ($offset, $limit) { |
123
|
|
|
$this->assertSame(0, $offset); |
124
|
|
|
$this->assertSame(5, $limit); // Includes the extra element to check if next page is available |
125
|
|
|
|
126
|
|
|
return $this->getData($offset); |
127
|
|
|
}); |
128
|
|
|
|
129
|
|
|
$result = $paginator->forward(new Argument(['first' => 4, 'after' => base64_encode('badcursor:aze')])); |
130
|
|
|
|
131
|
|
|
$this->assertCount(4, $result->edges); |
132
|
|
|
$this->assertSameEdgeNodeValue(['A', 'B', 'C', 'D'], $result); |
133
|
|
|
$this->assertTrue($result->pageInfo->hasNextPage); |
134
|
|
|
$this->assertFalse($result->pageInfo->hasPreviousPage); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testBackward() |
138
|
|
|
{ |
139
|
|
|
$paginator = new Paginator(function ($offset, $limit) { |
140
|
|
|
$this->assertSame(2, $offset); |
141
|
|
|
$this->assertSame(3, $limit); |
142
|
|
|
|
143
|
|
|
return $this->getData($offset); |
144
|
|
|
}); |
145
|
|
|
|
146
|
|
|
$result = $paginator->backward(new Argument(['last' => 3]), 5); |
147
|
|
|
|
148
|
|
|
$this->assertCount(3, $result->edges); |
149
|
|
|
$this->assertSameEdgeNodeValue(['C', 'D', 'E'], $result); |
150
|
|
|
$this->assertTrue($result->pageInfo->hasPreviousPage); |
151
|
|
|
$this->assertFalse($result->pageInfo->hasNextPage); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testBackwardWithLimitEqualsToTotal() |
155
|
|
|
{ |
156
|
|
|
$paginator = new Paginator(function ($offset, $limit) { |
157
|
|
|
$this->assertSame(0, $offset); |
158
|
|
|
$this->assertSame(5, $limit); |
159
|
|
|
|
160
|
|
|
return $this->getData($offset); |
161
|
|
|
}); |
162
|
|
|
|
163
|
|
|
$result = $paginator->backward(new Argument(['last' => 5]), 5); |
164
|
|
|
|
165
|
|
|
$this->assertCount(5, $result->edges); |
166
|
|
|
$this->assertSameEdgeNodeValue($this->data, $result); |
167
|
|
|
$this->assertFalse($result->pageInfo->hasPreviousPage); |
168
|
|
|
$this->assertFalse($result->pageInfo->hasNextPage); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
View Code Duplication |
public function testBackwardBeforeLast() |
|
|
|
|
172
|
|
|
{ |
173
|
|
|
$paginator = new Paginator(function ($offset, $limit) { |
174
|
|
|
$this->assertSame(4, $limit); |
175
|
|
|
|
176
|
|
|
return $this->getData($offset); |
177
|
|
|
}); |
178
|
|
|
|
179
|
|
|
$result = $paginator->backward(new Argument(['last' => 4, 'before' => base64_encode('arrayconnection:4')]), 5); |
180
|
|
|
|
181
|
|
|
$this->assertCount(4, $result->edges); |
182
|
|
|
$this->assertSameEdgeNodeValue(['A', 'B', 'C', 'D'], $result); |
183
|
|
|
$this->assertFalse($result->pageInfo->hasPreviousPage); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
View Code Duplication |
public function testBackwardPartialBeforeInMiddle() |
|
|
|
|
187
|
|
|
{ |
188
|
|
|
$paginator = new Paginator(function ($offset, $limit) { |
189
|
|
|
$this->assertSame(1, $offset); |
190
|
|
|
$this->assertSame(2, $limit); |
191
|
|
|
|
192
|
|
|
return $this->getData($offset); |
193
|
|
|
}); |
194
|
|
|
|
195
|
|
|
$result = $paginator->backward(new Argument(['last' => 2, 'before' => base64_encode('arrayconnection:3')]), 5); |
196
|
|
|
|
197
|
|
|
$this->assertCount(2, $result->edges); |
198
|
|
|
$this->assertSameEdgeNodeValue(['B', 'C'], $result); |
199
|
|
|
$this->assertTrue($result->pageInfo->hasPreviousPage); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
View Code Duplication |
public function testAutoBackward() |
|
|
|
|
203
|
|
|
{ |
204
|
|
|
$paginator = new Paginator(function ($offset, $limit) { |
205
|
|
|
$this->assertSame(1, $offset); |
206
|
|
|
$this->assertSame(4, $limit); |
207
|
|
|
|
208
|
|
|
return $this->getData($offset); |
209
|
|
|
}); |
210
|
|
|
|
211
|
|
|
$result = $paginator->auto(new Argument(['last' => 4]), 5); |
212
|
|
|
|
213
|
|
|
$this->assertCount(4, $result->edges); |
214
|
|
|
$this->assertSameEdgeNodeValue(['B', 'C', 'D', 'E'], $result); |
215
|
|
|
$this->assertTrue($result->pageInfo->hasPreviousPage); |
216
|
|
|
$this->assertFalse($result->pageInfo->hasNextPage); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
View Code Duplication |
public function testAutoForward() |
|
|
|
|
220
|
|
|
{ |
221
|
|
|
$paginator = new Paginator(function ($offset, $limit) { |
222
|
|
|
$this->assertSame(0, $offset); |
223
|
|
|
$this->assertSame(5, $limit); // Includes the extra element to check if next page is available |
224
|
|
|
|
225
|
|
|
return $this->getData($offset); |
226
|
|
|
}); |
227
|
|
|
|
228
|
|
|
$result = $paginator->auto(new Argument(['first' => 4]), 5); |
229
|
|
|
|
230
|
|
|
$this->assertCount(4, $result->edges); |
231
|
|
|
$this->assertSameEdgeNodeValue(['A', 'B', 'C', 'D'], $result); |
232
|
|
|
$this->assertTrue($result->pageInfo->hasNextPage); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function testAutoBackwardWithCallable() |
236
|
|
|
{ |
237
|
|
|
$paginator = new Paginator(function ($offset, $limit) { |
238
|
|
|
$this->assertSame(1, $offset); |
239
|
|
|
$this->assertSame(4, $limit); |
240
|
|
|
|
241
|
|
|
return $this->getData($offset); |
242
|
|
|
}); |
243
|
|
|
|
244
|
|
|
$countCalled = false; |
245
|
|
|
$result = $paginator->auto(new Argument(['last' => 4]), function () use (&$countCalled) { |
246
|
|
|
$countCalled = true; |
247
|
|
|
|
248
|
|
|
return 5; |
249
|
|
|
}); |
250
|
|
|
|
251
|
|
|
$this->assertTrue($countCalled); |
252
|
|
|
$this->assertCount(4, $result->edges); |
253
|
|
|
$this->assertSameEdgeNodeValue(['B', 'C', 'D', 'E'], $result); |
254
|
|
|
$this->assertTrue($result->pageInfo->hasPreviousPage); |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.