|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 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 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Doctrine\Common\Tests; |
| 15 | + |
| 16 | +use ApiPlatform\Doctrine\Common\CollectionPaginator; |
| 17 | +use Doctrine\Common\Collections\ArrayCollection; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +class CollectionPaginatorTest extends TestCase |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @dataProvider initializeProvider |
| 24 | + */ |
| 25 | + public function testInitialize($results, $currentPage, $itemsPerPage, $totalItems, $lastPage, $currentItems): void |
| 26 | + { |
| 27 | + $results = new ArrayCollection($results); |
| 28 | + $paginator = new CollectionPaginator($results, $currentPage, $itemsPerPage); |
| 29 | + |
| 30 | + $this->assertSame((float) $currentPage, $paginator->getCurrentPage()); |
| 31 | + $this->assertSame((float) $itemsPerPage, $paginator->getItemsPerPage()); |
| 32 | + $this->assertSame((float) $totalItems, $paginator->getTotalItems()); |
| 33 | + $this->assertCount($currentItems, $paginator); |
| 34 | + $this->assertSame((float) $lastPage, $paginator->getLastPage()); |
| 35 | + } |
| 36 | + |
| 37 | + public static function initializeProvider(): array |
| 38 | + { |
| 39 | + return [ |
| 40 | + 'First of three pages of 3 items each' => [[0, 1, 2, 3, 4, 5, 6], 1, 3, 7, 3, 3], |
| 41 | + 'Second of two pages of 3 items for the first page and 2 for the second' => [[0, 1, 2, 3, 4], 2, 3, 5, 2, 2], |
| 42 | + 'Empty results' => [[], 1, 2, 0, 1, 0], |
| 43 | + '0 items per page' => [[0, 1, 2, 3], 1, 0, 4, 1, 4], |
| 44 | + 'Total items less than items per page' => [[0, 1, 2], 1, 4, 3, 1, 3], |
| 45 | + ]; |
| 46 | + } |
| 47 | +} |
0 commit comments