Skip to content

Commit

Permalink
Fixes #3 - _getCacheInternalId() is too generic
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Brückner <[email protected]>
  • Loading branch information
froschdesign committed Jun 18, 2020
1 parent 1e7334a commit 8670b71
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Adapter/DbSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@

namespace Laminas\Paginator\Adapter;

use JsonSerializable;
use Laminas\Db\Adapter\Adapter;
use Laminas\Db\ResultSet\ResultSet;
use Laminas\Db\ResultSet\ResultSetInterface;
use Laminas\Db\Sql\Expression;
use Laminas\Db\Sql\Select;
use Laminas\Db\Sql\Sql;

class DbSelect implements AdapterInterface
class DbSelect implements AdapterInterface, JsonSerializable
{
const ROW_COUNT_COLUMN_NAME = 'C';

Expand Down Expand Up @@ -150,4 +151,17 @@ protected function getSelectCount()

return $countSelect;
}

/**
* @internal
*/
public function jsonSerialize()
{
return [
'select' => $this->sql->buildSqlString($this->select),
'count_select' => $this->sql->buildSqlString(
$this->getSelectCount()
),
];
}
}
50 changes: 50 additions & 0 deletions test/PaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,56 @@ public function testGetCacheIdWithInheritedClass()
$this->assertNotEquals($firstOutputGetCacheInternalId, $secondOutputGetCacheInternalId);
}

public function testDbSelectAdapterShouldProduceValidCacheId()
{
// Create first interal cache ID
$paginator = new Paginator\Paginator(
new TestAsset\TestDbSelectAdapter(
(new Sql\Select('table1'))
->where('id = 1')
->where("foo = 'bar'"),
new DbAdapter\Adapter(
new DbAdapter\Driver\Pdo\Pdo(
new DbAdapter\Driver\Pdo\Connection([])
)
)
)
);
$reflectionGetCacheInternalId = new ReflectionMethod(
$paginator,
'_getCacheInternalId'
);
$reflectionGetCacheInternalId->setAccessible(true);
$firstCacheId = $reflectionGetCacheInternalId->invoke(
$paginator
);

// Create second internal cache ID
$paginator = new Paginator\Paginator(
new TestAsset\TestDbSelectAdapter(
(new Sql\Select('table2'))
->where('id = 2')
->where("foo = 'bar'"),
new DbAdapter\Adapter(
new DbAdapter\Driver\Pdo\Pdo(
new DbAdapter\Driver\Pdo\Connection([])
)
)
)
);
$reflectionGetCacheInternalId = new ReflectionMethod(
$paginator,
'_getCacheInternalId'
);
$reflectionGetCacheInternalId->setAccessible(true);
$secondCacheIde = $reflectionGetCacheInternalId->invoke(
$paginator
);

// Test
$this->assertNotEquals($firstCacheId, $secondCacheIde);
}

public function testAcceptsComplexAdapters()
{
$paginator = new Paginator\Paginator(
Expand Down
30 changes: 30 additions & 0 deletions test/TestAsset/TestDbSelectAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/**
* @see https://github.com/laminas/laminas-paginator for the canonical source repository
* @copyright https://github.com/laminas/laminas-paginator/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-paginator/blob/master/LICENSE.md New BSD License
*/

namespace LaminasTest\Paginator\TestAsset;

use Laminas\Paginator\Adapter\DbSelect;

class TestDbSelectAdapter extends DbSelect
{
/**
* @inheritDoc
*/
public function count()
{
return 10;
}

/**
* @inheritDoc
*/
public function getItems($pageNumber, $itemCountPerPage)
{
return range(1, 10);
}
}

0 comments on commit 8670b71

Please sign in to comment.