4
4
5
5
namespace Doctrine \Tests \ORM \Functional ;
6
6
7
+ use Doctrine \DBAL \Types \EnumType ;
7
8
use Doctrine \ORM \AbstractQuery ;
8
9
use Doctrine \ORM \Mapping \Column ;
9
10
use Doctrine \ORM \Mapping \Driver \AttributeDriver ;
13
14
use Doctrine \Tests \Models \DataTransferObjects \DtoWithArrayOfEnums ;
14
15
use Doctrine \Tests \Models \DataTransferObjects \DtoWithEnum ;
15
16
use Doctrine \Tests \Models \Enums \Card ;
17
+ use Doctrine \Tests \Models \Enums \CardNativeEnum ;
16
18
use Doctrine \Tests \Models \Enums \CardWithDefault ;
17
19
use Doctrine \Tests \Models \Enums \CardWithNullable ;
18
20
use Doctrine \Tests \Models \Enums \Product ;
19
21
use Doctrine \Tests \Models \Enums \Quantity ;
20
22
use Doctrine \Tests \Models \Enums \Scale ;
21
23
use Doctrine \Tests \Models \Enums \Suit ;
22
24
use Doctrine \Tests \Models \Enums \TypedCard ;
25
+ use Doctrine \Tests \Models \Enums \TypedCardNativeEnum ;
23
26
use Doctrine \Tests \Models \Enums \Unit ;
24
27
use Doctrine \Tests \OrmFunctionalTestCase ;
25
28
use PHPUnit \Framework \Attributes \DataProvider ;
26
29
30
+ use function class_exists ;
27
31
use function dirname ;
28
32
use function sprintf ;
29
33
use function uniqid ;
@@ -55,7 +59,7 @@ public function testEnumMapping(string $cardClass): void
55
59
$ this ->_em ->flush ();
56
60
$ this ->_em ->clear ();
57
61
58
- $ fetchedCard = $ this ->_em ->find (Card::class , $ card ->id );
62
+ $ fetchedCard = $ this ->_em ->find ($ cardClass , $ card ->id );
59
63
60
64
$ this ->assertInstanceOf (Suit::class, $ fetchedCard ->suit );
61
65
$ this ->assertEquals (Suit::Clubs, $ fetchedCard ->suit );
@@ -417,6 +421,10 @@ public function testFindByEnum(): void
417
421
#[DataProvider('provideCardClasses ' )]
418
422
public function testEnumWithNonMatchingDatabaseValueThrowsException (string $ cardClass ): void
419
423
{
424
+ if ($ cardClass === TypedCardNativeEnum::class) {
425
+ self ::markTestSkipped ('MySQL won \'t allow us to insert invalid values in this case. ' );
426
+ }
427
+
420
428
$ this ->setUpEntitySchema ([$ cardClass ]);
421
429
422
430
$ card = new $ cardClass ();
@@ -429,15 +437,15 @@ public function testEnumWithNonMatchingDatabaseValueThrowsException(string $card
429
437
$ metadata = $ this ->_em ->getClassMetadata ($ cardClass );
430
438
$ this ->_em ->getConnection ()->update (
431
439
$ metadata ->table ['name ' ],
432
- [$ metadata ->fieldMappings ['suit ' ]->columnName => 'invalid ' ],
440
+ [$ metadata ->fieldMappings ['suit ' ]->columnName => 'Z ' ],
433
441
[$ metadata ->fieldMappings ['id ' ]->columnName => $ card ->id ],
434
442
);
435
443
436
444
$ this ->expectException (MappingException::class);
437
445
$ this ->expectExceptionMessage (sprintf (
438
446
<<<'EXCEPTION'
439
447
Context: Trying to hydrate enum property "%s::$suit"
440
- Problem: Case "invalid " is not listed in enum "Doctrine\Tests\Models\Enums\Suit"
448
+ Problem: Case "Z " is not listed in enum "Doctrine\Tests\Models\Enums\Suit"
441
449
Solution: Either add the case to the enum type or migrate the database column to use another case of the enum
442
450
EXCEPTION
443
451
,
@@ -447,13 +455,16 @@ public function testEnumWithNonMatchingDatabaseValueThrowsException(string $card
447
455
$ this ->_em ->find ($ cardClass , $ card ->id );
448
456
}
449
457
450
- /** @return array <string, array{class-string}> */
451
- public static function provideCardClasses (): array
458
+ /** @return iterable <string, array{class-string}> */
459
+ public static function provideCardClasses (): iterable
452
460
{
453
- return [
454
- Card::class => [Card::class],
455
- TypedCard::class => [TypedCard::class],
456
- ];
461
+ yield Card::class => [Card::class];
462
+ yield TypedCard::class => [TypedCard::class];
463
+
464
+ if (class_exists (EnumType::class)) {
465
+ yield CardNativeEnum::class => [CardNativeEnum::class];
466
+ yield TypedCardNativeEnum::class => [TypedCardNativeEnum::class];
467
+ }
457
468
}
458
469
459
470
public function testItAllowsReadingAttributes (): void
0 commit comments