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/.travis.yml b/.travis.yml deleted file mode 100644 index b4c3966..0000000 --- a/.travis.yml +++ /dev/null @@ -1,27 +0,0 @@ -language: php - -env: - global: - - PHPCS=0 - - RUN_TESTS=1 - -php: - - 7.3 - -sudo: false - -matrix: - include: - - php: 7.2 - 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 ./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 f2b1cde..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 4 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 @@ -161,13 +163,13 @@ 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 diff --git a/composer.json b/composer.json index 5de9c1f..f492cf4 100644 --- a/composer.json +++ b/composer.json @@ -17,12 +17,12 @@ ], "require": { "php": ">=7.2", - "cakephp/cakephp": "^4.0" + "cakephp/cakephp": "^4.2" }, "require-dev": { - "phpunit/phpunit" : "^8.5", + "phpunit/phpunit" : "^8.5.23", "scrutinizer/ocular": "1.7", - "cakephp/cakephp-codesniffer": "~4.0.0" + "cakephp/cakephp-codesniffer": "^4.7" }, "autoload": { "psr-4": { @@ -45,5 +45,10 @@ "branch-alias": { "dev-master": "1.0-dev" } + }, + "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 1136eba..5ce9bef 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -16,8 +16,7 @@ + class="\Cake\TestSuite\Fixture\FixtureInjector"> diff --git a/src/Controller/Component/ApiPaginationComponent.php b/src/Controller/Component/ApiPaginationComponent.php index 34057a8..bdf4168 100644 --- a/src/Controller/Component/ApiPaginationComponent.php +++ b/src/Controller/Component/ApiPaginationComponent.php @@ -46,7 +46,10 @@ public function beforeRender(Event $event) $subject = $event->getSubject(); $modelName = ucfirst($this->getConfig('model', $subject->getName())); - $this->pagingInfo = $this->getController()->getRequest()->getAttribute('paging')[$modelName]; + if (isset($this->getController()->getRequest()->getAttribute('paging')[$modelName])) { + $this->pagingInfo = $this->getController()->getRequest()->getAttribute('paging')[$modelName]; + } + $config = $this->getConfig(); if (!empty($config['aliases'])) { diff --git a/tests/Fixture/ArticlesFixture.php b/tests/Fixture/ArticlesFixture.php index d241bc2..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.'], 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 index 39a6c67..d158cfc 100644 --- a/tests/TestCase/Controller/Component/ApiPaginationComponentOnNonConventionalControllerNameTest.php +++ b/tests/TestCase/Controller/Component/ApiPaginationComponentOnNonConventionalControllerNameTest.php @@ -74,11 +74,10 @@ public function testVariousModelValueOnNonConventionalController(array $config, public function dataForTestVariousModelValueOnNonConventionalController(): array { return [ - - [[], null], + [[], []], [['model' => 'Articles'], $this->getDefaultPagination()], [['model' => 'articles'], $this->getDefaultPagination()], - [['model' => 'NonExistingModel'], null], + [['model' => 'NonExistingModel'], []], ]; } diff --git a/tests/test_app/TestApp/Controller/ArticlesController.php b/tests/test_app/TestApp/Controller/ArticlesController.php index 428e94c..bc2f5cd 100644 --- a/tests/test_app/TestApp/Controller/ArticlesController.php +++ b/tests/test_app/TestApp/Controller/ArticlesController.php @@ -10,6 +10,5 @@ class ArticlesController extends Controller public function initialize(): void { parent::initialize(); - $this->loadComponent('Paginator'); } } diff --git a/tests/test_app/TestApp/Controller/ArticlesIndexController.php b/tests/test_app/TestApp/Controller/ArticlesIndexController.php index a999113..45822d9 100644 --- a/tests/test_app/TestApp/Controller/ArticlesIndexController.php +++ b/tests/test_app/TestApp/Controller/ArticlesIndexController.php @@ -10,6 +10,5 @@ class ArticlesIndexController extends Controller public function initialize(): void { parent::initialize(); - $this->loadComponent('Paginator'); } }