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/33'
Browse files Browse the repository at this point in the history
Close #33
Fixes #1
  • Loading branch information
weierophinney committed Nov 1, 2017
2 parents 6a5721d + fdafd77 commit 0d4b37b
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -40,6 +40,10 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- [#33](https://github.com/zendframework/zend-paginator/pull/33) fixes how cache
identifiers are generated to work propertly with non-serializable pagination
adapters.

- [#26](https://github.com/zendframework/zend-paginator/pull/26) fixes an issue
in `Paginator::count()` whereby it would re-count when zero pages had been
previously detected.
Expand Down
1 change: 0 additions & 1 deletion mkdocs.yml
Expand Up @@ -11,4 +11,3 @@ site_name: zend-paginator
site_description: zend-paginator
repo_url: 'https://github.com/zendframework/zend-paginator'
copyright: 'Copyright (c) 2016 <a href="http://www.zend.com/">Zend Technologies USA Inc.</a>'

9 changes: 7 additions & 2 deletions src/Adapter/DbTableGateway.php
Expand Up @@ -24,8 +24,13 @@ class DbTableGateway extends DbSelect
* @param null|string|array $group
* @param null|Having|\Closure|string|array $having
*/
public function __construct(AbstractTableGateway $tableGateway, $where = null, $order = null, $group = null, $having = null)
{
public function __construct(
AbstractTableGateway $tableGateway,
$where = null,
$order = null,
$group = null,
$having = null
) {
$sql = $tableGateway->getSql();
$select = $sql->select();
if ($where) {
Expand Down
9 changes: 5 additions & 4 deletions src/Paginator.php
Expand Up @@ -863,10 +863,11 @@ protected function _getCacheId($page = null)
protected function _getCacheInternalId()
{
// @codingStandardsIgnoreEnd
return md5(serialize([
spl_object_hash($this->getAdapter()),
$this->getItemCountPerPage()
]));
return md5(
get_class($this->getAdapter())
. json_encode($this->getAdapter())
. $this->getItemCountPerPage()
);
}

/**
Expand Down
52 changes: 52 additions & 0 deletions test/PaginatorTest.php
Expand Up @@ -891,6 +891,58 @@ public function testGetCacheId()
$outputGetCacheInternalId = $reflectionGetCacheInternalId->invoke($paginator);

$this->assertEquals($outputGetCacheId, 'Zend_Paginator_1_' . $outputGetCacheInternalId);

// After a re-creation of the same object, cacheId should remains the same
$adapter = new TestAsset\TestAdapter;
$paginator = new Paginator\Paginator($adapter);
$reflectionGetCacheInternalId = new ReflectionMethod($paginator, '_getCacheInternalId');
$reflectionGetCacheInternalId->setAccessible(true);
$outputGetCacheInternalId = $reflectionGetCacheInternalId->invoke($paginator);
$this->assertEquals($outputGetCacheId, 'Zend_Paginator_1_' . $outputGetCacheInternalId);
}

public function testGetCacheIdWithSameAdapterAndDifferentAttributes()
{
$adapter = new TestAsset\TestAdapter([1, 2, 3, 4]);
$paginator = new Paginator\Paginator($adapter);

$reflectionGetCacheInternalId = new ReflectionMethod($paginator, '_getCacheInternalId');
$reflectionGetCacheInternalId->setAccessible(true);
$firstOutputGetCacheInternalId = $reflectionGetCacheInternalId->invoke($paginator);

$adapter = new TestAsset\TestAdapter([1, 2, 3, 4, 5, 6]);
$paginator = new Paginator\Paginator($adapter);
$reflectionGetCacheInternalId = new ReflectionMethod($paginator, '_getCacheInternalId');
$reflectionGetCacheInternalId->setAccessible(true);
$secondOutputGetCacheInternalId = $reflectionGetCacheInternalId->invoke($paginator);
$this->assertNotEquals($firstOutputGetCacheInternalId, $secondOutputGetCacheInternalId);
}

public function testGetCacheIdWithInheritedClass()
{
$adapter = new TestAsset\TestAdapter([1, 2, 3, 4]);
$paginator = new Paginator\Paginator($adapter);

$reflectionGetCacheInternalId = new ReflectionMethod($paginator, '_getCacheInternalId');
$reflectionGetCacheInternalId->setAccessible(true);
$firstOutputGetCacheInternalId = $reflectionGetCacheInternalId->invoke($paginator);

$adapter = new TestAsset\TestSimilarAdapter([1, 2, 3, 4]);
$paginator = new Paginator\Paginator($adapter);
$reflectionGetCacheInternalId = new ReflectionMethod($paginator, '_getCacheInternalId');
$reflectionGetCacheInternalId->setAccessible(true);
$secondOutputGetCacheInternalId = $reflectionGetCacheInternalId->invoke($paginator);
$this->assertNotEquals($firstOutputGetCacheInternalId, $secondOutputGetCacheInternalId);
}

public function testAcceptsComplexAdapters()
{
$paginator = new Paginator\Paginator(
new TestAsset\TestAdapter(function () {
return 'test';
})
);
$this->assertInstanceOf('ArrayObject', $paginator->getCurrentItems());
}

/**
Expand Down
12 changes: 11 additions & 1 deletion test/TestAsset/TestAdapter.php
Expand Up @@ -9,8 +9,18 @@

namespace ZendTest\Paginator\TestAsset;

class TestAdapter extends \ArrayObject implements \Zend\Paginator\Adapter\AdapterInterface
class TestAdapter implements \Zend\Paginator\Adapter\AdapterInterface
{
/**
* @var mixed
*/
public $property;

public function __construct($property = null)
{
$this->property = $property;
}

public function count()
{
return 10;
Expand Down
18 changes: 18 additions & 0 deletions test/TestAsset/TestSimilarAdapter.php
@@ -0,0 +1,18 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Paginator\TestAsset;

class TestSimilarAdapter extends TestAdapter implements \Zend\Paginator\Adapter\AdapterInterface
{
public function differentFunction()
{
return "test";
}
}

0 comments on commit 0d4b37b

Please sign in to comment.