Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for array of enums #9497

Merged
merged 16 commits into from
Apr 4, 2022
Prev Previous commit
Next Next commit
Add enum array mapping test
  • Loading branch information
Sander De la Marche committed Feb 9, 2022
commit 7235413f9395121d03820655976781061e95c2b1
48 changes: 48 additions & 0 deletions tests/Doctrine/Tests/Models/Enums/Scale.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\Enums;

use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;

/** @Entity */
#[Entity]
class Scale
{
/**
* @Id @GeneratedValue @Column(type="integer")
* @var int
*/
#[Id, GeneratedValue, Column(type: 'integer')]
public $id;

/**
* @Column(type="simple_array", enumType=Unit::class)
* @var Unit
*/
#[Column(type: 'simple_array', enumType: Unit::class)]
public $supportedUnits;

public static function loadMetadata(ClassMetadataInfo $metadata): void
{
$metadata->mapField(
[
'id' => true,
'fieldName' => 'id',
'type' => 'integer',
]
);
$metadata->mapField(
[
'fieldName' => 'supportedUnits',
'type' => 'simple_array',
'enumType' => Unit::class,
]
);
}
}
19 changes: 19 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Doctrine\Tests\Models\Enums\Card;
use Doctrine\Tests\Models\Enums\Product;
use Doctrine\Tests\Models\Enums\Quantity;
use Doctrine\Tests\Models\Enums\Scale;
use Doctrine\Tests\Models\Enums\Suit;
use Doctrine\Tests\Models\Enums\TypedCard;
use Doctrine\Tests\Models\Enums\Unit;
Expand Down Expand Up @@ -135,4 +136,22 @@ public function testEnumMappingWithEmbeddable(): void
$this->assertInstanceOf(Unit::class, $fetchedProduct->quantity->unit);
$this->assertEquals(Unit::Gram, $fetchedProduct->quantity->unit);
}

public function testEnumArrayMapping(): void
{
$this->setUpEntitySchema([Scale::class]);

$scale = new Scale();
$scale->supportedUnits = [Unit::Gram, Unit::Meter];

$this->_em->persist($scale);
$this->_em->flush();
$this->_em->clear();

$fetchedScale = $this->_em->find(Scale::class, $scale->id);

$this->assertIsArray($fetchedScale->supportedUnits);
$this->assertContains(Unit::Gram, $fetchedScale->supportedUnits);
$this->assertContains(Unit::Meter, $fetchedScale->supportedUnits);
}
}