Skip to content

Commit 11604d3

Browse files
committed
update for cake-4.x
1 parent c820cd6 commit 11604d3

File tree

5 files changed

+68
-46
lines changed

5 files changed

+68
-46
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
build
22
composer.lock
33
vendor
4+
.phpunit.result.cache

composer.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bcrowe/cakephp-api-pagination",
3-
"description": "CakePHP 3 plugin that injects pagination information into API responses.",
3+
"description": "CakePHP 4 plugin that injects pagination information into API responses.",
44
"type": "cakephp-plugin",
55
"keywords": [
66
"cakephp", "api", "pagination", "cakephp3"
@@ -16,13 +16,13 @@
1616
}
1717
],
1818
"require": {
19-
"php": ">=5.6",
20-
"cakephp/cakephp": "~3.6"
19+
"php": ">=7.2",
20+
"cakephp/cakephp": "^4.0"
2121
},
2222
"require-dev": {
23-
"phpunit/phpunit" : "~5.0",
24-
"scrutinizer/ocular": "1.1",
25-
"squizlabs/php_codesniffer": "~2.3.0"
23+
"phpunit/phpunit" : "^8.5",
24+
"scrutinizer/ocular": "1.7",
25+
"cakephp/cakephp-codesniffer": "~4.0.0"
2626
},
2727
"autoload": {
2828
"psr-4": {

src/Controller/Component/ApiPaginationComponent.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function beforeRender(Event $event)
4343
}
4444

4545
$subject = $event->getSubject();
46-
$this->pagingInfo = $this->request->getParam('paging')[$subject->getName()];
46+
$this->pagingInfo = $this->getController()->getRequest()->getAttribute('paging')[$subject->getName()];
4747
$config = $this->getConfig();
4848

4949
if (!empty($config['aliases'])) {
@@ -55,7 +55,9 @@ public function beforeRender(Event $event)
5555
}
5656

5757
$subject->set($config['key'], $this->pagingInfo);
58-
$subject->viewVars['_serialize'][] = $config['key'];
58+
$data = $subject->viewBuilder()->getVar('_serialize') ?? [];
59+
$data[] = $config['key'];
60+
$subject->set('_serialize', $data);
5961
}
6062

