8000 Fix invalid guess with enumType · symfony/symfony@68dd218 · GitHub
[go: up one dir, main page]

Skip to content

Commit 68dd218

Browse files
committed
Fix invalid guess with enumType
1 parent 9f5238d commit 68dd218

File tree

8 files changed

+160
-3
lines changed

8 files changed

+160
-3
lines changed

src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,17 @@ public function getTypes($class, $property, array $context = [])
152152
}
153153

154154
if ($metadata->hasField($property)) {
155+
$nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property);
156+
if (null !== $enumClass = $metadata->getFieldMapping($property)['enumType'] ?? null) {
157+
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $enumClass)];
158+
}
159+
155160
$typeOfField = $metadata->getTypeOfField($property);
156161

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

161-
$nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property);
162-
163166
switch ($builtinType) {
164167
case Type::BUILTIN_TYPE_OBJECT:
165168
switch ($typeOfField) {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\Fixtures;
13+
14+
use Doctrine\ORM\Mapping as ORM;
15+
16+
/**
17+
* @ORM\Entity
18+
*/
19+
class DoctrineLoaderEnum
20+
{
21+
/**
22+
* @ORM\Id
23+
* @ORM\Column
24+
*/
25+
public $id;
26+
27+
/**
28+
* @ORM\Column(type="string", enumType="Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumString", length=1)
29+
*/
30+
public $enumString;
31+
32+
/**
33+
* @ORM\Column(type="integer", enumType="Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumInt")
34+
*/
35+
public $enumInt;
36+
}

src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/DoctrineExtractorTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@
1515
use Doctrine\DBAL\Types\Type as DBALType;
1616
use Doctrine\DBAL\Types\Types;
1717
use Doctrine\ORM\EntityManager;
18+
use Doctrine\ORM\Mapping\Column;
1819
use Doctrine\ORM\Tools\Setup;
1920
use PHPUnit\Framework\TestCase;
2021
use Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor;
2122
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy210;
23+
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineEnum;
2224
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineGeneratedValue;
2325
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation;
26+
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumInt;
27+
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumString;
2428
use Symfony\Component\PropertyInfo\Type;
2529

2630
/**
@@ -171,6 +175,18 @@ private function doTestExtractWithEmbedded(bool $legacy)
171175
$this->assertEquals($expectedTypes, $actualTypes);
172176
}
173177

178+
/**
179+
* @requires PHP 8.1
180+
*/
181+
public function testExtractEnum()
182+
{
183+
if (!property_exists(Column::class, 'enumType')) {
184+
$this->markTestSkipped('The "enumType" requires doctrine/orm 2.11.');
185+
}
186+
$this->assertEquals([new Type(Type::BUILTIN_TYPE_OBJECT, false, EnumString::class)], $this->createExtractor()->getTypes(DoctrineEnum::class, 'enumString', []));
187+
$this->assertEquals([new Type(Type::BUILTIN_TYPE_OBJECT, false, EnumInt::class)], $this->createExtractor()->getTypes(DoctrineEnum::class, 'enumInt', []));
188+
}
189+
174190
public function typesProvider()
175191
{
176192
$provider = [
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures;
13+
14+
use Doctrine\ORM\Mapping\Id;
15+
use Doctrine\ORM\Mapping\Column;
16+
use Doctrine\ORM\Mapping\Entity;
17+
18+
/**
19+
* @Entity
20+
*/
21+
class DoctrineEnum
22+
{
23+
/**
24+
* @Id
25+
* @Column(type="smallint")
26+
*/
27+
public $id;
28+
29+
/**
30+
* @Column(type="string", enumType="Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumString")
31+
*/
32+
protected $enumString;
33+
34+
/**
35+
* @Column(type="integer", enumType="Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumInt")
36+
*/
37+
protected $enumInt;
38+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures;
13+
14+
enum EnumInt: int
15+
{
16+
case Foo = 0;
17+
case Bar = 1;
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures;
13+
14+
enum EnumString: string
15+
{
16+
case Foo = 'f';
17+
case Bar = 'b';
18+
}

src/Symfony/Bridge/Doctrine/Tests/Validator/DoctrineLoaderTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111

1212
namespace Symfony\Bridge\Doctrine\Tests\Validator;
1313

14+
use Doctrine\ORM\Mapping\Column;
1415
use PHPUnit\Framework\TestCase;
1516
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
1617
use Symfony\Bridge\Doctrine\Tests\Fixtures\BaseUser;
1718
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderEmbed;
1819
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderEntity;
20+
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderEnum;
1921
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderNestedEmbed;
2022
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderNoAutoMappingEntity;
2123
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderParentEntity;
@@ -149,6 +151,31 @@ public function testLoadClassMetadata()
149151
$this->assertSame(AutoMappingStrategy::DISABLED, $noAutoMappingMetadata[0]->getAutoMappingStrategy());
150152
}
151153

154+
/**
155+
* @requires PHP 8.1
156+
*/
157+
public function testExtractEnum()
158+
{
159+
if (!property_exists(Column::class, 'enumType')) {
160+
$this->markTestSkipped('The "enumType" requires doctrine/orm 2.11.');
161+
}
162+
163+
$validator = Validation::createValidatorBuilder()
164+
->addMethodMapping('loadValidatorMetadata')
165+
->enableAnnotationMapping()
166+
->addLoader(new DoctrineLoader(DoctrineTestHelper::createTestEntityManager(), '{^Symfony\\\\Bridge\\\\Doctrine\\\\Tests\\\\Fixtures\\\\DoctrineLoader}'))
167+
->getValidator()
168+
;
169+
170+
$classMetadata = $validator->getMetadataFor(new DoctrineLoaderEnum());
171+
172+
$enumStringMetadata = $classMetadata->getPropertyMetadata('enumString');
173+
$this->assertCount(0, $enumStringMetadata); // asserts the length constraint is not added to an enum
174+
175+
$enumStringMetadata = $classMetadata->getPropertyMetadata('enumInt');
176+
$this->assertCount(0, $enumStringMetadata); // asserts the length constraint is not added to an enum
177+
}
178+
152179
public function testFieldMappingsConfiguration()
153180
{
154181
$validator = Validation::createValidatorBuilder()

src/Symfony/Bridge/Doctrine/Validator/DoctrineLoader.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Doctrine\ORM\Mapping\MappingException as OrmMappingException;
1717
use Doctrine\Persistence\Mapping\MappingException;
1818
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
19+
use Symfony\Component\PropertyInfo\Type;
1920
use Symfony\Component\Validator\Constraints\Length;
2021
use Symfony\Component\Validator\Constraints\Valid;
2122
use Symfony\Component\Validator\Mapping\AutoMappingStrategy;
@@ -99,7 +100,7 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
99100
$loaded = true;
100101
}
101102

102-
if (null === ($mapping['length'] ?? null) || !\in_array($mapping['type'], ['string', 'text'], true)) {
103+
if (null === ($mapping['length'] ?? null) || null !== ($mapping['enumType'] ?? null) || !\in_array($mapping['type'], ['string', 'text'], true)) {
103104
continue;
104105
}
105106

0 commit comments

Comments
 (0)
0