Skip to content

Commit

Permalink
Update to test for pagination options with custom finders
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Conroy committed Aug 26, 2014
1 parent d1d63a8 commit f99d358
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 54 deletions.
21 changes: 21 additions & 0 deletions tests/TestCase/Controller/Component/PaginatorComponentTest.php
Expand Up @@ -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.
*
Expand Down
37 changes: 1 addition & 36 deletions tests/TestCase/Controller/ControllerTest.php
Expand Up @@ -207,10 +207,7 @@ class ControllerTest extends TestCase {
*/
public $fixtures = array(
'core.post',
'core.comment',
'core.article',
'core.articles_tag',
'core.tag'
'core.comment'
);

/**
Expand Down Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion tests/TestCase/Shell/Task/TestTaskTest.php
Expand Up @@ -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));
}

Expand Down
17 changes: 0 additions & 17 deletions tests/test_app/TestApp/Model/Table/ArticlesTable.php
Expand Up @@ -12,7 +12,6 @@
namespace TestApp\Model\Table;

use Cake\ORM\Query;
use Cake\ORM\Table;

/**
* Article table class
Expand Down Expand Up @@ -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
*
Expand Down
14 changes: 14 additions & 0 deletions tests/test_app/TestApp/Model/Table/PaginatorPostsTable.php
Expand Up @@ -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.