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\Output; |
13
|
|
|
|
14
|
|
|
use Overblog\GraphQLBundle\Relay\Connection\Output\Connection; |
15
|
|
|
use Overblog\GraphQLBundle\Relay\Connection\Output\ConnectionBuilder; |
16
|
|
|
use Overblog\GraphQLBundle\Relay\Connection\Output\Edge; |
17
|
|
|
use Overblog\GraphQLBundle\Relay\Connection\Output\PageInfo; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class ConnectionBuilderTest. |
21
|
|
|
* |
22
|
|
|
* @see https://github.com/graphql/graphql-relay-js/blob/master/src/connection/__tests__/arrayconnection.js |
23
|
|
|
*/ |
24
|
|
|
class ConnectionBuilderTest extends \PHPUnit_Framework_TestCase |
25
|
|
|
{ |
26
|
|
|
private $letters = ['A', 'B', 'C', 'D', 'E']; |
27
|
|
|
|
28
|
|
View Code Duplication |
public function testBasicSlicing() |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
$actual = ConnectionBuilder::connectionFromArray($this->letters); |
31
|
|
|
|
32
|
|
|
$expected = $this->getExpectedConnection($this->letters, false, false); |
33
|
|
|
|
34
|
|
|
$this->assertEquals($expected, $actual); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testRespectsASmallerFirst() |
38
|
|
|
{ |
39
|
|
|
$actual = ConnectionBuilder::connectionFromArray($this->letters, ['first' => 2]); |
40
|
|
|
|
41
|
|
|
$expected = $this->getExpectedConnection(['A', 'B'], false, true); |
42
|
|
|
|
43
|
|
|
$this->assertEquals($expected, $actual); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
View Code Duplication |
public function testRespectsAnOverlyLargeFirst() |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$actual = ConnectionBuilder::connectionFromArray($this->letters, ['first' => 10]); |
49
|
|
|
|
50
|
|
|
$expected = $this->getExpectedConnection($this->letters, false, false); |
51
|
|
|
|
52
|
|
|
$this->assertEquals($expected, $actual); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testRespectsASmallerLast() |
56
|
|
|
{ |
57
|
|
|
$actual = ConnectionBuilder::connectionFromArray($this->letters, ['last' => 2]); |
58
|
|
|
|
59
|
|
|
$expected = $this->getExpectedConnection(['D', 'E'], true, false); |
60
|
|
|
|
61
|
|
|
$this->assertEquals($expected, $actual); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
View Code Duplication |
public function testRespectsAnOverlyLargeLast() |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$actual = ConnectionBuilder::connectionFromArray($this->letters, ['last' => 10]); |
67
|
|
|
|
68
|
|
|
$expected = $this->getExpectedConnection($this->letters, false, false); |
69
|
|
|
|
70
|
|
|
$this->assertEquals($expected, $actual); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testRespectsFirstAndAfter() |
74
|
|
|
{ |
75
|
|
|
$actual = ConnectionBuilder::connectionFromArray($this->letters, ['first' => 2, 'after' => 'YXJyYXljb25uZWN0aW9uOjE=']); |
76
|
|
|
|
77
|
|
|
$expected = $this->getExpectedConnection(['C', 'D'], false, true); |
78
|
|
|
|
79
|
|
|
$this->assertEquals($expected, $actual); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
View Code Duplication |
public function testRespectsFirstAndAfterWithLongFirst() |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$actual = ConnectionBuilder::connectionFromArray($this->letters, ['first' => 10, 'after' => 'YXJyYXljb25uZWN0aW9uOjE=']); |
85
|
|
|
|
86
|
|
|
$expected = $this->getExpectedConnection(['C', 'D', 'E'], false, false); |
87
|
|
|
|
88
|
|
|
$this->assertEquals($expected, $actual); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testRespectsLastAndBefore() |
92
|
|
|
{ |
93
|
|
|
$actual = ConnectionBuilder::connectionFromArray($this->letters, ['last' => 2, 'before' => 'YXJyYXljb25uZWN0aW9uOjM=']); |
94
|
|
|
|
95
|
|
|
$expected = $this->getExpectedConnection(['B', 'C'], true, false); |
96
|
|
|
|
97
|
|
|
$this->assertEquals($expected, $actual); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
View Code Duplication |
public function testRespectsLastAndBeforeWithLongLast() |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$actual = ConnectionBuilder::connectionFromArray($this->letters, ['last' => 10, 'before' => 'YXJyYXljb25uZWN0aW9uOjM=']); |
103
|
|
|
|
104
|
|
|
$expected = $this->getExpectedConnection(['A', 'B', 'C'], false, false); |
105
|
|
|
|
106
|
|
|
$this->assertEquals($expected, $actual); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
View Code Duplication |
public function testRespectsFirstAndAfterAndBeforeTooFew() |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$actual = ConnectionBuilder::connectionFromArray( |
112
|
|
|
$this->letters, |
113
|
|
|
['first' => 2, 'after' => 'YXJyYXljb25uZWN0aW9uOjA=', 'before' => 'YXJyYXljb25uZWN0aW9uOjQ='] |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
$expected = $this->getExpectedConnection(['B', 'C'], false, true); |
117
|
|
|
|
118
|
|
|
$this->assertEquals($expected, $actual); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
View Code Duplication |
public function testRespectsFirstAndAfterAndBeforeTooMany() |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
$actual = ConnectionBuilder::connectionFromArray( |
124
|
|
|
$this->letters, |
125
|
|
|
['first' => 4, 'after' => 'YXJyYXljb25uZWN0aW9uOjA=', 'before' => 'YXJyYXljb25uZWN0aW9uOjQ='] |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$expected = $this->getExpectedConnection(['B', 'C', 'D'], false, false); |
129
|
|
|
|
130
|
|
|
$this->assertEquals($expected, $actual); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
View Code Duplication |
public function testRespectsFirstAndAfterAndBeforeExactlyRight() |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
$actual = ConnectionBuilder::connectionFromArray( |
136
|
|
|
$this->letters, |
137
|
|
|
['first' => 3, 'after' => 'YXJyYXljb25uZWN0aW9uOjA=', 'before' => 'YXJyYXljb25uZWN0aW9uOjQ='] |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
$expected = $this->getExpectedConnection(['B', 'C', 'D'], false, false); |
141
|
|
|
|
142
|
|
|
$this->assertEquals($expected, $actual); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
View Code Duplication |
public function testRespectsLastAndAfterAndBeforeTooFew() |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
$actual = ConnectionBuilder::connectionFromArray( |
148
|
|
|
$this->letters, |
149
|
|
|
['last' => 2, 'after' => 'YXJyYXljb25uZWN0aW9uOjA=', 'before' => 'YXJyYXljb25uZWN0aW9uOjQ='] |
150
|
|
|
); |
151
|
|
|
|
152
|
|
|
$expected = $this->getExpectedConnection(['C', 'D'], true, false); |
153
|
|
|
|
154
|
|
|
$this->assertEquals($expected, $actual); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
View Code Duplication |
public function testRespectsLastAndAfterAndBeforeTooMany() |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
$actual = ConnectionBuilder::connectionFromArray( |
160
|
|
|
$this->letters, |
161
|
|
|
['last' => 4, 'after' => 'YXJyYXljb25uZWN0aW9uOjA=', 'before' => 'YXJyYXljb25uZWN0aW9uOjQ='] |
162
|
|
|
); |
163
|
|
|
|
164
|
|
|
$expected = $this->getExpectedConnection(['B', 'C', 'D'], false, false); |
165
|
|
|
|
166
|
|
|
$this->assertEquals($expected, $actual); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
View Code Duplication |
public function testRespectsLastAndAfterAndBeforeExactlyRight() |
|
|
|
|
170
|
|
|
{ |
171
|
|
|
$actual = ConnectionBuilder::connectionFromArray( |
172
|
|
|
$this->letters, |
173
|
|
|
['last' => 3, 'after' => 'YXJyYXljb25uZWN0aW9uOjA=', 'before' => 'YXJyYXljb25uZWN0aW9uOjQ='] |
174
|
|
|
); |
175
|
|
|
|
176
|
|
|
$expected = $this->getExpectedConnection(['B', 'C', 'D'], false, false); |
177
|
|
|
|
178
|
|
|
$this->assertEquals($expected, $actual); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function testReturnsNoElementsIfFirstIs0() |
182
|
|
|
{ |
183
|
|
|
$actual = ConnectionBuilder::connectionFromArray( |
184
|
|
|
$this->letters, |
185
|
|
|
['first' => 0] |
186
|
|
|
); |
187
|
|
|
|
188
|
|
|
$expected = new Connection( |
189
|
|
|
[ |
190
|
|
|
], |
191
|
|
|
new PageInfo(null, null, false, true) |
192
|
|
|
); |
193
|
|
|
|
194
|
|
|
$this->assertEquals($expected, $actual); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function testReturnsAllElementsIfCursorsAreInvalid() |
198
|
|
|
{ |
199
|
|
|
$actual = ConnectionBuilder::connectionFromArray( |
200
|
|
|
$this->letters, |
201
|
|
|
['before' => 'invalid', 'after' => 'invalid'] |
202
|
|
|
); |
203
|
|
|
|
204
|
|
|
$expected = $this->getExpectedConnection($this->letters, false, false); |
205
|
|
|
|
206
|
|
|
$this->assertEquals($expected, $actual); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function testReturnsAllElementsIfCursorsAreOnTheOutside() |
210
|
|
|
{ |
211
|
|
|
$actual = ConnectionBuilder::connectionFromArray( |
212
|
|
|
$this->letters, |
213
|
|
|
['before' => 'YXJyYXljb25uZWN0aW9uOjYK', 'after' => 'YXJyYXljb25uZWN0aW9uOi0xCg=='] |
214
|
|
|
); |
215
|
|
|
|
216
|
|
|
$expected = $this->getExpectedConnection($this->letters, false, false); |
217
|
|
|
|
218
|
|
|
$this->assertEquals($expected, $actual); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function testReturnsNoElementsIfCursorsCross() |
222
|
|
|
{ |
223
|
|
|
$actual = ConnectionBuilder::connectionFromArray( |
224
|
|
|
$this->letters, |
225
|
|
|
['before' => 'YXJyYXljb25uZWN0aW9uOjI=', 'after' => 'YXJyYXljb25uZWN0aW9uOjQ='] |
226
|
|
|
); |
227
|
|
|
|
228
|
|
|
$expected = $this->getExpectedConnection([], false, false); |
229
|
|
|
|
230
|
|
|
$this->assertEquals($expected, $actual); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function testReturnsAnEdgesCursorGivenAnArrayAndAMemberObject() |
234
|
|
|
{ |
235
|
|
|
$letterCursor = ConnectionBuilder::cursorForObjectInConnection($this->letters, 'B'); |
236
|
|
|
|
237
|
|
|
$this->assertEquals('YXJyYXljb25uZWN0aW9uOjE=', $letterCursor); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
public function testReturnsAnEdgesCursorGivenAnArrayAndANonMemberObject() |
241
|
|
|
{ |
242
|
|
|
$letterCursor = ConnectionBuilder::cursorForObjectInConnection($this->letters, 'F'); |
243
|
|
|
|
244
|
|
|
$this->assertNull($letterCursor); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
private function getExpectedConnection(array $wantedEdges, $hasPreviousPage, $hasNextPage) |
248
|
|
|
{ |
249
|
|
|
$edges = [ |
250
|
|
|
'A' => new Edge('YXJyYXljb25uZWN0aW9uOjA=', 'A'), |
251
|
|
|
'B' => new Edge('YXJyYXljb25uZWN0aW9uOjE=', 'B'), |
252
|
|
|
'C' => new Edge('YXJyYXljb25uZWN0aW9uOjI=', 'C'), |
253
|
|
|
'D' => new Edge('YXJyYXljb25uZWN0aW9uOjM=', 'D'), |
254
|
|
|
'E' => new Edge('YXJyYXljb25uZWN0aW9uOjQ=', 'E'), |
255
|
|
|
]; |
256
|
|
|
|
257
|
|
|
$expectedEdges = array_values(array_intersect_key($edges, array_flip($wantedEdges))); |
258
|
|
|
|
259
|
|
|
return new Connection( |
260
|
|
|
$expectedEdges, |
261
|
|
|
new PageInfo( |
262
|
|
|
isset($expectedEdges[0]) ? $expectedEdges[0]->cursor : null, |
263
|
|
|
end($expectedEdges) ? end($expectedEdges)->cursor : null, |
264
|
|
|
$hasPreviousPage, |
265
|
|
|
$hasNextPage |
266
|
|
|
) |
267
|
|
|
); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
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.