Skip to content

Commit

Permalink
Merge pull request #959 from cakephp/enum-entity-annotation
Browse files Browse the repository at this point in the history
Fix annotation for entity field mapped to enum type.
  • Loading branch information
LordSimal committed Nov 2, 2023
2 parents 18486ac + 514c4b2 commit 369cfb1
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/View/Helper/DocBlockHelper.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions tests/TestCase/Command/ModelCommandTest.php
Expand Up @@ -46,6 +46,7 @@ class ModelCommandTest extends TestCase
* @var array<string>
*/
protected array $fixtures = [
'plugin.Bake.Articles',
'plugin.Bake.Comments',
'plugin.Bake.Tags',
'plugin.Bake.ArticlesTags',
Expand Down Expand Up @@ -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
*
Expand Down
23 changes: 23 additions & 0 deletions tests/comparisons/Model/testBakeEntityEnum.php
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

namespace Bake\Test\App\Model\Entity;

use Cake\ORM\Entity;

/**
* Article Entity
*
* @property int $id
* @property int|null $author_id
* @property string|null $title
* @property string|null $body
* @property \Bake\Test\App\Model\Enum\ArticleStatus|null $published
*
* @property \Bake\Test\App\Model\Entity\Author $author
* @property \Bake\Test\App\Model\Entity\Tag[] $tags
* @property \Bake\Test\App\Model\Entity\ArticlesTag[] $articles_tags
*/
class Article extends Entity
{
}
21 changes: 21 additions & 0 deletions tests/test_app/App/Model/Enum/ArticleStatus.php
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);

/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @since 3.0.4
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Bake\Test\App\Model\Enum;

enum ArticleStatus: string
{
case PUBLISHED = 'Y';
case UNPUBLISHED = 'N';
}
8 changes: 6 additions & 2 deletions tests/test_app/App/Model/Table/ArticlesTable.php
Expand Up @@ -16,6 +16,8 @@
*/
namespace Bake\Test\App\Model\Table;

use Bake\Test\App\Model\Enum\ArticleStatus;
use Cake\Database\Type\EnumType;
use Cake\ORM\Table;

/**
Expand All @@ -28,13 +30,15 @@ public function initialize(array $config): void
$this->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)
{
Expand Down
5 changes: 3 additions & 2 deletions tests/test_app/App/plugins.php
@@ -1,7 +1,8 @@
<?php

return [
'SimpleExample' => [],
'Simple' => [],
'Company/Example' => [],
'ComposerExample' => []
];
'ComposerExample' => [],
];

0 comments on commit 369cfb1

Please sign in to comment.