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

Commit

Permalink
Merge branch 'hotfix/paginator-db-count-subselect' of https://github.…
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanDotPro committed Jun 12, 2013
158 parents 177d204 + 2630a49 + 43cba4d + e4204b5 + d05ccf2 + e604cfb + ca97c01 + 287eb63 + 51a49db + a59e5dc + 548a07d + 8505b4c + 2716d1a + 37045c3 + 6903d5f + cdad453 + 9db7df2 + 9bb9d9a + 41b2dc0 + 5295cf8 + 27f9b6d + b273434 + 5b111b1 + ee4a022 + 5130ae3 + aff706f + f73a53d + d4a913a + b29f072 + 651a727 + 430d154 + 1839a34 + 0d5d64e + 5cde84e + dc8ee4d + 85d900a + 887c312 + 9c949ae + 0fc5669 + e29d393 + e25c698 + ca5bdbb + 18e54b8 + 73cc878 + a514416 + 778823f + fff1957 + 35beb1b + ecb2a1a + 67a31fa + c0ee14a + 7273469 + 62e104e + 293b0d0 + c59092f + b3e8431 + 615f1cb + 198a3b9 + 1630584 + 7627438 + 84a5bf6 + ad86c9d + a84a8ca + 4198a62 + e822871 + 431867b + 200dbe0 + 7277f0b + 2408da8 + 2413f67 + 4731ffc + d177c96 + b27c24b + ec13154 + 8e7b30c + 6c4437a + 2a2ed86 + 366d656 + 5f3d390 + 666d47b + dd0e03b + 1a5602a + c01cd9c + 6849552 + 8b79ca2 + e84f0d1 + 9f1b34e + c453b3e + 3d74be7 + 999502e + a46d96f + fba87c0 + ce665da + ef2dd94 + a66c89a + b997097 + da0e97d + 1302f0b + a1bbaa9 + 6bfa503 + b4b9ece + 9b1ac2d + 30880b8 + e308f6d + 8da89be + 199755f + 2310cd0 + 4e7316f + 1d6c476 + c2593db + c6ff37b + 5344902 + a35c230 + 39052a9 + 8152f2f + e0f8777 + d1f068b + 006fb88 + 6261074 + 8bf6181 + 33e79d4 + a778f16 + 57199ed + 3653e0c + 5b5a61e + 082fd49 + d56f4bf + d4ad9b4 + c2442b4 + a56d05b + 7400cfb + 8794fff + 0877be7 + 9c75f77 + 261c157 + 94f1dd4 + ae538e5 + bf68265 + c222657 + 56e71f6 + b95fff7 + 7736317 + 8a3b848 + e18ea15 + ae1a095 + a18a97f + c3ef55c + 09f0424 + 6369f0d + bbce3db + e7612d4 + af38e3f + cd3f4d4 + 498edd0 + a792bd9 + 9922da0 + 350dc8b + baeb3ad commit 622be5c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 27 deletions.
15 changes: 4 additions & 11 deletions src/Adapter/DbSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
47 changes: 31 additions & 16 deletions test/Adapter/DbSelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,53 @@ 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(
'Zend\Db\Adapter\Adapter',
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()
Expand All @@ -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);
Expand Down

0 comments on commit 622be5c

Please sign in to comment.