Skip to content

Commit

Permalink
Merge pull request #576 from frankvanhest/frankvanhest-patch-1
Browse files Browse the repository at this point in the history
ApiPaginationListener should use setConfig in beforeRender
  • Loading branch information
ADmad authored Feb 11, 2018
2 parents e4c2809 + 0d1c9e7 commit 2b9ae1a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Listener/ApiPaginationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ public function beforeRender(Event $event)
];

$controller->set('pagination', $paginationResponse);
$this->_action()->getConfig('serialize.pagination', 'pagination');
$this->_action()->setConfig('serialize.pagination', 'pagination');
}
}
82 changes: 78 additions & 4 deletions tests/TestCase/Listener/ApiPaginationListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
namespace Crud\Test\TestCase\Listener;

use Cake\Http\ServerRequest;
use Crud\Action\BaseAction;
use Crud\Listener\ApiPaginationListener;
use Crud\TestSuite\TestCase;

/**
Expand Down Expand Up @@ -150,11 +152,11 @@ public function testBeforeRenderWithPaginationData()
$Action = $this
->getMockBuilder('\Crud\Action\BaseAction')
->disableOriginalConstructor()
->setMethods(['getConfig'])
->setMethods(['setConfig'])
->getMock();
$Action
->expects($this->once())
->method('getConfig')
->method('setConfig')
->with('serialize.pagination', 'pagination');

$Instance = $this
Expand Down Expand Up @@ -224,11 +226,11 @@ public function testBeforeRenderWithPaginationDataForPluginModel()
$Action = $this
->getMockBuilder('\Crud\Action\BaseAction')
->disableOriginalConstructor()
->setMethods(['getConfig'])
->setMethods(['setConfig'])
->getMock();
$Action
->expects($this->once())
->method('getConfig')
->method('setConfig')
->with('serialize.pagination', 'pagination');

$Instance = $this
Expand All @@ -253,4 +255,76 @@ public function testBeforeRenderWithPaginationDataForPluginModel()

$Instance->beforeRender(new \Cake\Event\Event('something'));
}

/**
* Test if the pagination is set to be serialized in the beforeRender event
*
* @return void
*/
public function testBeforeRenderMakeSurePaginationDataIsSetToBeSerialized()
{
$Request = $this
->getMockBuilder(ServerRequest::class)
->setMethods(null)
->getMock();
$Request->paging = [
'MyModel' => [
'pageCount' => 10,
'page' => 2,
'nextPage' => true,
'prevPage' => true,
'count' => 100,
'limit' => 10
]
];

$expected = [
'page_count' => 10,
'current_page' => 2,
'has_next_page' => true,
'has_prev_page' => true,
'count' => 100,
'limit' => 10
];

$Controller = $this
->getMockBuilder('\Cake\Controller\Controller')
->disableOriginalConstructor()
->setMethods(['set'])
->getMock();
$Controller
->expects($this->once())
->method('set')
->with('pagination', $expected);

$Action = $this
->getMockBuilder('\Crud\Action\BaseAction')
->disableOriginalConstructor()
->setMethods(null)
->getMock();

$Instance = $this
->getMockBuilder('\Crud\Listener\ApiPaginationListener')
->disableOriginalConstructor()
->setMethods(['_request', '_controller', '_action'])
->getMock();
$Instance
->expects($this->once())
->method('_request')
->will($this->returnValue($Request));
$Instance
->expects($this->once())
->method('_controller')
->will($this->returnValue($Controller));
$Instance
->expects($this->once())
->method('_action')
->will($this->returnValue($Action));

$Controller->modelClass = 'MyModel';

$Instance->beforeRender(new \Cake\Event\Event('something'));

$this->assertSame('pagination', $Action->getConfig('serialize.pagination'));
}
}

0 comments on commit 2b9ae1a

Please sign in to comment.