Skip to content

Commit 931f121

Browse files
authored
Merge pull request #824 from s3tezsky/weird-behavior-of-has-page-fields
changed tests to expected behavior of object pageInfo.has*Page
2 parents 1c46382 + f37464a commit 931f121

File tree

4 files changed

+53
-26
lines changed

4 files changed

+53
-26
lines changed

src/Relay/Connection/ConnectionBuilder.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ public function connectionFromArraySlice(array $arraySlice, $args, array $meta):
150150
$beforeOffset = $this->getOffsetWithDefault($before, $arrayLength);
151151
$afterOffset = $this->getOffsetWithDefault($after, -1);
152152

153+
if ($afterOffset > $beforeOffset) {
154+
throw new InvalidArgumentException('Arguments "before" and "after" cannot be intersected');
155+
}
156+
153157
$startOffset = max($sliceStart - 1, $afterOffset, -1) + 1;
154158
$endOffset = min($sliceEnd, $beforeOffset, $arrayLength);
155159

@@ -182,14 +186,12 @@ public function connectionFromArraySlice(array $arraySlice, $args, array $meta):
182186

183187
$firstEdge = $edges[0] ?? null;
184188
$lastEdge = end($edges);
185-
$lowerBound = $after ? ($afterOffset + 1) : 0;
186-
$upperBound = $before ? $beforeOffset : $arrayLength;
187189

188190
$pageInfo = new PageInfo(
189191
$firstEdge instanceof EdgeInterface ? $firstEdge->getCursor() : null,
190192
$lastEdge instanceof EdgeInterface ? $lastEdge->getCursor() : null,
191-
null !== $last ? $startOffset > $lowerBound : false,
192-
null !== $first ? $endOffset < $upperBound : false
193+
$startOffset > 0,
194+
$endOffset < $arrayLength
193195
);
194196

195197
return $this->createConnection($edges, $pageInfo);

tests/Relay/Connection/AbstractConnectionBuilderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ protected function assertSameConnection(ConnectionInterface $expectedConnection,
5353
foreach (['getEndCursor', 'getHasNextPage', 'getHasPreviousPage', 'getStartCursor'] as $method) {
5454
$this->assertSame(
5555
$expectedConnection->getPageInfo()->$method(),
56-
$actualConnection->getPageInfo()->$method()
56+
$actualConnection->getPageInfo()->$method(),
57+
sprintf('Asserting failed on method "%s"', $method)
5758
);
5859
}
5960

tests/Relay/Connection/ConnectionBuilderTest.php

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ public function testRespectsFirstAndAfter(): void
7373
['first' => 2, 'after' => 'YXJyYXljb25uZWN0aW9uOjE=']
7474
);
7575

76-
$expected = $this->getExpectedConnection(['C', 'D'], false, true);
76+
// there actually is previous page for ['A', 'B']
77+
$expected = $this->getExpectedConnection(['C', 'D'], true, true);
7778

7879
$this->assertSameConnection($expected, $actual);
7980
}
@@ -86,7 +87,8 @@ public function testRespectsFirstAndAfterWithLongFirst(): void
8687
['first' => 10, 'after' => 'YXJyYXljb25uZWN0aW9uOjE=']
8788
);
8889

89-
$expected = $this->getExpectedConnection(['C', 'D', 'E'], false, false);
90+
// there actually is previous page for ['A', 'B']
91+
$expected = $this->getExpectedConnection(['C', 'D', 'E'], true, false);
9092

9193
$this->assertSameConnection($expected, $actual);
9294
}
@@ -99,7 +101,8 @@ public function testRespectsLastAndBefore(): void
99101
['last' => 2, 'before' => 'YXJyYXljb25uZWN0aW9uOjM=']
100102
);
101103

102-
$expected = $this->getExpectedConnection(['B', 'C'], true, false);
104+
// there actually is next page for ['D', 'E']
105+
$expected = $this->getExpectedConnection(['B', 'C'], true, true);
103106

104107
$this->assertSameConnection($expected, $actual);
105108
}
@@ -112,7 +115,8 @@ public function testRespectsLastAndBeforeWithLongLast(): void
112115
['last' => 10, 'before' => 'YXJyYXljb25uZWN0aW9uOjM=']
113116
);
114117

