Skip to content

Commit

Permalink
Allow setting custom paginator.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Jun 5, 2017
1 parent 40d86e7 commit a629580
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
30 changes: 29 additions & 1 deletion src/Controller/Component/PaginatorComponent.php
Expand Up @@ -66,7 +66,12 @@ class PaginatorComponent extends Component
*/
public function __construct(ComponentRegistry $registry, array $config = [])
{
$this->_paginator = new Paginator();
if (isset($config['paginator'])) {
$this->_paginator = $config['paginator'];
unset($config['paginator']);
} else {
$this->_paginator = new Paginator();
}

parent::__construct($registry, $config);
}
Expand Down Expand Up @@ -224,6 +229,29 @@ public function mergeOptions($alias, $settings)
return $this->_paginator->mergeOptions($alias, $settings);
}

/**
* Set paginator instance.
*
* @param \Cake\ORM\Paginator $paginator Paginator instance.
* @return self
*/
public function setPaginator(Paginator $paginator)
{
$this->_paginator = $paginator;

return $this;
}

/**
* Get paginator instance.
*
* @return \Cake\ORM\Paginator
*/
public function getPaginator()
{
return $this->_paginator;
}

/**
* Set paging params to request instance.
*
Expand Down
33 changes: 31 additions & 2 deletions tests/TestCase/Controller/Component/PaginatorComponentTest.php
Expand Up @@ -22,6 +22,7 @@
use Cake\Http\ServerRequest;
use Cake\Network\Exception\NotFoundException;
use Cake\ORM\Entity;
use Cake\ORM\Paginator;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;

Expand All @@ -39,6 +40,13 @@ class PaginatorTestController extends Controller
public $components = ['Paginator'];
}

/**
* Custom paginator
*/
class CustomPaginator extends Paginator
{
}

class PaginatorComponentTest extends TestCase
{

Expand Down Expand Up @@ -70,8 +78,8 @@ public function setUp()
$this->request = new ServerRequest('controller_posts/index');
$this->request->params['pass'] = [];
$controller = new Controller($this->request);
$registry = new ComponentRegistry($controller);
$this->Paginator = new PaginatorComponent($registry, []);
$this->registry = new ComponentRegistry($controller);
$this->Paginator = new PaginatorComponent($this->registry, []);

$this->Post = $this->getMockBuilder('Cake\Datasource\RepositoryInterface')
->disableOriginalConstructor()
Expand All @@ -89,6 +97,27 @@ public function tearDown()
TableRegistry::clear();
}

/**
* testPaginatorSetting
*
* @return void
*/
public function testPaginatorSetting()
{
$paginator = new CustomPaginator();
$component = new PaginatorComponent($this->registry, [
'paginator' => $paginator
]);

$this->assertSame($paginator, $component->getPaginator());

$component = new PaginatorComponent($this->registry, []);
$this->assertNotSame($paginator, $component->getPaginator());

$component->setPaginator($paginator);
$this->assertSame($paginator, $component->getPaginator());
}

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

0 comments on commit a629580

Please sign in to comment.