Skip to content

Commit 7305a3c

Browse files
committed
bug #19227 [DoctrineBridge] fixed default parameter value in UniqueEntityValidator (HeahDude)
This PR was merged into the 3.1 branch. Discussion ---------- [DoctrineBridge] fixed default parameter value in UniqueEntityValidator | Q | A | ------------- | --- | Branch? | 3.1 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #19209 | License | MIT | Doc PR | ~ Commits ------- 40c0c52 [DoctrineBridge] fixed default parameter value in UniqueEntityValidator
2 parents ecac6de + cc6a12b commit 7305a3c

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

Tests/Fixtures/AssociationEntity2.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 AssociationEntity2
20+
{
21+
/**
22+
* @var int
23+
* @ORM\Id @ORM\GeneratedValue
24+
* @ORM\Column(type="integer")
25+
*/
26+
private $id;
27+
28+
/**
29+
* @ORM\ManyToOne(targetEntity="SingleIntIdNoToStringEntity")
30+
*
31+
* @var \Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdNoToStringEntity
32+
*/
33+
public $single;
34+
35+
/**
36+
* @ORM\ManyToOne(targetEntity="CompositeIntIdEntity")
37+
* @ORM\JoinColumns({
38+
* @ORM\JoinColumn(name="composite_id1", referencedColumnName="id1"),
39+
* @ORM\JoinColumn(name="composite_id2", referencedColumnName="id2")
40+
* })
41+
*
42+
* @var \Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity
43+
*/
44+
public $composite;
45+
}

Tests/Validator/Constraints/UniqueEntityValidatorTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity;
2020
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity;
2121
use Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity;
22+
use Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity2;
23+
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdNoToStringEntity;
2224
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
2325
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator;
2426
use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest;
@@ -125,9 +127,11 @@ private function createSchema(ObjectManager $em)
125127
$schemaTool = new SchemaTool($em);
126128
$schemaTool->createSchema(array(
127129
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity'),
130+
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdNoToStringEntity'),
128131
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity'),
129132
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity'),
130133
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity'),
134+
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity2'),
131135
));
132136
}
133137

@@ -406,6 +410,42 @@ public function testAssociatedEntity()
406410
->assertRaised();
407411
}
408412

413+
public function testValidateUniquenessNotToStringEntityWithAssociatedEntity()
414+
{
415+
$constraint = new UniqueEntity(array(
416+
'message' => 'myMessage',
417+
'fields' => array('single'),
418+
'em' => self::EM_NAME,
419+
));
420+
421+
$entity1 = new SingleIntIdNoToStringEntity(1, 'foo');
422+
$associated = new AssociationEntity2();
423+
$associated->single = $entity1;
424+
$associated2 = new AssociationEntity2();
425+
$associated2->single = $entity1;
426+
427+
$this->em->persist($entity1);
428+
$this->em->persist($associated);
429+
$this->em->flush();
430+
431+
$this->validator->validate($associated, $constraint);
432+
433+
$this->assertNoViolation();
434+
435+
$this->em->persist($associated2);
436+
$this->em->flush();
437+
438+
$this->validator->validate($associated2, $constraint);
439+
440+
$expectedValue = 'Object of class "Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity2" identified by "2"';
441+
442+
$this->buildViolation('myMessage')
443+
->atPath('property.path.single')
444+
->setParameter('{{ value }}', $expectedValue)
445+
->setInvalidValue($expectedValue)
446+
->assertRaised();
447+
}
448+
409449
public function testAssociatedEntityWithNull()
410450
{
411451
$constraint = new UniqueEntity(array(

Validator/Constraints/UniqueEntityValidator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ public function validate($entity, Constraint $constraint)
127127
$errorPath = null !== $constraint->errorPath ? $constraint->errorPath : $fields[0];
128128
$invalidValue = isset($criteria[$errorPath]) ? $criteria[$errorPath] : $criteria[$fields[0]];
129129

130+
if (is_object($invalidValue) && !method_exists($invalidValue, '__toString')) {
131+
$invalidValue = sprintf('Object of class "%s" identified by "%s"', get_class($entity), implode(', ', $class->getIdentifierValues($entity)));
132+
}
133+
130134
$this->context->buildViolation($constraint->message)
131135
->atPath($errorPath)
132136
->setParameter('{{ value }}', $invalidValue)

0 commit comments

Comments
 (0)