Skip to content

Commit

Permalink
Merge pull request #10959 from chinpei215/3.next-fix-paginator-component
Browse files Browse the repository at this point in the history
Change type of PaginatorComponent::$_paginator to Paginator
  • Loading branch information
markstory committed Jul 27, 2017
2 parents 1e0e0c5 + 68fb13a commit e158802
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/Controller/Component/PaginatorComponent.php
Expand Up @@ -18,9 +18,9 @@
use Cake\Controller\ComponentRegistry;
use Cake\Datasource\Exception\PageOutOfBoundsException;
use Cake\Datasource\Paginator;
use Cake\Datasource\PaginatorInterface;
use Cake\Datasource\QueryInterface;
use Cake\Network\Exception\NotFoundException;
use InvalidArgumentException;

/**
* This component is used to handle automatic model data pagination. The primary way to use this
Expand Down Expand Up @@ -60,7 +60,7 @@ class PaginatorComponent extends Component
/**
* Datasource paginator instance.
*
* @var \Cake\Datasource\PaginatorInterface
* @var \Cake\Datasource\Paginator
*/
protected $_paginator;

Expand All @@ -70,6 +70,9 @@ class PaginatorComponent extends Component
public function __construct(ComponentRegistry $registry, array $config = [])
{
if (isset($config['paginator'])) {
if (!$config['paginator'] instanceof Paginator) {
throw new InvalidArgumentException('Paginator must be an instance of ' . Paginator::class);
}
$this->_paginator = $config['paginator'];
unset($config['paginator']);
} else {
Expand Down Expand Up @@ -237,10 +240,10 @@ public function mergeOptions($alias, $settings)
/**
* Set paginator instance.
*
* @param \Cake\Datasource\PaginatorInterface $paginator Paginator instance.
* @param \Cake\Datasource\Paginator $paginator Paginator instance.
* @return self
*/
public function setPaginator(PaginatorInterface $paginator)
public function setPaginator(Paginator $paginator)
{
$this->_paginator = $paginator;

Expand All @@ -250,7 +253,7 @@ public function setPaginator(PaginatorInterface $paginator)
/**
* Get paginator instance.
*
* @return \Cake\Datasource\PaginatorInterface
* @return \Cake\Datasource\Paginator
*/
public function getPaginator()
{
Expand All @@ -275,6 +278,7 @@ protected function _setPagingParams()
/**
* Proxy getting/setting config options to Paginator.
*
* @deprecated 3.5.0 use setConfig()/getConfig() instead.
* @param string|array|null $key The key to get/set, or a complete array of configs.
* @param mixed|null $value The value to set.
* @param bool $merge Whether to recursively merge or overwrite existing config, defaults to true.
Expand All @@ -283,7 +287,7 @@ protected function _setPagingParams()
public function config($key = null, $value = null, $merge = true)
{
$return = $this->_paginator->config($key, $value, $merge);
if ($return instanceof PaginatorInterface) {
if ($return instanceof Paginator) {
$return = $this;
}

Expand Down
15 changes: 15 additions & 0 deletions tests/TestCase/Controller/Component/PaginatorComponentTest.php
Expand Up @@ -25,6 +25,7 @@
use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
use stdClass;

/**
* PaginatorTestController class
Expand Down Expand Up @@ -121,6 +122,20 @@ public function testPaginatorSetting()
$this->assertSame($paginator, $component->getPaginator());
}

/**
* Test that an exception is thrown when paginator option is invalid.
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Paginator must be an instance of Cake\Datasource\Paginator
* @return void
*/
public function testInvalidPaginatorOption()
{
new PaginatorComponent($this->registry, [
'paginator' => new stdClass()
]);
}

/**
* Test that non-numeric values are rejected for page, and limit
*
Expand Down

0 comments on commit e158802

Please sign in to comment.