|
5 | 5 | namespace Tests\Tempest\Integration\Database\Builder; |
6 | 6 |
|
7 | 7 | use Tempest\Database\Builder\QueryBuilders\SelectQueryBuilder; |
8 | | -use Tempest\Database\Config\SQLiteConfig; |
9 | 8 | use Tempest\Database\Migrations\CreateMigrationsTable; |
10 | | -use Tempest\Database\Migrations\MigrationManager; |
11 | 9 | use Tests\Tempest\Fixtures\Migrations\CreateAuthorTable; |
12 | 10 | use Tests\Tempest\Fixtures\Migrations\CreateBookTable; |
13 | 11 | use Tests\Tempest\Fixtures\Migrations\CreateChapterTable; |
|
17 | 15 | use Tests\Tempest\Fixtures\Modules\Books\Models\Author; |
18 | 16 | use Tests\Tempest\Fixtures\Modules\Books\Models\AuthorType; |
19 | 17 | use Tests\Tempest\Fixtures\Modules\Books\Models\Book; |
| 18 | +use Tests\Tempest\Fixtures\Modules\Books\Models\Chapter; |
20 | 19 | use Tests\Tempest\Integration\FrameworkIntegrationTestCase; |
21 | 20 |
|
22 | 21 | use function Tempest\Database\query; |
@@ -441,6 +440,52 @@ public function test_having(): void |
441 | 440 | $this->assertSameWithoutBackticks($expected, $sql); |
442 | 441 | } |
443 | 442 |
|
| 443 | + public function test_paginate(): void |
| 444 | + { |
| 445 | + $this->seed(); |
| 446 | + |
| 447 | + $page1 = query(Chapter::class) |
| 448 | + ->select() |
| 449 | + ->paginate(itemsPerPage: 2); |
| 450 | + |
| 451 | + $this->assertSame(1, $page1->currentPage); |
| 452 | + $this->assertSame(7, $page1->totalPages); |
| 453 | + $this->assertSame(13, $page1->totalItems); |
| 454 | + $this->assertSame(2, $page1->itemsPerPage); |
| 455 | + $this->assertSame(0, $page1->offset); |
| 456 | + $this->assertSame(2, $page1->limit); |
| 457 | + $this->assertSame(2, $page1->nextPage); |
| 458 | + $this->assertSame(null, $page1->previousPage); |
| 459 | + $this->assertSame(true, $page1->hasNext); |
| 460 | + $this->assertSame(false, $page1->hasPrevious); |
| 461 | + |
| 462 | + $this->assertSame('LOTR 1.1', $page1->data[0]->title); |
| 463 | + $this->assertSame('LOTR 1.2', $page1->data[1]->title); |
| 464 | + |
| 465 | + $page3 = query(Chapter::class) |
| 466 | + ->select() |
| 467 | + ->paginate(itemsPerPage: 2, currentPage: 3); |
| 468 | + |
| 469 | + $this->assertSame(3, $page3->currentPage); |
| 470 | + $this->assertSame('LOTR 2.2', $page3->data[0]->title); |
| 471 | + $this->assertSame('LOTR 2.3', $page3->data[1]->title); |
| 472 | + |
| 473 | + $page7 = query(Chapter::class) |
| 474 | + ->select() |
| 475 | + ->paginate(itemsPerPage: 2, currentPage: 7); |
| 476 | + |
| 477 | + $this->assertSame(7, $page7->currentPage); |
| 478 | + $this->assertSame('Timeline Taxi Chapter 4', $page7->data[0]->title); |
| 479 | + |
| 480 | + // capped to last page, so this will be page 7 |
| 481 | + $page10 = query(Chapter::class) |
| 482 | + ->select() |
| 483 | + ->paginate(itemsPerPage: 2, currentPage: 10); |
| 484 | + |
| 485 | + $this->assertSame(7, $page10->currentPage); |
| 486 | + $this->assertSame('Timeline Taxi Chapter 4', $page10->data[0]->title); |
| 487 | + } |
| 488 | + |
444 | 489 | private function seed(): void |
445 | 490 | { |
446 | 491 | $this->migrate( |
|
0 commit comments