diff --git a/src/Adapter/DbSelect.php b/src/Adapter/DbSelect.php index bc09435..e9a76db 100644 --- a/src/Adapter/DbSelect.php +++ b/src/Adapter/DbSelect.php @@ -10,7 +10,12 @@ namespace Zend\Paginator\Adapter; -use Zend\Db\Sql; +use Zend\Db\Adapter\Adapter; +use Zend\Db\Sql\Sql; +use Zend\Db\Sql\Expression; +use Zend\Db\Sql\Select; +use Zend\Db\ResultSet\ResultSetInterface; +use Zend\Db\ResultSet\ResultSet; /** * @category Zend @@ -18,19 +23,11 @@ */ class DbSelect implements AdapterInterface { - /** - * Name of the row count column - * - * @var string - */ - const ROW_COUNT_COLUMN = 'zend_paginator_row_count'; /** - * The COUNT query - * - * @var \Zend\Db\Sql\Select + * @var Sql */ - protected $countSelect = null; + protected $sql = null; /** * Database query @@ -39,6 +36,11 @@ class DbSelect implements AdapterInterface */ protected $select = null; + /** + * @var ResultSet + */ + protected $resultSetPrototype = null; + /** * Total item count * @@ -51,54 +53,22 @@ class DbSelect implements AdapterInterface * * @param \Zend\Db\Sql\Select $select The select query */ - public function __construct(Sql\Select $select) + public function __construct(Select $select, $adapterOrSqlObject, ResultSetInterface $resultSetPrototype = null) { $this->select = $select; - } - - /** - * Sets the total row count, either directly or through a supplied - * query. Without setting this, {@link getPages()} selects the count - * as a subquery (SELECT COUNT ... FROM (SELECT ...)). While this - * yields an accurate count even with queries containing clauses like - * LIMIT, it can be slow in some circumstances. For example, in MySQL, - * subqueries are generally slow when using the InnoDB storage engine. - * Users are therefore encouraged to profile their queries to find - * the solution that best meets their needs. - * - * @param \Zend\Db\Sql\Select|integer $rowCount Total row count integer - * or query - * @throws Exception\InvalidArgumentException - * @return DbSelect - */ - public function setRowCount($rowCount) - { - if ($rowCount instanceof Sql\Select) { - $columns = $rowCount->getPart(Sql\Select::COLUMNS); - - $countColumnPart = $columns[0][1]; - - if ($countColumnPart instanceof Sql\ExpressionInterface) { - $countColumnPart = $countColumnPart->__toString(); - } - - $rowCountColumn = $this->select->getAdapter()->foldCase(self::ROW_COUNT_COLUMN); - - // The select query can contain only one column, which should be the row count column - if (false === strpos($countColumnPart, $rowCountColumn)) { - throw new Exception\InvalidArgumentException('Row count column not found'); - } - $result = $rowCount->query(Db\Db::FETCH_ASSOC)->fetch(); + if ($adapterOrSqlObject instanceof Adapter) { + $adapterOrSqlObject = new Sql($adapterOrSqlObject); + } - $this->rowCount = count($result) > 0 ? $result[$rowCountColumn] : 0; - } elseif (is_integer($rowCount)) { - $this->rowCount = $rowCount; - } else { - throw new Exception\InvalidArgumentException('Invalid row count'); + if (!$adapterOrSqlObject instanceof Sql) { + throw new Exception\InvalidArgumentException( + '$adapterOrSqlObject must be an instance of Zend\Db\Adapter\Adapter or Zend\Db\Sql\Sql' + ); } - return $this; + $this->sql = $adapterOrSqlObject; + $this->resultSetPrototype = ($resultSetPrototype) ?: new ResultSet; } /** @@ -110,9 +80,17 @@ public function setRowCount($rowCount) */ public function getItems($offset, $itemCountPerPage) { - $this->select->limit($itemCountPerPage, $offset); + $select = clone $this->select; + $select->offset($offset); + $select->limit($itemCountPerPage); + + $statement = $this->sql->prepareStatementForSqlObject($select); + $result = $statement->execute(); - return $this->select->query()->fetchAll(); + $resultSet = clone $this->resultSetPrototype; + $resultSet->initialize($result); + + return $resultSet; } /** @@ -123,109 +101,21 @@ public function getItems($offset, $itemCountPerPage) public function count() { if ($this->rowCount === null) { - $this->setRowCount( - $this->getCountSelect() - ); - } + $select = clone $this->select; + $select->reset(Select::COLUMNS); + $select->reset(Select::LIMIT); + $select->reset(Select::OFFSET); - return $this->rowCount; - } + $select->columns(array('c' => new Expression('COUNT(1)'))); - /** - * Get the COUNT select object for the provided query - * - * TODO: Have a look at queries that have both GROUP BY and DISTINCT specified. - * In that use-case I'm expecting problems when either GROUP BY or DISTINCT - * has one column. - * - * @return \Zend\Db\Sql\Select - */ - public function getCountSelect() - { - /** - * We only need to generate a COUNT query once. It will not change for - * this instance. - */ - if ($this->countSelect !== null) { - return $this->countSelect; - } + $statement = $this->sql->prepareStatementForSqlObject($select); + $result = $statement->execute(); + $row = $result->current(); - $rowCount = clone $this->select; - $rowCount->__toString(); // Workaround for ZF-3719 and related - - $db = $rowCount->getAdapter(); - - $countColumn = $db->quoteIdentifier($db->foldCase(self::ROW_COUNT_COLUMN)); - $countPart = 'COUNT(1) AS '; - $groupPart = null; - $unionParts = $rowCount->getPart(Sql\Select::UNION); - - /** - * If we're dealing with a UNION query, execute the UNION as a subquery - * to the COUNT query. - */ - if (!empty($unionParts)) { - $expression = new Sql\Expression($countPart . $countColumn); - $rowCount = $db - ->select() - ->bind($rowCount->getBind()) - ->from($rowCount, $expression); - } else { - $columnParts = $rowCount->getPart(Sql\Select::COLUMNS); - $groupParts = $rowCount->getPart(Sql\Select::GROUP); - $havingParts = $rowCount->getPart(Sql\Select::HAVING); - $isDistinct = $rowCount->getPart(Sql\Select::DISTINCT); - - /** - * If there is more than one column AND it's a DISTINCT query, more - * than one group, or if the query has a HAVING clause, then take - * the original query and use it as a subquery os the COUNT query. - */ - if (($isDistinct && count($columnParts) > 1) || count($groupParts) > 1 || !empty($havingParts)) { - $rowCount = $db->select()->from($this->select); - } elseif ($isDistinct) { - $part = $columnParts[0]; - - if ($part[1] !== Sql\Select::SQL_WILDCARD && !($part[1] instanceof Sql\ExpressionInterface)) { - $column = $db->quoteIdentifier($part[1], true); - - if (!empty($part[0])) { - $column = $db->quoteIdentifier($part[0], true) . '.' . $column; - } - - $groupPart = $column; - } - } elseif (!empty($groupParts) && $groupParts[0] !== Sql\Select::SQL_WILDCARD && - !($groupParts[0] instanceof Sql\ExpressionInterface) - ) { - $groupPart = $db->quoteIdentifier($groupParts[0], true); - } - - /** - * If the original query had a GROUP BY or a DISTINCT part and only - * one column was specified, create a COUNT(DISTINCT ) query instead - * of a regular COUNT query. - */ - if (!empty($groupPart)) { - $countPart = 'COUNT(DISTINCT ' . $groupPart . ') AS '; - } - - /** - * Create the COUNT part of the query - */ - $expression = new Sql\Expression($countPart . $countColumn); - - $rowCount->reset(Sql\Select::COLUMNS) - ->reset(Sql\Select::ORDER) - ->reset(Sql\Select::LIMIT_OFFSET) - ->reset(Sql\Select::GROUP) - ->reset(Sql\Select::DISTINCT) - ->reset(Sql\Select::HAVING) - ->columns($expression); + $this->rowCount = $row['c']; } - $this->countSelect = $rowCount; - - return $rowCount; + return $this->rowCount; } + } diff --git a/src/Adapter/DbTableSelect.php b/src/Adapter/DbTableSelect.php deleted file mode 100644 index 9e68677..0000000 --- a/src/Adapter/DbTableSelect.php +++ /dev/null @@ -1,32 +0,0 @@ -select->limit($itemCountPerPage, $offset); - - return $this->select->getTable()->fetchAll($this->select); - } -} diff --git a/src/Paginator.php b/src/Paginator.php index 30f0d82..430e60f 100644 --- a/src/Paginator.php +++ b/src/Paginator.php @@ -18,8 +18,6 @@ use Zend\Cache\Storage\IteratorInterface as CacheIterator; use Zend\Cache\Storage\StorageInterface as CacheStorage; use Zend\Db\Sql; -use Zend\Db\Table\AbstractRowset as DbAbstractRowset; -use Zend\Db\Table\Select as DbTableSelect; use Zend\Filter\FilterInterface; use Zend\Json\Json; use Zend\Paginator\Adapter\AdapterInterface; @@ -183,9 +181,7 @@ public static function factory($data, $adapter = self::INTERNAL_ADAPTER) if ($adapter == self::INTERNAL_ADAPTER) { if (is_array($data)) { $adapter = 'array'; - } elseif ($data instanceof DbTableSelect) { - $adapter = 'db_table_select'; - } elseif ($data instanceof DbSelect) { + } elseif ($data instanceof Sql\Select) { $adapter = 'db_select'; } elseif ($data instanceof Iterator) { $adapter = 'iterator'; diff --git a/test/Adapter/DbSelect/OracleTest.php b/test/Adapter/DbSelect/OracleTest.php deleted file mode 100644 index 8ff1eaf..0000000 --- a/test/Adapter/DbSelect/OracleTest.php +++ /dev/null @@ -1,119 +0,0 @@ -markTestIncomplete('Will skip until Zend\Db is refactored.'); - - if (! extension_loaded('oci8')) { - $this->markTestSkipped('Oci8 extension is not loaded'); - } - - if (! TESTS_ZEND_DB_ADAPTER_ORACLE_ENABLED) { - $this->markTestSkipped('Oracle is required'); - } - - $this->_db = new \Zend\Db\Adapter\Oracle( - array('host' => TESTS_ZEND_DB_ADAPTER_ORACLE_HOSTNAME , - 'username' => TESTS_ZEND_DB_ADAPTER_ORACLE_USERNAME , - 'password' => TESTS_ZEND_DB_ADAPTER_ORACLE_PASSWORD , - 'dbname' => TESTS_ZEND_DB_ADAPTER_ORACLE_SID)); - - $this->_dropTable(); - $this->_createTable(); - $this->_populateTable(); - - $this->_table = new \TestTable($this->_db); - - $this->_query = $this->_db->select() - ->from('test') - ->order('number ASC') // ZF-3740 - ->limit(1000, 0); // ZF-3727 - - $this->_adapter = new Adapter\DbSelect($this->_query); - } - - /** - * Cleans up the environment after running a test. - */ - protected function tearDown () - { - $this->markTestIncomplete('Will skip until Zend\Db is refactored.'); - $this->_dropTable(); - $this->_db = null; - $this->_adapter = null; - } - - protected function _createTable () - { - $this->_db->query( - 'create table "test" ( - "number" NUMBER(5), - "testgroup" NUMBER(3), - constraint "pk_test" primary key ("number") - )'); - $this->_db->query( - 'create table "test_empty" ( - "number" NUMBER(5), - "testgroup" NUMBER(3), - constraint "pk_test_empty" primary key ("number") - )'); - } - - protected function _populateTable () - { - for ($i = 1; $i < 251; $i ++) { - $this->_db->query('insert into "test" values (' . $i . ', 1)'); - $this->_db->query('insert into "test" values (' . ($i + 250) . ', 2)'); - } - } - - protected function _dropTable () - { - try { - $this->_db->query('drop table "test"'); - } catch (OracleException $e) {} - try { - $this->_db->query('drop table "test_empty"'); - } catch (OracleException $e) {} - } - - public function testGroupByQueryOnEmptyTableReturnsRowCountZero() - { - $query = $this->_db->select() - ->from('test_empty') - ->order('number ASC') - ->limit(1000, 0); - $adapter = new Adapter\DbSelect($query); - - $this->assertEquals(0, $adapter->count()); - } -} diff --git a/test/Adapter/DbSelectTest.php b/test/Adapter/DbSelectTest.php index 6a33c0e..7e63fb1 100644 --- a/test/Adapter/DbSelectTest.php +++ b/test/Adapter/DbSelectTest.php @@ -10,12 +10,10 @@ namespace ZendTest\Paginator\Adapter; -use Zend\Paginator\Adapter; -use Zend\Db\Adapter as DbAdapter; -use Zend\Db\Sql; -use Zend\Paginator\Exception; - -require_once __DIR__ . '/../_files/TestTable.php'; +use Zend\Paginator\Adapter\DbSelect; +use Zend\Db\Sql\Select; +use Zend\Db\Sql\Expression; +use Zend\Db\Adapter\Adapter as DbAdapter; /** * @category Zend @@ -25,455 +23,47 @@ */ class DbSelectTest extends \PHPUnit_Framework_TestCase { - /** - * @var \Zend\Paginator\Adapter\DbSelect - */ - protected $_adapter; - - /** - * @var \Zend\Db\Adapter\Adapter - */ - protected $_db; - - /** - * @var \Zend\Db\Sql\Select - */ - protected $_query; - - /** - * @var \Zend\Db\TableGateway\TableGateway - */ - protected $_table; - - /** - * Prepares the environment before running a test. - */ - protected function setUp() - { - $this->markTestIncomplete('Will skip until Zend\Db is refactored.'); - - if (!extension_loaded('pdo_sqlite')) { - $this->markTestSkipped('Pdo_Sqlite extension is not loaded'); - } - - parent::setUp(); - - $this->_db = new DbAdapter\Adapter(array( - 'driver' => 'Pdo_Sqlite', - 'database' => __DIR__ . '/../_files/test.sqlite', - )); - - $this->_table = new \ZendTest\Paginator\TestAsset\TestTable('test', $this->_db); - - $this->_query = new Sql\Select; - $this->_query->from('test') - ->order('number ASC'); // ZF-3740 - //->limit(1000, 0); // ZF-3727 - - $this->_adapter = new Adapter\DbSelect($this->_query); - } - /** - * Cleans up the environment after running a test. - */ - protected function tearDown() - { - $this->_adapter = null; - parent::tearDown(); - } - - public function testGetsItemsAtOffsetZero() - { - $actual = $this->_adapter->getItems(0, 10); + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $mockSelect; - $i = 1; - foreach ($actual as $item) { - $this->assertEquals($i, $item['number']); - $i++; - } - } + protected $mockResult; - public function testGetsItemsAtOffsetTen() - { - $actual = $this->_adapter->getItems(10, 10); + /** @var DbSelect */ + protected $dbSelect; - $i = 11; - foreach ($actual as $item) { - $this->assertEquals($i, $item['number']); - $i++; - } - } - - public function testAcceptsIntegerValueForRowCount() + public function setup() { - $this->_adapter->setRowCount(101); - $this->assertEquals(101, $this->_adapter->count()); - } - - public function testThrowsExceptionIfInvalidQuerySuppliedForRowCount() - { - $this->setExpectedException('Zend\Paginator\Adapter\Exception\InvalidArgumentException', 'Row count column not found'); - $this->_adapter->setRowCount($this->_db->select()->from('test')); - } - - public function testThrowsExceptionIfInvalidQuerySuppliedForRowCount2() - { - $wrongcolumn = $this->_db->quoteIdentifier('wrongcolumn'); - $expr = new Sql\Expression("COUNT(*) AS $wrongcolumn"); - $query = new Sql\Select; - $query->from('test'); - - $this->setExpectedException('Zend\Paginator\Adapter\Exception\InvalidArgumentException', 'Row count column not found'); - $this->_adapter->setRowCount($query); - } - - public function testAcceptsQueryForRowCount() - { - $row_count_column = $this->_db->quoteIdentifier(Adapter\DbSelect::ROW_COUNT_COLUMN); - $expression = new Sql\Expression("COUNT(*) AS $row_count_column"); - - $rowCount = clone $this->_query; - $rowCount->reset(Sql\Select::COLUMNS) - ->reset(Sql\Select::ORDER) // ZF-3740 - ->reset(Sql\Select::LIMIT_OFFSET) // ZF-3727 - ->reset(Sql\Select::GROUP) // ZF-4001 - ->columns($expression); - - $this->_adapter->setRowCount($rowCount); - - $this->assertEquals(500, $this->_adapter->count()); - } - - public function testThrowsExceptionIfInvalidRowCountValueSupplied() - { - $this->setExpectedException('Zend\Paginator\Adapter\Exception\InvalidArgumentException', 'Invalid row count'); - $this->_adapter->setRowCount('invalid'); - } - - public function testReturnsCorrectCountWithAutogeneratedQuery() - { - $expected = 500; - $actual = $this->_adapter->count(); - - $this->assertEquals($expected, $actual); - } - - public function testDbTableSelectDoesNotThrowException() - { - $adapter = new Adapter\DbSelect($this->_table->select()); - $count = $adapter->count(); - $this->assertEquals(500, $count); - } - - /** - * @group ZF-4001 - */ - public function testGroupByQueryReturnsOneRow() - { - $query = new Sql\Select; - $query->from('test') - ->order('number ASC') - ->limit(1000, 0) - ->group('number'); - - $adapter = new Adapter\DbSelect($query); - - $this->assertEquals(500, $adapter->count()); - } - - /** - * @group ZF-4001 - */ - public function testGroupByQueryOnEmptyTableReturnsRowCountZero() - { - $db = new DbAdapter\Adapter(array( - 'driver' => 'Pdo_Sqlite', - 'database' => __DIR__ . '/../_files/testempty.sqlite', - )); - - $query = new Sql\Select; - $query->from('test') - ->order('number ASC') - ->limit(1000, 0); - $adapter = new Adapter\DbSelect($query); - - $this->assertEquals(0, $adapter->count()); - } - - /** - * @group ZF-4001 - */ - public function testGroupByQueryReturnsCorrectResult() - { - $query = new Sql\Select; - $query->from('test') - ->order('number ASC') - ->limit(1000, 0) - ->group('testgroup'); - $adapter = new Adapter\DbSelect($query); - - $this->assertEquals(2, $adapter->count()); - } - - /** - * @group ZF-4032 - */ - public function testDistinctColumnQueryReturnsCorrectResult() - { - $query = new Sql\Select; - $query->from('test') - ->order('number ASC') - ->limit(1000, 0) - ->distinct(); - $adapter = new Adapter\DbSelect($query); - - $this->assertEquals(2, $adapter->count()); - } - - /** - * @group ZF-4094 - */ - public function testSelectSpecificColumns() - { - $number = $this->_db->quoteIdentifier('number'); - $query = $this->_db->select()->from('test', array('testgroup', 'number')) - ->where("$number >= ?", '1'); - $adapter = new Adapter\DbSelect($query); - - $this->assertEquals(500, $adapter->count()); - } + $mockStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface'); + $mockResult = $this->getMock('Zend\Db\Adapter\Driver\ResultInterface'); - /** - * @group ZF-4177 - */ - public function testSelectDistinctAllUsesRegularCountAll() - { - $query = $this->_db->select()->from('test') - ->distinct(); - $adapter = new Adapter\DbSelect($query); - - $this->assertEquals(500, $adapter->count()); - } - - /** - * @group ZF-5233 - */ - public function testSelectHasAliasedColumns() - { - $db = $this->_db; - - $db->query('DROP TABLE IF EXISTS `sandboxTransaction`'); - $db->query('DROP TABLE IF EXISTS `sandboxForeign`'); - - // A transaction table - $db->query( - 'CREATE TABLE `sandboxTransaction` ( - `id` INTEGER PRIMARY KEY, - `foreign_id` INT( 1 ) NOT NULL , - `name` TEXT NOT NULL - ) ' - ); - - // A foreign table - $db->query( - 'CREATE TABLE `sandboxForeign` ( - `id` INTEGER PRIMARY KEY, - `name` TEXT NOT NULL - ) ' - ); - - // Insert some data - $db->insert('sandboxTransaction', - array( - 'foreign_id' => 1, - 'name' => 'transaction 1 with foreign_id 1', - ) - ); - - $db->insert('sandboxTransaction', - array( - 'foreign_id' => 1, - 'name' => 'transaction 2 with foreign_id 1', - ) - ); - - $db->insert('sandboxForeign', - array( - 'name' => 'John Doe', - ) - ); - - $db->insert('sandboxForeign', - array( - 'name' => 'Jane Smith', - ) + $mockDriver = $this->getMock('Zend\Db\Adapter\Driver\DriverInterface'); + $mockDriver->expects($this->any())->method('createStatement')->will($this->returnValue($mockStatement)); + $mockStatement->expects($this->any())->method('execute')->will($this->returnValue($mockResult)); + $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) ); - $query = $db->select()->from(array('a'=>'sandboxTransaction'), array()) - ->join(array('b'=>'sandboxForeign'), 'a.foreign_id = b.id', array('name')) - ->distinct(true); - - $adapter = new Adapter\DbSelect($query); - $this->assertEquals(1, $adapter->count()); - } - - /** - * @group ZF-5956 - */ - public function testUnionSelect() - { - $union = $this->_db->select()->union(array( - $this->_db->select()->from('test')->where('number <= 250'), - $this->_db->select()->from('test')->where('number > 250') - )); - - $adapter = new Adapter\DbSelect($union); - $expected = 500; - $actual = $adapter->count(); - - $this->assertEquals($expected, $actual); - } - - /** - * @group ZF-7045 - */ - public function testGetCountSelect() - { - $union = $this->_db->select()->union(array( - $this->_db->select()->from('test')->where('number <= 250'), - $this->_db->select()->from('test')->where('number > 250') - )); - - $adapter = new Adapter\DbSelect($union); - - $expected = 'SELECT COUNT(1) AS "zend_paginator_row_count" FROM (SELECT "test".* FROM "test" WHERE (number <= 250) UNION SELECT "test".* FROM "test" WHERE (number > 250)) AS "t"'; - - $this->assertEquals($expected, $adapter->getCountSelect()->__toString()); - } - - /** - * @group ZF-5295 - */ - public function testMultipleDistinctColumns() - { - $select = $this->_db->select()->from('test', array('testgroup', 'number')) - ->distinct(true); - - $adapter = new Adapter\DbSelect($select); - - $expected = 'SELECT COUNT(1) AS "zend_paginator_row_count" FROM (SELECT DISTINCT "test"."testgroup", "test"."number" FROM "test") AS "t"'; - - $this->assertEquals($expected, $adapter->getCountSelect()->__toString()); - $this->assertEquals(500, $adapter->count()); + $this->mockSelect = $this->getMock('Zend\Db\Sql\Select'); + $this->mockResult = $mockResult; + $this->dbSelect = new DbSelect($this->mockSelect, $mockAdapter); } - /** - * @group ZF-5295 - */ - public function testSingleDistinctColumn() + public function testGetItems() { - $select = $this->_db->select()->from('test', 'testgroup') - ->distinct(true); - - $adapter = new Adapter\DbSelect($select); - - $expected = 'SELECT COUNT(DISTINCT "test"."testgroup") AS "zend_paginator_row_count" FROM "test"'; - - $this->assertEquals($expected, $adapter->getCountSelect()->__toString()); - $this->assertEquals(2, $adapter->count()); - } - - /** - * @group ZF-6330 - */ - public function testGroupByMultipleColumns() - { - $select = $this->_db->select()->from('test', 'testgroup') - ->group(array('number', 'testgroup')); - - $adapter = new Adapter\DbSelect($select); - - $expected = 'SELECT COUNT(1) AS "zend_paginator_row_count" FROM (SELECT "test"."testgroup" FROM "test" GROUP BY "number"' . ",\n\t" . '"testgroup") AS "t"'; - - $this->assertEquals($expected, $adapter->getCountSelect()->__toString()); - $this->assertEquals(500, $adapter->count()); - } - - /** - * @group ZF-6330 - */ - public function testGroupBySingleColumn() - { - $select = $this->_db->select()->from('test', 'testgroup') - ->group('test.testgroup'); - - $adapter = new Adapter\DbSelect($select); - - $expected = 'SELECT COUNT(DISTINCT "test"."testgroup") AS "zend_paginator_row_count" FROM "test"'; - - $this->assertEquals($expected, $adapter->getCountSelect()->__toString()); - $this->assertEquals(2, $adapter->count()); + $this->mockSelect->expects($this->once())->method('limit')->with($this->equalTo(10)); + $this->mockSelect->expects($this->once())->method('offset')->with($this->equalTo(2)); + $items = $this->dbSelect->getItems(2, 10); + $this->assertInstanceOf('Zend\Db\ResultSet\ResultSet', $items); } - /** - * @group ZF-6562 - */ - public function testSelectWithHaving() + public function testCount() { - $select = $this->_db->select()->from('test') - ->group('number') - ->having('number > 250'); - - $adapter = new Adapter\DbSelect($select); - - $expected = 'SELECT COUNT(1) AS "zend_paginator_row_count" FROM (SELECT "test".* FROM "test" GROUP BY "number" HAVING (number > 250)) AS "t"'; - - $this->assertEquals($expected, $adapter->getCountSelect()->__toString()); - $this->assertEquals(250, $adapter->count()); - } - - /** - * @group ZF-7127 - */ - public function testMultipleGroupSelect() - { - $select = $this->_db->select()->from('test') - ->group('testgroup') - ->group('number') - ->where('number > 250'); - - $adapter = new Adapter\DbSelect($select); - - $expected = 'SELECT COUNT(1) AS "zend_paginator_row_count" FROM (SELECT "test".* FROM "test" WHERE (number > 250) GROUP BY "testgroup"' . ",\n\t" . '"number") AS "t"'; - - $this->assertEquals($expected, $adapter->getCountSelect()->__toString()); - $this->assertEquals(250, $adapter->count()); - } - - /** - * @group ZF-10704 - */ - public function testObjectSelectWithBind() - { - $select = $this->_db->select(); - $select->from('test', array('number')) - ->where('number = ?') - ->distinct(true) - ->bind(array(250)); - - $adapter = new Adapter\DbSelect($select); - $this->assertEquals(1, $adapter->count()); - - $select->reset(Sql\Select::DISTINCT); - $select2 = clone $select; - $select2->reset(Sql\Select::WHERE) - ->where('number = 500'); - - $selectUnion = $this->_db - ->select() - ->bind(array(250)); - - $selectUnion->union(array($select, $select2)); - $adapter = new Adapter\DbSelect($selectUnion); - $this->assertEquals(2, $adapter->count()); + $this->mockSelect->expects($this->once())->method('columns')->with($this->equalTo(array('c' => new Expression('COUNT(1)')))); + $this->mockResult->expects($this->any())->method('current')->will($this->returnValue(array('c' => 5))); + $count = $this->dbSelect->count(); + $this->assertEquals(5, $count); } } diff --git a/test/Adapter/DbTableSelect/OracleTest.php b/test/Adapter/DbTableSelect/OracleTest.php deleted file mode 100644 index c2dfc02..0000000 --- a/test/Adapter/DbTableSelect/OracleTest.php +++ /dev/null @@ -1,37 +0,0 @@ -markTestIncomplete('Will skip until Zend\Db is refactored.'); - } - - /** - * @group ZF-3775 - */ - public function testSelectDoesReturnZendDbTableRowset() - { - $query = $this->_table->select(); - $adapter = new \Zend\Paginator\Adapter\DbTableSelect($query); - $items = $adapter->getItems(0, 10); - - $this->assertInstanceOf('Zend\Db\Table\Rowset', $items); - } -} diff --git a/test/Adapter/DbTableSelectTest.php b/test/Adapter/DbTableSelectTest.php deleted file mode 100644 index 156fde0..0000000 --- a/test/Adapter/DbTableSelectTest.php +++ /dev/null @@ -1,41 +0,0 @@ -_table->select(); - $adapter = new Adapter\DbTableSelect($query); - $items = $adapter->getItems(0, 10); - - $this->assertInstanceOf('Zend\\Db\\Table\\Rowset', $items); - } - - public function testToJsonWithRowset() - { - $query = $this->_table->select(); - $paginator = new \Zend\Paginator\Paginator(new Adapter\DbTableSelect($query)); - $this->assertGreaterThan(2, strlen($paginator->toJson())); - } -} diff --git a/test/PaginatorTest.php b/test/PaginatorTest.php index f0052f8..3beb66b 100644 --- a/test/PaginatorTest.php +++ b/test/PaginatorTest.php @@ -39,51 +39,42 @@ class PaginatorTest extends \PHPUnit_Framework_TestCase * * @var Paginator\Paginator */ - protected $_paginator = null; + protected $paginator = null; - protected $_testCollection = null; + protected $testCollection = null; - protected $_cache; + protected $cache; + protected $cacheDir; - protected $_query = null; + protected $select = null; - protected $_config = null; + protected $config = null; /** * @var DbAdapter\Adapter */ - protected $_adapter = null; + protected $adapter = null; protected function setUp() { - if (!extension_loaded('pdo_sqlite')) { - $this->markTestSkipped('Pdo_Sqlite extension is not loaded'); - } - - $this->_adapter = new DbAdapter\Adapter(array( - 'driver' => 'Pdo_Sqlite', - 'database' => __DIR__ . '/_files/test.sqlite', - )); + $this->select = new Sql\Select; + $this->select->from('test'); - $this->_query = new Sql\Select; - $this->_query->from('test'); + $this->testCollection = range(1, 101); + $this->paginator = Paginator\Paginator::factory($this->testCollection); - $this->_testCollection = range(1, 101); - $this->_paginator = Paginator\Paginator::factory($this->_testCollection); + $this->config = Config\Factory::fromFile(__DIR__ . '/_files/config.xml', true); - $this->_config = Config\Factory::fromFile(__DIR__ . '/_files/config.xml', true); - - $this->_cache = CacheFactory::adapterFactory('memory', array('memory_limit' => 0)); - Paginator\Paginator::setCache($this->_cache); + $this->cache = CacheFactory::adapterFactory('memory', array('memory_limit' => 0)); + Paginator\Paginator::setCache($this->cache); $this->_restorePaginatorDefaults(); } protected function tearDown() { - $this->_dbConn = null; - $this->_testCollection = null; - $this->_paginator = null; + $this->testCollection = null; + $this->paginator = null; } protected function _getTmpDir() @@ -115,31 +106,31 @@ protected function _rmDirRecursive($path) protected function _restorePaginatorDefaults() { - $this->_paginator->setItemCountPerPage(10); - $this->_paginator->setCurrentPageNumber(1); - $this->_paginator->setPageRange(10); - $this->_paginator->setView(); + $this->paginator->setItemCountPerPage(10); + $this->paginator->setCurrentPageNumber(1); + $this->paginator->setPageRange(10); + $this->paginator->setView(); Paginator\Paginator::setDefaultScrollingStyle(); Helper\PaginationControl::setDefaultViewPartial(null); - Paginator\Paginator::setOptions($this->_config->default); + Paginator\Paginator::setOptions($this->config->default); Paginator\Paginator::setScrollingStylePluginManager(new Paginator\ScrollingStylePluginManager()); - $this->_paginator->setCacheEnabled(true); + $this->paginator->setCacheEnabled(true); } public function testFactoryReturnsArrayAdapter() { - $paginator = Paginator\Paginator::factory($this->_testCollection); + $paginator = Paginator\Paginator::factory($this->testCollection); $this->assertInstanceOf('Zend\Paginator\Adapter\ArrayAdapter', $paginator->getAdapter()); } public function testFactoryReturnsDbSelectAdapter() { $this->markTestIncomplete('Will skip until Zend\Db is refactored.'); - $paginator = Paginator\Paginator::factory($this->_query); + $paginator = Paginator\Paginator::factory($this->select); $this->assertInstanceOf('Zend\Paginator\Adapter\DbSelect', $paginator->getAdapter()); } @@ -150,7 +141,7 @@ public function testFactoryReturnsDbSelectAdapter() public function testFactoryReturnsDbTableSelectAdapter() { $this->markTestIncomplete('Will skip until Zend\Db is refactored.'); - $table = new TestTable('test', $this->_adapter); + $table = new TestTable('test', $this->adapter); $paginator = Paginator\Paginator::factory($table->select()); @@ -159,7 +150,7 @@ public function testFactoryReturnsDbTableSelectAdapter() public function testFactoryReturnsIteratorAdapter() { - $paginator = Paginator\Paginator::factory(new \ArrayIterator($this->_testCollection)); + $paginator = Paginator\Paginator::factory(new \ArrayIterator($this->testCollection)); $this->assertInstanceOf('Zend\Paginator\Adapter\Iterator', $paginator->getAdapter()); } @@ -203,7 +194,7 @@ public function testHasCorrectCountOfAllItemsAfterInit() public function testLoadsFromConfig() { - Paginator\Paginator::setOptions($this->_config->testing); + Paginator\Paginator::setOptions($this->config->testing); $this->assertEquals('Scrolling', Paginator\Paginator::getDefaultScrollingStyle()); $plugins = Paginator\Paginator::getScrollingStylePluginManager(); @@ -234,7 +225,7 @@ public function testGetsPagesForPageOne() $expected->firstItemNumber = 1; $expected->lastItemNumber = 10; - $actual = $this->_paginator->getPages(); + $actual = $this->paginator->getPages(); $this->assertEquals($expected, $actual); } @@ -257,8 +248,8 @@ public function testGetsPagesForPageTwo() $expected->firstItemNumber = 11; $expected->lastItemNumber = 20; - $this->_paginator->setCurrentPageNumber(2); - $actual = $this->_paginator->getPages(); + $this->paginator->setCurrentPageNumber(2); + $actual = $this->paginator->getPages(); $this->assertEquals($expected, $actual); } @@ -269,8 +260,8 @@ public function testGetsPagesForPageTwo() */ public function testRendersWithoutPartial() { - $this->_paginator->setView(new View\Renderer\PhpRenderer()); - $string = @$this->_paginator->__toString(); + $this->paginator->setView(new View\Renderer\PhpRenderer()); + $string = @$this->paginator->__toString(); $this->assertEquals('', $string); } @@ -281,27 +272,27 @@ public function testRendersWithPartial() Helper\PaginationControl::setDefaultViewPartial('partial.phtml'); - $this->_paginator->setView($view); + $this->paginator->setView($view); - $string = $this->_paginator->__toString(); + $string = $this->paginator->__toString(); $this->assertEquals('partial rendered successfully', $string); } public function testGetsPageCount() { - $this->assertEquals(11, $this->_paginator->count()); + $this->assertEquals(11, $this->paginator->count()); } public function testGetsAndSetsItemCountPerPage() { Paginator\Paginator::setOptions(new Config\Config(array())); - $this->_paginator = new Paginator\Paginator(new Adapter\ArrayAdapter(range(1, 101))); - $this->assertEquals(10, $this->_paginator->getItemCountPerPage()); - $this->_paginator->setItemCountPerPage(15); - $this->assertEquals(15, $this->_paginator->getItemCountPerPage()); - $this->_paginator->setItemCountPerPage(0); - $this->assertEquals(101, $this->_paginator->getItemCountPerPage()); - $this->_paginator->setItemCountPerPage(10); + $this->paginator = new Paginator\Paginator(new Adapter\ArrayAdapter(range(1, 101))); + $this->assertEquals(10, $this->paginator->getItemCountPerPage()); + $this->paginator->setItemCountPerPage(15); + $this->assertEquals(15, $this->paginator->getItemCountPerPage()); + $this->paginator->setItemCountPerPage(0); + $this->assertEquals(101, $this->paginator->getItemCountPerPage()); + $this->paginator->setItemCountPerPage(10); } /** @@ -310,10 +301,10 @@ public function testGetsAndSetsItemCountPerPage() public function testGetsAndSetsItemCounterPerPageOfNegativeOne() { Paginator\Paginator::setOptions(new Config\Config(array())); - $this->_paginator = new Paginator\Paginator(new Paginator\Adapter\ArrayAdapter(range(1, 101))); - $this->_paginator->setItemCountPerPage(-1); - $this->assertEquals(101, $this->_paginator->getItemCountPerPage()); - $this->_paginator->setItemCountPerPage(10); + $this->paginator = new Paginator\Paginator(new Paginator\Adapter\ArrayAdapter(range(1, 101))); + $this->paginator->setItemCountPerPage(-1); + $this->assertEquals(101, $this->paginator->getItemCountPerPage()); + $this->paginator->setItemCountPerPage(10); } /** @@ -322,10 +313,10 @@ public function testGetsAndSetsItemCounterPerPageOfNegativeOne() public function testGetsAndSetsItemCounterPerPageOfZero() { Paginator\Paginator::setOptions(new Config\Config(array())); - $this->_paginator = new Paginator\Paginator(new Paginator\Adapter\ArrayAdapter(range(1, 101))); - $this->_paginator->setItemCountPerPage(0); - $this->assertEquals(101, $this->_paginator->getItemCountPerPage()); - $this->_paginator->setItemCountPerPage(10); + $this->paginator = new Paginator\Paginator(new Paginator\Adapter\ArrayAdapter(range(1, 101))); + $this->paginator->setItemCountPerPage(0); + $this->assertEquals(101, $this->paginator->getItemCountPerPage()); + $this->paginator->setItemCountPerPage(10); } /** @@ -334,29 +325,29 @@ public function testGetsAndSetsItemCounterPerPageOfZero() public function testGetsAndSetsItemCounterPerPageOfNull() { Paginator\Paginator::setOptions(new Config\Config(array())); - $this->_paginator = new Paginator\Paginator(new Paginator\Adapter\ArrayAdapter(range(1, 101))); - $this->_paginator->setItemCountPerPage(); - $this->assertEquals(101, $this->_paginator->getItemCountPerPage()); - $this->_paginator->setItemCountPerPage(10); + $this->paginator = new Paginator\Paginator(new Paginator\Adapter\ArrayAdapter(range(1, 101))); + $this->paginator->setItemCountPerPage(); + $this->assertEquals(101, $this->paginator->getItemCountPerPage()); + $this->paginator->setItemCountPerPage(10); } public function testGetsCurrentItemCount() { - $this->_paginator->setItemCountPerPage(10); - $this->_paginator->setPageRange(10); + $this->paginator->setItemCountPerPage(10); + $this->paginator->setPageRange(10); - $this->assertEquals(10, $this->_paginator->getCurrentItemCount()); + $this->assertEquals(10, $this->paginator->getCurrentItemCount()); - $this->_paginator->setCurrentPageNumber(11); + $this->paginator->setCurrentPageNumber(11); - $this->assertEquals(1, $this->_paginator->getCurrentItemCount()); + $this->assertEquals(1, $this->paginator->getCurrentItemCount()); - $this->_paginator->setCurrentPageNumber(1); + $this->paginator->setCurrentPageNumber(1); } public function testGetsCurrentItems() { - $items = $this->_paginator->getCurrentItems(); + $items = $this->paginator->getCurrentItems(); $this->assertInstanceOf('ArrayIterator', $items); $count = 0; @@ -370,7 +361,7 @@ public function testGetsCurrentItems() public function testGetsIterator() { - $items = $this->_paginator->getIterator(); + $items = $this->paginator->getIterator(); $this->assertInstanceOf('ArrayIterator', $items); $count = 0; @@ -384,29 +375,29 @@ public function testGetsIterator() public function testGetsAndSetsCurrentPageNumber() { - $this->assertEquals(1, $this->_paginator->getCurrentPageNumber()); - $this->_paginator->setCurrentPageNumber(-1); - $this->assertEquals(1, $this->_paginator->getCurrentPageNumber()); - $this->_paginator->setCurrentPageNumber(11); - $this->assertEquals(11, $this->_paginator->getCurrentPageNumber()); - $this->_paginator->setCurrentPageNumber(111); - $this->assertEquals(11, $this->_paginator->getCurrentPageNumber()); - $this->_paginator->setCurrentPageNumber(1); - $this->assertEquals(1, $this->_paginator->getCurrentPageNumber()); + $this->assertEquals(1, $this->paginator->getCurrentPageNumber()); + $this->paginator->setCurrentPageNumber(-1); + $this->assertEquals(1, $this->paginator->getCurrentPageNumber()); + $this->paginator->setCurrentPageNumber(11); + $this->assertEquals(11, $this->paginator->getCurrentPageNumber()); + $this->paginator->setCurrentPageNumber(111); + $this->assertEquals(11, $this->paginator->getCurrentPageNumber()); + $this->paginator->setCurrentPageNumber(1); + $this->assertEquals(1, $this->paginator->getCurrentPageNumber()); } public function testGetsAbsoluteItemNumber() { - $this->assertEquals(1, $this->_paginator->getAbsoluteItemNumber(1)); - $this->assertEquals(11, $this->_paginator->getAbsoluteItemNumber(1, 2)); - $this->assertEquals(24, $this->_paginator->getAbsoluteItemNumber(4, 3)); + $this->assertEquals(1, $this->paginator->getAbsoluteItemNumber(1)); + $this->assertEquals(11, $this->paginator->getAbsoluteItemNumber(1, 2)); + $this->assertEquals(24, $this->paginator->getAbsoluteItemNumber(4, 3)); } public function testGetsItem() { - $this->assertEquals(1, $this->_paginator->getItem(1)); - $this->assertEquals(11, $this->_paginator->getItem(1, 2)); - $this->assertEquals(24, $this->_paginator->getItem(4, 3)); + $this->assertEquals(1, $this->paginator->getItem(1)); + $this->assertEquals(11, $this->paginator->getItem(1, 2)); + $this->assertEquals(24, $this->paginator->getItem(4, 3)); } public function testThrowsExceptionWhenCollectionIsEmpty() @@ -420,29 +411,29 @@ public function testThrowsExceptionWhenCollectionIsEmpty() public function testThrowsExceptionWhenRetrievingNonexistentItemFromLastPage() { $this->setExpectedException('Zend\Paginator\Exception\InvalidArgumentException', 'Page 11 does not contain item number 10'); - $this->_paginator->getItem(10, 11); + $this->paginator->getItem(10, 11); } public function testNormalizesPageNumber() { - $this->assertEquals(1, $this->_paginator->normalizePageNumber(0)); - $this->assertEquals(1, $this->_paginator->normalizePageNumber(1)); - $this->assertEquals(2, $this->_paginator->normalizePageNumber(2)); - $this->assertEquals(5, $this->_paginator->normalizePageNumber(5)); - $this->assertEquals(10, $this->_paginator->normalizePageNumber(10)); - $this->assertEquals(11, $this->_paginator->normalizePageNumber(11)); - $this->assertEquals(11, $this->_paginator->normalizePageNumber(12)); + $this->assertEquals(1, $this->paginator->normalizePageNumber(0)); + $this->assertEquals(1, $this->paginator->normalizePageNumber(1)); + $this->assertEquals(2, $this->paginator->normalizePageNumber(2)); + $this->assertEquals(5, $this->paginator->normalizePageNumber(5)); + $this->assertEquals(10, $this->paginator->normalizePageNumber(10)); + $this->assertEquals(11, $this->paginator->normalizePageNumber(11)); + $this->assertEquals(11, $this->paginator->normalizePageNumber(12)); } public function testNormalizesItemNumber() { - $this->assertEquals(1, $this->_paginator->normalizeItemNumber(0)); - $this->assertEquals(1, $this->_paginator->normalizeItemNumber(1)); - $this->assertEquals(2, $this->_paginator->normalizeItemNumber(2)); - $this->assertEquals(5, $this->_paginator->normalizeItemNumber(5)); - $this->assertEquals(9, $this->_paginator->normalizeItemNumber(9)); - $this->assertEquals(10, $this->_paginator->normalizeItemNumber(10)); - $this->assertEquals(10, $this->_paginator->normalizeItemNumber(11)); + $this->assertEquals(1, $this->paginator->normalizeItemNumber(0)); + $this->assertEquals(1, $this->paginator->normalizeItemNumber(1)); + $this->assertEquals(2, $this->paginator->normalizeItemNumber(2)); + $this->assertEquals(5, $this->paginator->normalizeItemNumber(5)); + $this->assertEquals(9, $this->paginator->normalizeItemNumber(9)); + $this->assertEquals(10, $this->paginator->normalizeItemNumber(10)); + $this->assertEquals(10, $this->paginator->normalizeItemNumber(11)); } /** @@ -450,13 +441,13 @@ public function testNormalizesItemNumber() */ public function testNormalizesPageNumberWhenGivenAFloat() { - $this->assertEquals(1, $this->_paginator->normalizePageNumber(0.5)); - $this->assertEquals(1, $this->_paginator->normalizePageNumber(1.99)); - $this->assertEquals(2, $this->_paginator->normalizePageNumber(2.3)); - $this->assertEquals(5, $this->_paginator->normalizePageNumber(5.1)); - $this->assertEquals(10, $this->_paginator->normalizePageNumber(10.06)); - $this->assertEquals(11, $this->_paginator->normalizePageNumber(11.5)); - $this->assertEquals(11, $this->_paginator->normalizePageNumber(12.7889)); + $this->assertEquals(1, $this->paginator->normalizePageNumber(0.5)); + $this->assertEquals(1, $this->paginator->normalizePageNumber(1.99)); + $this->assertEquals(2, $this->paginator->normalizePageNumber(2.3)); + $this->assertEquals(5, $this->paginator->normalizePageNumber(5.1)); + $this->assertEquals(10, $this->paginator->normalizePageNumber(10.06)); + $this->assertEquals(11, $this->paginator->normalizePageNumber(11.5)); + $this->assertEquals(11, $this->paginator->normalizePageNumber(12.7889)); } /** @@ -464,24 +455,24 @@ public function testNormalizesPageNumberWhenGivenAFloat() */ public function testNormalizesItemNumberWhenGivenAFloat() { - $this->assertEquals(1, $this->_paginator->normalizeItemNumber(0.5)); - $this->assertEquals(1, $this->_paginator->normalizeItemNumber(1.99)); - $this->assertEquals(2, $this->_paginator->normalizeItemNumber(2.3)); - $this->assertEquals(5, $this->_paginator->normalizeItemNumber(5.1)); - $this->assertEquals(9, $this->_paginator->normalizeItemNumber(9.06)); - $this->assertEquals(10, $this->_paginator->normalizeItemNumber(10.5)); - $this->assertEquals(10, $this->_paginator->normalizeItemNumber(11.7889)); + $this->assertEquals(1, $this->paginator->normalizeItemNumber(0.5)); + $this->assertEquals(1, $this->paginator->normalizeItemNumber(1.99)); + $this->assertEquals(2, $this->paginator->normalizeItemNumber(2.3)); + $this->assertEquals(5, $this->paginator->normalizeItemNumber(5.1)); + $this->assertEquals(9, $this->paginator->normalizeItemNumber(9.06)); + $this->assertEquals(10, $this->paginator->normalizeItemNumber(10.5)); + $this->assertEquals(10, $this->paginator->normalizeItemNumber(11.7889)); } public function testGetsPagesInSubsetRange() { - $actual = $this->_paginator->getPagesInRange(3, 8); + $actual = $this->paginator->getPagesInRange(3, 8); $this->assertEquals(array_combine(range(3, 8), range(3, 8)), $actual); } public function testGetsPagesInOutOfBoundsRange() { - $actual = $this->_paginator->getPagesInRange(-1, 12); + $actual = $this->paginator->getPagesInRange(-1, 12); $this->assertEquals(array_combine(range(1, 11), range(1, 11)), $actual); } @@ -489,42 +480,42 @@ public function testGetsItemsByPage() { $expected = new \ArrayIterator(range(1, 10)); - $page1 = $this->_paginator->getItemsByPage(1); + $page1 = $this->paginator->getItemsByPage(1); $this->assertEquals($page1, $expected); - $this->assertEquals($page1, $this->_paginator->getItemsByPage(1)); + $this->assertEquals($page1, $this->paginator->getItemsByPage(1)); } public function testGetsItemCount() { - $this->assertEquals(101, $this->_paginator->getItemCount(range(1, 101))); + $this->assertEquals(101, $this->paginator->getItemCount(range(1, 101))); $limitIterator = new \LimitIterator(new \ArrayIterator(range(1, 101))); - $this->assertEquals(101, $this->_paginator->getItemCount($limitIterator)); + $this->assertEquals(101, $this->paginator->getItemCount($limitIterator)); } public function testGeneratesViewIfNonexistent() { - $this->assertInstanceOf('Zend\\View\\Renderer\\RendererInterface', $this->_paginator->getView()); + $this->assertInstanceOf('Zend\\View\\Renderer\\RendererInterface', $this->paginator->getView()); } public function testGetsAndSetsView() { - $this->_paginator->setView(new View\Renderer\PhpRenderer()); - $this->assertInstanceOf('Zend\\View\\Renderer\\RendererInterface', $this->_paginator->getView()); + $this->paginator->setView(new View\Renderer\PhpRenderer()); + $this->assertInstanceOf('Zend\\View\\Renderer\\RendererInterface', $this->paginator->getView()); } public function testRenders() { $this->setExpectedException('Zend\\View\\Exception\\ExceptionInterface', 'view partial'); - $this->_paginator->render(new View\Renderer\PhpRenderer()); + $this->paginator->render(new View\Renderer\PhpRenderer()); } public function testGetsAndSetsPageRange() { - $this->assertEquals(10, $this->_paginator->getPageRange()); - $this->_paginator->setPageRange(15); - $this->assertEquals(15, $this->_paginator->getPageRange()); + $this->assertEquals(10, $this->paginator->getPageRange()); + $this->paginator->setPageRange(15); + $this->assertEquals(15, $this->paginator->getPageRange()); } /** @@ -560,16 +551,16 @@ public function testKeepsCurrentPageNumberAfterItemCountPerPageSet() public function testCastsIntegerValuesToInteger() { // Current page number - $this->_paginator->setCurrentPageNumber(3.3); - $this->assertTrue($this->_paginator->getCurrentPageNumber() == 3); + $this->paginator->setCurrentPageNumber(3.3); + $this->assertTrue($this->paginator->getCurrentPageNumber() == 3); // Item count per page - $this->_paginator->setItemCountPerPage(3.3); - $this->assertTrue($this->_paginator->getItemCountPerPage() == 3); + $this->paginator->setItemCountPerPage(3.3); + $this->assertTrue($this->paginator->getItemCountPerPage() == 3); // Page range - $this->_paginator->setPageRange(3.3); - $this->assertTrue($this->_paginator->getPageRange() == 3); + $this->paginator->setPageRange(3.3); + $this->assertTrue($this->paginator->getPageRange() == 3); } /** @@ -583,11 +574,11 @@ public function testAcceptsTraversableInstanceFromAdapter() public function testCachedItem() { - $this->_paginator->setCurrentPageNumber(1)->getCurrentItems(); - $this->_paginator->setCurrentPageNumber(2)->getCurrentItems(); - $this->_paginator->setCurrentPageNumber(3)->getCurrentItems(); + $this->paginator->setCurrentPageNumber(1)->getCurrentItems(); + $this->paginator->setCurrentPageNumber(2)->getCurrentItems(); + $this->paginator->setCurrentPageNumber(3)->getCurrentItems(); - $pageItems = $this->_paginator->getPageItemCache(); + $pageItems = $this->paginator->getPageItemCache(); $expected = array( 1 => new \ArrayIterator(range(1, 10)), 2 => new \ArrayIterator(range(11, 20)), @@ -598,14 +589,13 @@ public function testCachedItem() public function testClearPageItemCache() { - $this->markTestIncomplete('Will skip until Zend\Db is refactored.'); - $this->_paginator->setCurrentPageNumber(1)->getCurrentItems(); - $this->_paginator->setCurrentPageNumber(2)->getCurrentItems(); - $this->_paginator->setCurrentPageNumber(3)->getCurrentItems(); + $this->paginator->setCurrentPageNumber(1)->getCurrentItems(); + $this->paginator->setCurrentPageNumber(2)->getCurrentItems(); + $this->paginator->setCurrentPageNumber(3)->getCurrentItems(); // clear only page 2 items - $this->_paginator->clearPageItemCache(2); - $pageItems = $this->_paginator->getPageItemCache(); + $this->paginator->clearPageItemCache(2); + $pageItems = $this->paginator->getPageItemCache(); $expected = array( 1 => new \ArrayIterator(range(1, 10)), 3 => new \ArrayIterator(range(21, 30)) @@ -613,48 +603,48 @@ public function testClearPageItemCache() $this->assertEquals($expected, $pageItems); // clear all - $this->_paginator->clearPageItemCache(); - $pageItems = $this->_paginator->getPageItemCache(); + $this->paginator->clearPageItemCache(); + $pageItems = $this->paginator->getPageItemCache(); $this->assertEquals(array(), $pageItems); } public function testWithCacheDisabled() { - $this->_paginator->setCacheEnabled(false); - $this->_paginator->setCurrentPageNumber(1)->getCurrentItems(); + $this->paginator->setCacheEnabled(false); + $this->paginator->setCurrentPageNumber(1)->getCurrentItems(); - $cachedPageItems = $this->_paginator->getPageItemCache(); + $cachedPageItems = $this->paginator->getPageItemCache(); $expected = new \ArrayIterator(range(1, 10)); $this->assertEquals(array(), $cachedPageItems); - $pageItems = $this->_paginator->getCurrentItems(); + $pageItems = $this->paginator->getCurrentItems(); $this->assertEquals($expected, $pageItems); } public function testCacheDoesNotDisturbResultsWhenChangingParam() { - $this->_paginator->setCurrentPageNumber(1)->getCurrentItems(); - $pageItems = $this->_paginator->setItemCountPerPage(5)->getCurrentItems(); + $this->paginator->setCurrentPageNumber(1)->getCurrentItems(); + $pageItems = $this->paginator->setItemCountPerPage(5)->getCurrentItems(); $expected = new \ArrayIterator(range(1, 5)); $this->assertEquals($expected, $pageItems); - $pageItems = $this->_paginator->getItemsByPage(2); + $pageItems = $this->paginator->getItemsByPage(2); $expected = new \ArrayIterator(range(6, 10)); $this->assertEquals($expected, $pageItems); // change the inside Paginator scale - $pageItems = $this->_paginator->setItemCountPerPage(8)->setCurrentPageNumber(3)->getCurrentItems(); + $pageItems = $this->paginator->setItemCountPerPage(8)->setCurrentPageNumber(3)->getCurrentItems(); - $pageItems = $this->_paginator->getPageItemCache(); + $pageItems = $this->paginator->getPageItemCache(); $expected = /*array(3 => */ new \ArrayIterator(range(17, 24)) /*) */; $this->assertEquals($expected, $pageItems[3]); // get back to already cached data - $this->_paginator->setItemCountPerPage(5); - $pageItems = $this->_paginator->getPageItemCache(); + $this->paginator->setItemCountPerPage(5); + $pageItems = $this->paginator->getPageItemCache(); $expected =array(1 => new \ArrayIterator(range(1, 5)), 2 => new \ArrayIterator(range(6, 10))); $this->assertEquals($expected[1], $pageItems[1]); @@ -663,9 +653,9 @@ public function testCacheDoesNotDisturbResultsWhenChangingParam() public function testToJson() { - $this->_paginator->setCurrentPageNumber(1); + $this->paginator->setCurrentPageNumber(1); - $json = $this->_paginator->toJson(); + $json = $this->paginator->toJson(); $expected = '"0":1,"1":2,"2":3,"3":4,"4":5,"5":6,"6":7,"7":8,"8":9,"9":10'; @@ -728,9 +718,9 @@ public function testItemCountPerPageByDefault() */ public function testNegativeItemNumbers() { - $this->assertEquals(10, $this->_paginator->getItem(-1, 1)); - $this->assertEquals(9, $this->_paginator->getItem(-2, 1)); - $this->assertEquals(101, $this->_paginator->getItem(-1, -1)); + $this->assertEquals(10, $this->paginator->getItem(-1, 1)); + $this->assertEquals(9, $this->paginator->getItem(-2, 1)); + $this->assertEquals(101, $this->paginator->getItem(-1, -1)); } /** @@ -793,7 +783,7 @@ public function testSetAdapterPluginManagerWithStringThrowsInvalidArgumentExcept 'Zend\Paginator\Exception\InvalidArgumentException', 'Unable to locate adapter plugin manager with class "invalid adapter"; class not found' ); - $this->_paginator->setAdapterPluginManager('invalid adapter'); + $this->paginator->setAdapterPluginManager('invalid adapter'); } public function testSetAdapterPluginManagerWithAdapterThrowsInvalidArgumentException() @@ -803,7 +793,7 @@ public function testSetAdapterPluginManagerWithAdapterThrowsInvalidArgumentExcep 'Pagination adapter manager must extend AdapterPluginManager; received "stdClass"' ); - $this->_paginator->setAdapterPluginManager( + $this->paginator->setAdapterPluginManager( new stdClass() ); } @@ -815,7 +805,7 @@ public function testSetAdapterPluginManagerWithAdaptersThrowsInvalidArgumentExce 'Pagination adapter manager must extend AdapterPluginManager; received "array"' ); - $this->_paginator->setAdapterPluginManager( + $this->paginator->setAdapterPluginManager( array( new stdClass() ) @@ -829,7 +819,7 @@ public function testSetOptionsThrowsInvalidArgumentException() 'setOptions expects an array or Traversable' ); - $this->_paginator->setOptions('not array'); + $this->paginator->setOptions('not array'); } public function testSetScrollingStylePluginManagerWithStringThrowsInvalidArgumentException() @@ -839,7 +829,7 @@ public function testSetScrollingStylePluginManagerWithStringThrowsInvalidArgumen 'Unable to locate scrolling style plugin manager with class "invalid adapter"; class not found' ); - $this->_paginator->setScrollingStylePluginManager('invalid adapter'); + $this->paginator->setScrollingStylePluginManager('invalid adapter'); } public function testSetScrollingStylePluginManagerWithAdapterThrowsInvalidArgumentException() @@ -849,7 +839,7 @@ public function testSetScrollingStylePluginManagerWithAdapterThrowsInvalidArgume 'Pagination scrolling-style manager must extend ScrollingStylePluginManager; received "stdClass"' ); - $this->_paginator->setScrollingStylePluginManager( + $this->paginator->setScrollingStylePluginManager( new stdClass() ); } diff --git a/test/_files/TestTable.php b/test/_files/TestTable.php deleted file mode 100644 index f17a95b..0000000 --- a/test/_files/TestTable.php +++ /dev/null @@ -1,18 +0,0 @@ -