Skip to content

Commit 771c4b3

Browse files
authored
Merge pull request #2532 from malarzm/array-of-enums
Test persisting array of enums
2 parents bdbb370 + f315ef5 commit 771c4b3

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

tests/Doctrine/ODM/MongoDB/Tests/Functional/EnumTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
use Documents81\Card;
1111
use Documents81\Suit;
1212
use Error;
13+
use Jean85\PrettyVersions;
1314
use MongoDB\BSON\ObjectId;
1415
use ValueError;
1516

1617
use function preg_quote;
1718
use function sprintf;
19+
use function version_compare;
1820

1921
/** @requires PHP >= 8.1 */
2022
class EnumTest extends BaseTestCase
@@ -35,6 +37,25 @@ public function testPersistNew(): void
3537
self::assertNull($saved->nullableSuit);
3638
}
3739

40+
public function testArrayOfEnums(): void
41+
{
42+
$persistenceVersion = PrettyVersions::getVersion('doctrine/persistence')->getPrettyVersion();
43+
if (version_compare('3.2.0', $persistenceVersion, '>')) {
44+
self::markTestSkipped('Support for array of enums was introduced in doctrine/persistence 3.2.0');
45+
}
46+
47+
$doc = new Card();
48+
$doc->suits = ['foo' => Suit::Clubs, 'bar' => Suit::Diamonds];
49+
50+
$this->dm->persist($doc);
51+
$this->dm->flush();
52+
$this->dm->clear();
53+
54+
$saved = $this->dm->find(Card::class, $doc->id);
55+
self::assertInstanceOf(Card::class, $saved);
56+
self::assertSame(['foo' => Suit::Clubs, 'bar' => Suit::Diamonds], $saved->suits);
57+
}
58+
3859
public function testLoadingInvalidBackingValueThrowsError(): void
3960
{
4061
$document = [

tests/Documents81/Card.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,13 @@ class Card
2626
#[ODM\Field(type: 'string', enumType: Suit::class, nullable: true)]
2727
public ?Suit $nullableSuit;
2828

29+
/**
30+
* @ODM\Field(enumType=Suit::class)
31+
*
32+
* @var Suit[]
33+
*/
34+
#[ODM\Field(enumType: Suit::class)]
35+
public array $suits;
36+
2937
public ?SuitNonBacked $suitNonBacked;
3038
}

0 commit comments

Comments
 (0)