From 92f5f3eb8ffe32180aec76ea3837ce13b861dd00 Mon Sep 17 00:00:00 2001 From: ADmad Date: Fri, 9 Jun 2023 22:45:34 +0530 Subject: [PATCH 1/3] Cake 5 updates --- .editorconfig | 5 +- .gitattributes | 4 +- .github/workflows/ci.yml | 22 +++++++ .gitignore | 1 + .travis.yml | 65 ------------------- composer.json | 16 +++-- phpstan.neon | 9 +++ phpunit.xml.dist | 33 ++++------ psalm-baseline.xml | 8 --- psalm.xml | 3 +- src/Model/Behavior/OrderlyBehavior.php | 19 +++--- src/{Plugin.php => OrderlyPlugin.php} | 12 ++-- .../Model/Behavior/OrderlyBehaviorTest.php | 33 +++++----- tests/schema.php | 35 ++++++++++ 14 files changed, 133 insertions(+), 132 deletions(-) create mode 100644 .github/workflows/ci.yml delete mode 100644 .travis.yml create mode 100644 phpstan.neon delete mode 100644 psalm-baseline.xml rename src/{Plugin.php => OrderlyPlugin.php} (59%) create mode 100644 tests/schema.php diff --git a/.editorconfig b/.editorconfig index a84f86c..41e9eef 100644 --- a/.editorconfig +++ b/.editorconfig @@ -15,4 +15,7 @@ end_of_line = crlf [*.yml] indent_style = space -indent_size = 2 \ No newline at end of file +indent_size = 2 + +[*.neon] +indent_style = tab diff --git a/.gitattributes b/.gitattributes index 2040a1c..931f1a3 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2,9 +2,9 @@ .editorconfig export-ignore .gitattributes export-ignore .gitignore export-ignore -.semver export-ignore +.github export-ignore phpunit.xml.dist export-ignore -.travis.yml export-ignore tests export-ignore psalm.xml export-ignore psalm-baseline.xml export-ignore +phpstan.neon export-ignore diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..c9ebe58 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,22 @@ +name: CI + +on: + push: + branches: + - master + - cake-5 + pull_request: + branches: + - '*' + +permissions: + contents: read + +jobs: + testsuite: + uses: ADmad/.github/.github/workflows/testsuite-with-db.yml@master + secrets: inherit + + cs-stan: + uses: ADmad/.github/.github/workflows/cs-stan.yml@master + secrets: inherit diff --git a/.gitignore b/.gitignore index 654564b..e8bd129 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /composer.lock /plugins /vendor +.phpunit.cache .phpunit.result.cache diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index bdfd9ea..0000000 --- a/.travis.yml +++ /dev/null @@ -1,65 +0,0 @@ -language: php - -php: - - 7.2 - - 7.4 - -services: - - mysql - - postgresql - -env: - matrix: - - DB=mysql db_dsn='mysql://root@127.0.0.1/cakephp_test' - - DB=pgsql db_dsn='postgres://postgres@127.0.0.1/cakephp_test' - - DB=sqlite db_dsn='sqlite:///:memory:' - - global: - - DEFAULT=1 - -matrix: - fast_finish: true - - include: - - php: 7.2 - env: PHPCS=1 DEFAULT=0 - - - php: 7.2 - env: STATIC_ANALYSIS=1 DEFAULT=0 - - - php: 7.2 - env: PREFER_LOWEST=1 - -before_script: - - if [[ $TRAVIS_PHP_VERSION != 7.2 ]]; then phpenv config-rm xdebug.ini; fi - - - if [[ $PREFER_LOWEST != 1 ]]; then composer update --no-interaction; fi - - if [[ $PREFER_LOWEST == 1 ]]; then composer update --no-interaction --prefer-lowest --prefer-stable; fi - - - if [[ $DB == 'mysql' ]]; then mysql -e 'CREATE DATABASE cakephp_test;'; fi - - if [[ $DB == 'pgsql' ]]; then psql -c 'CREATE DATABASE cakephp_test;' -U postgres; fi - - - if [[ $PHPCS == 1 ]]; then composer require cakephp/cakephp-codesniffer:^4.0; fi - -script: - - | - if [[ $DEFAULT == 1 && $TRAVIS_PHP_VERSION == 7.2 ]]; then - mkdir -p build/logs - vendor/bin/phpunit --coverage-clover=build/logs/clover.xml - fi - - if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.2 ]]; then vendor/bin/phpunit; fi - - - if [[ $PHPCS = 1 ]]; then vendor/bin/phpcs -p --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests; fi - - - if [[ $STATIC_ANALYSIS = 1 ]]; then composer require --dev psalm/phar:^3.7 && vendor/bin/psalm.phar; fi - -after_success: - - | - if [[ $DEFAULT == 1 && $TRAVIS_PHP_VERSION == 7.2 ]]; then - wget https://github.com/php-coveralls/php-coveralls/releases/download/v2.1.0/php-coveralls.phar - chmod +x php-coveralls.phar - ./php-coveralls.phar - fi - -notifications: - email: false diff --git a/composer.json b/composer.json index 7adc8a0..f991d89 100644 --- a/composer.json +++ b/composer.json @@ -30,11 +30,11 @@ "source": "https://github.com/usemuffin/orderly" }, "require": { - "cakephp/orm": "^4.0" + "cakephp/orm": "5.x-dev" }, "require-dev": { - "cakephp/cakephp": "^4.0", - "phpunit/phpunit": "~8.5.0" + "cakephp/cakephp": "5.x-dev", + "phpunit/phpunit": "^10.1" }, "autoload": { "psr-4": { @@ -46,5 +46,13 @@ "Muffin\\Orderly\\Test\\": "tests", "Cake\\Test\\Fixture\\": "vendor/cakephp/cakephp/tests/Fixture" } - } + }, + "config": { + "sort-packages": true, + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } + }, + "minimum-stability": "dev", + "prefer-stable": true } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..34cba21 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,9 @@ +parameters: + level: 7 + checkMissingIterableValueType: false + checkGenericClassInNonGenericObjectType: false + treatPhpDocTypesAsCertain: false + bootstrapFiles: + - tests/bootstrap.php + paths: + - src diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 00aa220..288487c 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,30 +1,19 @@ - - + ./tests/ - - - - - - - - - - - - + + + + + + + + ./src/ - - + + diff --git a/psalm-baseline.xml b/psalm-baseline.xml deleted file mode 100644 index d7b83aa..0000000 --- a/psalm-baseline.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - $this->_table->getDisplayField() - - - diff --git a/psalm.xml b/psalm.xml index c6b478c..ec6de6b 100644 --- a/psalm.xml +++ b/psalm.xml @@ -4,7 +4,8 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://getpsalm.org/schema/config" xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" - errorBaseline="psalm-baseline.xml" + findUnusedBaselineEntry="true" + findUnusedCode="false" > diff --git a/src/Model/Behavior/OrderlyBehavior.php b/src/Model/Behavior/OrderlyBehavior.php index d9c6b96..d206fb6 100644 --- a/src/Model/Behavior/OrderlyBehavior.php +++ b/src/Model/Behavior/OrderlyBehavior.php @@ -6,14 +6,14 @@ use ArrayObject; use Cake\Event\EventInterface; use Cake\ORM\Behavior; -use Cake\ORM\Query; +use Cake\ORM\Query\SelectQuery; class OrderlyBehavior extends Behavior { /** * Initialize behavior * - * @param array $config Config + * @param array $config Config * @return void */ public function initialize(array $config): void @@ -24,15 +24,15 @@ public function initialize(array $config): void } /** - * Add default order clause to query as necessary. + * Add the default order clause to the query as necessary. * * @param \Cake\Event\EventInterface $event Event - * @param \Cake\ORM\Query $query Query + * @param \Cake\ORM\Query\SelectQuery $query Query * @param \ArrayObject $options Options * @param bool $primary Boolean indicating whether it's primary query. * @return void */ - public function beforeFind(EventInterface $event, Query $query, ArrayObject $options, bool $primary) + public function beforeFind(EventInterface $event, SelectQuery $query, ArrayObject $options, bool $primary): void { if ($query->clause('order')) { return; @@ -44,7 +44,7 @@ public function beforeFind(EventInterface $event, Query $query, ArrayObject $opt empty($config['callback']) || call_user_func($config['callback'], $query, $options, $primary) ) { - $query->order($config['order']); + $query->orderBy($config['order']); } } } @@ -52,7 +52,7 @@ public function beforeFind(EventInterface $event, Query $query, ArrayObject $opt /** * Normalize configuration. * - * @param array $orders Orders config + * @param array $orders Orders config * @return void */ protected function _normalizeConfig(array $orders): void @@ -64,7 +64,10 @@ protected function _normalizeConfig(array $orders): void } $default = [ - 'order' => $this->_table->aliasField($this->_table->getDisplayField()), + 'order' => array_map( + $this->_table->aliasField(...), + (array)$this->_table->getDisplayField() + ), 'callback' => null, ]; diff --git a/src/Plugin.php b/src/OrderlyPlugin.php similarity index 59% rename from src/Plugin.php rename to src/OrderlyPlugin.php index dbd080e..3d4f572 100644 --- a/src/Plugin.php +++ b/src/OrderlyPlugin.php @@ -5,33 +5,33 @@ use Cake\Core\BasePlugin; -class Plugin extends BasePlugin +class OrderlyPlugin extends BasePlugin { /** * The name of this plugin * - * @var string + * @var string|null */ - protected $name = 'Orderly'; + protected ?string $name = 'Orderly'; /** * Do bootstrapping or not * * @var bool */ - protected $bootstrapEnabled = false; + protected bool $bootstrapEnabled = false; /** * Load routes or not * * @var bool */ - protected $routesEnabled = false; + protected bool $routesEnabled = false; /** * Console middleware * * @var bool */ - protected $consoleEnabled = false; + protected bool $consoleEnabled = false; } diff --git a/tests/TestCase/Model/Behavior/OrderlyBehaviorTest.php b/tests/TestCase/Model/Behavior/OrderlyBehaviorTest.php index 3887502..5e3f8e5 100644 --- a/tests/TestCase/Model/Behavior/OrderlyBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/OrderlyBehaviorTest.php @@ -3,30 +3,33 @@ namespace Muffin\Orderly\Test\TestCase\Model\Behavior; +use ArrayObject; use Cake\Database\ValueBinder; use Cake\Event\Event; -use Cake\ORM\TableRegistry; +use Cake\ORM\Table; use Cake\TestSuite\TestCase; class OrderlyBehaviorTest extends TestCase { - public $fixtures = [ + protected array $fixtures = [ 'core.Posts', ]; + protected Table $Table; + public function setUp(): void { parent::setUp(); - $this->Table = TableRegistry::get('Posts'); + $this->Table = $this->getTableLocator()->get('Posts'); } public function tearDown(): void { parent::tearDown(); - TableRegistry::clear(); - unset($this->Table, $this->Behavior); + $this->getTableLocator()->clear(); + unset($this->Table); } public function testInitialize() @@ -35,7 +38,7 @@ public function testInitialize() $expected = [ [ - 'order' => $this->Table->aliasField($this->Table->getDisplayField()), + 'order' => ['Posts.title'], 'callback' => null, ], ]; @@ -66,7 +69,7 @@ public function testInitialize() $expected = [ [ - 'order' => 'Posts.title', + 'order' => ['Posts.title'], 'callback' => $callback, ], ]; @@ -83,7 +86,7 @@ public function testInitialize() $expected = [ [ - 'order' => 'Posts.title', + 'order' => ['Posts.title'], 'callback' => null, ], [ @@ -104,7 +107,7 @@ public function testBeforeFind() $event = new Event('Model.beforeFind', $this); $query = $this->Table->query(); - $behavior->beforeFind($event, $query, new \ArrayObject(), true); + $behavior->beforeFind($event, $query, new ArrayObject(), true); $this->assertEquals(1, count($query->clause('order'))); } @@ -115,8 +118,8 @@ public function testBeforeFindQueryWithOrder() $event = new Event('Model.beforeFind', $this); $query = $this->Table->find() - ->order('author_id'); - $behavior->beforeFind($event, $query, new \ArrayObject(), true); + ->orderBy('author_id'); + $behavior->beforeFind($event, $query, new ArrayObject(), true); $this->assertEquals(1, count($query->clause('order'))); } @@ -149,24 +152,24 @@ public function testCallback() $event = new Event('Model.beforeFind', $this); $query = $this->Table->find(); - $behavior->beforeFind($event, $query, new \ArrayObject(['field' => null]), true); + $behavior->beforeFind($event, $query, new ArrayObject(['field' => null]), true); $this->assertNull($query->clause('order')); $valueBinder = new ValueBinder(); - $behavior->beforeFind($event, $query, new \ArrayObject(['field' => 'first']), true); + $behavior->beforeFind($event, $query, new ArrayObject(['field' => 'first']), true); $orderClause = $query->clause('order'); $this->assertCount(1, $orderClause); $this->assertEquals('ORDER BY first', $orderClause->sql($valueBinder)); $query = $this->Table->find(); - $behavior->beforeFind($event, $query, new \ArrayObject(['field' => 'second']), true); + $behavior->beforeFind($event, $query, new ArrayObject(['field' => 'second']), true); $orderClause = $query->clause('order'); $this->assertCount(1, $orderClause); $this->assertEquals('ORDER BY second', $orderClause->sql($valueBinder)); $query = $this->Table->find(); - $behavior->beforeFind($event, $query, new \ArrayObject(['field' => '_all_']), true); + $behavior->beforeFind($event, $query, new ArrayObject(['field' => '_all_']), true); $orderClause = $query->clause('order'); $this->assertCount(2, $orderClause); $this->assertEquals('ORDER BY first, second', $orderClause->sql($valueBinder)); diff --git a/tests/schema.php b/tests/schema.php new file mode 100644 index 0000000..0083cd1 --- /dev/null +++ b/tests/schema.php @@ -0,0 +1,35 @@ + 'posts', + 'columns' => [ + 'id' => [ + 'type' => 'integer', + ], + 'author_id' => [ + 'type' => 'integer', + 'null' => false, + ], + 'title' => [ + 'type' => 'string', + 'null' => false, + ], + 'body' => 'text', + 'published' => [ + 'type' => 'string', + 'length' => 1, + 'default' => 'N', + ], + ], + 'constraints' => [ + 'primary' => [ + 'type' => 'primary', + 'columns' => [ + 'id', + ], + ], + ], + ], +]; From 538f9c721fed20b47ad25d38639fb84a08b86f54 Mon Sep 17 00:00:00 2001 From: ADmad Date: Thu, 28 Sep 2023 00:08:04 +0530 Subject: [PATCH 2/3] Bump dependency to CakePHP 5 stable. --- .github/workflows/ci.yml | 1 - composer.json | 8 +++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c9ebe58..356a5ff 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,7 +4,6 @@ on: push: branches: - master - - cake-5 pull_request: branches: - '*' diff --git a/composer.json b/composer.json index f991d89..6a1f986 100644 --- a/composer.json +++ b/composer.json @@ -30,10 +30,10 @@ "source": "https://github.com/usemuffin/orderly" }, "require": { - "cakephp/orm": "5.x-dev" + "cakephp/orm": "^5.0" }, "require-dev": { - "cakephp/cakephp": "5.x-dev", + "cakephp/cakephp": "^5.0", "phpunit/phpunit": "^10.1" }, "autoload": { @@ -52,7 +52,5 @@ "allow-plugins": { "dealerdirect/phpcodesniffer-composer-installer": true } - }, - "minimum-stability": "dev", - "prefer-stable": true + } } From e411c93d695a7e56b2e65e0846920977576d9ab7 Mon Sep 17 00:00:00 2001 From: ADmad Date: Thu, 28 Sep 2023 00:23:44 +0530 Subject: [PATCH 3/3] update readme --- README.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 23b8de5..896ad10 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,9 @@ # Orderly -[![Build Status](https://img.shields.io/travis/UseMuffin/Orderly/master.svg?style=flat-square)](https://travis-ci.org/UseMuffin/Orderly) -[![Coverage](https://img.shields.io/coveralls/github/UseMuffin/Orderly?style=flat-square)](https://coveralls.io/github/UseMuffin/Orderly) +[![Build Status](https://img.shields.io/github/actions/workflow/status/UseMuffin/Orderly/ci.yml?style=flat-square +&branch=master)](https://github.com/UseMuffin/Orderly/actions/workflows/ci.yml?query=branch%3Amaster) +[![Coverage](https://img.shields.io/codecov/c/github/UseMuffin/Orderly/master?style=flat-square +)](https://app.codecov.io/gh/UseMuffin/Orderly) [![Total Downloads](https://img.shields.io/packagist/dt/muffin/orderly.svg?style=flat-square)](https://packagist.org/packages/muffin/orderly) [![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](LICENSE) @@ -23,8 +25,7 @@ bin/cake plugin load Muffin/Orderly ## What is does -Orderly allow you to set default `ORDER` clause for your table queries, similar -to the functionality provided by `Model::$order` property of CakePHP 2.x. +Orderly allow you to set default `ORDER` clause for your table's `SELECT` queries. ## Usage @@ -38,7 +39,7 @@ $this->addBehavior('Muffin/Orderly.Orderly'); $this->addBehavior('Muffin/Orderly.Orderly', ['order' => $this->aliasField('field_name')]); ``` -Value for `order` key can any valid value that `\Cake\ORM\Query::order()` takes. +Value for `order` key can any valid value that `\Cake\ORM\Query::orderBy()` takes. The default order clause will only be applied to the primary query and when no custom order clause has already been set for the query. @@ -97,4 +98,4 @@ Copyright (c) 2015-Present, [Use Muffin][muffin] and licensed under [The MIT Lic [composer]:http://getcomposer.org [mit]:http://www.opensource.org/licenses/mit-license.php [muffin]:http://usemuffin.com -[standards]:http://book.cakephp.org/3/en/contributing/cakephp-coding-conventions.html +[standards]:http://book.cakephp.org/5/en/contributing/cakephp-coding-conventions.html