From c49d65908cfc5be1f5070fba65b8b820bdbdd9c9 Mon Sep 17 00:00:00 2001 From: Ralph Schindler Date: Wed, 12 Jun 2013 12:28:45 -0500 Subject: [PATCH 1/4] Zend\Paginator\Adapter\DbSelect First attempt at using subselect to calculate full count --- library/Zend/Paginator/Adapter/DbSelect.php | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/library/Zend/Paginator/Adapter/DbSelect.php b/library/Zend/Paginator/Adapter/DbSelect.php index 92c7b532810..b6f6e821642 100644 --- a/library/Zend/Paginator/Adapter/DbSelect.php +++ b/library/Zend/Paginator/Adapter/DbSelect.php @@ -107,18 +107,12 @@ public function count() $select->reset(Select::LIMIT); $select->reset(Select::OFFSET); $select->reset(Select::ORDER); - $select->reset(Select::GROUP); - // get join information, clear, and repopulate without columns - $joins = $select->getRawState(Select::JOINS); - $select->reset(Select::JOINS); - foreach ($joins as $join) { - $select->join($join['name'], $join['on'], array(), $join['type']); - } - - $select->columns(array('c' => new Expression('COUNT(1)'))); + $countSelect = new Select; + $countSelect->columns(array('c' => new Expression('COUNT(1)'))); + $countSelect->from(array('original_select' => $select)); - $statement = $this->sql->prepareStatementForSqlObject($select); + $statement = $this->sql->prepareStatementForSqlObject($countSelect); $result = $statement->execute(); $row = $result->current(); From 570778c77eb7fbc6a9c806ef97c57a72121e000c Mon Sep 17 00:00:00 2001 From: Ralph Schindler Date: Wed, 12 Jun 2013 13:27:40 -0500 Subject: [PATCH 2/4] Zend\Db & Zend\Paginator - Paginator: removed clearing of columns from select - Db\Sql\Select: when resettings columns, reset value should be array(self::SQL_STAR) --- library/Zend/Db/Sql/Select.php | 2 +- library/Zend/Paginator/Adapter/DbSelect.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/library/Zend/Db/Sql/Select.php b/library/Zend/Db/Sql/Select.php index dea84756c09..b0d36afd5ea 100644 --- a/library/Zend/Db/Sql/Select.php +++ b/library/Zend/Db/Sql/Select.php @@ -478,7 +478,7 @@ public function reset($part) $this->quantifier = null; break; case self::COLUMNS: - $this->columns = array(); + $this->columns = array(self::SQL_STAR); break; case self::JOINS: $this->joins = array(); diff --git a/library/Zend/Paginator/Adapter/DbSelect.php b/library/Zend/Paginator/Adapter/DbSelect.php index b6f6e821642..c9ade7292ba 100644 --- a/library/Zend/Paginator/Adapter/DbSelect.php +++ b/library/Zend/Paginator/Adapter/DbSelect.php @@ -103,7 +103,6 @@ public function count() } $select = clone $this->select; - $select->reset(Select::COLUMNS); $select->reset(Select::LIMIT); $select->reset(Select::OFFSET); $select->reset(Select::ORDER); From fb5002519c513e26c78dc78c423594203722c159 Mon Sep 17 00:00:00 2001 From: Ralph Schindler Date: Wed, 12 Jun 2013 14:07:05 -0500 Subject: [PATCH 3/4] Zend\Paginator - added tests for DbSelect Zend\Db\Sql\Select - reverted reset on column --- library/Zend/Db/Sql/Select.php | 2 +- .../Paginator/Adapter/DbSelectTest.php | 38 ++++++++++++------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/library/Zend/Db/Sql/Select.php b/library/Zend/Db/Sql/Select.php index b0d36afd5ea..dea84756c09 100644 --- a/library/Zend/Db/Sql/Select.php +++ b/library/Zend/Db/Sql/Select.php @@ -478,7 +478,7 @@ public function reset($part) $this->quantifier = null; break; case self::COLUMNS: - $this->columns = array(self::SQL_STAR); + $this->columns = array(); break; case self::JOINS: $this->joins = array(); diff --git a/tests/ZendTest/Paginator/Adapter/DbSelectTest.php b/tests/ZendTest/Paginator/Adapter/DbSelectTest.php index ac21f28d5e8..30002ef4adc 100644 --- a/tests/ZendTest/Paginator/Adapter/DbSelectTest.php +++ b/tests/ZendTest/Paginator/Adapter/DbSelectTest.php @@ -25,14 +25,23 @@ class DbSelectTest extends \PHPUnit_Framework_TestCase /** @var \PHPUnit_Framework_MockObject_MockObject */ protected $mockSelect; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $mockStatement; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ protected $mockResult; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $mockSql; + /** @var DbSelect */ protected $dbSelect; public function setup() { $mockStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface'); + $this->mockStatement = $mockStatement; + $mockResult = $this->getMock('Zend\Db\Adapter\Driver\ResultInterface'); $mockDriver = $this->getMock('Zend\Db\Adapter\Driver\DriverInterface'); @@ -45,9 +54,16 @@ public function setup() array($mockDriver, $mockPlatform) ); + $mockSql = $this->getMock( + 'Zend\Db\Sql\Sql', + array('prepareStatementForSqlObject', 'execute'), + array($mockAdapter) + ); + + $this->mockSql = $mockSql; $this->mockSelect = $this->getMock('Zend\Db\Sql\Select'); $this->mockResult = $mockResult; - $this->dbSelect = new DbSelect($this->mockSelect, $mockAdapter); + $this->dbSelect = new DbSelect($this->mockSelect, $mockSql); } public function testGetItems() @@ -60,18 +76,14 @@ public function testGetItems() public function testCount() { - $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))); - - $this->mockSelect->expects($this->exactly(6))->method('reset'); // called for columns, limit, offset, order - $this->mockSelect->expects($this->once())->method('getRawState')->with($this->equalTo(Select::JOINS)) - ->will($this->returnValue(array(array('name' => 'Foo', 'on' => 'On Stuff', 'columns' => array('foo', 'bar'), 'type' => Select::JOIN_INNER)))); - $this->mockSelect->expects($this->once())->method('join')->with( - 'Foo', - 'On Stuff', - array(), - Select::JOIN_INNER - ); + $this->mockSql->expects($this->once()) + ->method('prepareStatementForSqlObject') + ->with($this->isInstanceOf('Zend\Db\Sql\Select')) + ->will($this->returnValue($this->mockStatement)); + $this->mockSql->expects($this->any())->method('execute')->will($this->returnValue($this->mockResult)); + $this->mockResult->expects($this->once())->method('current')->will($this->returnValue(array('c' => 5))); + + $this->mockSelect->expects($this->exactly(3))->method('reset'); // called for columns, limit, offset, order $count = $this->dbSelect->count(); $this->assertEquals(5, $count); From e04fb2a3334900e3ae4c318509b9c145fd80dd12 Mon Sep 17 00:00:00 2001 From: Ralph Schindler Date: Wed, 12 Jun 2013 14:20:11 -0500 Subject: [PATCH 4/4] Zend\Paginator - reorganized the Mock setup for getItems and count tests to work --- .../Paginator/Adapter/DbSelectTest.php | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/tests/ZendTest/Paginator/Adapter/DbSelectTest.php b/tests/ZendTest/Paginator/Adapter/DbSelectTest.php index 30002ef4adc..599cc29ad5e 100644 --- a/tests/ZendTest/Paginator/Adapter/DbSelectTest.php +++ b/tests/ZendTest/Paginator/Adapter/DbSelectTest.php @@ -39,14 +39,17 @@ class DbSelectTest extends \PHPUnit_Framework_TestCase public function setup() { + $mockResult = $this->getMock('Zend\Db\Adapter\Driver\ResultInterface'); + $this->mockResult = $mockResult; + $mockStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface'); $this->mockStatement = $mockStatement; - $mockResult = $this->getMock('Zend\Db\Adapter\Driver\ResultInterface'); + $this->mockStatement->expects($this->any())->method('execute')->will($this->returnValue($this->mockResult)); $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( @@ -59,10 +62,15 @@ public function setup() array('prepareStatementForSqlObject', 'execute'), array($mockAdapter) ); - $this->mockSql = $mockSql; + $this->mockSql->expects($this->once()) + ->method('prepareStatementForSqlObject') + ->with($this->isInstanceOf('Zend\Db\Sql\Select')) + ->will($this->returnValue($this->mockStatement)); + + $this->mockSelect = $this->getMock('Zend\Db\Sql\Select'); - $this->mockResult = $mockResult; + $this->dbSelect = new DbSelect($this->mockSelect, $mockSql); } @@ -76,11 +84,6 @@ public function testGetItems() public function testCount() { - $this->mockSql->expects($this->once()) - ->method('prepareStatementForSqlObject') - ->with($this->isInstanceOf('Zend\Db\Sql\Select')) - ->will($this->returnValue($this->mockStatement)); - $this->mockSql->expects($this->any())->method('execute')->will($this->returnValue($this->mockResult)); $this->mockResult->expects($this->once())->method('current')->will($this->returnValue(array('c' => 5))); $this->mockSelect->expects($this->exactly(3))->method('reset'); // called for columns, limit, offset, order