parent
581ca79746
commit
89e4e2c649
@ -0,0 +1 @@ |
||||
Subproject commit 6770ecbe0e8c7d806c5779ad38b20c9acf5427d9 |
@ -0,0 +1,80 @@ |
||||
<?php |
||||
|
||||
/* |
||||
* This file is part of the Pagerfanta package. |
||||
* |
||||
* (c) Pablo Díez <pablodip@gmail.com> |
||||
* |
||||
* For the full copyright and license information, please view the LICENSE |
||||
* file that was distributed with this source code. |
||||
*/ |
||||
|
||||
namespace Pagerfanta\Adapter; |
||||
|
||||
use Doctrine\Common\Collections\Criteria; |
||||
use Doctrine\Common\Collections\Selectable; |
||||
|
||||
/** |
||||
* DoctrineSelectableAdapter. |
||||
* |
||||
* @author Boris Guéry <guery.b@gmail.com> |
||||
*/ |
||||
class DoctrineSelectableAdapter implements AdapterInterface |
||||
{ |
||||
/** |
||||
* @var Selectable |
||||
*/ |
||||
private $selectable; |
||||
|
||||
/** |
||||
* @var Criteria |
||||
*/ |
||||
private $criteria; |
||||
|
||||
/** |
||||
* Constructor. |
||||
* |
||||
* @param Selectable $selectable An implementation of the Selectable interface. |
||||
* @param Criteria $criteria A Doctrine criteria. |
||||
*/ |
||||
public function __construct(Selectable $selectable, Criteria $criteria) |
||||
{ |
||||
$this->selectable = $selectable; |
||||
$this->criteria = $criteria; |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function getNbResults() |
||||
{ |
||||
$firstResult = null; |
||||
$maxResults = null; |
||||
|
||||
$criteria = $this->createCriteria($firstResult, $maxResults); |
||||
|
||||
return $this->selectable->matching($criteria)->count(); |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function getSlice($offset, $length) |
||||
{ |
||||
$firstResult = $offset; |
||||
$maxResults = $length; |
||||
|
||||
$criteria = $this->createCriteria($firstResult, $maxResults); |
||||
|
||||
return $this->selectable->matching($criteria); |
||||
} |
||||
|
||||
private function createCriteria($firstResult, $maxResult) |
||||
{ |
||||
$criteria = clone $this->criteria; |
||||
$criteria->setFirstResult($firstResult); |
||||
$criteria->setMaxResults($maxResult); |
||||
|
||||
return $criteria; |
||||
} |
||||
} |
@ -0,0 +1,89 @@ |
||||
<?php |
||||
|
||||
namespace Pagerfanta\Tests\Adapter; |
||||
|
||||
use Pagerfanta\Adapter\DoctrineSelectableAdapter; |
||||
use Doctrine\Common\Collections\Selectable; |
||||
use Doctrine\Common\Collections\Collection; |
||||
use Doctrine\Common\Collections\Criteria; |
||||
use Doctrine\ORM\Version; |
||||
|
||||
class DoctrineSelectableAdapterTest extends \PHPUnit_Framework_TestCase |
||||
{ |
||||
private $selectable; |
||||
private $criteria; |
||||
private $adapter; |
||||
|
||||
protected function setUp() |
||||
{ |
||||
if ($this->isDoctrine23OrGreaterNotAvailable()) { |
||||
$this->markTestSkipped('This test can only be run using Doctrine >= 2.3'); |
||||
} |
||||
|
||||
$this->selectable = $this->createSelectableMock(); |
||||
$this->criteria = $this->createCriteria(); |
||||
|
||||
$this->adapter = new DoctrineSelectableAdapter($this->selectable, $this->criteria); |
||||
} |
||||
|
||||
private function isDoctrine23OrGreaterNotAvailable() |
||||
{ |
||||
return version_compare(Version::VERSION, '2.3', '<'); |
||||
} |
||||
|
||||
private function createSelectableMock() |
||||
{ |
||||
return $this->getMock('Doctrine\Common\Collections\Selectable'); |
||||
} |
||||
|
||||
private function createCriteria() |
||||
{ |
||||
$criteria = new Criteria(); |
||||
$criteria->orderBy(array('username' => 'ASC')); |
||||
$criteria->setFirstResult(2); |
||||
$criteria->setMaxResults(3); |
||||
|
||||
return $criteria; |
||||
} |
||||
|
||||
public function testGetNbResults() |
||||
{ |
||||
$this->criteria->setFirstResult(null); |
||||
$this->criteria->setMaxResults(null); |
||||
|
||||
$collection = $this->createCollectionMock(); |
||||
$collection |
||||
->expects($this->any()) |
||||
->method('count') |
||||
->will($this->returnValue(10)); |
||||
|
||||
$this->selectable |
||||
->expects($this->once()) |
||||
->method('matching') |
||||
->with($this->equalTo($this->criteria)) |
||||
->will($this->returnValue($collection)); |
||||
|
||||
$this->assertSame(10, $this->adapter->getNbResults()); |
||||
} |
||||
|
||||
private function createCollectionMock() |
||||
{ |
||||
return $this->getMock('Doctrine\Common\Collections\Collection'); |
||||
} |
||||
|
||||
public function testGetSlice() |
||||
{ |
||||
$this->criteria->setFirstResult(10); |
||||
$this->criteria->setMaxResults(20); |
||||
|
||||
$slice = new \stdClass(); |
||||
|
||||
$this->selectable |
||||
->expects($this->once()) |
||||
->method('matching') |
||||
->with($this->equalTo($this->criteria)) |
||||
->will($this->returnValue($slice)); |
||||
|
||||
$this->assertSame($slice, $this->adapter->getSlice(10, 20)); |
||||
} |
||||
} |
Loading…
Reference in new issue