@@ -15,108 +15,99 @@ final class PaginatorTest extends TestCase
1515 private Paginator $ paginator ;
1616 private LoggerInterface &MockObject $ logger ;
1717
18- protected function setUp (): void
19- {
20- $ this ->logger = $ this ->createMock (LoggerInterface::class);
21- $ this ->paginator = new Paginator (
22- paginationLimit: 3 ,
23- logger: $ this ->logger ,
24- );
25- }
26-
2718 public function testPaginateWithNullCursor (): void
2819 {
2920 $ items = ['a ' , 'b ' , 'c ' , 'd ' , 'e ' , 'f ' ];
30-
21+
3122 $ result = $ this ->paginator ->paginate ($ items , null );
32-
23+
3324 $ this ->assertSame (['a ' , 'b ' , 'c ' ], $ result ['items ' ]);
3425 $ this ->assertNotNull ($ result ['nextCursor ' ]);
35- $ this ->assertSame ('offset=3 ' , base64_decode ($ result ['nextCursor ' ]));
26+ $ this ->assertSame ('offset=3 ' , \ base64_decode ($ result ['nextCursor ' ]));
3627 }
3728
3829 public function testPaginateWithValidCursor (): void
3930 {
4031 $ items = ['a ' , 'b ' , 'c ' , 'd ' , 'e ' , 'f ' ];
41- $ cursor = base64_encode ('offset=3 ' );
42-
32+ $ cursor = \ base64_encode ('offset=3 ' );
33+
4334 $ result = $ this ->paginator ->paginate ($ items , $ cursor );
44-
35+
4536 $ this ->assertSame (['d ' , 'e ' , 'f ' ], $ result ['items ' ]);
4637 $ this ->assertNull ($ result ['nextCursor ' ]);
4738 }
4839
4940 public function testPaginateEmptyItems (): void
5041 {
5142 $ items = [];
52-
43+
5344 $ result = $ this ->paginator ->paginate ($ items , null );
54-
45+
5546 $ this ->assertSame ([], $ result ['items ' ]);
5647 $ this ->assertNull ($ result ['nextCursor ' ]);
5748 }
5849
5950 public function testPaginateSinglePage (): void
6051 {
6152 $ items = ['a ' , 'b ' ];
62-
53+
6354 $ result = $ this ->paginator ->paginate ($ items , null );
64-
55+
6556 $ this ->assertSame (['a ' , 'b ' ], $ result ['items ' ]);
6657 $ this ->assertNull ($ result ['nextCursor ' ]);
6758 }
6859
6960 public function testPaginateExactPageBoundary (): void
7061 {
7162 $ items = ['a ' , 'b ' , 'c ' ];
72-
63+
7364 $ result = $ this ->paginator ->paginate ($ items , null );
74-
65+
7566 $ this ->assertSame (['a ' , 'b ' , 'c ' ], $ result ['items ' ]);
7667 $ this ->assertNull ($ result ['nextCursor ' ]);
7768 }
7869
7970 public function testPaginateWithOffsetBeyondItems (): void
8071 {
8172 $ items = ['a ' , 'b ' , 'c ' ];
82- $ cursor = base64_encode ('offset=10 ' );
83-
73+ $ cursor = \ base64_encode ('offset=10 ' );
74+
8475 $ result = $ this ->paginator ->paginate ($ items , $ cursor );
85-
76+
8677 $ this ->assertSame ([], $ result ['items ' ]);
8778 $ this ->assertNull ($ result ['nextCursor ' ]);
8879 }
8980
9081 public function testPaginateWithPartialLastPage (): void
9182 {
9283 $ items = ['a ' , 'b ' , 'c ' , 'd ' , 'e ' ];
93- $ cursor = base64_encode ('offset=3 ' );
94-
84+ $ cursor = \ base64_encode ('offset=3 ' );
85+
9586 $ result = $ this ->paginator ->paginate ($ items , $ cursor );
96-
87+
9788 $ this ->assertSame (['d ' , 'e ' ], $ result ['items ' ]);
9889 $ this ->assertNull ($ result ['nextCursor ' ]);
9990 }
10091
10192 public function testPaginateWithMultiplePages (): void
10293 {
103- $ items = range ('a ' , 'j ' ); // 10 items
104-
94+ $ items = \ range ('a ' , 'j ' ); // 10 items
95+
10596 // First page
10697 $ result1 = $ this ->paginator ->paginate ($ items , null );
10798 $ this ->assertSame (['a ' , 'b ' , 'c ' ], $ result1 ['items ' ]);
10899 $ this ->assertNotNull ($ result1 ['nextCursor ' ]);
109-
100+
110101 // Second page
111102 $ result2 = $ this ->paginator ->paginate ($ items , $ result1 ['nextCursor ' ]);
112103 $ this ->assertSame (['d ' , 'e ' , 'f ' ], $ result2 ['items ' ]);
113104 $ this ->assertNotNull ($ result2 ['nextCursor ' ]);
114-
105+
115106 // Third page
116107 $ result3 = $ this ->paginator ->paginate ($ items , $ result2 ['nextCursor ' ]);
117108 $ this ->assertSame (['g ' , 'h ' , 'i ' ], $ result3 ['items ' ]);
118109 $ this ->assertNotNull ($ result3 ['nextCursor ' ]);
119-
110+
120111 // Last page
121112 $ result4 = $ this ->paginator ->paginate ($ items , $ result3 ['nextCursor ' ]);
122113 $ this ->assertSame (['j ' ], $ result4 ['items ' ]);
@@ -127,77 +118,77 @@ public function testInvalidBase64Cursor(): void
127118 {
128119 $ items = ['a ' , 'b ' , 'c ' , 'd ' ];
129120 $ invalidCursor = 'invalid-base64!@# ' ;
130-
121+
131122 $ this ->logger
132123 ->expects ($ this ->once ())
133124 ->method ('warning ' )
134125 ->with (
135126 'Received invalid pagination cursor (not base64) ' ,
136- ['cursor ' => $ invalidCursor ]
127+ ['cursor ' => $ invalidCursor ],
137128 );
138-
129+
139130 $ result = $ this ->paginator ->paginate ($ items , $ invalidCursor );
140-
131+
141132 $ this ->assertSame (['a ' , 'b ' , 'c ' ], $ result ['items ' ]);
142133 }
143134
144135 public function testInvalidCursorFormat (): void
145136 {
146137 $ items = ['a ' , 'b ' , 'c ' , 'd ' ];
147- $ invalidFormatCursor = base64_encode ('invalid-format ' );
148-
138+ $ invalidFormatCursor = \ base64_encode ('invalid-format ' );
139+
149140 $ this ->logger
150141 ->expects ($ this ->once ())
151142 ->method ('warning ' )
152143 ->with (
153144 'Received invalid pagination cursor format ' ,
154- ['cursor ' => 'invalid-format ' ]
145+ ['cursor ' => 'invalid-format ' ],
155146 );
156-
147+
157148 $ result = $ this ->paginator ->paginate ($ items , $ invalidFormatCursor );
158-
149+
159150 $ this ->assertSame (['a ' , 'b ' , 'c ' ], $ result ['items ' ]);
160151 }
161152
162153 public function testCursorWithNonNumericOffset (): void
163154 {
164155 $ items = ['a ' , 'b ' , 'c ' , 'd ' ];
165- $ invalidOffsetCursor = base64_encode ('offset=abc ' );
166-
156+ $ invalidOffsetCursor = \ base64_encode ('offset=abc ' );
157+
167158 $ this ->logger
168159 ->expects ($ this ->once ())
169160 ->method ('warning ' )
170161 ->with (
171162 'Received invalid pagination cursor format ' ,
172- ['cursor ' => 'offset=abc ' ]
163+ ['cursor ' => 'offset=abc ' ],
173164 );
174-
165+
175166 $ result = $ this ->paginator ->paginate ($ items , $ invalidOffsetCursor );
176-
167+
177168 $ this ->assertSame (['a ' , 'b ' , 'c ' ], $ result ['items ' ]);
178169 }
179170
180171 public function testCustomPaginationLimit (): void
181172 {
182173 $ customPaginator = new Paginator (paginationLimit: 5 , logger: $ this ->logger );
183- $ items = range (1 , 12 );
184-
174+ $ items = \ range (1 , 12 );
175+
185176 $ result = $ customPaginator ->paginate ($ items , null );
186-
177+
187178 $ this ->assertSame ([1 , 2 , 3 , 4 , 5 ], $ result ['items ' ]);
188179 $ this ->assertNotNull ($ result ['nextCursor ' ]);
189- $ this ->assertSame ('offset=5 ' , base64_decode ($ result ['nextCursor ' ]));
180+ $ this ->assertSame ('offset=5 ' , \ base64_decode ($ result ['nextCursor ' ]));
190181 }
191182
192183 public function testDefaultPaginationLimit (): void
193184 {
194185 $ defaultPaginator = new Paginator ();
195- $ items = range (1 , 100 );
196-
186+ $ items = \ range (1 , 100 );
187+
197188 $ result = $ defaultPaginator ->paginate ($ items , null );
198-
189+
199190 $ this ->assertCount (50 , $ result ['items ' ]); // Default limit is 50
200- $ this ->assertSame (range (1 , 50 ), $ result ['items ' ]);
191+ $ this ->assertSame (\ range (1 , 50 ), $ result ['items ' ]);
201192 $ this ->assertNotNull ($ result ['nextCursor ' ]);
202193 }
203194
@@ -208,21 +199,21 @@ public function testArrayValuesReindexing(): void
208199 'key2 ' => 'value2 ' ,
209200 'key3 ' => 'value3 ' ,
210201 ];
211-
202+
212203 $ result = $ this ->paginator ->paginate ($ items , null );
213-
204+
214205 // array_values should reindex the array
215206 $ this ->assertSame (['value1 ' , 'value2 ' , 'value3 ' ], $ result ['items ' ]);
216- $ this ->assertSame ([0 , 1 , 2 ], array_keys ($ result ['items ' ]));
207+ $ this ->assertSame ([0 , 1 , 2 ], \ array_keys ($ result ['items ' ]));
217208 }
218209
219210 public function testZeroOffsetCursor (): void
220211 {
221212 $ items = ['a ' , 'b ' , 'c ' , 'd ' ];
222- $ zeroOffsetCursor = base64_encode ('offset=0 ' );
223-
213+ $ zeroOffsetCursor = \ base64_encode ('offset=0 ' );
214+
224215 $ result = $ this ->paginator ->paginate ($ items , $ zeroOffsetCursor );
225-
216+
226217 $ this ->assertSame (['a ' , 'b ' , 'c ' ], $ result ['items ' ]);
227218 $ this ->assertNotNull ($ result ['nextCursor ' ]);
228219 }
@@ -231,26 +222,26 @@ public function testCursorEncodingDecoding(): void
231222 {
232223 $ offset = 42 ;
233224 $ expectedCursorContent = "offset= {$ offset }" ;
234- $ cursor = base64_encode ($ expectedCursorContent );
235-
225+ $ cursor = \ base64_encode ($ expectedCursorContent );
226+
236227 // Verify we can decode what we encode
237- $ decoded = base64_decode ($ cursor , true );
228+ $ decoded = \ base64_decode ($ cursor , true );
238229 $ this ->assertSame ($ expectedCursorContent , $ decoded );
239-
230+
240231 // Test with large offset
241232 $ largeOffset = 999999 ;
242233 $ largeCursorContent = "offset= {$ largeOffset }" ;
243- $ largeCursor = base64_encode ($ largeCursorContent );
244- $ decodedLarge = base64_decode ($ largeCursor , true );
234+ $ largeCursor = \ base64_encode ($ largeCursorContent );
235+ $ decodedLarge = \ base64_decode ($ largeCursor , true );
245236 $ this ->assertSame ($ largeCursorContent , $ decodedLarge );
246237 }
247238
248239 public function testEdgeCaseWithSingleItem (): void
249240 {
250241 $ items = ['only-item ' ];
251-
242+
252243 $ result = $ this ->paginator ->paginate ($ items , null );
253-
244+
254245 $ this ->assertSame (['only-item ' ], $ result ['items ' ]);
255246 $ this ->assertNull ($ result ['nextCursor ' ]);
256247 }
@@ -263,9 +254,9 @@ public function testPaginateWithAssociativeArrayPreservesValues(): void
263254 ['id ' => 3 , 'name ' => 'Charlie ' ],
264255 ['id ' => 4 , 'name ' => 'Diana ' ],
265256 ];
266-
257+
267258 $ result = $ this ->paginator ->paginate ($ items , null );
268-
259+
269260 $ this ->assertCount (3 , $ result ['items ' ]);
270261 $ this ->assertSame (['id ' => 1 , 'name ' => 'Alice ' ], $ result ['items ' ][0 ]);
271262 $ this ->assertSame (['id ' => 2 , 'name ' => 'Bob ' ], $ result ['items ' ][1 ]);
@@ -278,53 +269,62 @@ public function testPaginateWithNullLogger(): void
278269 $ paginatorWithNullLogger = new Paginator (paginationLimit: 2 , logger: new NullLogger ());
279270 $ items = ['a ' , 'b ' , 'c ' , 'd ' ];
280271 $ invalidCursor = 'invalid-base64!@# ' ;
281-
272+
282273 // Should not throw any exceptions even with invalid cursor
283274 $ result = $ paginatorWithNullLogger ->paginate ($ items , $ invalidCursor );
284-
275+
285276 $ this ->assertSame (['a ' , 'b ' ], $ result ['items ' ]);
286277 $ this ->assertNotNull ($ result ['nextCursor ' ]);
287278 }
288279
289280 public function testPaginateWithLargeOffset (): void
290281 {
291282 $ items = ['a ' , 'b ' , 'c ' ];
292- $ cursor = base64_encode ('offset=1000 ' );
293-
283+ $ cursor = \ base64_encode ('offset=1000 ' );
284+
294285 $ result = $ this ->paginator ->paginate ($ items , $ cursor );
295-
286+
296287 $ this ->assertSame ([], $ result ['items ' ]);
297288 $ this ->assertNull ($ result ['nextCursor ' ]);
298289 }
299290
300291 public function testPaginateWithNegativeOffsetInCursor (): void
301292 {
302293 $ items = ['a ' , 'b ' , 'c ' , 'd ' ];
303- $ negativeOffsetCursor = base64_encode ('offset=-5 ' );
304-
294+ $ negativeOffsetCursor = \ base64_encode ('offset=-5 ' );
295+
305296 $ this ->logger
306297 ->expects ($ this ->once ())
307298 ->method ('warning ' )
308299 ->with (
309300 'Received invalid pagination cursor format ' ,
310- ['cursor ' => 'offset=-5 ' ]
301+ ['cursor ' => 'offset=-5 ' ],
311302 );
312-
303+
313304 $ result = $ this ->paginator ->paginate ($ items , $ negativeOffsetCursor );
314-
305+
315306 $ this ->assertSame (['a ' , 'b ' , 'c ' ], $ result ['items ' ]);
316307 }
317308
318309 public function testPaginateReturnsConsistentStructure (): void
319310 {
320311 $ items = ['test ' ];
321-
312+
322313 $ result = $ this ->paginator ->paginate ($ items , null );
323-
314+
324315 $ this ->assertIsArray ($ result );
325316 $ this ->assertArrayHasKey ('items ' , $ result );
326317 $ this ->assertArrayHasKey ('nextCursor ' , $ result );
327318 $ this ->assertIsArray ($ result ['items ' ]);
328- $ this ->assertTrue (is_string ($ result ['nextCursor ' ]) || is_null ($ result ['nextCursor ' ]));
319+ $ this ->assertTrue (\is_string ($ result ['nextCursor ' ]) || \is_null ($ result ['nextCursor ' ]));
320+ }
321+
322+ protected function setUp (): void
323+ {
324+ $ this ->logger = $ this ->createMock (LoggerInterface::class);
325+ $ this ->paginator = new Paginator (
326+ paginationLimit: 3 ,
327+ logger: $ this ->logger ,
328+ );
329329 }
330330}
0 commit comments