|
21 | 21 | use Doctrine\Persistence\ObjectManager;
|
22 | 22 | use PHPUnit\Framework\MockObject\MockObject;
|
23 | 23 | use Symfony\Bridge\Doctrine\Tests\DoctrineTestHelper;
|
| 24 | +use Symfony\Bridge\Doctrine\Tests\Fixtures\AssociatedEntityDto; |
24 | 25 | use Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity;
|
25 | 26 | use Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity2;
|
26 | 27 | use Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity;
|
@@ -609,6 +610,40 @@ public function testAssociatedEntityWithNull()
|
609 | 610 | $this->assertNoViolation();
|
610 | 611 | }
|
611 | 612 |
|
| 613 | + public function testAssociatedEntityReferencedByPrimaryKey() |
| 614 | + { |
| 615 | + $this->registry = $this->createRegistryMock($this->em); |
| 616 | + $this->registry->expects($this->any()) |
| 617 | + ->method('getManagerForClass') |
| 618 | + ->willReturn($this->em); |
| 619 | + $this->validator = $this->createValidator(); |
| 620 | + $this->validator->initialize($this->context); |
| 621 | + |
| 622 | + $entity = new SingleIntIdEntity(1, 'foo'); |
| 623 | + $associated = new AssociationEntity(); |
| 624 | + $associated->single = $entity; |
| 625 | + |
| 626 | + $this->em->persist($entity); |
| 627 | + $this->em->persist($associated); |
| 628 | + $this->em->flush(); |
| 629 | + |
| 630 | + $dto = new AssociatedEntityDto(); |
| 631 | + $dto->singleId = 1; |
| 632 | + |
| 633 | + $this->validator->validate($dto, new UniqueEntity( |
| 634 | + fields: ['singleId' => 'single'], |
| 635 | + entityClass: AssociationEntity::class, |
| 636 | + )); |
| 637 | + |
| 638 | + $this->buildViolation('This value is already used.') |
| 639 | + ->atPath('property.path.single') |
| 640 | + ->setParameter('{{ value }}', 1) |
| 641 | + ->setInvalidValue(1) |
| 642 | + ->setCode(UniqueEntity::NOT_UNIQUE_ERROR) |
| 643 | + ->setCause([$associated]) |
| 644 | + ->assertRaised(); |
| 645 | + } |
| 646 | + |
612 | 647 | public function testValidateUniquenessWithArrayValue()
|
613 | 648 | {
|
614 | 649 | $repository = $this->createRepositoryMock(SingleIntIdEntity::class);
|
|
0 commit comments