Skip to content
Permalink
Browse files

Sending all options to finder method from Paginator

Add a test for the paginator->find method to persist options.
  • Loading branch information...
Patrick Conroy
Patrick Conroy committed Aug 25, 2014
1 parent 4d5d3e1 commit df3451853fe8cf5ac767637df905c8a53236a40c
@@ -164,7 +164,7 @@ public function paginate($object, array $settings = []) {
unset($options['finder'], $options['maxLimit']);
if (empty($query)) {
$query = $object->find($type, isset($options['finderOptions']) ? $options['finderOptions'] : []);
$query = $object->find($type, $options);
}
$query->applyOptions($options);
@@ -207,7 +207,10 @@ class ControllerTest extends TestCase {
*/
public $fixtures = array(
'core.post',
'core.comment'
'core.comment',
'core.article',
'core.articles_tag',
'core.tag'
);
/**
@@ -871,4 +874,33 @@ public function testAddComponent() {
$this->assertTrue(isset($registry->Paginator));
}
/**
* Testing that when you paginate, your options persist over to your custom finder.
* Using fixture data, this tests by uses matching() in both places.
* If the Table gets the 'tags' array successfully, it will do a matching().
*
* @return void
*/
public function testPaginateSendsFinderOptions() {
$request = new Request('/');
$request->params['pass'] = array();
$response = $this->getMock('Cake\Network\Response');
$testTags = [2, 3];
$Controller = new Controller($request, $response);
$Controller->loadModel('Articles');
$Controller->paginate = [
'Articles' => [
'finder' => 'customTags',
'tags' => $testTags,
'maxLimit' => 1000
]
];
$result = $Controller->paginate('Articles')->count();
$expected = $Controller->Articles->find('all')->matching('Tags', function($q) use ($testTags) {
return $q->where(['Tags.id IN' => $testTags]);
})->count();
$this->assertEquals($expected, $result);
}
}
@@ -200,7 +200,7 @@ public function testOutputClassOptionsForTablePlugin() {
*/
public function testMethodIntrospection() {
$result = $this->Task->getTestableMethods('TestApp\Model\Table\ArticlesTable');
$expected = ['findpublished', 'dosomething', 'dosomethingelse'];
$expected = ['findpublished', 'dosomething', 'dosomethingelse', 'findcustomtags'];
$this->assertEquals($expected, array_map('strtolower', $result));
}
@@ -11,6 +11,7 @@
*/
namespace TestApp\Model\Table;
use Cake\ORM\Query;
use Cake\ORM\Table;
/**
@@ -51,6 +52,22 @@ public function doSomething() {
public function doSomethingElse() {
}
/**
* Custom finder, used with fixture data to ensure Paginator is sending options
*
* @param Cake\ORM\Query $query
* @param array $options
* @return Cake\ORM\Query
*/
public function findCustomTags(Query $query, array $options = []) {
if (isset($options['tags']) && is_array($options['tags'])) {
return $query->matching('Tags', function($q) use ($options) {
return $q->where(['Tags.id IN' => $options['tags']]);
});
}
return $query;
}
/**
* Example protected method
*

0 comments on commit df34518

Please sign in to comment.
You can’t perform that action at this time.