Skip to content

Commit

Permalink
simplify test with add smaller "c" column in columns list
Browse files Browse the repository at this point in the history
Signed-off-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
  • Loading branch information
samsonasik committed Aug 6, 2020
1 parent c22baae commit 17fc4a4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 28 deletions.
26 changes: 7 additions & 19 deletions src/Adapter/DbSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
namespace Laminas\Paginator\Adapter;

use Laminas\Db\Adapter\Adapter;
use Laminas\Db\Adapter\Driver\Pdo\Pdo;
use Laminas\Db\ResultSet\ResultSet;
use Laminas\Db\ResultSet\ResultSetInterface;
use Laminas\Db\Sql\Expression;
Expand All @@ -19,7 +18,6 @@
class DbSelect implements AdapterInterface
{
const ROW_COUNT_COLUMN_NAME = 'C';
const ROW_COUNT_COLUMN_NAME_SMALL = 'c';

/**
* @var Sql
Expand Down Expand Up @@ -124,7 +122,9 @@ public function count()
$result = $statement->execute();
$row = $result->current();

$this->rowCount = (int) $row[$this->getRowCountColumnName()];
$this->rowCount = (int) isset($row[self::ROW_COUNT_COLUMN_NAME])
? $row[self::ROW_COUNT_COLUMN_NAME]
: $row['c'];

return $this->rowCount;
}
Expand All @@ -147,7 +147,10 @@ protected function getSelectCount()

$countSelect = new Select;

$countSelect->columns([$this->getRowCountColumnName() => new Expression('COUNT(1)')]);
$countSelect->columns([
self::ROW_COUNT_COLUMN_NAME => new Expression('COUNT(1)'),
'c' => new Expression('COUNT(1)')
]);
$countSelect->from(['original_select' => $select]);

return $countSelect;
Expand All @@ -167,19 +170,4 @@ public function getArrayCopy()
),
];
}

private function getRowCountColumnName()
{
$driver = $this->sql->getAdapter()->getDriver();
if (get_class($driver) !== Pdo::class) {
return self::ROW_COUNT_COLUMN_NAME;
}

$attrCase = $driver->getConnection()
->getResource()
->getAttribute(\PDO::ATTR_CASE);
return $attrCase !== \PDO::CASE_LOWER
? self::ROW_COUNT_COLUMN_NAME
: self::ROW_COUNT_COLUMN_NAME_SMALL;
}
}
21 changes: 12 additions & 9 deletions test/Adapter/DbSelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,6 @@ public function setUp()
->with($this->isInstanceOf('Laminas\Db\Sql\Select'))
->will($this->returnValue($this->mockStatement));

$this->mockAdapter = $this->getMockBuilder('Laminas\Db\Adapter\AdapterInterface')->getMock();
$this->mockAdapter->expects($this->any())
->method('getDriver')
->will($this->returnValue($mockDriver));
$this->mockSql
->expects($this->any())
->method('getAdapter')
->will($this->returnValue($this->mockAdapter));

$this->mockSelect = $this->createMock('Laminas\Db\Sql\Select');
$this->mockSelectCount = $this->createMock('Laminas\Db\Sql\Select');
$this->dbSelect = new DbSelect($this->mockSelect, $this->mockSql);
Expand All @@ -99,6 +90,18 @@ public function testCount()
$this->assertEquals(5, $count);
}

public function testCountQueryWithLowerColumnNameShouldReturnValidResult()
{
$this->dbSelect = new DbSelect($this->mockSelect, $this->mockSql);
$this->mockResult
->expects($this->once())
->method('current')
->will($this->returnValue(['c' => 7]));

$count = $this->dbSelect->count();
$this->assertEquals(7, $count);
}

public function testCustomCount()
{
$this->dbSelect = new DbSelect($this->mockSelect, $this->mockSql, null, $this->mockSelectCount);
Expand Down

0 comments on commit 17fc4a4

Please sign in to comment.