Skip to content

Commit 6e47a57

Browse files
GiuseppeArcutinicolas-grekas
authored andcommitted
[DoctrineBridge] Fix UniqueEntity for non-integer identifiers
1 parent d030ea0 commit 6e47a57

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

Tests/Fixtures/UserUuidNameDto.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 Symfony\Component\Uid\Uuid;
15+
16+
class UserUuidNameDto
17+
{
18+
public function __construct(
19+
public ?Uuid $id,
20+
public ?string $fullName,
21+
public ?string $address,
22+
) {
23+
}
24+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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\Column;
15+
use Doctrine\ORM\Mapping\Entity;
16+
use Doctrine\ORM\Mapping\Id;
17+
use Symfony\Component\Uid\Uuid;
18+
19+
#[Entity]
20+
class UserUuidNameEntity
21+
{
22+
public function __construct(
23+
#[Id, Column]
24+
public ?Uuid $id = null,
25+
#[Column(unique: true)]
26+
public ?string $fullName = null,
27+
) {
28+
}
29+
}

Tests/Validator/Constraints/UniqueEntityValidatorTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@
4141
use Symfony\Bridge\Doctrine\Tests\Fixtures\UpdateCompositeIntIdEntity;
4242
use Symfony\Bridge\Doctrine\Tests\Fixtures\UpdateCompositeObjectNoToStringIdEntity;
4343
use Symfony\Bridge\Doctrine\Tests\Fixtures\UpdateEmployeeProfile;
44+
use Symfony\Bridge\Doctrine\Tests\Fixtures\UserUuidNameDto;
45+
use Symfony\Bridge\Doctrine\Tests\Fixtures\UserUuidNameEntity;
4446
use Symfony\Bridge\Doctrine\Tests\TestRepositoryFactory;
4547
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
4648
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator;
49+
use Symfony\Component\Uid\Uuid;
4750
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
4851
use Symfony\Component\Validator\Exception\UnexpectedValueException;
4952
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
@@ -116,6 +119,7 @@ private function createSchema($em)
116119
$em->getClassMetadata(Employee::class),
117120
$em->getClassMetadata(CompositeObjectNoToStringIdEntity::class),
118121
$em->getClassMetadata(SingleIntIdStringWrapperNameEntity::class),
122+
$em->getClassMetadata(UserUuidNameEntity::class),
119123
]);
120124
}
121125

@@ -1401,4 +1405,25 @@ public function testEntityManagerNullObjectWhenDTODoctrineStyle()
14011405

14021406
$this->validator->validate($dto, $constraint);
14031407
}
1408+
1409+
public function testUuidIdentifierWithSameValueDifferentInstanceDoesNotCauseViolation()
1410+
{
1411+
$uuidString = 'ec562e21-1fc8-4e55-8de7-a42389ac75c5';
1412+
$existingPerson = new UserUuidNameEntity(Uuid::fromString($uuidString), 'Foo Bar');
1413+
$this->em->persist($existingPerson);
1414+
$this->em->flush();
1415+
1416+
$dto = new UserUuidNameDto(Uuid::fromString($uuidString), 'Foo Bar', '');
1417+
1418+
$constraint = new UniqueEntity(
1419+
fields: ['fullName'],
1420+
entityClass: UserUuidNameEntity::class,
1421+
identifierFieldNames: ['id'],
1422+
em: self::EM_NAME,
1423+
);
1424+
1425+
$this->validator->validate($dto, $constraint);
1426+
1427+
$this->assertNoViolation();
1428+
}
14041429
}

0 commit comments

Comments
 (0)