From f274afc494d0828b5e0ae746b33d631e9bbbea12 Mon Sep 17 00:00:00 2001 From: mariojrrc Date: Fri, 2 Feb 2018 11:10:41 -0200 Subject: [PATCH 1/2] fix issue #43 _getCacheInterlaId too generic --- src/Adapter/AdapterInterface.php | 7 ++ src/Adapter/ArrayAdapter.php | 10 +++ src/Adapter/Callback.php | 10 +++ src/Adapter/DbSelect.php | 8 ++ src/Adapter/Iterator.php | 10 +++ src/Adapter/NullFill.php | 10 +++ src/Paginator.php | 2 +- test/PaginatorTest.php | 87 +++++++++++++++++++ test/TestAsset/TestAdapter.php | 10 +++ test/TestAsset/TestDbSelectAdapterCount10.php | 23 +++++ test/TestAsset/TestDbSelectAdapterCount5.php | 23 +++++ 11 files changed, 199 insertions(+), 1 deletion(-) create mode 100644 test/TestAsset/TestDbSelectAdapterCount10.php create mode 100644 test/TestAsset/TestDbSelectAdapterCount5.php diff --git a/src/Adapter/AdapterInterface.php b/src/Adapter/AdapterInterface.php index 513517e..ac01f56 100644 --- a/src/Adapter/AdapterInterface.php +++ b/src/Adapter/AdapterInterface.php @@ -24,4 +24,11 @@ interface AdapterInterface extends Countable * @return array */ public function getItems($offset, $itemCountPerPage); + + /** + * Returns the internal cache id + * + * @return string + */ + public function getCacheInternalId(); } diff --git a/src/Adapter/ArrayAdapter.php b/src/Adapter/ArrayAdapter.php index 3044820..8e01fc1 100644 --- a/src/Adapter/ArrayAdapter.php +++ b/src/Adapter/ArrayAdapter.php @@ -57,4 +57,14 @@ public function count() { return $this->count; } + + /** + * Returns the internal cache id + * + * @return string + */ + public function getCacheInternalId() + { + return json_encode($this); + } } diff --git a/src/Adapter/Callback.php b/src/Adapter/Callback.php index 523a1b4..51fd3e4 100644 --- a/src/Adapter/Callback.php +++ b/src/Adapter/Callback.php @@ -62,4 +62,14 @@ public function count() { return call_user_func($this->countCallback); } + + /** + * Returns the internal cache id + * + * @return string + */ + public function getCacheInternalId() + { + return json_encode($this); + } } diff --git a/src/Adapter/DbSelect.php b/src/Adapter/DbSelect.php index ff2db0e..154867d 100644 --- a/src/Adapter/DbSelect.php +++ b/src/Adapter/DbSelect.php @@ -151,4 +151,12 @@ protected function getSelectCount() return $countSelect; } + + /** + * @return string + */ + public function getCacheInternalId() + { + return hash('sha512', $this->select->getSqlString()); + } } diff --git a/src/Adapter/Iterator.php b/src/Adapter/Iterator.php index d01f7f2..abf1102 100644 --- a/src/Adapter/Iterator.php +++ b/src/Adapter/Iterator.php @@ -68,4 +68,14 @@ public function count() { return $this->count; } + + /** + * Returns the internal cache id + * + * @return string + */ + public function getCacheInternalId() + { + return json_encode($this); + } } diff --git a/src/Adapter/NullFill.php b/src/Adapter/NullFill.php index b343d27..1de5d33 100644 --- a/src/Adapter/NullFill.php +++ b/src/Adapter/NullFill.php @@ -56,4 +56,14 @@ public function count() { return $this->count; } + + /** + * Returns the internal cache id + * + * @return string + */ + public function getCacheInternalId() + { + return json_encode($this); + } } diff --git a/src/Paginator.php b/src/Paginator.php index da85902..5bb959f 100644 --- a/src/Paginator.php +++ b/src/Paginator.php @@ -865,7 +865,7 @@ protected function _getCacheInternalId() // @codingStandardsIgnoreEnd return md5( get_class($this->getAdapter()) - . json_encode($this->getAdapter()) + . $this->getAdapter()->getCacheInternalId() . $this->getItemCountPerPage() ); } diff --git a/test/PaginatorTest.php b/test/PaginatorTest.php index 006b4de..c75213b 100644 --- a/test/PaginatorTest.php +++ b/test/PaginatorTest.php @@ -945,6 +945,93 @@ public function testAcceptsComplexAdapters() $this->assertInstanceOf('ArrayObject', $paginator->getCurrentItems()); } + /** + * This piece if code test the failure in determine different cache_id for different queries when using the + * generic TestAdapter. All the results will hit the same cache_id when simulating DbSelectAdapterObject. + */ + public function testDbSelectAdapterLikeFailure() + { + $paginator = new Paginator\Paginator( + new TestAsset\TestAdapter(function () { + // Simulate some "real" params + return [ + 'driver' => [ + 'name' => 'mysql', + 'uptime' => new \DateTime('Y-m-d H:i:s'), + ], + 'sql' => 'Select * FROM table1 where ID = 1' + ]; + }) + ); + + $reflectionGetCacheInternalId = new ReflectionMethod($paginator, '_getCacheInternalId'); + $reflectionGetCacheInternalId->setAccessible(true); + $firstOutputGetCacheInternalId = $reflectionGetCacheInternalId->invoke($paginator); + + sleep(1); + + $paginator = new Paginator\Paginator( + new TestAsset\TestAdapter(function () { + return [ + // Simulate some "real" params + 'driver' => [ + 'name' => 'mysql', + 'uptime' => new \DateTime('Y-m-d H:i:s'), + ], + 'sql' => 'Select * FROM table2 where ID = 2' + ]; + }) + ); + $reflectionGetCacheInternalId = new ReflectionMethod($paginator, '_getCacheInternalId'); + $reflectionGetCacheInternalId->setAccessible(true); + $secondOutputGetCacheInternalId = $reflectionGetCacheInternalId->invoke($paginator); + + $this->assertEquals($firstOutputGetCacheInternalId, $secondOutputGetCacheInternalId); + + $this->assertInstanceOf('ArrayObject', $paginator->getCurrentItems()); + } + + /** + * This piece if code test the success in determine different cache_id for different queries. + */ + public function testDbSelectAdapterLikeSuccess() + { + $select = new Sql\Select('table1'); + $select->where('id = 1'); + $select->where('nick = \'test\''); + $paginator = new Paginator\Paginator( + new TestAsset\TestDbSelectAdapterCount10($select, + new DbAdapter\Adapter( + new DbAdapter\Driver\Pdo\Pdo( + new DbAdapter\Driver\Pdo\Connection([ + ])))) + ); + + $reflectionGetCacheInternalId = new ReflectionMethod($paginator, '_getCacheInternalId'); + $reflectionGetCacheInternalId->setAccessible(true); + $firstOutputGetCacheInternalId = $reflectionGetCacheInternalId->invoke($paginator); + $this->assertCount(10, $paginator->getCurrentItems()); + $this->assertInstanceOf('ArrayObject', $paginator->getCurrentItems()); + + $select = new Sql\Select('table2'); + $select->where('id = 2'); + $select->where('nick = \'test\''); + $paginator = new Paginator\Paginator( + new TestAsset\TestDbSelectAdapterCount5($select, + new DbAdapter\Adapter( + new DbAdapter\Driver\Pdo\Pdo( + new DbAdapter\Driver\Pdo\Connection()))) + ); + $reflectionGetCacheInternalId = new ReflectionMethod($paginator, '_getCacheInternalId'); + $reflectionGetCacheInternalId->setAccessible(true); + $secondOutputGetCacheInternalId = $reflectionGetCacheInternalId->invoke($paginator); + + $this->assertInstanceOf('ArrayObject', $paginator->getCurrentItems()); + $this->assertCount(5, $paginator->getCurrentItems()); + + $this->assertNotEquals($firstOutputGetCacheInternalId, $secondOutputGetCacheInternalId); + } + /** * @group 6808 * @group 6809 diff --git a/test/TestAsset/TestAdapter.php b/test/TestAsset/TestAdapter.php index 95065e3..68bb39d 100644 --- a/test/TestAsset/TestAdapter.php +++ b/test/TestAsset/TestAdapter.php @@ -30,4 +30,14 @@ public function getItems($pageNumber, $itemCountPerPage) { return new \ArrayObject(range(1, 10)); } + + /** + * Returns the internal cache id + * + * @return string + */ + public function getCacheInternalId() + { + return json_encode($this); + } } diff --git a/test/TestAsset/TestDbSelectAdapterCount10.php b/test/TestAsset/TestDbSelectAdapterCount10.php new file mode 100644 index 0000000..498755a --- /dev/null +++ b/test/TestAsset/TestDbSelectAdapterCount10.php @@ -0,0 +1,23 @@ + Date: Fri, 2 Feb 2018 11:41:33 -0200 Subject: [PATCH 2/2] fix paginator test for DbSelectAdapter --- test/PaginatorTest.php | 27 ++++++++++++------- ...terCount10.php => TestDbSelectAdapter.php} | 2 +- test/TestAsset/TestDbSelectAdapterCount5.php | 23 ---------------- 3 files changed, 18 insertions(+), 34 deletions(-) rename test/TestAsset/{TestDbSelectAdapterCount10.php => TestDbSelectAdapter.php} (87%) delete mode 100644 test/TestAsset/TestDbSelectAdapterCount5.php diff --git a/test/PaginatorTest.php b/test/PaginatorTest.php index c75213b..bb712d2 100644 --- a/test/PaginatorTest.php +++ b/test/PaginatorTest.php @@ -9,7 +9,6 @@ namespace ZendTest\Paginator; -use ArrayIterator; use ArrayObject; use PHPUnit\Framework\TestCase; use ReflectionMethod; @@ -946,8 +945,8 @@ public function testAcceptsComplexAdapters() } /** - * This piece if code test the failure in determine different cache_id for different queries when using the - * generic TestAdapter. All the results will hit the same cache_id when simulating DbSelectAdapterObject. + * This piece of code tests the failure in determine different cache_id for different queries when using the + * generic TestAdapter. All the results will hit the same cache_id when simulating DbSelectAdapter object. */ public function testDbSelectAdapterLikeFailure() { @@ -992,7 +991,8 @@ public function testDbSelectAdapterLikeFailure() } /** - * This piece if code test the success in determine different cache_id for different queries. + * This piece of code tests the success in determine different cache_id + * for different queries when using DbSelectAdapter object. */ public function testDbSelectAdapterLikeSuccess() { @@ -1000,11 +1000,14 @@ public function testDbSelectAdapterLikeSuccess() $select->where('id = 1'); $select->where('nick = \'test\''); $paginator = new Paginator\Paginator( - new TestAsset\TestDbSelectAdapterCount10($select, + new TestAsset\TestDbSelectAdapter( + $select, new DbAdapter\Adapter( new DbAdapter\Driver\Pdo\Pdo( - new DbAdapter\Driver\Pdo\Connection([ - ])))) + new DbAdapter\Driver\Pdo\Connection([]) + ) + ) + ) ); $reflectionGetCacheInternalId = new ReflectionMethod($paginator, '_getCacheInternalId'); @@ -1017,17 +1020,21 @@ public function testDbSelectAdapterLikeSuccess() $select->where('id = 2'); $select->where('nick = \'test\''); $paginator = new Paginator\Paginator( - new TestAsset\TestDbSelectAdapterCount5($select, + new TestAsset\TestDbSelectAdapter( + $select, new DbAdapter\Adapter( new DbAdapter\Driver\Pdo\Pdo( - new DbAdapter\Driver\Pdo\Connection()))) + new DbAdapter\Driver\Pdo\Connection([]) + ) + ) + ) ); $reflectionGetCacheInternalId = new ReflectionMethod($paginator, '_getCacheInternalId'); $reflectionGetCacheInternalId->setAccessible(true); $secondOutputGetCacheInternalId = $reflectionGetCacheInternalId->invoke($paginator); $this->assertInstanceOf('ArrayObject', $paginator->getCurrentItems()); - $this->assertCount(5, $paginator->getCurrentItems()); + $this->assertCount(10, $paginator->getCurrentItems()); $this->assertNotEquals($firstOutputGetCacheInternalId, $secondOutputGetCacheInternalId); } diff --git a/test/TestAsset/TestDbSelectAdapterCount10.php b/test/TestAsset/TestDbSelectAdapter.php similarity index 87% rename from test/TestAsset/TestDbSelectAdapterCount10.php rename to test/TestAsset/TestDbSelectAdapter.php index 498755a..b82a9bd 100644 --- a/test/TestAsset/TestDbSelectAdapterCount10.php +++ b/test/TestAsset/TestDbSelectAdapter.php @@ -9,7 +9,7 @@ namespace ZendTest\Paginator\TestAsset; -class TestDbSelectAdapterCount10 extends \Zend\Paginator\Adapter\DbSelect +class TestDbSelectAdapter extends \Zend\Paginator\Adapter\DbSelect { public function count() { diff --git a/test/TestAsset/TestDbSelectAdapterCount5.php b/test/TestAsset/TestDbSelectAdapterCount5.php deleted file mode 100644 index e7a347c..0000000 --- a/test/TestAsset/TestDbSelectAdapterCount5.php +++ /dev/null @@ -1,23 +0,0 @@ -