6163
/**
@@ -96,8 +98,8 @@ protected function setVisibility()
9698
*/
9799
protected function isPaginatedApiRequest()
98100
{
99-
if ($this->request->getParam('paging') &&
100-
$this->request->is(['json', 'xml'])
101+
if ($this->getController()->getRequest()->getAttribute('paging') &&
102+
$this->getController()->getRequest()->is(['json', 'xml'])
101103
) {
102104
return true;
103105
}

tests/TestCase/Controller/Component/ApiPaginationComponentTest.php

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ class ApiPaginationComponentTest extends TestCase
2323
*
2424
* @return void
2525
*/
26-
public function setUp()
26+
public function setUp(): void
2727
{
28-
$this->request = new Request('/articles');
28+
$this->request = new Request(['url' => '/articles']);
2929
$this->response = $this->createMock('Cake\Http\Response');
3030
$this->controller = new ArticlesController($this->request, $this->response);
3131
$this->Articles = TableRegistry::get('BryanCrowe/ApiPagination.Articles', ['table' => 'bryancrowe_articles']);
@@ -37,7 +37,7 @@ public function setUp()
3737
*
3838
* @return void
3939
*/
40-
public function tearDown()
40+
public function tearDown(): void
4141
{
4242
parent::tearDown();
4343
}
@@ -63,29 +63,34 @@ public function testNonApiPaginatedRequest()
6363
*/
6464
public function testDefaultPaginationSettings()
6565
{
66-
$this->controller->request = $this->controller->request->withEnv('HTTP_ACCEPT', 'application/json');
66+
$this->controller->setRequest(
67+
$this->controller->getRequest()->withEnv('HTTP_ACCEPT', 'application/json')
68+
);
6769
$this->controller->set('data', $this->controller->paginate($this->Articles));
6870
$apiPaginationComponent = new ApiPaginationComponent($this->controller->components());
6971
$event = new Event('Controller.beforeRender', $this->controller);
7072
$apiPaginationComponent->beforeRender($event);
7173

72-
$result = $apiPaginationComponent->_registry->getController()->viewVars['pagination'];
74+
$result = $apiPaginationComponent->getController()->viewBuilder()->getVar('pagination');
7375
$expected = [
74-
'finder' => 'all',
75-
'page' => 1,
76-
'current' => 20,
7776
'count' => 23,
77+
'current' => 20,
7878
'perPage' => 20,
79+
'page' => 1,
80+
'requestedPage' => 1,
81+
'pageCount' => 2,
82+
'start' => 1,
83+
'end' => 20,
7984
'prevPage' => false,
8085
'nextPage' => true,
81-
'pageCount' => 2,
8286
'sort' => null,
83-
'direction' => false,
84-
'limit' => null,
87+
'direction' => null,
8588
'sortDefault' => false,
8689
'directionDefault' => false,
90+
'completeSort' => [],
91+
'limit' => null,
8792
'scope' => null,
88-
'completeSort' => []
93+
'finder' => 'all',
8994
];
9095

9196
$this->assertSame($expected, $result);
@@ -98,7 +103,9 @@ public function testDefaultPaginationSettings()
98103
*/
99104
public function testVisibilitySettings()
100105
{
101-
$this->controller->request = $this->controller->request->withEnv('HTTP_ACCEPT', 'application/json');
106+
$this->controller->setRequest(
107+
$this->controller->getRequest()->withEnv('HTTP_ACCEPT', 'application/json')
108+
);
102109
$this->controller->set('data', $this->controller->paginate($this->Articles));
103110
$apiPaginationComponent = new ApiPaginationComponent($this->controller->components(), [
104111
'visible' => [
@@ -113,14 +120,14 @@ public function testVisibilitySettings()
113120
$event = new Event('Controller.beforeRender', $this->controller);
114121
$apiPaginationComponent->beforeRender($event);
115122

116-
$result = $apiPaginationComponent->_registry->getController()->viewVars['pagination'];
123+
$result = $apiPaginationComponent->getController()->viewBuilder()->getVar('pagination');
117124
$expected = [
118-
'page' => 1,
119-
'current' => 20,
120125
'count' => 23,
126+
'current' => 20,
127+
'page' => 1,
128+
'pageCount' => 2,
121129
'prevPage' => false,
122130
'nextPage' => true,
123-
'pageCount' => 2
124131
];
125132

126133
$this->assertSame($expected, $result);
@@ -133,7 +140,9 @@ public function testVisibilitySettings()
133140
*/
134141
public function testAliasSettings()
135142
{
136-
$this->controller->request = $this->controller->request->withEnv('HTTP_ACCEPT', 'application/json');
143+
$this->controller->setRequest(
144+
$this->controller->getRequest()->withEnv('HTTP_ACCEPT', 'application/json')
145+
);
137146
$this->controller->set('data', $this->controller->paginate($this->Articles));
138147
$apiPaginationComponent = new ApiPaginationComponent($this->controller->components(), [
139148
'aliases' => [
@@ -145,20 +154,23 @@ public function testAliasSettings()
145154
$event = new Event('Controller.beforeRender', $this->controller);
146155
$apiPaginationComponent->beforeRender($event);
147156

148-
$result = $apiPaginationComponent->_registry->getController()->viewVars['pagination'];
157+
$result = $apiPaginationComponent->getController()->viewBuilder()->getVar('pagination');
149158
$expected = [
150-
'finder' => 'all',
151159
'perPage' => 20,
160+
'requestedPage' => 1,
161+
'pageCount' => 2,
162+
'start' => 1,
163+
'end' => 20,
152164
'prevPage' => false,
153165
'nextPage' => true,
154-
'pageCount' => 2,
155166
'sort' => null,
156-
'direction' => false,
157-
'limit' => null,
167+
'direction' => null,
158168
'sortDefault' => false,
159169
'directionDefault' => false,
160-
'scope' => null,
161170
'completeSort' => [],
171+
'limit' => null,
172+
'scope' => null,
173+
'finder' => 'all',
162174
'curPage' => 1,
163175
'currentCount' => 20,
164176
'totalCount' => 23,
@@ -174,31 +186,36 @@ public function testAliasSettings()
174186
*/
175187
public function testKeySetting()
176188
{
177-
$this->controller->request = $this->controller->request->withEnv('HTTP_ACCEPT', 'application/json');
189+
$this->controller->setRequest(
190+
$this->controller->getRequest()->withEnv('HTTP_ACCEPT', 'application/json')
191+
);
178192
$this->controller->set('data', $this->controller->paginate($this->Articles));
179193
$apiPaginationComponent = new ApiPaginationComponent($this->controller->components(), [
180194
'key' => 'paging'
181195
]);
182196
$event = new Event('Controller.beforeRender', $this->controller);
183197
$apiPaginationComponent->beforeRender($event);
184198

185-
$result = $apiPaginationComponent->_registry->getController()->viewVars['paging'];
199+
$result = $apiPaginationComponent->getController()->viewBuilder()->getVar('paging');
186200
$expected = [
187-
'finder' => 'all',
188-
'page' => 1,
189-
'current' => 20,
190201
'count' => 23,
202+
'current' => 20,
191203
'perPage' => 20,
204+
'page' => 1,
205+
'requestedPage' => 1,
206+
'pageCount' => 2,
207+
'start' => 1,
208+
'end' => 20,
192209
'prevPage' => false,
193210
'nextPage' => true,
194-
'pageCount' => 2,
195211
'sort' => null,
196-
'direction' => false,
197-
'limit' => null,
212+
'direction' => null,
198213
'sortDefault' => false,
199214
'directionDefault' => false,
215+
'completeSort' => [],
216+
'limit' => null,
200217
'scope' => null,
201-
'completeSort' => []
218+
'finder' => 'all',
202219
];
203220

204221
$this->assertSame($expected, $result);
@@ -211,7 +228,9 @@ public function testKeySetting()
211228
*/
212229
public function testAllSettings()
213230
{
214-
$this->controller->request = $this->controller->request->withEnv('HTTP_ACCEPT', 'application/json');
231+
$this->controller->setRequest(
232+
$this->controller->getRequest()->withEnv('HTTP_ACCEPT', 'application/json')
233+
);
215234
$this->controller->set('data', $this->controller->paginate($this->Articles));
216235
$apiPaginationComponent = new ApiPaginationComponent($this->controller->components(), [
217236
'key' => 'fun',
@@ -231,7 +250,7 @@ public function testAllSettings()
231250
$event = new Event('Controller.beforeRender', $this->controller);
232251
$apiPaginationComponent->beforeRender($event);
233252

234-
$result = $apiPaginationComponent->_registry->getController()->viewVars['fun'];
253+
$result = $apiPaginationComponent->getController()->viewBuilder()->getVar('fun');
235254
$expected = [
236255
'prevPage' => false,
237256
'nextPage' => true,

tests/test_app/TestApp/Controller/ArticlesController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
class ArticlesController extends Controller
77
{
8-
public function initialize()
8+
public function initialize(): void
99
{
1010
parent::initialize();
1111
$this->loadComponent('Paginator');

0 commit comments

Comments
 (0)