115-
$expected = $this->getExpectedConnection(['A', 'B', 'C'], false, false);
118+
// there actually is next page for ['E']
119+
$expected = $this->getExpectedConnection(['A', 'B', 'C'], false, true);
116120

117121
$this->assertSameConnection($expected, $actual);
118122
}
@@ -125,7 +129,8 @@ public function testRespectsFirstAndAfterAndBeforeTooFew(): void
125129
['first' => 2, 'after' => 'YXJyYXljb25uZWN0aW9uOjA=', 'before' => 'YXJyYXljb25uZWN0aW9uOjQ=']
126130
);
127131

128-
$expected = $this->getExpectedConnection(['B', 'C'], false, true);
132+
// there actually is previous page for ['A']
133+
$expected = $this->getExpectedConnection(['B', 'C'], true, true);
129134

130135
$this->assertSameConnection($expected, $actual);
131136
}
@@ -138,7 +143,8 @@ public function testRespectsFirstAndAfterAndBeforeTooMany(): void
138143
['first' => 4, 'after' => 'YXJyYXljb25uZWN0aW9uOjA=', 'before' => 'YXJyYXljb25uZWN0aW9uOjQ=']
139144
);
140145

141-
$expected = $this->getExpectedConnection(['B', 'C', 'D'], false, false);
146+
// there actually is previous and next page (for ['A'] or ['E'])
147+
$expected = $this->getExpectedConnection(['B', 'C', 'D'], true, true);
142148

143149
$this->assertSameConnection($expected, $actual);
144150
}
@@ -151,7 +157,8 @@ public function testRespectsFirstAndAfterAndBeforeExactlyRight(): void
151157
['first' => 3, 'after' => 'YXJyYXljb25uZWN0aW9uOjA=', 'before' => 'YXJyYXljb25uZWN0aW9uOjQ=']
152158
);
153159

154-
$expected = $this->getExpectedConnection(['B', 'C', 'D'], false, false);
160+
// there actually is previous and next page (for ['A'] or ['E'])
161+
$expected = $this->getExpectedConnection(['B', 'C', 'D'], true, true);
155162

156163
$this->assertSameConnection($expected, $actual);
157164
}
@@ -164,7 +171,8 @@ public function testRespectsLastAndAfterAndBeforeTooFew(): void
164171
['last' => 2, 'after' => 'YXJyYXljb25uZWN0aW9uOjA=', 'before' => 'YXJyYXljb25uZWN0aW9uOjQ=']
165172
);
166173

167-
$expected = $this->getExpectedConnection(['C', 'D'], true, false);
174+
// there actually is next page for ['E']
175+
$expected = $this->getExpectedConnection(['C', 'D'], true, true);
168176

169177
$this->assertSameConnection($expected, $actual);
170178
}
@@ -177,7 +185,8 @@ public function testRespectsLastAndAfterAndBeforeTooMany(): void
177185
['last' => 4, 'after' => 'YXJyYXljb25uZWN0aW9uOjA=', 'before' => 'YXJyYXljb25uZWN0aW9uOjQ=']
178186
);
179187

180-
$expected = $this->getExpectedConnection(['B', 'C', 'D'], false, false);
188+
// there actually is previous and next page (for ['A'] or ['E'])
189+
$expected = $this->getExpectedConnection(['B', 'C', 'D'], true, true);
181190

182191
$this->assertSameConnection($expected, $actual);
183192
}
@@ -190,7 +199,8 @@ public function testRespectsLastAndAfterAndBeforeExactlyRight(): void
190199
['last' => 3, 'after' => 'YXJyYXljb25uZWN0aW9uOjA=', 'before' => 'YXJyYXljb25uZWN0aW9uOjQ=']
191200
);
192201

193-
$expected = $this->getExpectedConnection(['B', 'C', 'D'], false, false);
202+
// there actually is previous and next page (for ['A'] or ['E'])
203+
$expected = $this->getExpectedConnection(['B', 'C', 'D'], true, true);
194204

195205
$this->assertSameConnection($expected, $actual);
196206
}
@@ -284,7 +294,8 @@ public function testWorksWithAJustRightArraySlice(): void
284294
['sliceStart' => 1, 'arrayLength' => 5]
285295
);
286296

