From c767219eb6325db5cb37f689e2eec059f857d4a2 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Thu, 13 Mar 2014 21:15:00 +0100 Subject: [PATCH] More refactoring and fixing tests --- .../Component/PaginatorComponent.php | 16 +++-- .../Component/PaginatorComponentTest.php | 59 +++++++++---------- 2 files changed, 36 insertions(+), 39 deletions(-) diff --git a/src/Controller/Component/PaginatorComponent.php b/src/Controller/Component/PaginatorComponent.php index edcbee2adf8..e0907952aec 100644 --- a/src/Controller/Component/PaginatorComponent.php +++ b/src/Controller/Component/PaginatorComponent.php @@ -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); diff --git a/tests/TestCase/Controller/Component/PaginatorComponentTest.php b/tests/TestCase/Controller/Component/PaginatorComponentTest.php index efe1238ffd1..9dd26c10e05 100644 --- a/tests/TestCase/Controller/Component/PaginatorComponentTest.php +++ b/tests/TestCase/Controller/Component/PaginatorComponentTest.php @@ -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); } @@ -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); } @@ -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']); @@ -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, @@ -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); } @@ -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();