Skip to content

Commit f15a893

Browse files
committed
Merge pull request #320 from jchampion/cache-paginator-iterator
Cache paginator iterator
2 parents bac081a + a3f042e commit f15a893

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

Doctrine/Orm/Paginator.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ class Paginator implements \IteratorAggregate, PaginatorInterface
4242
*/
4343
private $totalItems;
4444

45+
/**
46+
* @var \Traversable
47+
*/
48+
private $iterator;
49+
4550
public function __construct(DoctrineOrmPaginator $paginator)
4651
{
4752
$this->paginator = $paginator;
@@ -88,7 +93,11 @@ public function getTotalItems()
8893
*/
8994
public function getIterator()
9095
{
91-
return $this->paginator->getIterator();
96+
if (null === $this->iterator) {
97+
$this->iterator = $this->paginator->getIterator();
98+
}
99+
100+
return $this->iterator;
92101
}
93102

94103
/**

Tests/Doctrine/Orm/PaginatorTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
}

Tests/Fixtures/Query.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Dunglas\ApiBundle\Tests\Fixtures;
4+
5+
/**
6+
* Replace Doctrine\ORM\Query in tests because it cannot be mocked.
7+
*/
8+
class Query
9+
{
10+
public function getFirstResult()
11+
{
12+
}
13+
public function getMaxResults()
14+
{
15+
}
16+
}

0 commit comments

Comments
 (0)