@@ -166,15 +166,41 @@ public function testThrowsExceptionForWithSortNull(): void
166166 (new KeysetPaginator ($ dataReader ))->withSort (null );
167167 }
168168
169- public function testThrowsExceptionWhenNotSorted (): void
169+ public function testDefaultOrderUsage (): void
170170 {
171- $ sort = Sort::only (['id ' , 'name ' ]);
171+ $ sort = Sort::only (['name ' , 'id ' ]);
172172 $ dataReader = (new IterableDataReader (self ::getDataSet ()))->withSort ($ sort );
173+ $ paginator = new KeysetPaginator ($ dataReader );
173174
174- $ this ->expectException (InvalidArgumentException::class);
175- $ this ->expectExceptionMessage ('Data should be always sorted to work with keyset pagination. ' );
175+ $ result = $ paginator ->read ();
176176
177- new KeysetPaginator ($ dataReader );
177+ $ this ->assertSame (
178+ self ::getDataSet ([4 , 3 , 2 , 0 , 1 ]),
179+ array_values ($ this ->iterableToArray ($ result )),
180+ );
181+ }
182+
183+ public function testDefaultOrderUsageInPrevious (): void
184+ {
185+ $ sort = Sort::only (['name ' , 'id ' ]);
186+ $ data = [
187+ ['id ' => 2 , 'name ' => 'A ' ],
188+ ['id ' => 1 , 'name ' => 'A ' ],
189+ ['id ' => 3 , 'name ' => 'B ' ],
190+ ];
191+ $ dataReader = (new IterableDataReader ($ data ))->withSort ($ sort );
192+ $ paginator = (new KeysetPaginator ($ dataReader ))
193+ ->withToken (PageToken::previous ('B ' ));
194+
195+ $ result = $ paginator ->read ();
196+
197+ $ this ->assertSame (
198+ [
199+ ['id ' => 2 , 'name ' => 'A ' ],
200+ ['id ' => 1 , 'name ' => 'A ' ],
201+ ],
202+ array_values ($ this ->iterableToArray ($ result )),
203+ );
178204 }
179205
180206 public function testThrowsExceptionForWithSortNotSorted (): void
@@ -185,7 +211,7 @@ public function testThrowsExceptionForWithSortNotSorted(): void
185211 $ this ->expectException (InvalidArgumentException::class);
186212 $ this ->expectExceptionMessage ('Data should be always sorted to work with keyset pagination. ' );
187213
188- (new KeysetPaginator ($ dataReader ))->withSort (Sort::only ([' id ' , ' name ' ]));
214+ (new KeysetPaginator ($ dataReader ))->withSort (Sort::only ([]));
189215 }
190216
191217 public function testPageSizeCannotBeLessThanOne (): void
0 commit comments