Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Created an adapter Zend Paginator instance using TableGateway #3878

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions library/Zend/Paginator/Adapter/DbTableGateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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;

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);
}
}
80 changes: 80 additions & 0 deletions tests/ZendTest/Paginator/Adapter/DbTableGatewayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?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;

/**
* @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);
}
}