287-
$expected = $this->getExpectedConnection(['B', 'C'], false, true);
297+
// there actually is previous page for ['A']
298+
$expected = $this->getExpectedConnection(['B', 'C'], true, true);
288299

289300
$this->assertSameConnection($expected, $actual);
290301
}
@@ -301,7 +312,8 @@ public function testWorksWithAnOversizedArraySliceLeftSide(): void
301312
['sliceStart' => 0, 'arrayLength' => 5]
302313
);
303314

304-
$expected = $this->getExpectedConnection(['B', 'C'], false, true);
315+
// there actually is previous page for ['A']
316+
$expected = $this->getExpectedConnection(['B', 'C'], true, true);
305317

306318
$this->assertSameConnection($expected, $actual);
307319
}
@@ -318,7 +330,8 @@ public function testWorksWithAnOversizedArraySliceRightSide(): void
318330
['sliceStart' => 2, 'arrayLength' => 5]
319331
);
320332

321-
$expected = $this->getExpectedConnection(['C'], false, true);
333+
// there actually is previous page for ['A', 'B']
334+
$expected = $this->getExpectedConnection(['C'], true, true);
322335

323336
$this->assertSameConnection($expected, $actual);
324337
}
@@ -335,7 +348,8 @@ public function testWorksWithAnOversizedArraySliceBothSides(): void
335348
['sliceStart' => 1, 'arrayLength' => 5]
336349
);
337350

338-
$expected = $this->getExpectedConnection(['C'], false, true);
351+
// there actually is previous page for ['A', 'B']
352+
$expected = $this->getExpectedConnection(['C'], true, true);
339353

340354
$this->assertSameConnection($expected, $actual);
341355
}
@@ -352,7 +366,8 @@ public function testWorksWithAnUndersizedArraySliceLeftSide(): void
352366
['sliceStart' => 3, 'arrayLength' => 5]
353367
);
354368

355-
$expected = $this->getExpectedConnection(['D', 'E'], false, false);
369+
// there actually is previous page for ['A', 'B', 'C']
370+
$expected = $this->getExpectedConnection(['D', 'E'], true, false);
356371

357372
$this->assertSameConnection($expected, $actual);
358373
}
@@ -369,7 +384,8 @@ public function testWorksWithAnUndersizedArraySliceRightSide(): void
369384
['sliceStart' => 2, 'arrayLength' => 5]
370385
);
371386

372-
$expected = $this->getExpectedConnection(['C', 'D'], false, true);
387+
// there actually is previous page for ['A', 'B']
388+
$expected = $this->getExpectedConnection(['C', 'D'], true, true);
373389

374390
$this->assertSameConnection($expected, $actual);
375391
}
@@ -386,7 +402,8 @@ public function worksWithAnUndersizedArraySliceBothSides(): void
386402
['sliceStart' => 3, 'arrayLength' => 5]
387403
);
388404

389-
$expected = $this->getExpectedConnection(['D'], false, true);
405+
// there actually is previous page for ['A', 'B', 'C']
406+
$expected = $this->getExpectedConnection(['D'], true, true);
390407

391408
$this->assertSameConnection($expected, $actual);
392409
}

