Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'hotfix/paginator-db-count-subselect' of https://github.…

…com/ralphschindler/zf2 into develop

PR #4641
Closes #4590
Closes #3647
  • Loading branch information...
commit 462d3c4b300305fdd2a94b4e4616c5b0dcbc2d0c 2 parents 44b73b0 + e04fb2a
@EvanDotPro EvanDotPro authored
View
15 library/Zend/Paginator/Adapter/DbSelect.php
@@ -103,22 +103,15 @@ public function count()
}
$select = clone $this->select;
- $select->reset(Select::COLUMNS);
$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();
View
47 tests/ZendTest/Paginator/Adapter/DbSelectTest.php
@@ -25,19 +25,31 @@ 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');
$mockResult = $this->getMock('Zend\Db\Adapter\Driver\ResultInterface');
+ $this->mockResult = $mockResult;
+
+ $mockStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface');
+ $this->mockStatement = $mockStatement;
+
+ $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(
@@ -45,9 +57,21 @@ public function setup()
array($mockDriver, $mockPlatform)
);
+ $mockSql = $this->getMock(
+ 'Zend\Db\Sql\Sql',
+ 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, $mockAdapter);
+
+ $this->dbSelect = new DbSelect($this->mockSelect, $mockSql);
}
public function testGetItems()
@@ -60,18 +84,9 @@ 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->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);
Please sign in to comment.
Something went wrong with that request. Please try again.