From 8670b71c5f2716465091e218520354e8ec5b5260 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20Bru=CC=88ckner?= Date: Thu, 18 Jun 2020 21:37:06 +0200 Subject: [PATCH] Fixes #3 - _getCacheInternalId() is too generic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Frank Brückner --- src/Adapter/DbSelect.php | 16 ++++++++- test/PaginatorTest.php | 50 ++++++++++++++++++++++++++ test/TestAsset/TestDbSelectAdapter.php | 30 ++++++++++++++++ 3 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 test/TestAsset/TestDbSelectAdapter.php diff --git a/src/Adapter/DbSelect.php b/src/Adapter/DbSelect.php index a6a6212b..1e6fe42a 100644 --- a/src/Adapter/DbSelect.php +++ b/src/Adapter/DbSelect.php @@ -8,6 +8,7 @@ namespace Laminas\Paginator\Adapter; +use JsonSerializable; use Laminas\Db\Adapter\Adapter; use Laminas\Db\ResultSet\ResultSet; use Laminas\Db\ResultSet\ResultSetInterface; @@ -15,7 +16,7 @@ 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'; @@ -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() + ), + ]; + } } diff --git a/test/PaginatorTest.php b/test/PaginatorTest.php index e653ea2c..a1bae952 100644 --- a/test/PaginatorTest.php +++ b/test/PaginatorTest.php @@ -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( diff --git a/test/TestAsset/TestDbSelectAdapter.php b/test/TestAsset/TestDbSelectAdapter.php new file mode 100644 index 00000000..b85304d0 --- /dev/null +++ b/test/TestAsset/TestDbSelectAdapter.php @@ -0,0 +1,30 @@ +