tests/Relay/Connection/PaginatorTest.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function testForward(): void
6666
public function testForwardAfterInMiddle(): void
6767
{
6868
$paginator = new Paginator(function ($offset, $limit) {
69-
$this->assertSame(2, $offset);
69+
$this->assertSame(2, $offset); // offset should equal to 1 to provide information about previous page
7070
$this->assertSame(3, $limit); // Includes the extra element to check if next page is available
7171

7272
return $this->getData($offset);
@@ -78,12 +78,13 @@ public function testForwardAfterInMiddle(): void
7878
$this->assertCount(1, $result->getEdges());
7979
$this->assertSameEdgeNodeValue(['D'], $result);
8080
$this->assertTrue($result->getPageInfo()->getHasNextPage());
81+
$this->assertTrue($result->getPageInfo()->getHasPreviousPage()); // on previous page there is still ['A', 'B', 'C']
8182
}
8283

8384
public function testForwardAfterAtTheEnd(): void
8485
{
8586
$paginator = new Paginator(function ($offset, $limit) {
86-
$this->assertSame(2, $offset);
87+
$this->assertSame(2, $offset); // offset should equal to 1 to provide information about previous page
8788
$this->assertSame(4, $limit); // Includes the extra element to check if next page is available
8889

8990
return $this->getData($offset);
@@ -95,12 +96,13 @@ public function testForwardAfterAtTheEnd(): void
9596
$this->assertCount(2, $result->getEdges());
9697
$this->assertSameEdgeNodeValue(['D', 'E'], $result);
9798
$this->assertFalse($result->getPageInfo()->getHasNextPage());
99+
$this->assertTrue($result->getPageInfo()->getHasPreviousPage()); // on previous page there is still ['A', 'B', 'C']
98100
}
99101

100102
public function testForwardAfterLast(): void
101103
{
102104
$paginator = new Paginator(function ($offset, $limit) {
103-
$this->assertSame(4, $offset);
105+
$this->assertSame(4, $offset); // offset should equal to 3 to provide information about previous page
104106
$this->assertSame(7, $limit); // Includes the extra element to check if next page is available
105107

106108
return $this->getData($offset);
@@ -112,6 +114,7 @@ public function testForwardAfterLast(): void
112114
$this->assertCount(0, $result->getEdges());
113115
$this->assertSameEdgeNodeValue([], $result);
114116
$this->assertFalse($result->getPageInfo()->getHasNextPage());
117+
$this->assertTrue($result->getPageInfo()->getHasPreviousPage()); // on previous page there is still ['A', 'B', 'C', 'D', 'E']
115118
}
116119

117120
public function testForwardAfterWithUnvalidCursorAndSlice(): void
@@ -182,6 +185,7 @@ public function testBackwardBeforeLast(): void
182185
$this->assertCount(4, $result->getEdges());
183186
$this->assertSameEdgeNodeValue(['A', 'B', 'C', 'D'], $result);
184187
$this->assertFalse($result->getPageInfo()->getHasPreviousPage());
188+
$this->assertTrue($result->getPageInfo()->getHasNextPage()); // on next page there is still ['E']
185189
}
186190

187191
public function testBackwardPartialBeforeInMiddle(): void
@@ -199,6 +203,7 @@ public function testBackwardPartialBeforeInMiddle(): void
199203
$this->assertCount(2, $result->getEdges());
200204
$this->assertSameEdgeNodeValue(['B', 'C'], $result);
201205
$this->assertTrue($result->getPageInfo()->getHasPreviousPage());
206+
$this->assertTrue($result->getPageInfo()->getHasNextPage()); // on next page there is still ['D', 'E']
202207
}
203208

204209
public function testAutoBackward(): void
@@ -215,7 +220,7 @@ public function testAutoBackward(): void
215220

216221
$this->assertCount(4, $result->getEdges());
217222
$this->assertSameEdgeNodeValue(['B', 'C', 'D', 'E'], $result);
218-
$this->assertTrue($result->getPageInfo()->getHasPreviousPage());
223+
$this->assertTrue($result->getPageInfo()->getHasPreviousPage()); // on previous page there is still ['A']
219224
$this->assertFalse($result->getPageInfo()->getHasNextPage());
220225
}
221226

@@ -234,6 +239,7 @@ public function testAutoForward(): void
234239
$this->assertCount(4, $result->getEdges());
235240
$this->assertSameEdgeNodeValue(['A', 'B', 'C', 'D'], $result);
236241
$this->assertTrue($result->getPageInfo()->getHasNextPage());
242+
$this->assertFalse($result->getPageInfo()->getHasPreviousPage());
237243
}
238244

239245
public function testAutoBackwardWithCallable(): void
@@ -258,6 +264,7 @@ public function testAutoBackwardWithCallable(): void
258264
$this->assertCount(4, $result->getEdges());
259265
$this->assertSameEdgeNodeValue(['B', 'C', 'D', 'E'], $result);
260266
$this->assertTrue($result->getPageInfo()->getHasPreviousPage());
267+
$this->assertFalse($result->getPageInfo()->getHasNextPage());
261268
}
262269

263270
public function testTotalCallableWithArguments(): void

0 commit comments

Comments
 (0)