Skip to content
Permalink
Browse files

Update to test for pagination options with custom finders

  • Loading branch information...
Patrick Conroy
Patrick Conroy committed Aug 26, 2014
1 parent d1d63a8 commit f99d3587d6d105e8b93104d23760e6b85b18abb1
@@ -141,6 +141,27 @@ public function testPaginateExtraParams() {
$this->Paginator->paginate($table, $settings);
}
/**
* Test to make sure options get sent to custom finder methods via paginate
* @return void
*/
public function testPaginateCustomFinderOptions() {
$this->loadFixtures('Post');
$settings = [
'PaginatorPosts' => [
'finder' => 'author',
'author_id' => 1
]
];
$table = TableRegistry::get('PaginatorPosts');
$expected = $table->find('author', ['conditions' => ['PaginatorPosts.author_id' => $settings['PaginatorPosts']['author_id']]])
->count();
$result = $this->Paginator->paginate($table, $settings)->count();
$this->assertEquals($expected, $result);
}
/**
* Test that special paginate types are called and that the type param doesn't leak out into defaults or options.
*
@@ -207,10 +207,7 @@ class ControllerTest extends TestCase {
*/
public $fixtures = array(
'core.post',
'core.comment',
'core.article',
'core.articles_tag',
'core.tag'
'core.comment'
);
/**
@@ -874,36 +871,4 @@ 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');
$this->assertInstanceOf('Cake\ORM\Table', $Controller->Articles);
$this->assertInstanceOf('Cake\ORM\Association\BelongsToMany', $Controller->Articles->Tags);
$Controller->paginate = [
'Articles' => [
'finder' => 'customTags',
'tags' => $testTags,
'maxLimit' => 1000
]
];
$result = $Controller->paginate('Articles')->count();
$expected = $Controller->Articles->find('all')->contain(['Tags'])->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', 'findcustomtags'];
$expected = ['findpublished', 'dosomething', 'dosomethingelse'];
$this->assertEquals($expected, array_map('strtolower', $result));
}
@@ -12,7 +12,6 @@
namespace TestApp\Model\Table;
use Cake\ORM\Query;
use Cake\ORM\Table;
/**
* Article table class
@@ -52,22 +51,6 @@ 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->contain(['Tags'])->matching('Tags', function($q) use ($options) {
return $q->where(['Tags.id IN' => $options['tags']]);
});
}
return $query;
}
/**
* Example protected method
*
@@ -52,4 +52,18 @@ public function findPublished(Query $query, array $options) {
return $query;
}
/**
* 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 findAuthor(Query $query, array $options = []) {
if (isset($options['author_id'])) {
$query->where(['PaginatorPosts.author_id' => $options['author_id']]);
}
return $query;
}
}

0 comments on commit f99d358

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