forked from zendframework/zendframework
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created an adapter Zend Paginator instance using TableGateway
Isseus: zendframework#3802
- Loading branch information
Showing
2 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
namespace Zend\Paginator\Adapter; | ||
|
||
use Zend\Paginator\Adapter\DbSelect; | ||
use Zend\Db\TableGateway\TableGateway; | ||
|
||
/** | ||
* @category Zend | ||
* @package Zend_Paginator | ||
* @subpackage Adapter | ||
*/ | ||
class DbTableGateway extends DbSelect | ||
{ | ||
/** | ||
* Construnct | ||
* | ||
* @param TableGateway $tableGateway | ||
* @param Where|\Closure|string|array $where | ||
*/ | ||
public function __construct(TableGateway $tableGateway, $where = null) | ||
{ | ||
$select = $tableGateway->getSql()->select($where); | ||
$dbAdapter = $tableGateway->getAdapter(); | ||
$resultSetPrototype = $tableGateway->getResultSetPrototype(); | ||
|
||
parent::__construct($select, $dbAdapter, $resultSetPrototype); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
* @package Zend_Paginator | ||
*/ | ||
|
||
namespace ZendTest\Paginator\Adapter; | ||
|
||
use Zend\Paginator\Adapter\DbTableGateway; | ||
use Zend\Paginator\Adapter\DbSelect; | ||
use Zend\Db\ResultSet\ResultSet; | ||
|
||
/** | ||
* @category Zend | ||
* @package Zend_Paginator | ||
* @subpackage UnitTests | ||
* @group Zend_Paginator | ||
*/ | ||
class DbTableGatewayTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** @var \PHPUnit_Framework_MockObject_MockObject */ | ||
protected $mockStatement; | ||
|
||
/** @var DbTableGateway */ | ||
protected $dbTableGateway; | ||
|
||
public function setup() | ||
{ | ||
$mockStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface'); | ||
$mockDriver = $this->getMock('Zend\Db\Adapter\Driver\DriverInterface'); | ||
$mockDriver->expects($this->any()) | ||
->method('createStatement') | ||
->will($this->returnValue($mockStatement)); | ||
$mockPlatform = $this->getMock('Zend\Db\Adapter\Platform\PlatformInterface'); | ||
$mockPlatform->expects($this->any()) | ||
->method('getName') | ||
->will($this->returnValue('platform')); | ||
$mockAdapter = $this->getMockForAbstractClass( | ||
'Zend\Db\Adapter\Adapter', | ||
array($mockDriver, $mockPlatform) | ||
); | ||
|
||
$tableName = 'foobar'; | ||
$mockTableGateway = $this->getMockForAbstractClass( | ||
'Zend\Db\TableGateway\TableGateway', | ||
array($tableName, $mockAdapter) | ||
); | ||
|
||
$this->mockStatement = $mockStatement; | ||
$this->dbTableGateway = new DbTableGateway($mockTableGateway); | ||
} | ||
|
||
public function testGetItems() | ||
{ | ||
$mockResult = $this->getMock('Zend\Db\Adapter\Driver\ResultInterface'); | ||
$this->mockStatement | ||
->expects($this->any()) | ||
->method('execute') | ||
->will($this->returnValue($mockResult)); | ||
|
||
$items = $this->dbTableGateway->getItems(2, 10); | ||
$this->assertInstanceOf('Zend\Db\ResultSet\ResultSet', $items); | ||
} | ||
|
||
public function testCount() | ||
{ | ||
$mockResult = $this->getMock('Zend\Db\Adapter\Driver\ResultInterface'); | ||
$mockResult->expects($this->any()) | ||
->method('current') | ||
->will($this->returnValue(array('c' => 10))); | ||
|
||
$this->mockStatement->expects($this->any()) | ||
->method('execute') | ||
->will($this->returnValue($mockResult)); | ||
|
||
$count = $this->dbTableGateway->count(); | ||
$this->assertEquals(10, $count); | ||
} | ||
} |