8000 [DoctrineBridge] Fix invalid guess with enumType by jderusse · Pull Request #45012 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DoctrineBridge] Fix invalid guess with enumType #45012

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

Merged
merged 1 commit into from
Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,17 @@ public function getTypes($class, $property, array $context = [])
}

if ($metadata->hasField($property)) {
$nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property);
if (null !== $enumClass = $metadata->getFieldMapping($property)['enumType'] ?? null) {
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $enumClass)];
}

$typeOfField = $metadata->getTypeOfField($property);

if (!$builtinType = $this->getPhpType($typeOfField)) {
return null;
}

$nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property);

switch ($builtinType) {
case Type::BUILTIN_TYPE_OBJECT:
switch ($typeOfField) {
Expand Down
36 changes: 36 additions & 0 deletions src/Symfony/Bridge/Doctrine/Tests/Fixtures/DoctrineLoaderEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Tests\Fixtures;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
*/
class DoctrineLoaderEnum
{
/**
* @ORM\Id
* @ORM\Column
*/
public $id;

/**
* @ORM\Column(type="string", enumType="Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumString", length=1)
*/
public $enumString;

/**
* @ORM\Column(type="integer", enumType="Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumInt")
*/
public $enumInt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
use Doctrine\DBAL\Types\Type as DBALType;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Tools\Setup;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor;
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy210;
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineEnum;
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineGeneratedValue;
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation;
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumInt;
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumString;
use Symfony\Component\PropertyInfo\Type;

/**
Expand Down Expand Up @@ -171,6 +175,18 @@ private function doTestExtractWithEmbedded(bool $legacy)
$this->assertEquals($expectedTypes, $actualTypes);
}

/**
* @requires PHP 8.1
*/
public function testExtractEnum()
{
if (!property_exists(Column::class, 'enumType')) {
$this->markTestSkipped('The "enumType" requires doctrine/orm 2.11.');
}
$this->assertEquals([new Type(Type::BUILTIN_TYPE_OBJECT, false, EnumString::class)], $this->createExtractor()->getTypes(DoctrineEnum::class, 'enumString', []));
$this->assertEquals([new Type(Type::BUILTIN_TYPE_OBJECT, false, EnumInt::class)], $this->createExtractor()->getTypes(DoctrineEnum::class, 'enumInt', []));
}

public function typesProvider()
{
$provider = [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures;

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

/**
* @Entity
*/
class DoctrineEnum
{
/**
* @Id
* @Column(type="smallint")
*/
public $id;

/**
* @Column(type="string", enumType="Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumString")
*/
protected $enumString;

/**
* @Column(type="integer", enumType="Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumInt")
*/
protected $enumInt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures;

enum EnumInt: int
{
case Foo = 0;
case Bar = 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures;

enum EnumString: string
{
case Foo = 'f';
case Bar = 'b';
}
27 changes: 27 additions & 0 deletions src/Symfony/Bridge/Doctrine/Tests/Validator/DoctrineLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@

namespace Symfony\Bridge\Doctrine\Tests\Validator;

use Doctrine\ORM\Mapping\Column;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
use Symfony\Bridge\Doctrine\Tests\Fixtures\BaseUser;
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderEmbed;
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderEntity;
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderEnum;
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderNestedEmbed;
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderNoAutoMappingEntity;
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderParentEntity;
Expand Down Expand Up @@ -149,6 +151,31 @@ public function testLoadClassMetadata()
$this->assertSame(AutoMappingStrategy::DISABLED, $noAutoMappingMetadata[0]->getAutoMappingStrategy());
}

/**
* @requires PHP 8.1
*/
public function testExtractEnum()
{
if (!property_exists(Column::class, 'enumType')) {
$this->markTestSkipped('The "enumType" requires doctrine/orm 2.11.');
}

$validator = Validation::createValidatorBuilder()
->addMethodMapping('loadValidatorMetadata')
->enableAnnotationMapping()
->addLoader(new DoctrineLoader(DoctrineTestHelper::createTestEntityManager(), '{^Symfony\\\\Bridge\\\\Doctrine\\\\Tests\\\\Fixtures\\\\DoctrineLoader}'))
->getValidator()
;

$classMetadata = $validator->getMetadataFor(new DoctrineLoaderEnum());

$enumStringMetadata = $classMetadata->getPropertyMetadata('enumString');
$this->assertCount(0, $enumStringMetadata); // asserts the length constraint is not added to an enum

$enumStringMetadata = $classMetadata->getPropertyMetadata('enumInt');
$this->assertCount(0, $enumStringMetadata); // asserts the length constraint is not added to an enum
}

public function testFieldMappingsConfiguration()
{
$validator = Validation::createValidatorBuilder()
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Bridge/Doctrine/Validator/DoctrineLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\ORM\Mapping\MappingException as OrmMappingException;
use Doctrine\Persistence\Mapping\MappingException;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\Mapping\AutoMappingStrategy;
Expand Down Expand Up @@ -99,7 +100,7 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
$loaded = true;
}

if (null === ($mapping['length'] ?? null) || !\in_array($mapping['type'], ['string', 'text'], true)) {
if (null === ($mapping['length'] ?? null) || null !== ($mapping['enumType'] ?? null) || !\in_array($mapping['type'], ['string', 'text'], true)) {
continue;
}

Expand Down
0