Skip to content

Commit

Permalink
Merge pull request #2532 from malarzm/array-of-enums
Browse files Browse the repository at this point in the history
Test persisting array of enums
  • Loading branch information
malarzm authored Oct 23, 2023
2 parents bdbb370 + f315ef5 commit 771c4b3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
21 changes: 21 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
use Documents81\Card;
use Documents81\Suit;
use Error;
use Jean85\PrettyVersions;
use MongoDB\BSON\ObjectId;
use ValueError;

use function preg_quote;
use function sprintf;
use function version_compare;

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

public function testArrayOfEnums(): void
{
$persistenceVersion = PrettyVersions::getVersion('doctrine/persistence')->getPrettyVersion();
if (version_compare('3.2.0', $persistenceVersion, '>')) {
self::markTestSkipped('Support for array of enums was introduced in doctrine/persistence 3.2.0');
}

$doc = new Card();
$doc->suits = ['foo' => Suit::Clubs, 'bar' => Suit::Diamonds];

$this->dm->persist($doc);
$this->dm->flush();
$this->dm->clear();

$saved = $this->dm->find(Card::class, $doc->id);
self::assertInstanceOf(Card::class, $saved);
self::assertSame(['foo' => Suit::Clubs, 'bar' => Suit::Diamonds], $saved->suits);
}

public function testLoadingInvalidBackingValueThrowsError(): void
{
$document = [
Expand Down
8 changes: 8 additions & 0 deletions tests/Documents81/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,13 @@ class Card
#[ODM\Field(type: 'string', enumType: Suit::class, nullable: true)]
public ?Suit $nullableSuit;

/**
* @ODM\Field(enumType=Suit::class)
*
* @var Suit[]
*/
#[ODM\Field(enumType: Suit::class)]
public array $suits;

public ?SuitNonBacked $suitNonBacked;
}

0 comments on commit 771c4b3

Please sign in to comment.