From 514c4b208abe87476358c482c48b4903145fa87d Mon Sep 17 00:00:00 2001 From: ADmad Date: Thu, 2 Nov 2023 20:16:36 +0530 Subject: [PATCH] Fix annotation for entity field mapped to enum type. Closes #958 --- src/View/Helper/DocBlockHelper.php | 9 ++++++++ tests/TestCase/Command/ModelCommandTest.php | 17 ++++++++++++++ .../comparisons/Model/testBakeEntityEnum.php | 23 +++++++++++++++++++ .../test_app/App/Model/Enum/ArticleStatus.php | 21 +++++++++++++++++ .../App/Model/Table/ArticlesTable.php | 8 +++++-- tests/test_app/App/plugins.php | 5 ++-- 6 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 tests/comparisons/Model/testBakeEntityEnum.php create mode 100644 tests/test_app/App/Model/Enum/ArticleStatus.php diff --git a/src/View/Helper/DocBlockHelper.php b/src/View/Helper/DocBlockHelper.php index 6e5a3b58..f93c9917 100644 --- a/src/View/Helper/DocBlockHelper.php +++ b/src/View/Helper/DocBlockHelper.php @@ -5,6 +5,7 @@ use Cake\Collection\Collection; use Cake\Core\App; +use Cake\Database\Type\EnumType; use Cake\Database\TypeFactory; use Cake\ORM\Association; use Cake\Utility\Inflector; @@ -230,6 +231,14 @@ public function columnTypeToHintType(string $type): ?string } return '\Cake\I18n\Time'; + + default: + if (str_starts_with($type, 'enum-')) { + $dbType = TypeFactory::build($type); + if ($dbType instanceof EnumType) { + return '\\' . $dbType->getEnumClassName(); + } + } } // Any unique or custom types will have a `string` type hint diff --git a/tests/TestCase/Command/ModelCommandTest.php b/tests/TestCase/Command/ModelCommandTest.php index 0e3b3930..3e5f8bd9 100644 --- a/tests/TestCase/Command/ModelCommandTest.php +++ b/tests/TestCase/Command/ModelCommandTest.php @@ -46,6 +46,7 @@ class ModelCommandTest extends TestCase * @var array */ protected array $fixtures = [ + 'plugin.Bake.Articles', 'plugin.Bake.Comments', 'plugin.Bake.Tags', 'plugin.Bake.ArticlesTags', @@ -1924,6 +1925,22 @@ public function testBakeEntityWithPlugin() $this->assertSameAsFile(__FUNCTION__ . '.php', $result); } + /** + * test baking an entity class with an enum field + * + * @return void + */ + public function testBakeEntityEnum() + { + $this->generatedFile = APP . 'Model/Entity/Article.php'; + $this->exec('bake model --no-test --no-fixture --no-table --no-fields Articles'); + + $this->assertExitCode(CommandInterface::CODE_SUCCESS); + $this->assertFileExists($this->generatedFile); + $result = file_get_contents($this->generatedFile); + $this->assertSameAsFile(__FUNCTION__ . '.php', $result); + } + /** * Tests baking a table with rules * diff --git a/tests/comparisons/Model/testBakeEntityEnum.php b/tests/comparisons/Model/testBakeEntityEnum.php new file mode 100644 index 00000000..c238ee51 --- /dev/null +++ b/tests/comparisons/Model/testBakeEntityEnum.php @@ -0,0 +1,23 @@ +belongsTo('authors'); $this->belongsToMany('tags'); $this->hasMany('ArticlesTags'); + + $this->getSchema()->setColumnType('published', EnumType::from(ArticleStatus::class)); } /** * Find published * - * @param Cake\ORM\Query\SelectQuery $query The query - * @return Cake\ORM\Query\SelectQuery + * @param \Cake\ORM\Query\SelectQuery $query The query + * @return \Cake\ORM\Query\SelectQuery */ public function findPublished($query) { diff --git a/tests/test_app/App/plugins.php b/tests/test_app/App/plugins.php index 827d5207..34c433aa 100644 --- a/tests/test_app/App/plugins.php +++ b/tests/test_app/App/plugins.php @@ -1,7 +1,8 @@ [], 'Simple' => [], 'Company/Example' => [], - 'ComposerExample' => [] -]; \ No newline at end of file + 'ComposerExample' => [], +];