From 95c16f432672170dfede793dfd82cc39469b053f Mon Sep 17 00:00:00 2001 From: Yevgeny Tomenko Date: Mon, 5 Apr 2021 00:47:32 +0300 Subject: [PATCH 01/10] fix tests --- composer.json | 12 ++++++------ tests/TestCase/Model/Behavior/EnumBehaviorTest.php | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 4725f25..9293e0b 100644 --- a/composer.json +++ b/composer.json @@ -14,13 +14,13 @@ "source": "https://github.com/cakedc/enum" }, "require": { - "php": ">=7.2.0", - "cakephp/cakephp": "4.x-dev as 4.0.0" + "php": ">=7.3", + "cakephp/cakephp": "^4.0" }, "require-dev": { - "phpunit/phpunit": "^7", + "phpunit/phpunit": "^8.0", "vlucas/phpdotenv": "^3.3", - "cakephp/cakephp-codesniffer": "dev-next" + "cakephp/cakephp-codesniffer": "^4.0" }, "autoload": { "psr-4": { @@ -45,8 +45,8 @@ "test": "phpunit --stderr", "stan": "phpstan analyse src/ && psalm --show-info=false", "psalm": "psalm --show-info=false", - "stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:^0.11 vimeo/psalm:^3.0 && mv composer.backup composer.json", - "rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:^0.4.11 && mv composer.backup composer.json", + "stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:^0.11 vimeo/psalm:^3.0 && mv composer.backup composer.json", + "rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:^0.4.11 && mv composer.backup composer.json", "coverage-test": "phpunit --stderr --coverage-clover=clover.xml" } } diff --git a/tests/TestCase/Model/Behavior/EnumBehaviorTest.php b/tests/TestCase/Model/Behavior/EnumBehaviorTest.php index fbba954..b31a17b 100644 --- a/tests/TestCase/Model/Behavior/EnumBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/EnumBehaviorTest.php @@ -287,7 +287,7 @@ public function testEnumNested() public function testAssociationsCreated() { $result = $this->Articles->associations()->keys(); - $expected = ['priorities']; + $expected = ['Priorities']; $this->assertEquals($expected, $result); foreach ($result as $assoc) { From b9a8dad3c0fa39e8bebe0800a90ce76166ae4843 Mon Sep 17 00:00:00 2001 From: Yevgeny Tomenko Date: Sat, 24 Apr 2021 21:31:26 +0300 Subject: [PATCH 02/10] release 2.0.2 --- .semver | 6 +++--- CHANGELOG.md | 2 ++ README.md | 11 ++++++----- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/.semver b/.semver index 971d0dd..d6ad2f9 100644 --- a/.semver +++ b/.semver @@ -1,6 +1,6 @@ --- -:major: 1 -:minor: 5 -:patch: 0 +:major: 2 +:minor: 0 +:patch: 2 :special: '' :metadata: '' diff --git a/CHANGELOG.md b/CHANGELOG.md index bd8ef4c..b2ab7cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ Changelog ========= +* 2.0.0 CakePHP 4.x support + * 1.2.0 * Documentation improved * Allow saving entity with no value for enum field diff --git a/README.md b/README.md index a81f15a..600abdd 100644 --- a/README.md +++ b/README.md @@ -6,13 +6,14 @@ CakeDC Enum Plugin [![Total Downloads](https://img.shields.io/packagist/dt/cakedc/enum.svg?style=flat-square)](https://packagist.org/packages/cakedc/enum) [![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](LICENSE) -Enumeration list for [CakePHP 3](http://cakephp.org). +Enumeration list for [CakePHP 4](http://cakephp.org). Versions and branches --------------------- | CakePHP | CakeDC Enum Plugin | Tag | Notes | | :-------------: | :------------------------: | :--: | :---- | +| ^4.0 | [2.next](https://github.com/cakedc/enum/tree/2.next) | 2.0.0 | stable | | ^3.7 | [master](https://github.com/cakedc/enum/tree/master) | 1.5.0 | stable | | ^3.7 | [develop](https://github.com/cakedc/enum/tree/develop) | - | unstable | | ^3.6 | [master](https://github.com/cakedc/enum/tree/1.4.0) | 1.4.0 | stable | @@ -25,7 +26,7 @@ Install Using [Composer](http://getcomposer.org): ``` -composer require cakedc/enum:1.5.0 +composer require cakedc/enum:2.0.0 ``` You then need to load the plugin. You can use the shell command: @@ -43,8 +44,8 @@ Plugin::load('CakeDC/Enum'); Requirements ------------ -* CakePHP 3.7+ -* PHP 5.6+ +* CakePHP 4.0+ +* PHP 7.3+ Documentation ------------- @@ -66,6 +67,6 @@ This repository follows the [CakeDC Plugin Standard](http://cakedc.com/plugin-st License ------- -Copyright 2015 - 2019 Cake Development Corporation (CakeDC). All rights reserved. +Copyright 2015 - 2021 Cake Development Corporation (CakeDC). All rights reserved. Licensed under the [MIT](http://www.opensource.org/licenses/mit-license.php) License. Redistributions of the source code included in this repository must retain the copyright notice found in each file. From f531c0053419f869fedf558db81512eecf56603e Mon Sep 17 00:00:00 2001 From: Yevgeny Tomenko Date: Wed, 19 May 2021 21:14:47 +0300 Subject: [PATCH 03/10] add callBeforeFind setting default true --- .semver | 2 +- README.md | 2 +- src/Model/Behavior/EnumBehavior.php | 2 +- .../Behavior/Strategy/AbstractStrategy.php | 3 ++ .../Model/Behavior/EnumBehaviorTest.php | 48 +++++++++++++++---- 5 files changed, 45 insertions(+), 12 deletions(-) diff --git a/.semver b/.semver index d6ad2f9..1bbf1c0 100644 --- a/.semver +++ b/.semver @@ -1,6 +1,6 @@ --- :major: 2 :minor: 0 -:patch: 2 +:patch: 4 :special: '' :metadata: '' diff --git a/README.md b/README.md index 600abdd..5f22e2a 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Versions and branches | CakePHP | CakeDC Enum Plugin | Tag | Notes | | :-------------: | :------------------------: | :--: | :---- | -| ^4.0 | [2.next](https://github.com/cakedc/enum/tree/2.next) | 2.0.0 | stable | +| ^4.0 | [2.next](https://github.com/cakedc/enum/tree/2.next) | 2.0.4 | stable | | ^3.7 | [master](https://github.com/cakedc/enum/tree/master) | 1.5.0 | stable | | ^3.7 | [develop](https://github.com/cakedc/enum/tree/develop) | - | unstable | | ^3.6 | [master](https://github.com/cakedc/enum/tree/1.4.0) | 1.4.0 | stable | diff --git a/src/Model/Behavior/EnumBehavior.php b/src/Model/Behavior/EnumBehavior.php index 16dbafe..2d2457a 100644 --- a/src/Model/Behavior/EnumBehavior.php +++ b/src/Model/Behavior/EnumBehavior.php @@ -302,7 +302,7 @@ public function beforeFind(\Cake\Event\EventInterface $event, \Cake\ORM\Query $q { foreach ($this->getConfig('lists') as $alias => $config) { $strategy = $this->strategy($alias, $config['strategy']); - if (method_exists($strategy, 'beforeFind')) { + if (method_exists($strategy, 'beforeFind') && $strategy->getConfig('callBeforeFind')) { $strategy->beforeFind($event, $query, $options); } } diff --git a/src/Model/Behavior/Strategy/AbstractStrategy.php b/src/Model/Behavior/Strategy/AbstractStrategy.php index 13c581c..235cd70 100644 --- a/src/Model/Behavior/Strategy/AbstractStrategy.php +++ b/src/Model/Behavior/Strategy/AbstractStrategy.php @@ -73,6 +73,9 @@ public function initialize(array $config): void if (empty($config['errorMessage'])) { $config['errorMessage'] = __d('cake', 'The provided value is invalid'); } + if (!isset($config['callBeforeFind'])) { + $config['callBeforeFind'] = true; + } $this->setConfig($config); } diff --git a/tests/TestCase/Model/Behavior/EnumBehaviorTest.php b/tests/TestCase/Model/Behavior/EnumBehaviorTest.php index 10880d9..76a9346 100644 --- a/tests/TestCase/Model/Behavior/EnumBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/EnumBehaviorTest.php @@ -26,6 +26,10 @@ class ArticlesTable extends Table public const STATUS_DRAFT = 'Drafted'; public const STATUS_ARCHIVE = 'Archived'; + public const NO_CHECK_PUBLIC = 'Published'; + public const NO_CHECK_DRAFT = 'Drafted'; + public const NO_CHECK_ARCHIVE = 'Archived'; + public const NODE_TYPE_PAGE = 'Page'; public const NODE_TYPE_BLOG = 'Blog'; @@ -37,15 +41,18 @@ class ArticlesTable extends Table public function initialize(array $config): void { - $this->addBehavior('CakeDC/Enum.Enum', ['lists' => [ - 'priority' => ['errorMessage' => 'Invalid priority', 'prefix' => 'PRIORITY'], - 'status' => ['strategy' => 'const'], - 'category' => ['strategy' => 'config'], - 'node_type' => ['strategy' => 'const'], - 'node_group' => ['strategy' => 'const', 'lowercase' => true], - 'norules' => ['strategy' => 'const', 'applicationRules' => false], - 'optional' => ['strategy' => 'const', 'lowercase' => true, 'allowEmpty' => true], - ]]); + $this->addBehavior('CakeDC/Enum.Enum', [ + 'lists' => [ + 'no_check' => ['strategy' => 'const', 'callBeforeFind' => false], + 'priority' => ['errorMessage' => 'Invalid priority', 'prefix' => 'PRIORITY'], + 'status' => ['strategy' => 'const'], + 'category' => ['strategy' => 'config'], + 'node_type' => ['strategy' => 'const'], + 'node_group' => ['strategy' => 'const', 'lowercase' => true], + 'norules' => ['strategy' => 'const', 'applicationRules' => false], + 'optional' => ['strategy' => 'const', 'lowercase' => true, 'allowEmpty' => true], + ], + ]); } } @@ -104,6 +111,7 @@ public function provideBasicConfiguration() 'prefix' => 'PRIORITY', 'field' => 'priority', 'errorMessage' => 'Invalid priority', + 'callBeforeFind' => true, ], 'status' => [ 'strategy' => 'const', @@ -111,12 +119,22 @@ public function provideBasicConfiguration() 'field' => 'status', 'errorMessage' => 'The provided value is invalid', 'lowercase' => false, + 'callBeforeFind' => true, + ], + 'no_check' => [ + 'strategy' => 'const', + 'prefix' => 'STATUS', + 'field' => 'no_check', + 'errorMessage' => 'The provided value is invalid', + 'lowercase' => false, + 'callBeforeFind' => false, ], 'category' => [ 'strategy' => 'config', 'prefix' => 'ARTICLE_CATEGORY', 'field' => 'category', 'errorMessage' => 'The provided value is invalid', + 'callBeforeFind' => true, ], 'node_type' => [ 'strategy' => 'const', @@ -124,6 +142,7 @@ public function provideBasicConfiguration() 'field' => 'node_type', 'errorMessage' => 'The provided value is invalid', 'lowercase' => false, + 'callBeforeFind' => true, ], 'node_group' => [ 'strategy' => 'const', @@ -131,6 +150,7 @@ public function provideBasicConfiguration() 'field' => 'node_group', 'errorMessage' => 'The provided value is invalid', 'lowercase' => true, + 'callBeforeFind' => true, ], 'optional' => [ 'strategy' => 'const', @@ -138,6 +158,7 @@ public function provideBasicConfiguration() 'field' => 'optional', 'errorMessage' => 'The provided value is invalid', 'lowercase' => true, + 'callBeforeFind' => true, ], ], 'classMap' => [], @@ -149,6 +170,7 @@ public function provideBasicConfiguration() 'lists' => [ 'priority' => ['errorMessage' => 'Invalid priority', 'prefix' => 'PRIORITY'], 'status' => ['strategy' => 'const', 'prefix' => 'STATUS'], + 'no_check' => ['strategy' => 'const', 'prefix' => 'STATUS', 'callBeforeFind' => false], 'category' => ['strategy' => 'config'], 'node_type' => ['strategy' => 'const'], 'node_group' => ['strategy' => 'const', 'lowercase' => true], @@ -236,6 +258,7 @@ public function provideBuildRules() [ 'priority' => 'URGENT', 'status' => 'DRAFT', + 'no_check' => 'DRAFT', 'category' => 2, 'node_type' => 'BLOG', 'node_group' => 'active', @@ -250,6 +273,7 @@ public function provideBuildRules() [ 'priority' => 'Urgent', 'status' => 'Drafted', + 'no_check' => 'Drafted', 'category' => 1, 'node_type' => 'Invalid value', 'node_group' => 'active', @@ -259,6 +283,7 @@ public function provideBuildRules() 'priority' => ['isValidPriority' => 'Invalid priority'], 'status' => ['isValidStatus' => 'The provided value is invalid'], 'node_type' => ['isValidNodeType' => 'The provided value is invalid'], + 'no_check' => ['isValidNoCheck' => 'The provided value is invalid'], ], ], ]; @@ -314,6 +339,11 @@ public function testEnumMultipleAlias() 'DRAFT' => 'Drafted', 'ARCHIVE' => 'Archived', ], + 'no_check' => [ + 'PUBLIC' => 'Published', + 'DRAFT' => 'Drafted', + 'ARCHIVE' => 'Archived', + ], 'category' => [ 'CakePHP', 'Open Source Software', From 14ac610c7117bbf331dd9d297a9b7c7fb6e03bc8 Mon Sep 17 00:00:00 2001 From: Yevgeny Tomenko Date: Fri, 13 Aug 2021 06:43:36 +0300 Subject: [PATCH 04/10] code style improvements --- composer.json | 11 +++++++---- rector.yml | 4 ---- src/Model/Behavior/EnumBehavior.php | 13 ++++++------- .../Exception/InvalidAliasListException.php | 4 ++-- .../MissingEnumConfigurationException.php | 4 ++-- .../Exception/MissingEnumStrategyException.php | 4 ++-- .../MissingEnumStrategyPrefixException.php | 4 ++-- .../Behavior/Strategy/AbstractStrategy.php | 4 +--- src/Model/Behavior/Strategy/ConfigStrategy.php | 2 +- src/Model/Behavior/Strategy/ConstStrategy.php | 8 ++++---- src/Model/Behavior/Strategy/LookupStrategy.php | 18 +++++++++--------- src/Model/Table/LookupsTable.php | 2 +- .../Model/Behavior/EnumBehaviorTest.php | 6 +++--- .../Behavior/Strategy/ConstStrategyTest.php | 2 +- .../Behavior/Strategy/LookupStrategyTest.php | 4 ++++ tests/bootstrap.php | 4 ++-- 16 files changed, 47 insertions(+), 47 deletions(-) delete mode 100644 rector.yml diff --git a/composer.json b/composer.json index dd3d937..7d6df90 100644 --- a/composer.json +++ b/composer.json @@ -47,10 +47,13 @@ "cs-check": "phpcs -p --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/", "cs-fix": "phpcbf --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/", "test": "phpunit --stderr", - "stan": "phpstan analyse src/ && psalm --show-info=false", - "psalm": "psalm --show-info=false", - "stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:^0.11 vimeo/psalm:^3.0 && mv composer.backup composer.json", - "rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:^0.4.11 && mv composer.backup composer.json", + "stan": "phpstan analyse src/", + "psalm": "php vendor/psalm/phar/psalm.phar --show-info=false src/ ", + "stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:0.12.94 psalm/phar:~4.9.2 && mv composer.backup composer.json", + "stan-rebuild-baseline": "phpstan analyse --configuration phpstan.neon --error-format baselineNeon src/ > phpstan-baseline.neon", + "psalm-rebuild-baseline": "php vendor/psalm/phar/psalm.phar --show-info=false --set-baseline=psalm-baseline.xml src/", + "rector": "rector process", + "rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:^0.11.2 && mv composer.backup composer.json", "coverage-test": "phpunit --stderr --coverage-clover=clover.xml" } } diff --git a/rector.yml b/rector.yml deleted file mode 100644 index eaa99e6..0000000 --- a/rector.yml +++ /dev/null @@ -1,4 +0,0 @@ -# rector.yaml -services: - Rector\Php\Rector\FunctionLike\ParamTypeDeclarationRector: ~ - Rector\Php\Rector\FunctionLike\ReturnTypeDeclarationRector: ~ diff --git a/src/Model/Behavior/EnumBehavior.php b/src/Model/Behavior/EnumBehavior.php index 2d2457a..dcd2fb2 100644 --- a/src/Model/Behavior/EnumBehavior.php +++ b/src/Model/Behavior/EnumBehavior.php @@ -14,8 +14,7 @@ namespace CakeDC\Enum\Model\Behavior; use BadMethodCallException; -use Cake\Event\Event; -use Cake\ORM\Behavior; +use Cake\Event\EventInterface;use Cake\ORM\Behavior; use Cake\ORM\RulesChecker; use Cake\Utility\Hash; use Cake\Utility\Inflector; @@ -77,9 +76,9 @@ class EnumBehavior extends Behavior * @var array */ protected $_classMap = [ - 'lookup' => 'CakeDC\Enum\Model\Behavior\Strategy\LookupStrategy', - 'const' => 'CakeDC\Enum\Model\Behavior\Strategy\ConstStrategy', - 'config' => 'CakeDC\Enum\Model\Behavior\Strategy\ConfigStrategy', + 'lookup' => \CakeDC\Enum\Model\Behavior\Strategy\LookupStrategy::class, + 'const' => \CakeDC\Enum\Model\Behavior\Strategy\ConstStrategy::class, + 'config' => \CakeDC\Enum\Model\Behavior\Strategy\ConfigStrategy::class, ]; /** @@ -245,7 +244,7 @@ protected function _translate(array $list): array * @param \Cake\ORM\RulesChecker $rules Rules checker. * @return \Cake\ORM\RulesChecker */ - public function buildRules(Event $event, RulesChecker $rules): \Cake\ORM\RulesChecker + public function buildRules(EventInterface $event, RulesChecker $rules): \Cake\ORM\RulesChecker { foreach ($this->getConfig('lists') as $alias => $config) { if (Hash::get($config, 'applicationRules') === false) { @@ -298,7 +297,7 @@ public function __call(string $method, array $args): bool * @param \ArrayObject $options The options for the query * @return void */ - public function beforeFind(\Cake\Event\EventInterface $event, \Cake\ORM\Query $query, \ArrayObject $options) + public function beforeFind(EventInterface $event, \Cake\ORM\Query $query, \ArrayObject $options) { foreach ($this->getConfig('lists') as $alias => $config) { $strategy = $this->strategy($alias, $config['strategy']); diff --git a/src/Model/Behavior/Exception/InvalidAliasListException.php b/src/Model/Behavior/Exception/InvalidAliasListException.php index d7aaffd..d9ecf6f 100644 --- a/src/Model/Behavior/Exception/InvalidAliasListException.php +++ b/src/Model/Behavior/Exception/InvalidAliasListException.php @@ -13,9 +13,9 @@ namespace CakeDC\Enum\Model\Behavior\Exception; -use Cake\Core\Exception\Exception; +use \Cake\Core\Exception\CakeException; -class InvalidAliasListException extends Exception +class InvalidAliasListException extends CakeException { protected $_messageTemplate = 'Invalid alias %s. %s defined association %s.'; } diff --git a/src/Model/Behavior/Exception/MissingEnumConfigurationException.php b/src/Model/Behavior/Exception/MissingEnumConfigurationException.php index 67c69b5..c032cf1 100644 --- a/src/Model/Behavior/Exception/MissingEnumConfigurationException.php +++ b/src/Model/Behavior/Exception/MissingEnumConfigurationException.php @@ -13,9 +13,9 @@ namespace CakeDC\Enum\Model\Behavior\Exception; -use Cake\Core\Exception\Exception; +use \Cake\Core\Exception\CakeException; -class MissingEnumConfigurationException extends Exception +class MissingEnumConfigurationException extends CakeException { protected $_templateMessage = 'Missing enum configuration (%s)'; } diff --git a/src/Model/Behavior/Exception/MissingEnumStrategyException.php b/src/Model/Behavior/Exception/MissingEnumStrategyException.php index 753ad81..5d4db5e 100644 --- a/src/Model/Behavior/Exception/MissingEnumStrategyException.php +++ b/src/Model/Behavior/Exception/MissingEnumStrategyException.php @@ -13,9 +13,9 @@ namespace CakeDC\Enum\Model\Behavior\Exception; -use Cake\Core\Exception\Exception; +use \Cake\Core\Exception\CakeException; -class MissingEnumStrategyException extends Exception +class MissingEnumStrategyException extends CakeException { protected $_templateMessage = 'Missing enum strategy class (%s)'; } diff --git a/src/Model/Behavior/Exception/MissingEnumStrategyPrefixException.php b/src/Model/Behavior/Exception/MissingEnumStrategyPrefixException.php index d170eda..79fa7e5 100644 --- a/src/Model/Behavior/Exception/MissingEnumStrategyPrefixException.php +++ b/src/Model/Behavior/Exception/MissingEnumStrategyPrefixException.php @@ -13,9 +13,9 @@ namespace CakeDC\Enum\Model\Behavior\Exception; -use Cake\Core\Exception\Exception; +use \Cake\Core\Exception\CakeException; -class MissingEnumStrategyPrefixException extends Exception +class MissingEnumStrategyPrefixException extends CakeException { protected $_messageTemplate = 'Missing prefix for strategy (%s)'; } diff --git a/src/Model/Behavior/Strategy/AbstractStrategy.php b/src/Model/Behavior/Strategy/AbstractStrategy.php index 235cd70..d43dc30 100644 --- a/src/Model/Behavior/Strategy/AbstractStrategy.php +++ b/src/Model/Behavior/Strategy/AbstractStrategy.php @@ -55,9 +55,7 @@ public function __construct(string $alias, Table $table) } /** - * {@inheritdoc} - * - * @return void + * @inheritDoc */ public function initialize(array $config): void { diff --git a/src/Model/Behavior/Strategy/ConfigStrategy.php b/src/Model/Behavior/Strategy/ConfigStrategy.php index cf11c5a..6c2fb89 100644 --- a/src/Model/Behavior/Strategy/ConfigStrategy.php +++ b/src/Model/Behavior/Strategy/ConfigStrategy.php @@ -20,7 +20,7 @@ class ConfigStrategy extends AbstractStrategy public const KEY = 'CakeDC/Enum'; /** - * {@inheritdoc} + * {@inheritDoc} * * @param array $config (unused in this case). * @return array diff --git a/src/Model/Behavior/Strategy/ConstStrategy.php b/src/Model/Behavior/Strategy/ConstStrategy.php index d4f6231..dc006f9 100644 --- a/src/Model/Behavior/Strategy/ConstStrategy.php +++ b/src/Model/Behavior/Strategy/ConstStrategy.php @@ -29,7 +29,7 @@ class ConstStrategy extends AbstractStrategy protected $_constants; /** - * {@inheritdoc} + * {@inheritDoc} * * @param string $alias Strategy's alias. * @param \Cake\ORM\Table $table Table object. @@ -42,7 +42,7 @@ public function __construct(string $alias, Table $table) } /** - * {@inheritdoc} + * {@inheritDoc} * * @param array $config List of callable filters to limit items generated from list. * @return array @@ -72,7 +72,7 @@ public function enum(array $config = []): array */ protected function _getConstants(): array { - if (isset($this->_constants)) { + if ($this->_constants !== null) { return $this->_constants; } @@ -84,7 +84,7 @@ protected function _getConstants(): array $constants = []; foreach ($classConstants as $key => $value) { - if (strpos($key, $prefix) === 0) { + if (strpos($key, (string)$prefix) === 0) { $listKey = substr($key, $length); if ($lowercase) { $listKey = strtolower($listKey); diff --git a/src/Model/Behavior/Strategy/LookupStrategy.php b/src/Model/Behavior/Strategy/LookupStrategy.php index f25c636..3cf06b5 100644 --- a/src/Model/Behavior/Strategy/LookupStrategy.php +++ b/src/Model/Behavior/Strategy/LookupStrategy.php @@ -24,7 +24,7 @@ class LookupStrategy extends AbstractStrategy use ModelAwareTrait; /** - * {@inheritdoc} + * {@inheritDoc} * * @param string $alias Strategy's alias. * @param \Cake\ORM\Table $table Table object. @@ -37,7 +37,7 @@ public function __construct(string $alias, Table $table) } /** - * {@inheritdoc} + * {@inheritDoc} * * @param array $config (unused in this case). * @return array @@ -63,7 +63,7 @@ public function enum(array $config = []): array } /** - * {@inheritdoc} + * {@inheritDoc} * * @param array $config Strategy's configuration. * @return void @@ -74,11 +74,11 @@ public function initialize(array $config): void $config = $this->getConfig(); $assocName = Inflector::pluralize(Inflector::classify($this->_alias)); - $this->_table->belongsTo($assocName, [ - 'className' => $this->modelClass, - 'foreignKey' => $config['field'], - 'bindingKey' => 'name', - 'conditions' => [$assocName . '.prefix' => $config['prefix']], - ]); + $this->_table + ->belongsTo($assocName) + ->setClassName($this->modelClass) + ->setForeignKey($config['field']) + ->setBindingKey('name') + ->setConditions([$assocName . '.prefix' => $config['prefix']]); } } diff --git a/src/Model/Table/LookupsTable.php b/src/Model/Table/LookupsTable.php index aea74c1..4f7cb07 100644 --- a/src/Model/Table/LookupsTable.php +++ b/src/Model/Table/LookupsTable.php @@ -18,7 +18,7 @@ class LookupsTable extends Table { /** - * {@inheritdoc} + * {@inheritDoc} * * @param array $config Table's configuration. * @return void diff --git a/tests/TestCase/Model/Behavior/EnumBehaviorTest.php b/tests/TestCase/Model/Behavior/EnumBehaviorTest.php index 76a9346..d7b4abf 100644 --- a/tests/TestCase/Model/Behavior/EnumBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/EnumBehaviorTest.php @@ -87,7 +87,7 @@ public function setUp(): void ]); $this->Articles = TableRegistry::get('CakeDC/Enum.Articles', [ - 'className' => 'CakeDC\Enum\Test\TestCase\Model\Behavior\ArticlesTable', + 'className' => \CakeDC\Enum\Test\TestCase\Model\Behavior\ArticlesTable::class, 'table' => 'enum_articles', ]); } @@ -319,7 +319,7 @@ public function testAssociationsCreated() $this->assertEquals($expected, $result); foreach ($result as $assoc) { - $this->assertInstanceOf('\Cake\ORM\Association\BelongsTo', $this->Articles->getAssociation($assoc)); + $this->assertInstanceOf(\Cake\ORM\Association\BelongsTo::class, $this->Articles->getAssociation($assoc)); } $result = $this->Articles->get(1); @@ -404,7 +404,7 @@ public function provideThirdPartyStrategy() return [ [ [ - 'classMap' => ['third_party' => 'CakeDC\Enum\Test\TestCase\Model\Behavior\ThirdPartyStrategy'], + 'classMap' => ['third_party' => \CakeDC\Enum\Test\TestCase\Model\Behavior\ThirdPartyStrategy::class], 'lists' => [ 'article_category' => ['strategy' => 'third_party'], ], diff --git a/tests/TestCase/Model/Behavior/Strategy/ConstStrategyTest.php b/tests/TestCase/Model/Behavior/Strategy/ConstStrategyTest.php index f2206e9..a24cedd 100644 --- a/tests/TestCase/Model/Behavior/Strategy/ConstStrategyTest.php +++ b/tests/TestCase/Model/Behavior/Strategy/ConstStrategyTest.php @@ -53,7 +53,7 @@ public function setUp(): void $this->StrategyEntity->initialize([ 'prefix' => 'STATUS', 'lowercase' => true, - 'className' => 'CakeDC\\Enum\\Test\\TestCase\\Model\\Behavior\\Strategy\\Article', + 'className' => \CakeDC\Enum\Test\TestCase\Model\Behavior\Strategy\Article::class, ]); } diff --git a/tests/TestCase/Model/Behavior/Strategy/LookupStrategyTest.php b/tests/TestCase/Model/Behavior/Strategy/LookupStrategyTest.php index 61f3a93..f0c214a 100644 --- a/tests/TestCase/Model/Behavior/Strategy/LookupStrategyTest.php +++ b/tests/TestCase/Model/Behavior/Strategy/LookupStrategyTest.php @@ -19,6 +19,10 @@ class LookupStrategyTest extends TestCase { + /** + * @var \CakeDC\Enum\Model\Behavior\Strategy\LookupStrategy|mixed + */ + public $Strategy; public $fixtures = [ 'plugin.CakeDC/Enum.Lookups', ]; diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 2b731dc..903a1a7 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -23,7 +23,7 @@ } } while ($root !== $lastRoot); - throw new Exception("Cannot find the root of the application, unable to run tests"); + throw new Exception('Cannot find the root of the application, unable to run tests'); }; $root = $findRoot(__FILE__); unset($findRoot); @@ -35,7 +35,7 @@ require $root . '/vendor/cakephp/cakephp/tests/bootstrap.php'; -\Cake\Core\Plugin::getCollection()->add(new \CakeDC\Enum\Plugin(['path' => dirname(dirname(__FILE__)) . DS])); +\Cake\Core\Plugin::getCollection()->add(new \CakeDC\Enum\Plugin(['path' => dirname(__FILE__, 2) . DS])); I18n::config('default', function ($name, $locale) { $package = new Package('default'); $messages = [ From ca06bfdf096f7de50eea5f98bb790a5edfa0f9c0 Mon Sep 17 00:00:00 2001 From: Yevgeny Tomenko Date: Sun, 26 Jun 2022 23:58:44 +0300 Subject: [PATCH 05/10] update plugin to 4.4. fix warnings --- composer.json | 5 +++++ phpunit.xml.dist | 45 +++++++++++++++++---------------------------- tests/schema.php | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 28 deletions(-) create mode 100644 tests/schema.php diff --git a/composer.json b/composer.json index 7d6df90..18982c3 100644 --- a/composer.json +++ b/composer.json @@ -55,5 +55,10 @@ "rector": "rector process", "rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:^0.11.2 && mv composer.backup composer.json", "coverage-test": "phpunit --stderr --coverage-clover=clover.xml" + }, + "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 097679a..867f3ad 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,30 +1,19 @@ - - - - - ./tests/ - - - - - - - - - - - - - - - ./src/ - - + + + + + + + + + + ./tests/TestCase + + + + + + + diff --git a/tests/schema.php b/tests/schema.php new file mode 100644 index 0000000..457d96b --- /dev/null +++ b/tests/schema.php @@ -0,0 +1,38 @@ + 'enum_articles', + 'columns' => [ + 'id' => ['type' => 'integer'], + 'title' => ['type' => 'string'], + 'body' => ['type' => 'text'], + 'priority' => ['type' => 'string'], + 'status' => ['type' => 'string'], + 'article_category' => ['type' => 'integer'], + ], + 'constraints' => [ + 'primary' => ['type' => 'primary', 'columns' => ['id'], 'length' => []], + ], + ], + [ + 'table' => 'enum_lookups', + 'columns' => [ + 'id' => ['type' => 'integer'], + 'label' => ['type' => 'string'], + 'prefix' => ['type' => 'string'], + 'name' => ['type' => 'string'], + ], + 'constraints' => [ + 'primary' => ['type' => 'primary', 'columns' => ['id'], 'length' => []], + ], + ], +]; From 3119e5c6f8a7dd51e9eaabbabd2b20708b27edb6 Mon Sep 17 00:00:00 2001 From: Adam Rusinowski Date: Wed, 8 Feb 2023 21:18:46 +0100 Subject: [PATCH 06/10] removed deprecations --- src/Model/Behavior/EnumBehavior.php | 13 ++++++---- .../Exception/InvalidAliasListException.php | 2 +- .../MissingEnumConfigurationException.php | 2 +- .../MissingEnumStrategyException.php | 2 +- .../MissingEnumStrategyPrefixException.php | 2 +- src/Model/Behavior/Strategy/ConstStrategy.php | 11 ++++++--- .../Behavior/Strategy/LookupStrategy.php | 24 ++++--------------- 7 files changed, 25 insertions(+), 31 deletions(-) diff --git a/src/Model/Behavior/EnumBehavior.php b/src/Model/Behavior/EnumBehavior.php index dcd2fb2..983fc51 100644 --- a/src/Model/Behavior/EnumBehavior.php +++ b/src/Model/Behavior/EnumBehavior.php @@ -13,13 +13,18 @@ namespace CakeDC\Enum\Model\Behavior; +use ArrayObject; use BadMethodCallException; use Cake\Event\EventInterface;use Cake\ORM\Behavior; +use Cake\ORM\Query; use Cake\ORM\RulesChecker; use Cake\Utility\Hash; use Cake\Utility\Inflector; use CakeDC\Enum\Model\Behavior\Exception\MissingEnumConfigurationException; use CakeDC\Enum\Model\Behavior\Exception\MissingEnumStrategyException; +use CakeDC\Enum\Model\Behavior\Strategy\ConfigStrategy; +use CakeDC\Enum\Model\Behavior\Strategy\ConstStrategy; +use CakeDC\Enum\Model\Behavior\Strategy\LookupStrategy; use CakeDC\Enum\Model\Behavior\Strategy\StrategyInterface; class EnumBehavior extends Behavior @@ -76,9 +81,9 @@ class EnumBehavior extends Behavior * @var array */ protected $_classMap = [ - 'lookup' => \CakeDC\Enum\Model\Behavior\Strategy\LookupStrategy::class, - 'const' => \CakeDC\Enum\Model\Behavior\Strategy\ConstStrategy::class, - 'config' => \CakeDC\Enum\Model\Behavior\Strategy\ConfigStrategy::class, + 'lookup' => LookupStrategy::class, + 'const' => ConstStrategy::class, + 'config' => ConfigStrategy::class, ]; /** @@ -297,7 +302,7 @@ public function __call(string $method, array $args): bool * @param \ArrayObject $options The options for the query * @return void */ - public function beforeFind(EventInterface $event, \Cake\ORM\Query $query, \ArrayObject $options) + public function beforeFind(EventInterface $event, Query $query, ArrayObject $options) { foreach ($this->getConfig('lists') as $alias => $config) { $strategy = $this->strategy($alias, $config['strategy']); diff --git a/src/Model/Behavior/Exception/InvalidAliasListException.php b/src/Model/Behavior/Exception/InvalidAliasListException.php index d9ecf6f..bb32383 100644 --- a/src/Model/Behavior/Exception/InvalidAliasListException.php +++ b/src/Model/Behavior/Exception/InvalidAliasListException.php @@ -13,7 +13,7 @@ namespace CakeDC\Enum\Model\Behavior\Exception; -use \Cake\Core\Exception\CakeException; +use Cake\Core\Exception\CakeException; class InvalidAliasListException extends CakeException { diff --git a/src/Model/Behavior/Exception/MissingEnumConfigurationException.php b/src/Model/Behavior/Exception/MissingEnumConfigurationException.php index c032cf1..fb5e7dc 100644 --- a/src/Model/Behavior/Exception/MissingEnumConfigurationException.php +++ b/src/Model/Behavior/Exception/MissingEnumConfigurationException.php @@ -13,7 +13,7 @@ namespace CakeDC\Enum\Model\Behavior\Exception; -use \Cake\Core\Exception\CakeException; +use Cake\Core\Exception\CakeException; class MissingEnumConfigurationException extends CakeException { diff --git a/src/Model/Behavior/Exception/MissingEnumStrategyException.php b/src/Model/Behavior/Exception/MissingEnumStrategyException.php index 5d4db5e..229720a 100644 --- a/src/Model/Behavior/Exception/MissingEnumStrategyException.php +++ b/src/Model/Behavior/Exception/MissingEnumStrategyException.php @@ -13,7 +13,7 @@ namespace CakeDC\Enum\Model\Behavior\Exception; -use \Cake\Core\Exception\CakeException; +use Cake\Core\Exception\CakeException; class MissingEnumStrategyException extends CakeException { diff --git a/src/Model/Behavior/Exception/MissingEnumStrategyPrefixException.php b/src/Model/Behavior/Exception/MissingEnumStrategyPrefixException.php index 79fa7e5..908d196 100644 --- a/src/Model/Behavior/Exception/MissingEnumStrategyPrefixException.php +++ b/src/Model/Behavior/Exception/MissingEnumStrategyPrefixException.php @@ -13,7 +13,7 @@ namespace CakeDC\Enum\Model\Behavior\Exception; -use \Cake\Core\Exception\CakeException; +use Cake\Core\Exception\CakeException; class MissingEnumStrategyPrefixException extends CakeException { diff --git a/src/Model/Behavior/Strategy/ConstStrategy.php b/src/Model/Behavior/Strategy/ConstStrategy.php index dc006f9..2938796 100644 --- a/src/Model/Behavior/Strategy/ConstStrategy.php +++ b/src/Model/Behavior/Strategy/ConstStrategy.php @@ -13,6 +13,11 @@ namespace CakeDC\Enum\Model\Behavior\Strategy; +use ArrayObject; +use Cake\Collection\CollectionInterface; +use Cake\Event\EventInterface; +use Cake\ORM\Entity; +use Cake\ORM\Query; use Cake\ORM\Table; use Cake\Utility\Hash; use Cake\Utility\Inflector; @@ -102,7 +107,7 @@ protected function _getConstants(): array * @param \ArrayObject $options The options for the query * @return void */ - public function beforeFind(\Cake\Event\EventInterface $event, \Cake\ORM\Query $query, \ArrayObject $options) + public function beforeFind(EventInterface $event, Query $query, ArrayObject $options) { $assocName = Inflector::pluralize(Inflector::classify($this->_alias)); if ($this->_table->hasAssociation($assocName)) { @@ -115,7 +120,7 @@ public function beforeFind(\Cake\Event\EventInterface $event, \Cake\ORM\Query $q $query->clearContain()->contain($contain); - $query->formatResults(function (\Cake\Collection\CollectionInterface $results) { + $query->formatResults(function (CollectionInterface $results) { return $results->map(function ($row) { if (is_string($row) || !$row) { return $row; @@ -124,7 +129,7 @@ public function beforeFind(\Cake\Event\EventInterface $event, \Cake\ORM\Query $q $constant = Hash::get($row, $this->getConfig('field')); $field = Inflector::singularize(Inflector::underscore($this->_alias)); - $value = new \Cake\ORM\Entity([ + $value = new Entity([ 'label' => Hash::get($this->_getConstants(), $constant, $constant), 'prefix' => $this->getConfig('prefix'), 'value' => $constant, diff --git a/src/Model/Behavior/Strategy/LookupStrategy.php b/src/Model/Behavior/Strategy/LookupStrategy.php index 3cf06b5..7c5f4be 100644 --- a/src/Model/Behavior/Strategy/LookupStrategy.php +++ b/src/Model/Behavior/Strategy/LookupStrategy.php @@ -2,39 +2,23 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ namespace CakeDC\Enum\Model\Behavior\Strategy; -use Cake\Datasource\ModelAwareTrait; use Cake\ORM\Locator\LocatorAwareTrait; -use Cake\ORM\Table; use Cake\Utility\Inflector; class LookupStrategy extends AbstractStrategy { use LocatorAwareTrait; - use ModelAwareTrait; - - /** - * {@inheritDoc} - * - * @param string $alias Strategy's alias. - * @param \Cake\ORM\Table $table Table object. - */ - public function __construct(string $alias, Table $table) - { - parent::__construct($alias, $table); - $this->modelClass = 'CakeDC/Enum.Lookups'; - $this->modelFactory('Table', [$this->getTableLocator(), 'get']); - } /** * {@inheritDoc} @@ -44,7 +28,7 @@ public function __construct(string $alias, Table $table) */ public function enum(array $config = []): array { - $query = $this->loadModel() + $query = $this->fetchTable('CakeDC/Enum.Lookups') ->find('list', [ 'keyField' => 'name', 'valueField' => 'label', @@ -76,7 +60,7 @@ public function initialize(array $config): void $this->_table ->belongsTo($assocName) - ->setClassName($this->modelClass) + ->setClassName('CakeDC/Enum.Lookups') ->setForeignKey($config['field']) ->setBindingKey('name') ->setConditions([$assocName . '.prefix' => $config['prefix']]); From 120273d141e8fa899b69a29a6584e6a06bb97f56 Mon Sep 17 00:00:00 2001 From: Adam Rusinowski Date: Wed, 8 Feb 2023 21:24:56 +0100 Subject: [PATCH 07/10] added dev dependencies --- composer.json | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 18982c3..8ec885b 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "cakedc/enum", - "description": "Enumeration list Plugin for CakePHP 3", + "description": "Enumeration list Plugin for CakePHP 5", "type": "cakephp-plugin", "keywords": [ "cakephp", @@ -14,13 +14,14 @@ "source": "https://github.com/cakedc/enum" }, "require": { - "php": ">=7.3", - "cakephp/cakephp": "^4.0" + "php": ">=8.1", + "cakephp/cakephp": "5.x-dev" }, "require-dev": { - "phpunit/phpunit": "^8.5", - "vlucas/phpdotenv": "^3.3", - "cakephp/cakephp-codesniffer": "^4.0" + "phpunit/phpunit": "^9.5.19", + "cakephp/cakephp-codesniffer": "^5.0", + "cakephp/bake": "3.x-dev", + "cakephp/migrations": "4.x-dev" }, "autoload": { "psr-4": { @@ -38,7 +39,7 @@ "analyse": [ "@stan", "@psalm" - ], + ], "check": [ "@cs-check", "@test", From 1bcc10ada6ba2d93cd8b9a848c08aaad037340bf Mon Sep 17 00:00:00 2001 From: Adam Rusinowski Date: Wed, 8 Feb 2023 22:19:31 +0100 Subject: [PATCH 08/10] updated enum plugin to CakePHP 5.x --- CHANGELOG.md | 2 + Docs/Documentation/Installation.md | 10 ++-- Docs/Home.md | 4 +- README.md | 29 +++++----- composer.json | 3 +- config/Migrations/001_create_enum_lookups.php | 2 +- src/{Plugin.php => EnumPlugin.php} | 13 ++--- src/Model/Behavior/EnumBehavior.php | 53 ++++++++++--------- .../Exception/InvalidAliasListException.php | 6 +-- .../MissingEnumConfigurationException.php | 6 +-- .../MissingEnumStrategyException.php | 6 +-- .../MissingEnumStrategyPrefixException.php | 6 +-- .../Behavior/Strategy/AbstractStrategy.php | 24 ++++----- .../Behavior/Strategy/ConfigStrategy.php | 4 +- src/Model/Behavior/Strategy/ConstStrategy.php | 42 ++++++++------- .../Behavior/Strategy/LookupStrategy.php | 4 +- .../Behavior/Strategy/StrategyInterface.php | 6 +-- src/Model/Table/LookupsTable.php | 4 +- tests/Fixture/ArticlesFixture.php | 11 ++-- tests/Fixture/LookupsFixture.php | 11 ++-- .../Model/Behavior/EnumBehaviorTest.php | 39 +++++++------- .../Behavior/Strategy/ConfigStrategyTest.php | 6 +-- .../Behavior/Strategy/ConstStrategyTest.php | 14 +++-- .../Behavior/Strategy/LookupStrategyTest.php | 9 ++-- tests/bootstrap.php | 11 ++-- 25 files changed, 170 insertions(+), 155 deletions(-) rename src/{Plugin.php => EnumPlugin.php} (54%) diff --git a/CHANGELOG.md b/CHANGELOG.md index b2ab7cc..2dcde51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ Changelog ========= +* 3.0.0-dev CakePHP 5.x support + * 2.0.0 CakePHP 4.x support * 1.2.0 diff --git a/Docs/Documentation/Installation.md b/Docs/Documentation/Installation.md index 3497bf4..0349a7e 100644 --- a/Docs/Documentation/Installation.md +++ b/Docs/Documentation/Installation.md @@ -3,7 +3,7 @@ ## Composer ``` -composer require cakedc/enum:dev-master +composer require cakedc/enum:3.0.0-dev ``` ## Creating Required Tables @@ -16,12 +16,8 @@ bin/cake migrations migrate -p CakeDC/Enum ## Load the Plugin -Ensure the Enum Plugin is loaded in your config/bootstrap.php file +Ensure the Enum Plugin is loaded in your `Application::bootstrap()` method: ```php -Plugin::load('CakeDC/Enum'); +$this->addPlugin('CakeDC/Enum'); ``` -or -```php -Plugin::loadAll(); -``` \ No newline at end of file diff --git a/Docs/Home.md b/Docs/Home.md index ebe6109..7d5d621 100644 --- a/Docs/Home.md +++ b/Docs/Home.md @@ -4,8 +4,8 @@ Home Requirements ------------ -* CakePHP 3.1+ -* PHP 5.4.16+ Note CakePHP 3.2 requires PHP 5.5 so 5.4 compatibility would be dropped sooner than later... +* CakePHP 5.0+ +* PHP 8.1+ Documentation ------------- diff --git a/README.md b/README.md index 5f22e2a..8ac371d 100644 --- a/README.md +++ b/README.md @@ -6,18 +6,19 @@ CakeDC Enum Plugin [![Total Downloads](https://img.shields.io/packagist/dt/cakedc/enum.svg?style=flat-square)](https://packagist.org/packages/cakedc/enum) [![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](LICENSE) -Enumeration list for [CakePHP 4](http://cakephp.org). +Enumeration list for [CakePHP 5](http://cakephp.org). Versions and branches --------------------- -| CakePHP | CakeDC Enum Plugin | Tag | Notes | -| :-------------: | :------------------------: | :--: | :---- | -| ^4.0 | [2.next](https://github.com/cakedc/enum/tree/2.next) | 2.0.4 | stable | -| ^3.7 | [master](https://github.com/cakedc/enum/tree/master) | 1.5.0 | stable | -| ^3.7 | [develop](https://github.com/cakedc/enum/tree/develop) | - | unstable | -| ^3.6 | [master](https://github.com/cakedc/enum/tree/1.4.0) | 1.4.0 | stable | -| 3.1 - 3.5 | | 1.3.0 | stable | +| CakePHP | CakeDC Enum Plugin | Tag | Notes | +|:-----------|:--------------------------------------------------------|:----------|:---------| +| ^5.0.0-dev | [3.next](https://github.com/cakedc/enum/tree/3next) | 3.0.0-dev | unstable | +| ^4.0 | [2.next](https://github.com/cakedc/enum/tree/2.next) | 2.0.4 | stable | +| ^3.7 | [master](https://github.com/cakedc/enum/tree/master) | 1.5.0 | stable | +| ^3.7 | [develop](https://github.com/cakedc/enum/tree/develop) | - | unstable | +| ^3.6 | [master](https://github.com/cakedc/enum/tree/1.4.0) | 1.4.0 | stable | +| 3.1 - 3.5 | | 1.3.0 | stable | Install @@ -26,7 +27,7 @@ Install Using [Composer](http://getcomposer.org): ``` -composer require cakedc/enum:2.0.0 +composer require cakedc/enum:3.0.0-dev ``` You then need to load the plugin. You can use the shell command: @@ -35,17 +36,17 @@ You then need to load the plugin. You can use the shell command: bin/cake plugin load CakeDC/Enum ``` -or by manually adding statement shown below to `bootstrap.php`: +or by manually adding statement shown below to `Application::bootstrap()` method: ```php -Plugin::load('CakeDC/Enum'); +$this->addPlugin('CakeDC/Enum'); ``` Requirements ------------ -* CakePHP 4.0+ -* PHP 7.3+ +* CakePHP 5.0+ +* PHP 8.1+ Documentation ------------- @@ -67,6 +68,6 @@ This repository follows the [CakeDC Plugin Standard](http://cakedc.com/plugin-st License ------- -Copyright 2015 - 2021 Cake Development Corporation (CakeDC). All rights reserved. +Copyright 2015 - 2023 Cake Development Corporation (CakeDC). All rights reserved. Licensed under the [MIT](http://www.opensource.org/licenses/mit-license.php) License. Redistributions of the source code included in this repository must retain the copyright notice found in each file. diff --git a/composer.json b/composer.json index 8ec885b..0048be4 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,8 @@ "phpunit/phpunit": "^9.5.19", "cakephp/cakephp-codesniffer": "^5.0", "cakephp/bake": "3.x-dev", - "cakephp/migrations": "4.x-dev" + "cakephp/migrations": "4.x-dev", + "aura/intl": "^3.0" }, "autoload": { "psr-4": { diff --git a/config/Migrations/001_create_enum_lookups.php b/config/Migrations/001_create_enum_lookups.php index 2ffa2f1..49b4713 100644 --- a/config/Migrations/001_create_enum_lookups.php +++ b/config/Migrations/001_create_enum_lookups.php @@ -8,7 +8,7 @@ class CreateEnumLookups extends AbstractMigration * * @return void */ - public function change() + public function change(): void { $table = $this->table('enum_lookups'); diff --git a/src/Plugin.php b/src/EnumPlugin.php similarity index 54% rename from src/Plugin.php rename to src/EnumPlugin.php index b01c19f..6bd6e81 100644 --- a/src/Plugin.php +++ b/src/EnumPlugin.php @@ -2,12 +2,12 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ @@ -15,12 +15,7 @@ use Cake\Core\BasePlugin; -class Plugin extends BasePlugin +class EnumPlugin extends BasePlugin { - /** - * Plugin name. - * - * @var string - */ - protected $name = 'BootstrapUI'; + } diff --git a/src/Model/Behavior/EnumBehavior.php b/src/Model/Behavior/EnumBehavior.php index 983fc51..8206b50 100644 --- a/src/Model/Behavior/EnumBehavior.php +++ b/src/Model/Behavior/EnumBehavior.php @@ -2,12 +2,12 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ @@ -15,7 +15,8 @@ use ArrayObject; use BadMethodCallException; -use Cake\Event\EventInterface;use Cake\ORM\Behavior; +use Cake\Event\EventInterface; +use Cake\ORM\Behavior; use Cake\ORM\Query; use Cake\ORM\RulesChecker; use Cake\Utility\Hash; @@ -64,7 +65,7 @@ class EnumBehavior extends Behavior * * @var array */ - protected $_defaultConfig = [ + protected array $_defaultConfig = [ 'defaultStrategy' => 'lookup', 'translate' => false, 'translationDomain' => 'default', @@ -80,7 +81,7 @@ class EnumBehavior extends Behavior * * @var array */ - protected $_classMap = [ + protected array $classMap = [ 'lookup' => LookupStrategy::class, 'const' => ConstStrategy::class, 'config' => ConfigStrategy::class, @@ -91,7 +92,7 @@ class EnumBehavior extends Behavior * * @var array */ - protected $_strategies = []; + protected array $strategies = []; /** * Initializes the behavior. @@ -102,7 +103,7 @@ class EnumBehavior extends Behavior public function initialize(array $config): void { parent::initialize($config); - $this->_normalizeConfig(); + $this->normalizeConfig(); } /** @@ -113,28 +114,28 @@ public function initialize(array $config): void * @return \CakeDC\Enum\Model\Behavior\Strategy\StrategyInterface * @throws \CakeDC\Enum\Model\Behavior\Exception\MissingEnumStrategyException */ - public function strategy(string $alias, $strategy): \CakeDC\Enum\Model\Behavior\Strategy\StrategyInterface + public function strategy(string $alias, mixed $strategy): StrategyInterface { - if (!empty($this->_strategies[$alias])) { - return $this->_strategies[$alias]; + if (!empty($this->strategies[$alias])) { + return $this->strategies[$alias]; } - $this->_strategies[$alias] = $strategy; + $this->strategies[$alias] = $strategy; if ($strategy instanceof StrategyInterface) { return $strategy; } $class = null; - if (isset($this->_classMap[$strategy])) { - $class = $this->_classMap[$strategy]; + if (isset($this->classMap[$strategy])) { + $class = $this->classMap[$strategy]; } if ($class === null || !class_exists($class)) { throw new MissingEnumStrategyException([$class]); } - return $this->_strategies[$alias] = new $class($alias, $this->_table); + return $this->strategies[$alias] = new $class($alias, $this->_table); } /** @@ -142,10 +143,10 @@ public function strategy(string $alias, $strategy): \CakeDC\Enum\Model\Behavior\ * * @return void */ - protected function _normalizeConfig(): void + protected function normalizeConfig(): void { $classMap = $this->getConfig('classMap'); - $this->_classMap = array_merge($this->_classMap, $classMap); + $this->classMap = array_merge($this->classMap, $classMap); $lists = $this->getConfig('lists'); $defaultStrategy = $this->getConfig('defaultStrategy'); @@ -175,11 +176,11 @@ protected function _normalizeConfig(): void } /** - * @param string|array|null $alias Defined list's alias/name. + * @param array|string|null $alias Defined list's alias/name. * @return array * @throws \CakeDC\Enum\Model\Behavior\Exception\MissingEnumConfigurationException */ - public function enum($alias = null): array + public function enum(array|string|null $alias = null): array { if (is_string($alias)) { $config = $this->getConfig('lists.' . $alias); @@ -187,7 +188,7 @@ public function enum($alias = null): array throw new MissingEnumConfigurationException([$alias]); } - return $this->_enumList($alias, $config); + return $this->enumList($alias, $config); } $lists = $this->getConfig('lists'); @@ -197,7 +198,7 @@ public function enum($alias = null): array $return = []; foreach ($lists as $alias => $config) { - $return[$alias] = $this->_enumList($alias, $config); + $return[$alias] = $this->enumList($alias, $config); } return $return; @@ -208,11 +209,11 @@ public function enum($alias = null): array * @param array $config Config * @return array */ - protected function _enumList(string $alias, array $config): array + protected function enumList(string $alias, array $config): array { $return = $this->strategy($alias, $config['strategy'])->enum($config); if ($this->getConfig('translate')) { - $return = $this->_translate($return); + $return = $this->translate($return); } if ($this->getConfig('nested')) { @@ -235,7 +236,7 @@ function (&$item, $val) { * @param array $list List. * @return array */ - protected function _translate(array $list): array + protected function translate(array $list): array { $domain = $this->getConfig('translationDomain'); @@ -249,7 +250,7 @@ protected function _translate(array $list): array * @param \Cake\ORM\RulesChecker $rules Rules checker. * @return \Cake\ORM\RulesChecker */ - public function buildRules(EventInterface $event, RulesChecker $rules): \Cake\ORM\RulesChecker + public function buildRules(EventInterface $event, RulesChecker $rules): RulesChecker { foreach ($this->getConfig('lists') as $alias => $config) { if (Hash::get($config, 'applicationRules') === false) { @@ -277,7 +278,7 @@ public function buildRules(EventInterface $event, RulesChecker $rules): \Cake\OR */ public function __call(string $method, array $args): bool { - if (strpos($method, 'isValid') !== 0) { + if (!str_starts_with($method, 'isValid')) { throw new BadMethodCallException(sprintf('Call to undefined method (%s)', $method)); } @@ -302,7 +303,7 @@ public function __call(string $method, array $args): bool * @param \ArrayObject $options The options for the query * @return void */ - public function beforeFind(EventInterface $event, Query $query, ArrayObject $options) + public function beforeFind(EventInterface $event, Query $query, ArrayObject $options): void { foreach ($this->getConfig('lists') as $alias => $config) { $strategy = $this->strategy($alias, $config['strategy']); diff --git a/src/Model/Behavior/Exception/InvalidAliasListException.php b/src/Model/Behavior/Exception/InvalidAliasListException.php index bb32383..57374e3 100644 --- a/src/Model/Behavior/Exception/InvalidAliasListException.php +++ b/src/Model/Behavior/Exception/InvalidAliasListException.php @@ -2,12 +2,12 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ @@ -17,5 +17,5 @@ class InvalidAliasListException extends CakeException { - protected $_messageTemplate = 'Invalid alias %s. %s defined association %s.'; + protected string $_messageTemplate = 'Invalid alias %s. %s defined association %s.'; } diff --git a/src/Model/Behavior/Exception/MissingEnumConfigurationException.php b/src/Model/Behavior/Exception/MissingEnumConfigurationException.php index fb5e7dc..ae0810d 100644 --- a/src/Model/Behavior/Exception/MissingEnumConfigurationException.php +++ b/src/Model/Behavior/Exception/MissingEnumConfigurationException.php @@ -2,12 +2,12 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ @@ -17,5 +17,5 @@ class MissingEnumConfigurationException extends CakeException { - protected $_templateMessage = 'Missing enum configuration (%s)'; + protected string $_messageTemplate = 'Missing enum configuration (%s)'; } diff --git a/src/Model/Behavior/Exception/MissingEnumStrategyException.php b/src/Model/Behavior/Exception/MissingEnumStrategyException.php index 229720a..0e29578 100644 --- a/src/Model/Behavior/Exception/MissingEnumStrategyException.php +++ b/src/Model/Behavior/Exception/MissingEnumStrategyException.php @@ -2,12 +2,12 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ @@ -17,5 +17,5 @@ class MissingEnumStrategyException extends CakeException { - protected $_templateMessage = 'Missing enum strategy class (%s)'; + protected string $_messageTemplate = 'Missing enum strategy class (%s)'; } diff --git a/src/Model/Behavior/Exception/MissingEnumStrategyPrefixException.php b/src/Model/Behavior/Exception/MissingEnumStrategyPrefixException.php index 908d196..e539421 100644 --- a/src/Model/Behavior/Exception/MissingEnumStrategyPrefixException.php +++ b/src/Model/Behavior/Exception/MissingEnumStrategyPrefixException.php @@ -2,12 +2,12 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ @@ -17,5 +17,5 @@ class MissingEnumStrategyPrefixException extends CakeException { - protected $_messageTemplate = 'Missing prefix for strategy (%s)'; + protected string $_messageTemplate = 'Missing prefix for strategy (%s)'; } diff --git a/src/Model/Behavior/Strategy/AbstractStrategy.php b/src/Model/Behavior/Strategy/AbstractStrategy.php index d43dc30..3dae0f6 100644 --- a/src/Model/Behavior/Strategy/AbstractStrategy.php +++ b/src/Model/Behavior/Strategy/AbstractStrategy.php @@ -2,12 +2,12 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ @@ -26,21 +26,21 @@ abstract class AbstractStrategy implements StrategyInterface * * @var array */ - protected $_defaultConfig = []; + protected array $_defaultConfig = []; /** * Target table. * * @var \Cake\ORM\Table */ - protected $_table; + protected Table $table; /** * Table alias. * * @var string */ - protected $_alias; + protected string $alias; /** * Constructor. @@ -50,8 +50,8 @@ abstract class AbstractStrategy implements StrategyInterface */ public function __construct(string $alias, Table $table) { - $this->_alias = $alias; - $this->_table = $table; + $this->alias = $alias; + $this->table = $table; } /** @@ -61,11 +61,11 @@ public function initialize(array $config): void { $prefix = $this->getConfig('prefix'); if (empty($config['prefix']) && empty($prefix)) { - $config['prefix'] = $this->_generatePrefix(); + $config['prefix'] = $this->generatePrefix(); } if (empty($config['field'])) { - $config['field'] = Inflector::underscore($this->_alias); + $config['field'] = Inflector::underscore($this->alias); } if (empty($config['errorMessage'])) { @@ -83,10 +83,10 @@ public function initialize(array $config): void * * @return string */ - protected function _generatePrefix(): string + protected function generatePrefix(): string { - $prefix = Inflector::underscore(Inflector::singularize($this->_table->getAlias())); - $prefix .= '_' . $this->_alias; + $prefix = Inflector::underscore(Inflector::singularize($this->table->getAlias())); + $prefix .= '_' . $this->alias; return strtoupper($prefix); } diff --git a/src/Model/Behavior/Strategy/ConfigStrategy.php b/src/Model/Behavior/Strategy/ConfigStrategy.php index 6c2fb89..1055dc3 100644 --- a/src/Model/Behavior/Strategy/ConfigStrategy.php +++ b/src/Model/Behavior/Strategy/ConfigStrategy.php @@ -2,12 +2,12 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ diff --git a/src/Model/Behavior/Strategy/ConstStrategy.php b/src/Model/Behavior/Strategy/ConstStrategy.php index 2938796..6bd80fe 100644 --- a/src/Model/Behavior/Strategy/ConstStrategy.php +++ b/src/Model/Behavior/Strategy/ConstStrategy.php @@ -2,12 +2,12 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ @@ -31,7 +31,7 @@ class ConstStrategy extends AbstractStrategy * * @var array */ - protected $_constants; + protected array $constants; /** * {@inheritDoc} @@ -51,10 +51,11 @@ public function __construct(string $alias, Table $table) * * @param array $config List of callable filters to limit items generated from list. * @return array + * @throws \ReflectionException */ public function enum(array $config = []): array { - $constants = $this->_getConstants(); + $constants = $this->getConstants(); $keys = array_keys($constants); foreach ($config as $callable) { @@ -74,22 +75,23 @@ public function enum(array $config = []): array * Returns defined constants for the current `$_table`. * * @return array + * @throws \ReflectionException */ - protected function _getConstants(): array + protected function getConstants(): array { - if ($this->_constants !== null) { - return $this->_constants; + if ($this->constants !== null) { + return $this->constants; } $prefix = $this->getConfig('prefix'); $lowercase = $this->getConfig('lowercase'); - $className = $this->getConfig('className') ?: get_class($this->_table); + $className = $this->getConfig('className') ?: get_class($this->table); $length = strlen($prefix) + 1; $classConstants = (new ReflectionClass($className))->getConstants(); $constants = []; foreach ($classConstants as $key => $value) { - if (strpos($key, (string)$prefix) === 0) { + if (str_starts_with($key, (string)$prefix)) { $listKey = substr($key, $length); if ($lowercase) { $listKey = strtolower($listKey); @@ -98,7 +100,7 @@ protected function _getConstants(): array } } - return $this->_constants = $constants; + return $this->constants = $constants; } /** @@ -107,16 +109,18 @@ protected function _getConstants(): array * @param \ArrayObject $options The options for the query * @return void */ - public function beforeFind(EventInterface $event, Query $query, ArrayObject $options) + public function beforeFind(EventInterface $event, Query $query, ArrayObject $options): void { - $assocName = Inflector::pluralize(Inflector::classify($this->_alias)); - if ($this->_table->hasAssociation($assocName)) { - throw new InvalidAliasListException([$this->_alias, $this->_table->getAlias(), $assocName]); + $assocName = Inflector::pluralize(Inflector::classify($this->alias)); + if ($this->table->hasAssociation($assocName)) { + throw new InvalidAliasListException([$this->alias, $this->table->getAlias(), $assocName]); } - $contain = array_filter($query->getContain(), function ($value) use ($assocName) { - return $value !== $assocName; - }, ARRAY_FILTER_USE_KEY); + $contain = array_filter( + $query->getContain(), + fn($value): bool => $value !== $assocName, + ARRAY_FILTER_USE_KEY + ); $query->clearContain()->contain($contain); @@ -128,9 +132,9 @@ public function beforeFind(EventInterface $event, Query $query, ArrayObject $opt $constant = Hash::get($row, $this->getConfig('field')); - $field = Inflector::singularize(Inflector::underscore($this->_alias)); + $field = Inflector::singularize(Inflector::underscore($this->alias)); $value = new Entity([ - 'label' => Hash::get($this->_getConstants(), $constant, $constant), + 'label' => Hash::get($this->getConstants(), $constant, $constant), 'prefix' => $this->getConfig('prefix'), 'value' => $constant, ], ['markClean' => true, 'markNew' => false]); diff --git a/src/Model/Behavior/Strategy/LookupStrategy.php b/src/Model/Behavior/Strategy/LookupStrategy.php index 7c5f4be..4055f83 100644 --- a/src/Model/Behavior/Strategy/LookupStrategy.php +++ b/src/Model/Behavior/Strategy/LookupStrategy.php @@ -56,9 +56,9 @@ public function initialize(array $config): void { parent::initialize($config); $config = $this->getConfig(); - $assocName = Inflector::pluralize(Inflector::classify($this->_alias)); + $assocName = Inflector::pluralize(Inflector::classify($this->alias)); - $this->_table + $this->table ->belongsTo($assocName) ->setClassName('CakeDC/Enum.Lookups') ->setForeignKey($config['field']) diff --git a/src/Model/Behavior/Strategy/StrategyInterface.php b/src/Model/Behavior/Strategy/StrategyInterface.php index 44d50da..364ac53 100644 --- a/src/Model/Behavior/Strategy/StrategyInterface.php +++ b/src/Model/Behavior/Strategy/StrategyInterface.php @@ -2,12 +2,12 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ @@ -60,5 +60,5 @@ public function initialize(array $config): void; * @param mixed $default The return value when the key does not exist. * @return mixed Config value being read. */ - public function getConfig(?string $key = null, $default = null); + public function getConfig(?string $key = null, mixed $default = null): mixed; } diff --git a/src/Model/Table/LookupsTable.php b/src/Model/Table/LookupsTable.php index 4f7cb07..f968657 100644 --- a/src/Model/Table/LookupsTable.php +++ b/src/Model/Table/LookupsTable.php @@ -2,12 +2,12 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ diff --git a/tests/Fixture/ArticlesFixture.php b/tests/Fixture/ArticlesFixture.php index ec0bd78..6140f70 100644 --- a/tests/Fixture/ArticlesFixture.php +++ b/tests/Fixture/ArticlesFixture.php @@ -1,12 +1,13 @@ ['type' => 'integer'], 'title' => ['type' => 'string'], 'body' => ['type' => 'text'], @@ -28,7 +29,7 @@ class ArticlesFixture extends TestFixture '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]], ]; - public $records = [ + public array $records = [ [ 'title' => 'Dummy article', 'body' => '', diff --git a/tests/Fixture/LookupsFixture.php b/tests/Fixture/LookupsFixture.php index 6de7a05..75cdabb 100644 --- a/tests/Fixture/LookupsFixture.php +++ b/tests/Fixture/LookupsFixture.php @@ -1,12 +1,13 @@ ['type' => 'integer'], 'label' => ['type' => 'string'], 'prefix' => ['type' => 'string'], @@ -26,7 +27,7 @@ class LookupsFixture extends TestFixture '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]], ]; - public $records = [ + public array $records = [ [ 'label' => 'Urgent', 'prefix' => 'PRIORITY', diff --git a/tests/TestCase/Model/Behavior/EnumBehaviorTest.php b/tests/TestCase/Model/Behavior/EnumBehaviorTest.php index d7b4abf..09b3203 100644 --- a/tests/TestCase/Model/Behavior/EnumBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/EnumBehaviorTest.php @@ -2,20 +2,21 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ namespace CakeDC\Enum\Test\TestCase\Model\Behavior; use Cake\Core\Configure; +use Cake\ORM\Association\BelongsTo; +use Cake\ORM\Entity; use Cake\ORM\Table; -use Cake\ORM\TableRegistry; use Cake\TestSuite\TestCase; use CakeDC\Enum\Model\Behavior\Strategy\AbstractStrategy; @@ -70,12 +71,12 @@ public function enum(array $config = []): array class EnumBehaviorTest extends TestCase // @codingStandardsIgnoreEnd { - public $fixtures = [ + public array $fixtures = [ 'plugin.CakeDC/Enum.Articles', 'plugin.CakeDC/Enum.Lookups', ]; - protected $Articles; + protected Table $Articles; public function setUp(): void { @@ -86,8 +87,8 @@ public function setUp(): void 'Open Source Software', ]); - $this->Articles = TableRegistry::get('CakeDC/Enum.Articles', [ - 'className' => \CakeDC\Enum\Test\TestCase\Model\Behavior\ArticlesTable::class, + $this->Articles = $this->getTableLocator()->get('CakeDC/Enum.Articles', [ + 'className' => ArticlesTable::class, 'table' => 'enum_articles', ]); } @@ -95,10 +96,10 @@ public function setUp(): void public function tearDown(): void { parent::tearDown(); - TableRegistry::clear(); + $this->getTableLocator()->clear(); } - public function provideBasicConfiguration() + public function provideBasicConfiguration(): array { $expected = [ 'defaultStrategy' => 'lookup', @@ -187,14 +188,14 @@ public function provideBasicConfiguration() */ public function testBasicConfiguration(array $config, array $expected) { - TableRegistry::clear(); - $Articles = TableRegistry::get('CakeDC/Enum.Articles', ['table' => 'enum_articles']); + $this->getTableLocator()->clear(); + $Articles = $this->getTableLocator()->get('CakeDC/Enum.Articles', ['table' => 'enum_articles']); $Articles->addBehavior('CakeDC/Enum.Enum', $config); $result = $Articles->behaviors()->Enum->getConfig(); $this->assertEquals($expected, $result); } - public function provideBasicLookups() + public function provideBasicLookups(): array { return [ [ @@ -251,7 +252,7 @@ public function testBasicLookups($group, $expected) $this->assertEquals($expected, $result); } - public function provideBuildRules() + public function provideBuildRules(): array { return [ [ @@ -294,7 +295,7 @@ public function provideBuildRules() */ public function testBuildRules($data, $expected) { - $article = new \Cake\ORM\Entity($data); + $article = new Entity($data); $this->Articles->save($article); $result = $article->getErrors(); $this->assertEquals($expected, $result); @@ -319,7 +320,7 @@ public function testAssociationsCreated() $this->assertEquals($expected, $result); foreach ($result as $assoc) { - $this->assertInstanceOf(\Cake\ORM\Association\BelongsTo::class, $this->Articles->getAssociation($assoc)); + $this->assertInstanceOf(BelongsTo::class, $this->Articles->getAssociation($assoc)); } $result = $this->Articles->get(1); @@ -399,12 +400,12 @@ public function testTranslatedValues() $this->assertEquals($expected, $result); } - public function provideThirdPartyStrategy() + public function provideThirdPartyStrategy(): array { return [ [ [ - 'classMap' => ['third_party' => \CakeDC\Enum\Test\TestCase\Model\Behavior\ThirdPartyStrategy::class], + 'classMap' => ['third_party' => ThirdPartyStrategy::class], 'lists' => [ 'article_category' => ['strategy' => 'third_party'], ], @@ -422,8 +423,8 @@ public function provideThirdPartyStrategy() */ public function testThirdPartyStrategy(array $config, array $expected) { - TableRegistry::clear(); - $Articles = TableRegistry::get('CakeDC/Enum.Articles', ['table' => 'enum_articles']); + $this->getTableLocator()->clear(); + $Articles = $this->getTableLocator()->get('CakeDC/Enum.Articles', ['table' => 'enum_articles']); $Articles->addBehavior('CakeDC/Enum.Enum', $config); $result = $Articles->enum('article_category'); $this->assertEquals($expected, $result); diff --git a/tests/TestCase/Model/Behavior/Strategy/ConfigStrategyTest.php b/tests/TestCase/Model/Behavior/Strategy/ConfigStrategyTest.php index e0dee36..5526323 100644 --- a/tests/TestCase/Model/Behavior/Strategy/ConfigStrategyTest.php +++ b/tests/TestCase/Model/Behavior/Strategy/ConfigStrategyTest.php @@ -2,12 +2,12 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ @@ -20,7 +20,7 @@ class ConfigStrategyTest extends TestCase { - public $Strategy; + public ConfigStrategy $Strategy; public function setUp(): void { diff --git a/tests/TestCase/Model/Behavior/Strategy/ConstStrategyTest.php b/tests/TestCase/Model/Behavior/Strategy/ConstStrategyTest.php index a24cedd..e672258 100644 --- a/tests/TestCase/Model/Behavior/Strategy/ConstStrategyTest.php +++ b/tests/TestCase/Model/Behavior/Strategy/ConstStrategyTest.php @@ -2,12 +2,12 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ @@ -39,9 +39,9 @@ class Article extends Entity class ConstStrategyTest extends TestCase // @codingStandardsIgnoreEnd { - public $StrategyTable; + public ConstStrategy $StrategyTable; - public $StrategyEntity; + public ConstStrategy $StrategyEntity; public function setUp(): void { @@ -64,6 +64,9 @@ public function tearDown(): void unset($this->StrategyEntity); } + /** + * @throws \ReflectionException + */ public function testEnumTable() { $result = $this->StrategyTable->enum(); @@ -78,6 +81,9 @@ public function testEnumTable() $this->assertEquals($expected, $this->StrategyTable->enum()); } + /** + * @throws \ReflectionException + */ public function testEnumEntity() { $result = $this->StrategyEntity->enum(); diff --git a/tests/TestCase/Model/Behavior/Strategy/LookupStrategyTest.php b/tests/TestCase/Model/Behavior/Strategy/LookupStrategyTest.php index f0c214a..08b3ef6 100644 --- a/tests/TestCase/Model/Behavior/Strategy/LookupStrategyTest.php +++ b/tests/TestCase/Model/Behavior/Strategy/LookupStrategyTest.php @@ -2,12 +2,12 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ @@ -22,8 +22,9 @@ class LookupStrategyTest extends TestCase /** * @var \CakeDC\Enum\Model\Behavior\Strategy\LookupStrategy|mixed */ - public $Strategy; - public $fixtures = [ + public mixed $Strategy; + + public array $fixtures = [ 'plugin.CakeDC/Enum.Lookups', ]; diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 903a1a7..e7f7571 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -2,18 +2,23 @@ declare(strict_types=1); /** - * Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com) + * @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ use Aura\Intl\Package; +use Cake\Core\Plugin; use Cake\I18n\I18n; +use CakeDC\Enum\EnumPlugin; +/** + * @throws \Exception + */ $findRoot = function ($root) { do { $lastRoot = $root; @@ -35,7 +40,7 @@ require $root . '/vendor/cakephp/cakephp/tests/bootstrap.php'; -\Cake\Core\Plugin::getCollection()->add(new \CakeDC\Enum\Plugin(['path' => dirname(__FILE__, 2) . DS])); +Plugin::getCollection()->add(new EnumPlugin(['path' => dirname(__FILE__, 2) . DS])); I18n::config('default', function ($name, $locale) { $package = new Package('default'); $messages = [ From 6d23fe3548f5ba82383c8157bda97a1fe94c3bfd Mon Sep 17 00:00:00 2001 From: Adam Rusinowski Date: Wed, 26 Apr 2023 15:17:15 +0200 Subject: [PATCH 09/10] dependency check, gitignore update --- .gitignore | 3 +++ VERSION | 1 + 2 files changed, 4 insertions(+) create mode 100644 VERSION diff --git a/.gitignore b/.gitignore index 364f9bd..92d9288 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ /composer.lock /plugins /vendor +/auth.json +/.ddev +/.idea diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..2468aa9 --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +3.0.0-dev From 72f10337e02fc1dc81105e3fc456e0f5a4f19a73 Mon Sep 17 00:00:00 2001 From: Adam Rusinowski Date: Tue, 23 May 2023 10:10:54 +0200 Subject: [PATCH 10/10] added github ci workflow --- .github/workflows/ci.yml | 120 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..db9c870 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,120 @@ +name: CI + +on: + push: + + pull_request: + branches: + - '*' + +jobs: + testsuite: + runs-on: ubuntu-22.04 + strategy: + fail-fast: false + matrix: + php-version: ['8.1'] + db-type: [sqlite, mysql, pgsql] + prefer-lowest: [''] + + steps: + - name: Setup MySQL latest + 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 --default-authentication-plugin=mysql_native_password --disable-log-bin + + - name: Setup PostgreSQL latest + if: matrix.db-type == 'pgsql' + run: docker run --rm --name=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=cakephp -p 5432:5432 -d postgres + + - uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-version }} + extensions: mbstring, intl, apcu, sqlite, pdo_sqlite, pdo_${{ matrix.db-type }}, ${{ matrix.db-type }} + ini-values: apc.enable_cli = 1 + 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@v1 + 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: Setup problem matchers for PHPUnit + if: matrix.php-version == '7.4' && matrix.db-type == 'mysql' + run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" + + - name: Run PHPUnit + run: | + if [[ ${{ matrix.db-type }} == 'sqlite' ]]; then export DB_URL='sqlite:///:memory:'; fi + if [[ ${{ matrix.db-type }} == 'mysql' ]]; then export DB_URL='mysql://root:root@127.0.0.1/cakephp?encoding=utf8'; fi + if [[ ${{ matrix.db-type }} == 'pgsql' ]]; then export DB_URL='postgres://postgres:postgres@127.0.0.1/postgres'; fi + if [[ ${{ matrix.php-version }} == '7.4' ]]; then + export CODECOVERAGE=1 && vendor/bin/phpunit --verbose --coverage-clover=coverage.xml + else + vendor/bin/phpunit + fi + + - name: Submit code coverage + if: matrix.php-version == '7.4' + uses: codecov/codecov-action@v1 + + cs-stan: + name: Coding Standard & Static Analysis + runs-on: ubuntu-22.04 + + steps: + - uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '7.4' + extensions: mbstring, intl, apcu + 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@v1 + 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 psalm + # if: success() || failure() + # run: vendor/bin/psalm.phar --output-format=github + + - name: Run phpstan + if: success() || failure() + run: composer stan