diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..cc05456 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,125 @@ +name: CI + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "*" ] + schedule: + - cron: "0 0 * * 0" # Runs at 00:00 UTC on Sun. + +permissions: + contents: read + +jobs: + ######################### + # Run PHPUnit testsuite # + ######################### + testsuite: + + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + php-versions: ['7.4', '8.0', '8.1', '8.2', '8.3'] + db-type: [mysql] + prefer-lowest: ['', 'prefer-lowest'] + + steps: + - uses: actions/checkout@v3 + + - name: Setup MySQL 8 + if: matrix.db-type == 'mysql' + run: docker run --rm --name=mysqld -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=cakephp -p 3306:3306 -d mysql:8 --default-authentication-plugin=mysql_native_password --disable-log-bin + + - name: Validate composer.json and composer.lock + run: composer validate --strict + + - name: Get composer cache directory + id: composer-cache + run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT + + - name: Get date part for cache key + id: key-date + run: echo "date=$(date +'%Y-%m')" >> $GITHUB_OUTPUT + + - name: Cache composer dependencies + uses: actions/cache@v3 + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: test-${{ runner.os }}-composer-${{ steps.key-date.outputs.date }}-${{ hashFiles('composer.json') }}-${{ matrix.prefer-lowest }} + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + extensions: mbstring, intl + + - name: Install composer dependencies + run: | + if ${{ matrix.prefer-lowest == 'prefer-lowest' }}; then + composer update --prefer-lowest --prefer-stable + elif ${{ matrix.php-version == '8.2' }}; then + composer update --ignore-platform-req=php + else + composer update + fi + + - name: Wait for MySQL + if: matrix.db-type == 'mysql' + run: while ! `mysqladmin ping -h 127.0.0.1 --silent`; do printf 'Waiting for MySQL...\n'; sleep 2; done; + + - name: Run PHPUnit testsuite + run: | + if [[ ${{ matrix.db-type }} == 'mysql' ]]; then + export DB_URL='mysql://root:root@127.0.0.1/cakephp'; + mysql -h 127.0.0.1 -u root -proot cakephp < ./tests/Schema/articles.sql + fi + vendor/bin/phpunit --stderr; + + ############## + # Code style # + ############## + cs: + + runs-on: ubuntu-latest + + strategy: + matrix: + php-versions: ['7.4'] + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Validate composer.json and composer.lock + run: composer validate --strict + + - name: Get composer cache directory + id: composer-cache + run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT + + - name: Get date part for cache key + id: key-date + run: echo "date=$(date +'%Y-%m')" >> $GITHUB_OUTPUT + + - name: Cache composer dependencies + uses: actions/cache@v3 + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: cs-${{ runner.os }}-composer-${{ steps.key-date.outputs.date }}-${{ hashFiles('composer.json') }}-${{ matrix.prefer-lowest }} + restore-keys: | + cs-${{ runner.os }}-composer-${{ steps.key-date.outputs.date }}-${{ hashFiles('composer.json') }}-${{ matrix.prefer-lowest }} + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + extensions: mbstring, intl + + - name: Install composer dependencies + run: composer update --no-interaction + + - name: Run CS check + run: composer cs-check diff --git a/.gitignore b/.gitignore index 073e37a..6bbc55f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ build composer.lock vendor +.phpunit.result.cache +.idea diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 905a3eb..0000000 --- a/.travis.yml +++ /dev/null @@ -1,29 +0,0 @@ -language: php - -env: - global: - - PHPCS=0 - - RUN_TESTS=1 - -php: - - 5.6 - - 7.0 - - hhvm - -sudo: false - -matrix: - include: - - php: 5.6 - env: PHPCS=1 RUN_TESTS=0 - -before_script: - - travis_retry composer self-update - - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source - -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" - -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/README.md b/README.md index 0415cf0..1166f97 100644 --- a/README.md +++ b/README.md @@ -2,14 +2,16 @@ [![Latest Version on Packagist][ico-version]][link-packagist] [![Software License][ico-license]](LICENSE.md) -[![Build Status][ico-travis]][link-travis] +[![Build Status][ico-github]][link-github] [![Coverage Status][ico-scrutinizer]][link-scrutinizer] [![Quality Score][ico-code-quality]][link-code-quality] [![Total Downloads][ico-downloads]][link-downloads] -This is a simple component for CakePHP 3 which injects pagination information +This is a simple component for CakePHP 4.2+ which injects pagination information from CakePHP's Paginator into serialized JsonView and XmlView responses. +See `1.x` and `2.x` releases and branches of this plugin for support of previous versions of CakePHP before `4.2`. + ## Install Via Composer @@ -18,10 +20,19 @@ Via Composer $ composer require bcrowe/cakephp-api-pagination ``` -Load the plugin in your application's `bootstrap.php` file: +Load the plugin by adding `$this->addPlugin('BryanCrowe/ApiPagination');` to the `bootsrap` method in your project’s `src/Application.php`: ``` php -Plugin::load('BryanCrowe/ApiPagination'); +public function bootstrap(): void +{ + parent::bootstrap(); + + // ... bootstrap code ... + + // load more plugins here + + $this->addPlugin('BryanCrowe/ApiPagination'); +} ``` ## Usage @@ -40,10 +51,9 @@ Then, go ahead and set your paginated view variable like so: ``` php $this->set('articles', $this->paginate($this->Articles)); -$this->set('_serialize', ['articles']); +$this->viewBuilder()->setOption('serialize', ['articles']); ``` - -**Note:** It is important that your `_serialize` variable is an array, e.g. +**Note:** It is important that your `serialize` option is an array, e.g. `['articles']`, so that your pagination information can be set under its own pagination key. @@ -73,7 +83,7 @@ and will look something like this: ### Configuring the Pagination Output -ApiPagination has three keys for configuration: `key`, `aliases`, and `visible`. +ApiPagination has four keys for configuration: `key`, `aliases`, `visible` and `model`. * `key` allows you to change the name of the pagination key. @@ -83,6 +93,10 @@ ApiPagination has three keys for configuration: `key`, `aliases`, and `visible`. response. **Note:** Whenever setting a key's visibility, make sure to use the aliased name if you've given it one. +* `model` allows you to set the name of the model the pagination is applied on + if the controller does not follow CakePHP conventions, e.g. `ArticlesIndexController`. + Per default the model is the name of the controller, e.g. `Articles` for `ArticlesController`. + An example using all these configuration keys: ``` php @@ -97,7 +111,8 @@ $this->loadComponent('BryanCrowe/ApiPagination.ApiPagination', [ 'resultCount', 'prevPage', 'nextPage' - ] + ], + 'model' => 'Articles', ]); ``` @@ -148,16 +163,16 @@ information. [ico-version]: https://img.shields.io/packagist/v/bcrowe/cakephp-api-pagination.svg?style=flat-square [ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square -[ico-travis]: https://img.shields.io/travis/bcrowe/cakephp-api-pagination/master.svg?style=flat-square +[ico-github]: https://github.com/bcrowe/cakephp-api-pagination/workflows/CI/badge.svg [ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/bcrowe/cakephp-api-pagination.svg?style=flat-square [ico-code-quality]: https://img.shields.io/scrutinizer/g/bcrowe/cakephp-api-pagination.svg?style=flat-square [ico-downloads]: https://img.shields.io/packagist/dt/bcrowe/cakephp-api-pagination.svg?style=flat-square [link-packagist]: https://packagist.org/packages/bcrowe/cakephp-api-pagination -[link-travis]: https://travis-ci.org/bcrowe/cakephp-api-pagination +[link-github]: https://github.com/bcrowe/cakephp-api-pagination/actions [link-scrutinizer]: https://scrutinizer-ci.com/g/bcrowe/cakephp-api-pagination/code-structure [link-code-quality]: https://scrutinizer-ci.com/g/bcrowe/cakephp-api-pagination [link-downloads]: https://packagist.org/packages/bcrowe/cakephp-api-pagination [link-author]: https://github.com/bcrowe [link-contributors]: ../../contributors -[link-dataviews]: http://book.cakephp.org/3.0/en/views/json-and-xml-views.html#enabling-data-views-in-your-application +[link-dataviews]: https://book.cakephp.org/4/en/views/json-and-xml-views.html#enabling-data-views-in-your-application diff --git a/composer.json b/composer.json index a40ea6c..f492cf4 100644 --- a/composer.json +++ b/composer.json @@ -1,9 +1,9 @@ { "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" + "cakephp", "api", "pagination", "cakephp3", "cakephp4" ], "homepage": "https://github.com/bcrowe/cakephp-api-pagination", "license": "MIT", @@ -16,13 +16,13 @@ } ], "require": { - "php": ">=5.6", - "cakephp/cakephp": "~3.6" + "php": ">=7.2", + "cakephp/cakephp": "^4.2" }, "require-dev": { - "phpunit/phpunit" : "~5.0", - "scrutinizer/ocular": "1.1", - "squizlabs/php_codesniffer": "~2.3.0" + "phpunit/phpunit" : "^8.5.23", + "scrutinizer/ocular": "1.7", + "cakephp/cakephp-codesniffer": "^4.7" }, "autoload": { "psr-4": { @@ -37,11 +37,18 @@ } }, "scripts": { - "test": "phpunit" + "test": "phpunit", + "cs-check": "phpcs src/ tests/", + "cs-fix": "phpcbf src/ tests/" }, "extra": { "branch-alias": { "dev-master": "1.0-dev" } + }, + "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } } } 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..5ce9bef 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -16,8 +16,7 @@ + class="\Cake\TestSuite\Fixture\FixtureInjector"> @@ -31,7 +30,7 @@ - + diff --git a/src/Controller/Component/ApiPaginationComponent.php b/src/Controller/Component/ApiPaginationComponent.php index b5944ea..bdf4168 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,11 @@ public function beforeRender(Event $event) } $subject = $event->getSubject(); - $this->pagingInfo = $this->request->getParam('paging')[$subject->getName()]; + $modelName = ucfirst($this->getConfig('model', $subject->getName())); + if (isset($this->getController()->getRequest()->getAttribute('paging')[$modelName])) { + $this->pagingInfo = $this->getController()->getRequest()->getAttribute('paging')[$modelName]; + } + $config = $this->getConfig(); if (!empty($config['aliases'])) { @@ -55,7 +61,12 @@ public function beforeRender(Event $event) } $subject->set($config['key'], $this->pagingInfo); - $subject->viewVars['_serialize'][] = $config['key']; + $data = $subject->viewBuilder()->getOption('serialize') ?? []; + + if (is_array($data)) { + $data[] = $config['key']; + $subject->viewBuilder()->setOption('serialize', $data); + } } /** @@ -96,8 +107,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..dca49ce 100644 --- a/tests/Fixture/ArticlesFixture.php +++ b/tests/Fixture/ArticlesFixture.php @@ -7,13 +7,6 @@ class ArticlesFixture extends TestFixture { public $table = 'bryancrowe_articles'; - public $fields = [ - 'id' => ['type' => 'integer'], - 'title' => ['type' => 'string', 'null' => false], - 'body' => 'text', - '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]] - ]; - public $records = [ ['title' => 'Post #1', 'body' => 'This is the article body.'], ['title' => 'Post #2', 'body' => 'This is the article body.'], @@ -37,6 +30,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/Schema/articles.sql b/tests/Schema/articles.sql new file mode 100644 index 0000000..0d3baf8 --- /dev/null +++ b/tests/Schema/articles.sql @@ -0,0 +1,5 @@ +CREATE TABLE `bryancrowe_articles` ( + `id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, + `title` varchar(255) NOT NULL, + `body` text NOT NULL +); diff --git a/tests/TestCase/Controller/Component/ApiPaginationComponentOnNonConventionalControllerNameTest.php b/tests/TestCase/Controller/Component/ApiPaginationComponentOnNonConventionalControllerNameTest.php new file mode 100644 index 0000000..d158cfc --- /dev/null +++ b/tests/TestCase/Controller/Component/ApiPaginationComponentOnNonConventionalControllerNameTest.php @@ -0,0 +1,112 @@ +request = new Request(['url' => '/articles']); + $this->response = $this->createMock('Cake\Http\Response'); + $this->controller = new ArticlesIndexController($this->request, $this->response); + $this->Articles = TableRegistry::getTableLocator()->get('BryanCrowe/ApiPagination.Articles', ['table' => 'bryancrowe_articles']); + parent::setUp(); + } + + /** + * tearDown method + * + * @return void + */ + public function tearDown(): void + { + parent::tearDown(); + } + + /** + * Test that a non conventional controller name is supported using the 'model' config. + * + * @dataProvider dataForTestVariousModelValueOnNonConventionalController + * @param array $config + * @param $expected + * @return void + */ + public function testVariousModelValueOnNonConventionalController(array $config, $expected) + { + $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(), $config); + $event = new Event('Controller.beforeRender', $this->controller); + $apiPaginationComponent->beforeRender($event); + + $result = $apiPaginationComponent->getController()->viewBuilder()->getVar('pagination'); + $this->assertSame($expected, $result); + } + + /** + * If the name of the paginated model is not specified, the result of the pagination + * on a controller not having the same name as the model fails. + * + * @return array[] + */ + public function dataForTestVariousModelValueOnNonConventionalController(): array + { + return [ + [[], []], + [['model' => 'Articles'], $this->getDefaultPagination()], + [['model' => 'articles'], $this->getDefaultPagination()], + [['model' => 'NonExistingModel'], []], + ]; + } + + /** + * Returns the standard pagination result. + * + * @return array + */ + private function getDefaultPagination(): array + { + return [ + 'count' => 23, + 'current' => 20, + 'perPage' => 20, + 'page' => 1, + 'requestedPage' => 1, + 'pageCount' => 2, + 'start' => 1, + 'end' => 20, + 'prevPage' => false, + 'nextPage' => true, + 'sort' => null, + 'direction' => null, + 'sortDefault' => false, + 'directionDefault' => false, + 'completeSort' => [], + 'limit' => null, + 'scope' => null, + 'finder' => 'all', + ]; + } +} diff --git a/tests/TestCase/Controller/Component/ApiPaginationComponentTest.php b/tests/TestCase/Controller/Component/ApiPaginationComponentTest.php index be75d76..2d13563 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']); + $this->Articles = TableRegistry::getTableLocator()->get('BryanCrowe/ApiPagination.Articles', ['table' => 'bryancrowe_articles']); parent::setUp(); } @@ -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..bc2f5cd 100644 --- a/tests/test_app/TestApp/Controller/ArticlesController.php +++ b/tests/test_app/TestApp/Controller/ArticlesController.php @@ -1,13 +1,14 @@ loadComponent('Paginator'); } } diff --git a/tests/test_app/TestApp/Controller/ArticlesIndexController.php b/tests/test_app/TestApp/Controller/ArticlesIndexController.php new file mode 100644 index 0000000..45822d9 --- /dev/null +++ b/tests/test_app/TestApp/Controller/ArticlesIndexController.php @@ -0,0 +1,14 @@ +