Skip to content

Commit b71eb1d

Browse files
Merge branch '4.4' into 5.3
* 4.4: [HttpClient] fix resetting DNS/etc when calling CurlHttpClient::reset() Fix invalid guess with enumType
2 parents 627a04e + 899a603 commit b71eb1d

File tree

8 files changed

+160
-3
lines changed

8 files changed

+160
-3
lines changed

PropertyInfo/DoctrineExtractor.php

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

136136
if ($metadata->hasField($property)) {
137+
$nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property);
138+
if (null !== $enumClass = $metadata->getFieldMapping($property)['enumType'] ?? null) {
139+
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $enumClass)];
140+
}
141+
137142
$typeOfField = $metadata->getTypeOfField($property);
138143

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

143-
$nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property);
144-
145148
switch ($builtinType) {
146149
case Type::BUILTIN_TYPE_OBJECT:
147150
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 <[email protected]>
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+
}

Tests/PropertyInfo/DoctrineExtractorTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@
1414
use Doctrine\Common\Collections\Collection;
1515
use Doctrine\DBAL\Types\Type as DBALType;
1616
use Doctrine\ORM\EntityManager;
17+
use Doctrine\ORM\Mapping\Column;
1718
use Doctrine\ORM\Tools\Setup;
1819
use PHPUnit\Framework\TestCase;
1920
use Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor;
2021
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy;
22+
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineEnum;
2123
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineGeneratedValue;
2224
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation;
25+
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumInt;
26+
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumString;
2327
use Symfony\Component\PropertyInfo\Type;
2428

2529
/**
@@ -124,6 +128,18 @@ public function testExtractWithEmbedded()
124128
$this->assertEquals($expectedTypes, $actualTypes);
125129
}
126130

131+
/**
132+
* @requires PHP 8.1
133+
*/
134+
public function testExtractEnum()
135+
{
136+
if (!property_exists(Column::class, 'enumType')) {
137+
$this->markTestSkipped('The "enumType" requires doctrine/orm 2.11.');
138+
}
139+
$this->assertEquals([new Type(Type::BUILTIN_TYPE_OBJECT, false, EnumString::class)], $this->createExtractor()->getTypes(DoctrineEnum::class, 'enumString', []));
140+
$this->assertEquals([new Type(Type::BUILTIN_TYPE_OBJECT, false, EnumInt::class)], $this->createExtractor()->getTypes(DoctrineEnum::class, 'enumInt', []));
141+
}
142+
127143
public function typesProvider()
128144
{
129145
$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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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+
}

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\Tests\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()

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)