Skip to content

Commit 753b3b8

Browse files
committed
minor symfony#59098 [DoctrineBridge] add test covering associated entities referenced by their primary key (xabbuh)
This PR was merged into the 7.1 branch. Discussion ---------- [DoctrineBridge] add test covering associated entities referenced by their primary key | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | | License | MIT Commits ------- 26504c9 add test covering associated entities referenced by their primary key
2 parents d5919dd + 26504c9 commit 753b3b8

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
class AssociatedEntityDto
15+
{
16+
public $singleId;
17+
}

src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Doctrine\Persistence\ObjectManager;
2222
use PHPUnit\Framework\MockObject\MockObject;
2323
use Symfony\Bridge\Doctrine\Tests\DoctrineTestHelper;
24+
use Symfony\Bridge\Doctrine\Tests\Fixtures\AssociatedEntityDto;
2425
use Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity;
2526
use Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity2;
2627
use Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity;
@@ -609,6 +610,40 @@ public function testAssociatedEntityWithNull()
609610
$this->assertNoViolation();
610611
}
611612

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+
612647
public function testValidateUniquenessWithArrayValue()
613648
{
614649
$repository = $this->createRepositoryMock(SingleIntIdEntity::class);

0 commit comments

Comments
 (0)