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..d66f685 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) { 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..5a566ea 100644 --- a/tests/TestCase/Model/Behavior/OrderlyBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/OrderlyBehaviorTest.php @@ -5,28 +5,31 @@ use Cake\Database\ValueBinder; use Cake\Event\Event; +use Cake\ORM\Table; use Cake\ORM\TableRegistry; 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, ], [ 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', + ], + ], + ], + ], +];