Skip to content

Commit

Permalink
More refactoring and fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Mar 13, 2014
1 parent 39a4242 commit c767219
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 39 deletions.
16 changes: 7 additions & 9 deletions src/Controller/Component/PaginatorComponent.php
Expand Up @@ -133,24 +133,22 @@ class PaginatorComponent extends Component {
* @throws \Cake\Error\NotFoundException
*/
public function paginate($object, array $settings = []) {
$query = $object;
$type = 'all';

if ($object instanceof RepositoryInterface) {
$type = !empty($settings['findType']) ? $settings['findType'] : $type;
$query = $object->find();
}

$alias = $object->alias();
$options = $this->mergeOptions($alias, $settings);
$options = $this->validateSort($object, $options);
$options = $this->checkLimit($options);

$options += ['page' => 1];
$options['page'] = intval($options['page']) < 1 ? 1 : (int)$options['page'];

$type = !empty($options['findType']) ? $options['findType'] : 'all';
unset($options['findType'], $options['maxLimit']);

$query->applyOptions($options);
if ($object instanceof RepositoryInterface) {
$query = $object->find($type);
}

$query->applyOptions($options);
$results = $query->all();
$numResults = count($results);

Expand Down
59 changes: 29 additions & 30 deletions tests/TestCase/Controller/Component/PaginatorComponentTest.php
Expand Up @@ -118,17 +118,18 @@ public function testPaginateExtraParams() {
$query = $this->_getMockFindQuery();
$table->expects($this->once())
->method('find')
->with('all', [
'conditions' => [],
->with('all')
->will($this->returnValue($query));

$query->expects($this->once())
->method('applyOptions')
->with([
'contain' => ['PaginatorAuthor'],
'fields' => null,
'group' => 'PaginatorPosts.published',
'limit' => 10,
'order' => ['PaginatorPosts.id' => 'ASC'],
'page' => 1,
])
->will($this->returnValue($query));

]);
$this->Paginator->paginate($table, $settings);
}

Expand Down Expand Up @@ -173,14 +174,15 @@ public function testDefaultPaginateParams() {

$table->expects($this->once())
->method('find')
->with('all', [
'conditions' => [],
'fields' => null,
->with('all')
->will($this->returnValue($query));
$query->expects($this->once())
->method('applyOptions')
->with([
'limit' => 10,
'page' => 1,
'order' => ['PaginatorPosts.id' => 'DESC']
])
->will($this->returnValue($query));
]);

$this->Paginator->paginate($table, $settings);
}
Expand All @@ -201,14 +203,15 @@ public function testDefaultPaginateParamsIntoRequest() {

$table->expects($this->once())
->method('find')
->with('all', [
'conditions' => [],
'fields' => null,
->with('all')
->will($this->returnValue($query));
$query->expects($this->once())
->method('applyOptions')
->with([
'limit' => 10,
'page' => 1,
'order' => ['PaginatorPosts.id' => 'DESC']
])
->will($this->returnValue($query));
]);

$this->Paginator->paginate($table, $settings);
$this->assertEquals('PaginatorPosts.id', $this->request->params['paging']['PaginatorPosts']['sortDefault']);
Expand Down Expand Up @@ -370,14 +373,14 @@ public function testValidateSortInvalid() {

$table->expects($this->once())
->method('find')
->with('all', [
'fields' => null,
->with('all')
->will($this->returnValue($query));
$query->expects($this->once())->method('applyOptions')
->with([
'limit' => 20,
'conditions' => [],
'page' => 1,
'order' => ['PaginatorPosts.id' => 'asc'],
])
->will($this->returnValue($query));
'order' => ['PaginatorPosts.id' => 'asc']
]);

$this->request->query = [
'page' => 1,
Expand Down Expand Up @@ -718,15 +721,11 @@ public function testPaginateCustomFindCount() {
$query = $this->_getMockFindQuery();
$table->expects($this->once())
->method('find')
->with('published', [
'conditions' => [],
'order' => [],
'limit' => 2,
'fields' => null,
'page' => 1,
])
->with('published')
->will($this->returnValue($query));

$query->expects($this->once())->method('applyOptions')
->with(['limit' => 2, 'page' => 1, 'order' => []]);
$this->Paginator->paginate($table, $settings);
}

Expand All @@ -751,7 +750,7 @@ protected function _getMockPosts($methods = []) {
*/
protected function _getMockFindQuery() {
$query = $this->getMockBuilder('Cake\ORM\Query')
->setMethods(['total', 'all', 'count'])
->setMethods(['total', 'all', 'count', 'applyOptions'])
->disableOriginalConstructor()
->getMock();

Expand Down

0 comments on commit c767219

Please sign in to comment.