diff --git a/.gitignore b/.gitignore
index 073e37a..b6bd0e7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
build
composer.lock
vendor
+.phpunit.result.cache
diff --git a/.travis.yml b/.travis.yml
index 905a3eb..b4c3966 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -6,15 +6,13 @@ env:
- RUN_TESTS=1
php:
- - 5.6
- - 7.0
- - hhvm
+ - 7.3
sudo: false
matrix:
include:
- - php: 5.6
+ - php: 7.2
env: PHPCS=1 RUN_TESTS=0
before_script:
@@ -23,7 +21,7 @@ before_script:
script:
- sh -c "if [ '$RUN_TESTS' = '1' ]; then vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover; fi"
- - sh -c "if [ '$PHPCS' = '1' ]; then vendor/bin/phpcs -p -n --extensions=php --standard=psr2 ./src ./tests; fi"
+ - sh -c "if [ '$PHPCS' = '1' ]; then vendor/bin/phpcs -p ./src ./tests; fi"
after_script:
- if [[ $TRAVIS_PHP_VERSION != 'hhvm' && $TRAVIS_PHP_VERSION != '7.0' ]]; then php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover; fi
diff --git a/composer.json b/composer.json
index a40ea6c..4b0cf5c 100644
--- a/composer.json
+++ b/composer.json
@@ -1,6 +1,6 @@
{
"name": "bcrowe/cakephp-api-pagination",
- "description": "CakePHP 3 plugin that injects pagination information into API responses.",
+ "description": "CakePHP 4 plugin that injects pagination information into API responses.",
"type": "cakephp-plugin",
"keywords": [
"cakephp", "api", "pagination", "cakephp3"
@@ -16,13 +16,13 @@
}
],
"require": {
- "php": ">=5.6",
- "cakephp/cakephp": "~3.6"
+ "php": ">=7.2",
+ "cakephp/cakephp": "^4.0"
},
"require-dev": {
- "phpunit/phpunit" : "~5.0",
- "scrutinizer/ocular": "1.1",
- "squizlabs/php_codesniffer": "~2.3.0"
+ "phpunit/phpunit" : "^8.5",
+ "scrutinizer/ocular": "1.7",
+ "cakephp/cakephp-codesniffer": "~4.0.0"
},
"autoload": {
"psr-4": {
@@ -37,7 +37,9 @@
}
},
"scripts": {
- "test": "phpunit"
+ "test": "phpunit",
+ "cs-check": "phpcs src/ tests/",
+ "cs-fix": "phpcbf src/ tests/"
},
"extra": {
"branch-alias": {
diff --git a/phpcs.xml.dist b/phpcs.xml.dist
new file mode 100644
index 0000000..10c5a08
--- /dev/null
+++ b/phpcs.xml.dist
@@ -0,0 +1,11 @@
+
+
+
+ tests/bootstrap.php
+
+
+
+
+
+
+
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index c6c4b9c..1136eba 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -31,7 +31,7 @@
-
+
diff --git a/src/Controller/Component/ApiPaginationComponent.php b/src/Controller/Component/ApiPaginationComponent.php
index b5944ea..3f12012 100644
--- a/src/Controller/Component/ApiPaginationComponent.php
+++ b/src/Controller/Component/ApiPaginationComponent.php
@@ -1,4 +1,6 @@
'pagination',
'aliases' => [],
- 'visible' => []
+ 'visible' => [],
];
/**
@@ -33,7 +35,7 @@ class ApiPaginationComponent extends Component
* Injects the pagination info into the response if the current request is a
* JSON or XML request with pagination.
*
- * @param \Cake\Event\Event $event The Controller.beforeRender event.
+ * @param \Cake\Event\Event $event The Controller.beforeRender event.
* @return void
*/
public function beforeRender(Event $event)
@@ -43,7 +45,7 @@ public function beforeRender(Event $event)
}
$subject = $event->getSubject();
- $this->pagingInfo = $this->request->getParam('paging')[$subject->getName()];
+ $this->pagingInfo = $this->getController()->getRequest()->getAttribute('paging')[$subject->getName()];
$config = $this->getConfig();
if (!empty($config['aliases'])) {
@@ -55,7 +57,9 @@ public function beforeRender(Event $event)
}
$subject->set($config['key'], $this->pagingInfo);
- $subject->viewVars['_serialize'][] = $config['key'];
+ $data = $subject->viewBuilder()->getVar('_serialize') ?? [];
+ $data[] = $config['key'];
+ $subject->set('_serialize', $data);
}
/**
@@ -96,8 +100,9 @@ protected function setVisibility()
*/
protected function isPaginatedApiRequest()
{
- if ($this->request->getParam('paging') &&
- $this->request->is(['json', 'xml'])
+ if (
+ $this->getController()->getRequest()->getAttribute('paging')
+ && $this->getController()->getRequest()->is(['json', 'xml'])
) {
return true;
}
diff --git a/tests/Fixture/ArticlesFixture.php b/tests/Fixture/ArticlesFixture.php
index 44dbc8c..d241bc2 100644
--- a/tests/Fixture/ArticlesFixture.php
+++ b/tests/Fixture/ArticlesFixture.php
@@ -11,7 +11,7 @@ class ArticlesFixture extends TestFixture
'id' => ['type' => 'integer'],
'title' => ['type' => 'string', 'null' => false],
'body' => 'text',
- '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
+ '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
];
public $records = [
@@ -37,6 +37,6 @@ class ArticlesFixture extends TestFixture
['title' => 'Post #20', 'body' => 'This is the article body.'],
['title' => 'Post #21', 'body' => 'This is the article body.'],
['title' => 'Post #22', 'body' => 'This is the article body.'],
- ['title' => 'Post #23', 'body' => 'This is the article body.']
+ ['title' => 'Post #23', 'body' => 'This is the article body.'],
];
}
diff --git a/tests/TestCase/Controller/Component/ApiPaginationComponentTest.php b/tests/TestCase/Controller/Component/ApiPaginationComponentTest.php
index be75d76..ddb7f95 100644
--- a/tests/TestCase/Controller/Component/ApiPaginationComponentTest.php
+++ b/tests/TestCase/Controller/Component/ApiPaginationComponentTest.php
@@ -1,11 +1,12 @@
request = new Request('/articles');
+ $this->request = new Request(['url' => '/articles']);
$this->response = $this->createMock('Cake\Http\Response');
$this->controller = new ArticlesController($this->request, $this->response);
$this->Articles = TableRegistry::get('BryanCrowe/ApiPagination.Articles', ['table' => 'bryancrowe_articles']);
@@ -37,7 +38,7 @@ public function setUp()
*
* @return void
*/
- public function tearDown()
+ public function tearDown(): void
{
parent::tearDown();
}
@@ -63,29 +64,34 @@ public function testNonApiPaginatedRequest()
*/
public function testDefaultPaginationSettings()
{
- $this->controller->request = $this->controller->request->withEnv('HTTP_ACCEPT', 'application/json');
+ $this->controller->setRequest(
+ $this->controller->getRequest()->withEnv('HTTP_ACCEPT', 'application/json')
+ );
$this->controller->set('data', $this->controller->paginate($this->Articles));
$apiPaginationComponent = new ApiPaginationComponent($this->controller->components());
$event = new Event('Controller.beforeRender', $this->controller);
$apiPaginationComponent->beforeRender($event);
- $result = $apiPaginationComponent->_registry->getController()->viewVars['pagination'];
+ $result = $apiPaginationComponent->getController()->viewBuilder()->getVar('pagination');
$expected = [
- 'finder' => 'all',
- 'page' => 1,
- 'current' => 20,
'count' => 23,
+ 'current' => 20,
'perPage' => 20,
+ 'page' => 1,
+ 'requestedPage' => 1,
+ 'pageCount' => 2,
+ 'start' => 1,
+ 'end' => 20,
'prevPage' => false,
'nextPage' => true,
- 'pageCount' => 2,
'sort' => null,
- 'direction' => false,
- 'limit' => null,
+ 'direction' => null,
'sortDefault' => false,
'directionDefault' => false,
+ 'completeSort' => [],
+ 'limit' => null,
'scope' => null,
- 'completeSort' => []
+ 'finder' => 'all',
];
$this->assertSame($expected, $result);
@@ -98,29 +104,34 @@ public function testDefaultPaginationSettings()
*/
public function testVisibilitySettings()
{
- $this->controller->request = $this->controller->request->withEnv('HTTP_ACCEPT', 'application/json');
+ $this->controller->setRequest(
+ $this->controller->getRequest()->withEnv('HTTP_ACCEPT', 'application/json')
+ );
$this->controller->set('data', $this->controller->paginate($this->Articles));
- $apiPaginationComponent = new ApiPaginationComponent($this->controller->components(), [
+ $apiPaginationComponent = new ApiPaginationComponent(
+ $this->controller->components(),
+ [
'visible' => [
'page',
'current',
'count',
'prevPage',
'nextPage',
- 'pageCount'
+ 'pageCount',
+ ],
]
- ]);
+ );
$event = new Event('Controller.beforeRender', $this->controller);
$apiPaginationComponent->beforeRender($event);
- $result = $apiPaginationComponent->_registry->getController()->viewVars['pagination'];
+ $result = $apiPaginationComponent->getController()->viewBuilder()->getVar('pagination');
$expected = [
- 'page' => 1,
- 'current' => 20,
'count' => 23,
+ 'current' => 20,
+ 'page' => 1,
+ 'pageCount' => 2,
'prevPage' => false,
'nextPage' => true,
- 'pageCount' => 2
];
$this->assertSame($expected, $result);
@@ -133,32 +144,40 @@ public function testVisibilitySettings()
*/
public function testAliasSettings()
{
- $this->controller->request = $this->controller->request->withEnv('HTTP_ACCEPT', 'application/json');
+ $this->controller->setRequest(
+ $this->controller->getRequest()->withEnv('HTTP_ACCEPT', 'application/json')
+ );
$this->controller->set('data', $this->controller->paginate($this->Articles));
- $apiPaginationComponent = new ApiPaginationComponent($this->controller->components(), [
+ $apiPaginationComponent = new ApiPaginationComponent(
+ $this->controller->components(),
+ [
'aliases' => [
'page' => 'curPage',
'current' => 'currentCount',
'count' => 'totalCount',
+ ],
]
- ]);
+ );
$event = new Event('Controller.beforeRender', $this->controller);
$apiPaginationComponent->beforeRender($event);
- $result = $apiPaginationComponent->_registry->getController()->viewVars['pagination'];
+ $result = $apiPaginationComponent->getController()->viewBuilder()->getVar('pagination');
$expected = [
- 'finder' => 'all',
'perPage' => 20,
+ 'requestedPage' => 1,
+ 'pageCount' => 2,
+ 'start' => 1,
+ 'end' => 20,
'prevPage' => false,
'nextPage' => true,
- 'pageCount' => 2,
'sort' => null,
- 'direction' => false,
- 'limit' => null,
+ 'direction' => null,
'sortDefault' => false,
'directionDefault' => false,
- 'scope' => null,
'completeSort' => [],
+ 'limit' => null,
+ 'scope' => null,
+ 'finder' => 'all',
'curPage' => 1,
'currentCount' => 20,
'totalCount' => 23,
@@ -174,31 +193,39 @@ public function testAliasSettings()
*/
public function testKeySetting()
{
- $this->controller->request = $this->controller->request->withEnv('HTTP_ACCEPT', 'application/json');
+ $this->controller->setRequest(
+ $this->controller->getRequest()->withEnv('HTTP_ACCEPT', 'application/json')
+ );
$this->controller->set('data', $this->controller->paginate($this->Articles));
- $apiPaginationComponent = new ApiPaginationComponent($this->controller->components(), [
- 'key' => 'paging'
- ]);
+ $apiPaginationComponent = new ApiPaginationComponent(
+ $this->controller->components(),
+ [
+ 'key' => 'paging',
+ ]
+ );
$event = new Event('Controller.beforeRender', $this->controller);
$apiPaginationComponent->beforeRender($event);
- $result = $apiPaginationComponent->_registry->getController()->viewVars['paging'];
+ $result = $apiPaginationComponent->getController()->viewBuilder()->getVar('paging');
$expected = [
- 'finder' => 'all',
- 'page' => 1,
- 'current' => 20,
'count' => 23,
+ 'current' => 20,
'perPage' => 20,
+ 'page' => 1,
+ 'requestedPage' => 1,
+ 'pageCount' => 2,
+ 'start' => 1,
+ 'end' => 20,
'prevPage' => false,
'nextPage' => true,
- 'pageCount' => 2,
'sort' => null,
- 'direction' => false,
- 'limit' => null,
+ 'direction' => null,
'sortDefault' => false,
'directionDefault' => false,
+ 'completeSort' => [],
+ 'limit' => null,
'scope' => null,
- 'completeSort' => []
+ 'finder' => 'all',
];
$this->assertSame($expected, $result);
@@ -211,27 +238,32 @@ public function testKeySetting()
*/
public function testAllSettings()
{
- $this->controller->request = $this->controller->request->withEnv('HTTP_ACCEPT', 'application/json');
+ $this->controller->setRequest(
+ $this->controller->getRequest()->withEnv('HTTP_ACCEPT', 'application/json')
+ );
$this->controller->set('data', $this->controller->paginate($this->Articles));
- $apiPaginationComponent = new ApiPaginationComponent($this->controller->components(), [
+ $apiPaginationComponent = new ApiPaginationComponent(
+ $this->controller->components(),
+ [
'key' => 'fun',
'aliases' => [
'page' => 'currentPage',
'count' => 'totalCount',
- 'limit' => 'unusedAlias'
+ 'limit' => 'unusedAlias',
],
'visible' => [
'currentPage',
'totalCount',
'limit',
'prevPage',
- 'nextPage'
+ 'nextPage',
+ ],
]
- ]);
+ );
$event = new Event('Controller.beforeRender', $this->controller);
$apiPaginationComponent->beforeRender($event);
- $result = $apiPaginationComponent->_registry->getController()->viewVars['fun'];
+ $result = $apiPaginationComponent->getController()->viewBuilder()->getVar('fun');
$expected = [
'prevPage' => false,
'nextPage' => true,
diff --git a/tests/test_app/TestApp/Controller/ArticlesController.php b/tests/test_app/TestApp/Controller/ArticlesController.php
index f6a6155..428e94c 100644
--- a/tests/test_app/TestApp/Controller/ArticlesController.php
+++ b/tests/test_app/TestApp/Controller/ArticlesController.php
@@ -1,11 +1,13 @@
loadComponent('Paginator');