|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the DunglasApiBundle package. |
| 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 | +namespace Doctrine\Orm; |
| 13 | + |
| 14 | +use Dunglas\ApiBundle\Doctrine\Orm\Paginator; |
| 15 | + |
| 16 | +class PaginatorTest extends \PHPUnit_Framework_TestCase |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @dataProvider initializeProvider |
| 20 | + */ |
| 21 | + public function testInitialize($firstResult, $maxResults, $totalItems, $currentPage, $lastPage) |
| 22 | + { |
| 23 | + $paginator = $this->getPaginator($firstResult, $maxResults, $totalItems); |
| 24 | + |
| 25 | + $this->assertEquals($currentPage, $paginator->getCurrentPage()); |
| 26 | + $this->assertEquals($lastPage, $paginator->getLastPage()); |
| 27 | + $this->assertEquals($maxResults, $paginator->getItemsPerPage()); |
| 28 | + } |
| 29 | + |
| 30 | + public function testGetIterator() |
| 31 | + { |
| 32 | + $paginator = $this->getPaginator(); |
| 33 | + |
| 34 | + $this->assertSame($paginator->getIterator(), $paginator->getIterator(), 'Iterator should be cached'); |
| 35 | + } |
| 36 | + |
| 37 | + public function getPaginator($firstResult = 1, $maxResults = 15, $totalItems = 42) |
| 38 | + { |
| 39 | + $query = $this->prophesize('Dunglas\ApiBundle\Tests\Fixtures\Query'); |
| 40 | + $query->getFirstResult()->willReturn($firstResult)->shouldBeCalled(); |
| 41 | + $query->getMaxResults()->willReturn($maxResults)->shouldBeCalled(); |
| 42 | + |
| 43 | + $doctrinePaginator = $this->prophesize('Doctrine\ORM\Tools\Pagination\Paginator'); |
| 44 | + |
| 45 | + $doctrinePaginator->getQuery()->willReturn($query->reveal())->shouldBeCalled(); |
| 46 | + $doctrinePaginator->count()->willReturn($totalItems)->shouldBeCalled(); |
| 47 | + |
| 48 | + $doctrinePaginator->getIterator()->will(function () { |
| 49 | + return new \ArrayIterator(); |
| 50 | + }); |
| 51 | + |
| 52 | + return new Paginator($doctrinePaginator->reveal()); |
| 53 | + } |
| 54 | + |
| 55 | + public function initializeProvider() |
| 56 | + { |
| 57 | + return [ |
| 58 | + 'First of three pages of 15 items each' => [0, 15, 42, 1, 3], |
| 59 | + 'Second of two pages of 10 items each' => [10, 10, 20, 2, 2], |
| 60 | + ]; |
| 61 | + } |
| 62 | +} |
0 commit comments