Skip to content

Commit

Permalink
feat(metadata): detect enumeration type hint
Browse files Browse the repository at this point in the history
This commit does not contains a test because AFAIK for the moment there
is no test scoped to a specific version of PHP.

It fixes doctrine#9021
  • Loading branch information
Nek- committed Oct 25, 2021
1 parent 3361691 commit 24e29f1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,10 @@ private function validateAndCompleteTypedFieldMapping(array $mapping): array
case 'string':
$mapping['type'] = Types::STRING;
break;
default:
if (PHP_VERSION_ID >= 80100 && enum_exists($type->getName())) {
$mapping['type'] = Types::ENUM;
}
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions tests/Doctrine/Tests/Models/TypedProperties/Article.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\TypedProperties;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity, ORM\Table(name: 'cms_articles_enumed')]
class Article
{
#[ORM\Id, ORM\Column, ORM\GeneratedValue]
public int $id;

#[ORM\Column]
public ArticleStateEnum $enum;
}
11 changes: 11 additions & 0 deletions tests/Doctrine/Tests/Models/TypedProperties/ArticleStateEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\TypedProperties;

enum ArticleStateEnum
{
case DRAFT;
case PUBLISHED;
}
15 changes: 14 additions & 1 deletion tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function testFieldIsNullableByType(): void
public function testFieldTypeFromReflection(): void
{
if (PHP_VERSION_ID < 70400) {
self::markTestSkipped('requies PHP 7.4');
self::markTestSkipped('requires PHP 7.4');
}

$cm = new ClassMetadata(TypedProperties\UserTyped::class);
Expand Down Expand Up @@ -174,6 +174,19 @@ public function testFieldTypeFromReflection(): void
self::assertEquals('float', $cm->getTypeOfField('float'));
}

public function testFieldTypeEnumFromReflection(): void
{
if (PHP_VERSION_ID < 80100) {
self::markTestSkipped('requires PHP 8.1');
}

$cm = new ClassMetadata(TypedProperties\Article::class);
$cm->initializeReflection(new RuntimeReflectionService());

$cm->mapField(['fieldName' => 'enum']);
self::assertEquals('enum', $cm->getTypeOfField('enum'));
}

/**
* @group DDC-115
*/
Expand Down

0 comments on commit 24e29f1

Please sign in to comment.