From b659f7a6bae9769b2cf6bf42fec213dde9850370 Mon Sep 17 00:00:00 2001 From: mscherer Date: Fri, 20 Jan 2023 20:04:23 +0100 Subject: [PATCH 01/13] Init Cake5 upgrade. --- .github/workflows/ci.yml | 12 +++++----- composer.json | 33 +++++++++++++------------- phpstan.neon | 1 + phpunit.xml.dist | 23 +++++++----------- src/Plugin.php | 9 +++---- src/View/CsvView.php | 18 +++++++------- tests/TestCase/View/CsvViewTest.php | 13 +++++----- tests/bootstrap.php | 6 +++++ tests/test_app/TestApp/Application.php | 3 ++- 9 files changed, 61 insertions(+), 57 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9ea52ef..edeb0d8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,10 +14,10 @@ jobs: strategy: fail-fast: false matrix: - php-version: ['7.4', '8.2'] + php-version: ['8.1', '8.2'] prefer-lowest: [''] include: - - php-version: '7.4' + - php-version: '8.1' prefer-lowest: 'prefer-lowest' steps: @@ -53,19 +53,19 @@ jobs: fi - name: Configure PHPUnit matcher - if: matrix.php-version == '7.4' + if: matrix.php-version == '8.1' uses: mheap/phpunit-matcher-action@main - name: Run PHPUnit run: | - if [[ ${{ matrix.php-version }} == '7.4' ]]; then + if [[ ${{ matrix.php-version }} == '8.1' ]]; then export CODECOVERAGE=1 && vendor/bin/phpunit --verbose --coverage-clover=coverage.xml else vendor/bin/phpunit fi - name: Submit code coverage - if: success() && matrix.php-version == '7.4' + if: success() && matrix.php-version == '8.1' uses: codecov/codecov-action@v3 cs-stan: @@ -78,7 +78,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '7.4' + php-version: '8.1' extensions: mbstring, intl tools: cs2pr coverage: none diff --git a/composer.json b/composer.json index d401ee2..d81d2aa 100644 --- a/composer.json +++ b/composer.json @@ -18,38 +18,38 @@ "role": "Maintainer" }, { - "name":"ADmad", - "role":"Contributor", + "name": "ADmad", + "role": "Contributor", "homepage": "https://github.com/admad" }, { - "name":"Mark Scherer", - "role":"Contributor", + "name": "Mark Scherer", + "role": "Contributor", "homepage": "https://github.com/dereuromark" }, { - "name":"Joshua Paling", - "role":"Contributor", + "name": "Joshua Paling", + "role": "Contributor", "homepage": "https://github.com/joshuapaling" }, { - "name":"Gaurish Sharma", - "role":"Contributor", + "name": "Gaurish Sharma", + "role": "Contributor", "homepage": "https://github.com/gaurish" }, { - "name":"Gregory Gaskill", - "role":"Contributor", - "homepage":"https://github.com/chronon" + "name": "Gregory Gaskill", + "role": "Contributor", + "homepage": "https://github.com/chronon" } ], - "require":{ - "php": ">=7.4", - "cakephp/cakephp": "^4.2" + "require": { + "php": ">=8.1", + "cakephp/cakephp": "5.x-dev" }, "require-dev": { "phpunit/phpunit": "^9.5.0", - "cakephp/cakephp-codesniffer": "^4.0" + "cakephp/cakephp-codesniffer": "^5.0" }, "autoload": { "psr-4": { @@ -85,5 +85,6 @@ "allow-plugins": { "dealerdirect/phpcodesniffer-composer-installer": true } - } + }, + "minimum-stability": "dev" } diff --git a/phpstan.neon b/phpstan.neon index 91bea65..89d9c92 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,6 +1,7 @@ parameters: level: 8 checkMissingIterableValueType: false + checkGenericClassInNonGenericObjectType: false paths: - src/ bootstrapFiles: diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 9910dce..49a14eb 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,8 +1,5 @@ - + @@ -12,21 +9,17 @@ - - - - - - - + + + - - + + src/ src/Plugin.php - - + + diff --git a/src/Plugin.php b/src/Plugin.php index 33a783c..c62c904 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -16,21 +16,21 @@ class Plugin extends BasePlugin * * @var string */ - protected $name = 'CsvView'; + protected ?string $name = 'CsvView'; /** * Load routes or not * * @var bool */ - protected $routesEnabled = false; + protected bool $routesEnabled = false; /** * Console middleware * * @var bool */ - protected $consoleEnabled = false; + protected bool $consoleEnabled = false; /** * @inheritDoc @@ -42,7 +42,8 @@ public function bootstrap(PluginApplicationInterface $app): void * * @link https://book.cakephp.org/4/en/controllers/components/request-handling.html#using-custom-viewclasses */ - EventManager::instance()->on('Controller.initialize', function (EventInterface $event) { + EventManager::instance()->on('Controller.initialize', function (EventInterface $event): void { + /** @var \Cake\Controller\Controller $controller */ $controller = $event->getSubject(); if ($controller->components()->has('RequestHandler')) { $controller->RequestHandler->setConfig('viewClassMap.csv', 'CsvView.Csv'); diff --git a/src/View/CsvView.php b/src/View/CsvView.php index 5c6ebf9..def9ca2 100644 --- a/src/View/CsvView.php +++ b/src/View/CsvView.php @@ -67,7 +67,7 @@ class CsvView extends SerializedView * * @var string */ - protected $layoutPath = 'csv'; + protected string $layoutPath = 'csv'; /** * CSV views are always located in the 'csv' sub directory for a @@ -75,21 +75,21 @@ class CsvView extends SerializedView * * @var string */ - protected $subDir = 'csv'; + protected string $subDir = 'csv'; /** * Response type. * * @var string */ - protected $_responseType = 'text/csv'; + protected string $_responseType = 'text/csv'; /** * Whether or not to reset static variables in use * * @var bool */ - protected $_resetStaticVariables = false; + protected bool $_resetStaticVariables = false; /** * Iconv extension. @@ -110,14 +110,14 @@ class CsvView extends SerializedView * * @var array */ - protected $bomMap; + protected array $bomMap; /** * BOM first appearance * * @var bool */ - protected $isFirstBom = true; + protected bool $isFirstBom = true; /** * Default config. @@ -146,7 +146,7 @@ class CsvView extends SerializedView * * @var array */ - protected $_defaultConfig = [ + protected array $_defaultConfig = [ 'extract' => null, 'footer' => null, 'header' => null, @@ -195,7 +195,7 @@ public function initialize(): void * need(s) to be serialized * @return string The serialized data or false. */ - protected function _serialize($serialize): string + protected function _serialize(array|string $serialize): string { $this->_renderRow($this->getConfig('header')); $this->_renderContent(); @@ -296,7 +296,7 @@ protected function _renderRow(?array $row = null): string * @param array|null $row Row data * @return string|false String with the row in csv-syntax, false on fputscv failure */ - protected function _generateRow(?array $row = null) + protected function _generateRow(?array $row = null): string|false { static $fp = false; diff --git a/tests/TestCase/View/CsvViewTest.php b/tests/TestCase/View/CsvViewTest.php index ebaab48..553c5c2 100644 --- a/tests/TestCase/View/CsvViewTest.php +++ b/tests/TestCase/View/CsvViewTest.php @@ -5,17 +5,18 @@ use Cake\Http\Response; use Cake\Http\ServerRequest as Request; -use Cake\I18n\FrozenTime; +use Cake\I18n\DateTime; use Cake\ORM\TableRegistry; use Cake\TestSuite\TestCase; use CsvView\View\CsvView; +use Exception; /** * CsvViewTest */ class CsvViewTest extends TestCase { - public $fixtures = ['core.Articles', 'core.Authors']; + protected array $fixtures = ['core.Articles', 'core.Authors']; /** * @var \CsvView\View\CsvView @@ -34,7 +35,7 @@ class CsvViewTest extends TestCase public function setUp(): void { - FrozenTime::setToStringFormat('yyyy-MM-dd HH:mm:ss'); + DateTime::setToStringFormat('yyyy-MM-dd HH:mm:ss'); $this->request = new Request(); $this->response = new Response(); @@ -267,7 +268,7 @@ public function testRenderViaExtract() [ 'User' => [ 'username' => 'jose', - 'created' => new FrozenTime('2010-01-05'), + 'created' => new DateTime('2010-01-05'), ], 'Item' => [ 'name' => 'beach', @@ -343,7 +344,7 @@ public function testRenderViaExtractWithCallable() $data = [ [ 'username' => 'jose', - 'created' => new FrozenTime('2010-01-05'), + 'created' => new DateTime('2010-01-05'), 'item' => [ 'name' => 'beach', ], @@ -511,7 +512,7 @@ public function testRenderWithCustomNull() */ public function testInvalidViewVarThrowsException() { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->view->set(['data' => 'invaliddata']); $this->view->setConfig('serialize', 'data'); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index f2f2e40..17be3de 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -37,7 +37,13 @@ } Configure::write('App', [ 'namespace' => 'CsvView\Test\App', + 'encoding' => 'UTF-8', 'paths' => [ 'templates' => [dirname(__FILE__) . DS . 'test_app' . DS . 'templates' . DS], ], ]); + +if (env('FIXTURE_SCHEMA_METADATA')) { + $loader = new Cake\TestSuite\Fixture\SchemaLoader(); + $loader->loadInternalFile(env('FIXTURE_SCHEMA_METADATA')); +} diff --git a/tests/test_app/TestApp/Application.php b/tests/test_app/TestApp/Application.php index fca4cdc..d999115 100644 --- a/tests/test_app/TestApp/Application.php +++ b/tests/test_app/TestApp/Application.php @@ -18,6 +18,7 @@ use Cake\Error\Middleware\ErrorHandlerMiddleware; use Cake\Http\BaseApplication; +use Cake\Http\MiddlewareQueue; use Cake\Routing\Middleware\AssetMiddleware; use Cake\Routing\Middleware\RoutingMiddleware; @@ -49,7 +50,7 @@ public function bootstrap(): void * @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to setup. * @return \Cake\Http\MiddlewareQueue The updated middleware queue. */ - public function middleware(\Cake\Http\MiddlewareQueue $middlewareQueue): \Cake\Http\MiddlewareQueue + public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue { $middlewareQueue // Catch any exceptions in the lower layers, From 1bb8e537d0f2fb962caba9a142b8558882d6dff9 Mon Sep 17 00:00:00 2001 From: ADmad Date: Mon, 23 Jan 2023 22:40:38 +0530 Subject: [PATCH 02/13] Update composer.json --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index d81d2aa..8660501 100644 --- a/composer.json +++ b/composer.json @@ -86,5 +86,6 @@ "dealerdirect/phpcodesniffer-composer-installer": true } }, - "minimum-stability": "dev" + "minimum-stability": "dev", + "prefer-stable": true } From 9b57a121c286a587ce24e9ff2b31b4b31e38b6eb Mon Sep 17 00:00:00 2001 From: ADmad Date: Mon, 23 Jan 2023 22:42:52 +0530 Subject: [PATCH 03/13] Update CI action to use common jobs. --- .github/workflows/ci.yml | 102 +++------------------------------------ 1 file changed, 8 insertions(+), 94 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index edeb0d8..2e410c8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,105 +4,19 @@ on: push: branches: - 4.x + - 5.x pull_request: branches: - '*' +permissions: + contents: read + jobs: testsuite: - runs-on: ubuntu-22.04 - strategy: - fail-fast: false - matrix: - php-version: ['8.1', '8.2'] - prefer-lowest: [''] - include: - - php-version: '8.1' - prefer-lowest: 'prefer-lowest' - - steps: - - uses: actions/checkout@v3 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-version }} - extensions: mbstring, intl - coverage: pcov - - - name: Get composer cache directory - id: composer-cache - run: echo "::set-output name=dir::$(composer config cache-files-dir)" - - - name: Get date part for cache key - id: key-date - run: echo "::set-output name=date::$(date +'%Y-%m')" - - - name: Cache composer dependencies - uses: actions/cache@v3 - with: - path: ${{ steps.composer-cache.outputs.dir }} - key: ${{ runner.os }}-composer-${{ steps.key-date.outputs.date }}-${{ hashFiles('composer.json') }}-${{ matrix.prefer-lowest }} - - - name: Composer Install - run: | - if ${{ matrix.prefer-lowest == 'prefer-lowest' }}; then - composer update --prefer-lowest --prefer-stable - else - composer update - fi - - - name: Configure PHPUnit matcher - if: matrix.php-version == '8.1' - uses: mheap/phpunit-matcher-action@main - - - name: Run PHPUnit - run: | - if [[ ${{ matrix.php-version }} == '8.1' ]]; then - export CODECOVERAGE=1 && vendor/bin/phpunit --verbose --coverage-clover=coverage.xml - else - vendor/bin/phpunit - fi - - - name: Submit code coverage - if: success() && matrix.php-version == '8.1' - uses: codecov/codecov-action@v3 + uses: cakephp/.github/.github/workflows/testsuite-with-db.yml@5.x + secrets: inherit cs-stan: - name: Coding Standard & Static Analysis - runs-on: ubuntu-22.04 - - steps: - - uses: actions/checkout@v3 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: '8.1' - extensions: mbstring, intl - tools: cs2pr - coverage: none - - - name: Get composer cache directory - id: composer-cache - run: echo "::set-output name=dir::$(composer config cache-files-dir)" - - - name: Get date part for cache key - id: key-date - run: echo "::set-output name=date::$(date +'%Y-%m')" - - - name: Cache composer dependencies - uses: actions/cache@v3 - with: - path: ${{ steps.composer-cache.outputs.dir }} - key: ${{ runner.os }}-composer-${{ steps.key-date.outputs.date }}-${{ hashFiles('composer.json') }}-${{ matrix.prefer-lowest }} - - - name: Composer install - run: composer stan-setup - - - name: Run PHP CodeSniffer - run: composer cs-check - - - name: Run phpstan - if: success() || failure() - run: vendor/bin/phpstan analyse --error-format=github + uses: cakephp/.github/.github/workflows/cs-stan.yml@5.x + secrets: inherit From 1c6f1c2ce4bdf0013729401851761dd58d95e7d3 Mon Sep 17 00:00:00 2001 From: ADmad Date: Mon, 23 Jan 2023 22:58:21 +0530 Subject: [PATCH 04/13] Use new fixture schema loading. --- phpunit.xml.dist | 6 ++-- tests/Fixture/ArticlesFixture.php | 13 +++++++ tests/Fixture/AuthorsFixture.php | 14 ++++++++ tests/TestCase/View/CsvViewTest.php | 7 ++-- tests/schema.php | 53 +++++++++++++++++++++++++++++ 5 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 tests/Fixture/ArticlesFixture.php create mode 100644 tests/Fixture/AuthorsFixture.php create mode 100644 tests/schema.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 49a14eb..027d1d4 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -16,10 +16,10 @@ src/ - - src/Plugin.php - + + + diff --git a/tests/Fixture/ArticlesFixture.php b/tests/Fixture/ArticlesFixture.php new file mode 100644 index 0000000..ba27cd2 --- /dev/null +++ b/tests/Fixture/ArticlesFixture.php @@ -0,0 +1,13 @@ + 1, 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y'], + ['author_id' => 3, 'title' => 'Second Article', 'body' => 'Second Article Body', 'published' => 'Y'], + ['author_id' => 1, 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y'], + ]; +} diff --git a/tests/Fixture/AuthorsFixture.php b/tests/Fixture/AuthorsFixture.php new file mode 100644 index 0000000..24ef38d --- /dev/null +++ b/tests/Fixture/AuthorsFixture.php @@ -0,0 +1,14 @@ + 'mariano'], + ['name' => 'nate'], + ['name' => 'larry'], + ['name' => 'garrett'], + ]; +} diff --git a/tests/TestCase/View/CsvViewTest.php b/tests/TestCase/View/CsvViewTest.php index 553c5c2..7da9a68 100644 --- a/tests/TestCase/View/CsvViewTest.php +++ b/tests/TestCase/View/CsvViewTest.php @@ -6,7 +6,6 @@ use Cake\Http\Response; use Cake\Http\ServerRequest as Request; use Cake\I18n\DateTime; -use Cake\ORM\TableRegistry; use Cake\TestSuite\TestCase; use CsvView\View\CsvView; use Exception; @@ -16,7 +15,7 @@ */ class CsvViewTest extends TestCase { - protected array $fixtures = ['core.Articles', 'core.Authors']; + protected array $fixtures = ['plugin.CsvView.Articles', 'plugin.CsvView.Authors']; /** * @var \CsvView\View\CsvView @@ -35,6 +34,8 @@ class CsvViewTest extends TestCase public function setUp(): void { + parent::setUp(); + DateTime::setToStringFormat('yyyy-MM-dd HH:mm:ss'); $this->request = new Request(); @@ -432,7 +433,7 @@ public function testRenderWithSpecialCharacters() */ public function testPassingQueryAsData() { - $articles = TableRegistry::getTableLocator()->get('Articles'); + $articles = $this->getTableLocator()->get('Articles'); $query = $articles->find(); $this->view->set(['data' => $query]) diff --git a/tests/schema.php b/tests/schema.php new file mode 100644 index 0000000..93062fe --- /dev/null +++ b/tests/schema.php @@ -0,0 +1,53 @@ + [ + 'columns' => [ + 'id' => [ + 'type' => 'integer', + ], + 'author_id' => [ + 'type' => 'integer', + 'null' => true, + ], + 'title' => [ + 'type' => 'string', + 'null' => true, + ], + 'body' => 'text', + 'published' => [ + 'type' => 'string', + 'length' => 1, + 'default' => 'N', + ], + ], + 'constraints' => [ + 'primary' => [ + 'type' => 'primary', + 'columns' => [ + 'id', + ], + ], + ], + ], + 'authors' => [ + 'columns' => [ + 'id' => [ + 'type' => 'integer', + ], + 'name' => [ + 'type' => 'string', + 'default' => null, + ], + ], + 'constraints' => [ + 'primary' => [ + 'type' => 'primary', + 'columns' => [ + 'id', + ], + ], + ], + ], +]; From c963faaed34165719a8f2e7d1c9f615c177ad227 Mon Sep 17 00:00:00 2001 From: ADmad Date: Mon, 23 Jan 2023 23:04:44 +0530 Subject: [PATCH 05/13] Update CsvView class to fix test failures. --- src/Plugin.php | 15 --------------- src/View/CsvView.php | 17 ++++++++++------- 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/src/Plugin.php b/src/Plugin.php index c62c904..211b0eb 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -5,8 +5,6 @@ use Cake\Core\BasePlugin; use Cake\Core\PluginApplicationInterface; -use Cake\Event\EventInterface; -use Cake\Event\EventManager; use Cake\Http\ServerRequest; class Plugin extends BasePlugin @@ -37,19 +35,6 @@ class Plugin extends BasePlugin */ public function bootstrap(PluginApplicationInterface $app): void { - /** - * Add CsvView to View class map through RequestHandler, if available, on Controller initialisation - * - * @link https://book.cakephp.org/4/en/controllers/components/request-handling.html#using-custom-viewclasses - */ - EventManager::instance()->on('Controller.initialize', function (EventInterface $event): void { - /** @var \Cake\Controller\Controller $controller */ - $controller = $event->getSubject(); - if ($controller->components()->has('RequestHandler')) { - $controller->RequestHandler->setConfig('viewClassMap.csv', 'CsvView.Csv'); - } - }); - /** * Add a request detector named "csv" to check whether the request was for a CSV, * either through accept header or file extension diff --git a/src/View/CsvView.php b/src/View/CsvView.php index def9ca2..fc5095f 100644 --- a/src/View/CsvView.php +++ b/src/View/CsvView.php @@ -77,13 +77,6 @@ class CsvView extends SerializedView */ protected string $subDir = 'csv'; - /** - * Response type. - * - * @var string - */ - protected string $_responseType = 'text/csv'; - /** * Whether or not to reset static variables in use * @@ -188,6 +181,16 @@ public function initialize(): void parent::initialize(); } + /** + * Mime-type this view class renders as. + * + * @return string The JSON content type. + */ + public static function contentType(): string + { + return 'text/csv'; + } + /** * Serialize view vars. * From 3ff1973361b301b75c918e07f3a73bb3bb1bdef3 Mon Sep 17 00:00:00 2001 From: ADmad Date: Mon, 23 Jan 2023 23:06:58 +0530 Subject: [PATCH 06/13] Update main plugin class. --- src/{Plugin.php => CsvViewPlugin.php} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/{Plugin.php => CsvViewPlugin.php} (96%) diff --git a/src/Plugin.php b/src/CsvViewPlugin.php similarity index 96% rename from src/Plugin.php rename to src/CsvViewPlugin.php index 211b0eb..e3755c3 100644 --- a/src/Plugin.php +++ b/src/CsvViewPlugin.php @@ -7,7 +7,7 @@ use Cake\Core\PluginApplicationInterface; use Cake\Http\ServerRequest; -class Plugin extends BasePlugin +class CsvViewPlugin extends BasePlugin { /** * Plugin name. From d20c09dbe81903322d14367e6b61889f984b5e3f Mon Sep 17 00:00:00 2001 From: ADmad Date: Mon, 23 Jan 2023 23:11:12 +0530 Subject: [PATCH 07/13] Update readme --- README.md | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 18c5d80..c0982db 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Quickly enable CSV output of your model data. -This branch is for CakePHP **4.x**. For details see [version map](https://github.com/FriendsOfCake/cakephp-csvview/wiki#cakephp-version-map). +This branch is for CakePHP **5.x**. For details see [version map](https://github.com/FriendsOfCake/cakephp-csvview/wiki#cakephp-version-map). ## Background @@ -182,18 +182,20 @@ to set how null values should be displayed in the CSV. #### Automatic view class switching -You can use router's extension parsing feature and the `RequestHandlerComponent` to -automatically have the CsvView class switched in as follows. +You can use the controller content negotiation feature to automatically have the +CsvView class switched in as follows. -Enable `csv` extension parsing for all routes using `Router::extensions('csv')` -in your app's `routes.php` or using `$routes->addExtensions()` within required -scope. +Enable `csv` extension parsing using `$routes->addExtensions(['csv'])` within required +scope in your app's `routes.php`. ```php // PostsController.php -// In your controller's initialize() method: -$this->loadComponent('RequestHandler'); +// Override the viewClasses method of your controller: +public function viewClasses() +{ + return ['csv' => 'CsvView.Csv']; +} // Controller action public function index() From d662d177180123da3e4ad477269742ee77c88c73 Mon Sep 17 00:00:00 2001 From: ADmad Date: Mon, 23 Jan 2023 23:35:09 +0530 Subject: [PATCH 08/13] Try to fix phpcs job --- phpcs.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpcs.xml b/phpcs.xml index ca08438..e747af8 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -1,7 +1,7 @@ - + - + src/ tests/ From 76e7c73b01ffcbca5abdaf9ad557869a816bad41 Mon Sep 17 00:00:00 2001 From: ADmad Date: Mon, 23 Jan 2023 23:37:44 +0530 Subject: [PATCH 09/13] Drop stickler --- .gitattributes | 4 +--- .stickler.yml | 10 ---------- 2 files changed, 1 insertion(+), 13 deletions(-) delete mode 100644 .stickler.yml diff --git a/.gitattributes b/.gitattributes index 3461ac8..c5678cd 100644 --- a/.gitattributes +++ b/.gitattributes @@ -12,9 +12,7 @@ tests export-ignore .editorconfig export-ignore .gitattributes export-ignore .gitignore export-ignore -.stickler.yml export-ignore -.travis.yml export-ignore -phpcs.xml.dist export-ignore +phpcs.xml export-ignore phpstan.neon export-ignore phpstan-baseline.neon export-ignore phpunit.xml.dist export-ignore diff --git a/.stickler.yml b/.stickler.yml deleted file mode 100644 index b8f57db..0000000 --- a/.stickler.yml +++ /dev/null @@ -1,10 +0,0 @@ ---- -linters: - phpcs: - standard: CakePHP4 - extensions: 'php' - fixer: true - -fixers: - enable: true - workflow: commit From e998245e0fce3c56c1bf5306f8e1cdc7be1a17e1 Mon Sep 17 00:00:00 2001 From: ADmad Date: Mon, 23 Jan 2023 23:41:10 +0530 Subject: [PATCH 10/13] Fix grammar --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c0982db..19e51dc 100644 --- a/README.md +++ b/README.md @@ -182,8 +182,8 @@ to set how null values should be displayed in the CSV. #### Automatic view class switching -You can use the controller content negotiation feature to automatically have the -CsvView class switched in as follows. +You can use the controller's content negotiation feature to automatically have +the CsvView class switched in as follows. Enable `csv` extension parsing using `$routes->addExtensions(['csv'])` within required scope in your app's `routes.php`. From 2cac2cf386bc86265c557ee02b1df25a6baf6a1f Mon Sep 17 00:00:00 2001 From: ADmad Date: Mon, 23 Jan 2023 23:48:37 +0530 Subject: [PATCH 11/13] Harmonize exception class. --- src/View/CsvView.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/View/CsvView.php b/src/View/CsvView.php index fc5095f..5286c3f 100644 --- a/src/View/CsvView.php +++ b/src/View/CsvView.php @@ -7,7 +7,6 @@ use Cake\Datasource\EntityInterface; use Cake\Utility\Hash; use Cake\View\SerializedView; -use Exception; /** * A view class that is used for CSV responses. @@ -214,7 +213,7 @@ protected function _serialize(array|string $serialize): string * Renders the body of the data to the csv * * @return void - * @throws \Exception + * @throws \Cake\Core\Exception\CakeException */ protected function _renderContent(): void { @@ -227,7 +226,7 @@ protected function _renderContent(): void foreach ((array)$serialize as $viewVar) { if (is_scalar($this->viewVars[$viewVar])) { - throw new Exception("'" . $viewVar . "' is not an array or iteratable object."); + throw new CakeException("'" . $viewVar . "' is not an array or iteratable object."); } foreach ($this->viewVars[$viewVar] as $_data) { From 0cf63fac25a63778228c764e7b7d773fa160b5a2 Mon Sep 17 00:00:00 2001 From: ADmad Date: Mon, 23 Jan 2023 23:48:54 +0530 Subject: [PATCH 12/13] Fix docblock --- src/View/CsvView.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/View/CsvView.php b/src/View/CsvView.php index 5286c3f..88b1d3b 100644 --- a/src/View/CsvView.php +++ b/src/View/CsvView.php @@ -183,7 +183,7 @@ public function initialize(): void /** * Mime-type this view class renders as. * - * @return string The JSON content type. + * @return string The CSV content type. */ public static function contentType(): string { From cfec296f9c0005e343e9525803440936489e7a39 Mon Sep 17 00:00:00 2001 From: ADmad Date: Fri, 13 Oct 2023 23:38:21 +0530 Subject: [PATCH 13/13] Bump up to CakePHP 5 stable. Other misc. changes. --- .gitattributes | 1 + .phive/phars.xml | 5 +++++ README.md | 8 +++++--- composer.json | 9 +++------ phpunit.xml.dist | 22 ++++++++-------------- tests/TestCase/View/CsvViewTest.php | 2 +- tests/bootstrap.php | 7 ++++--- 7 files changed, 27 insertions(+), 27 deletions(-) create mode 100644 .phive/phars.xml diff --git a/.gitattributes b/.gitattributes index c5678cd..9eeb392 100644 --- a/.gitattributes +++ b/.gitattributes @@ -18,3 +18,4 @@ phpstan-baseline.neon export-ignore phpunit.xml.dist export-ignore psalm.xml export-ignore psalm-baseline.xml export-ignore +.phive export-ignore diff --git a/.phive/phars.xml b/.phive/phars.xml new file mode 100644 index 0000000..d2fe06f --- /dev/null +++ b/.phive/phars.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/README.md b/README.md index 19e51dc..caa0f5e 100644 --- a/README.md +++ b/README.md @@ -191,10 +191,12 @@ scope in your app's `routes.php`. ```php // PostsController.php -// Override the viewClasses method of your controller: -public function viewClasses() +// Add the CsvView class for content type negotiation +public function initialize(): void { - return ['csv' => 'CsvView.Csv']; + parent::initialize(); + + $this->addViewClasses(['csv' => 'CsvView.Csv']); } // Controller action diff --git a/composer.json b/composer.json index 8660501..c0bbb6a 100644 --- a/composer.json +++ b/composer.json @@ -44,11 +44,10 @@ } ], "require": { - "php": ">=8.1", - "cakephp/cakephp": "5.x-dev" + "cakephp/cakephp": "^5.0" }, "require-dev": { - "phpunit/phpunit": "^9.5.0", + "phpunit/phpunit": "^10.1", "cakephp/cakephp-codesniffer": "^5.0" }, "autoload": { @@ -85,7 +84,5 @@ "allow-plugins": { "dealerdirect/phpcodesniffer-composer-installer": true } - }, - "minimum-stability": "dev", - "prefer-stable": true + } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 027d1d4..b3a3168 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,25 +1,19 @@ - - - + - tests/TestCase/ + tests/TestCase/ - - - + - - - - src/ - - - + + + src/ + + diff --git a/tests/TestCase/View/CsvViewTest.php b/tests/TestCase/View/CsvViewTest.php index 7da9a68..4dad49b 100644 --- a/tests/TestCase/View/CsvViewTest.php +++ b/tests/TestCase/View/CsvViewTest.php @@ -441,7 +441,7 @@ public function testPassingQueryAsData() $output = $this->view->render(); $articles->belongsTo('Authors'); - $query = $articles->find('all', ['contain' => 'Authors']); + $query = $articles->find('all', contain: 'Authors'); $_extract = ['title', 'body', 'author.name']; $this->view->set(['data' => $query]) ->setConfig(['extract' => $_extract, 'serialize' => 'data']); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 17be3de..168d2bf 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -2,6 +2,7 @@ declare(strict_types=1); use Cake\Core\Configure; +use Cake\TestSuite\Fixture\SchemaLoader; /** * Test suite bootstrap @@ -43,7 +44,7 @@ ], ]); -if (env('FIXTURE_SCHEMA_METADATA')) { - $loader = new Cake\TestSuite\Fixture\SchemaLoader(); - $loader->loadInternalFile(env('FIXTURE_SCHEMA_METADATA')); +if (getenv('FIXTURE_SCHEMA_METADATA')) { + $loader = new SchemaLoader(); + $loader->loadInternalFile(getenv('FIXTURE_SCHEMA_METADATA')); }