Skip to content

Commit

Permalink
add test covering associated entities referenced by their primary key
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Dec 5, 2024
1 parent 893cc4f commit daccae6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Tests/Fixtures/AssociatedEntityDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Tests\Fixtures;

class AssociatedEntityDto
{
public $singleId;
}
35 changes: 35 additions & 0 deletions Tests/Validator/Constraints/UniqueEntityValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Doctrine\Persistence\ObjectManager;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Bridge\Doctrine\Tests\DoctrineTestHelper;
use Symfony\Bridge\Doctrine\Tests\Fixtures\AssociatedEntityDto;
use Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity;
use Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity2;
use Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity;
Expand Down Expand Up @@ -609,6 +610,40 @@ public function testAssociatedEntityWithNull()
$this->assertNoViolation();
}

public function testAssociatedEntityReferencedByPrimaryKey()
{
$this->registry = $this->createRegistryMock($this->em);
$this->registry->expects($this->any())
->method('getManagerForClass')
->willReturn($this->em);
$this->validator = $this->createValidator();
$this->validator->initialize($this->context);

$entity = new SingleIntIdEntity(1, 'foo');
$associated = new AssociationEntity();
$associated->single = $entity;

$this->em->persist($entity);
$this->em->persist($associated);
$this->em->flush();

$dto = new AssociatedEntityDto();
$dto->singleId = 1;

$this->validator->validate($dto, new UniqueEntity(
fields: ['singleId' => 'single'],
entityClass: AssociationEntity::class,
));

$this->buildViolation('This value is already used.')
->atPath('property.path.single')
->setParameter('{{ value }}', 1)
->setInvalidValue(1)
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
->setCause([$associated])
->assertRaised();
}

public function testValidateUniquenessWithArrayValue()
{
$repository = $this->createRepositoryMock(SingleIntIdEntity::class);
Expand Down

0 comments on commit daccae6

Please sign in to comment.