Skip to content

Commit

Permalink
Merge pull request #4 from tyrellsys/cake-4.x
Browse files Browse the repository at this point in the history
Update for Cake 4.x
  • Loading branch information
bcrowe authored Jul 10, 2020
2 parents c820cd6 + 79655f0 commit b8f1af7
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 70 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
build
composer.lock
vendor
.phpunit.result.cache
8 changes: 3 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
16 changes: 9 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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": {
Expand All @@ -37,7 +37,9 @@
}
},
"scripts": {
"test": "phpunit"
"test": "phpunit",
"cs-check": "phpcs src/ tests/",
"cs-fix": "phpcbf src/ tests/"
},
"extra": {
"branch-alias": {
Expand Down
11 changes: 11 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<ruleset name="CustomCakePHPCodeSniffer">

<exclude-pattern>tests/bootstrap.php</exclude-pattern>

<rule ref="vendor/cakephp/cakephp-codesniffer/CakePHP">
<exclude name="CakePHP.Commenting"/>
</rule>

</ruleset>

2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-html" target="build/coverage"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
Expand Down
17 changes: 11 additions & 6 deletions src/Controller/Component/ApiPaginationComponent.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

namespace BryanCrowe\ApiPagination\Controller\Component;

use Cake\Controller\Component;
Expand All @@ -19,7 +21,7 @@ class ApiPaginationComponent extends Component
protected $_defaultConfig = [
'key' => 'pagination',
'aliases' => [],
'visible' => []
'visible' => [],
];

/**
Expand All @@ -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)
Expand All @@ -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'])) {
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixture/ArticlesFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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.'],
];
}
Loading

0 comments on commit b8f1af7

Please sign in to comment.