Skip to content

Commit 80e9156

Browse files
committed
Merge branch '3.1'
* 3.1: Fixed BC Layer in DoctrineChoiceLoader [HttpKernel] Add listener that checks when request has both Forwarded and X-Forwarded-For [HttpKernel] Move conflicting origin IPs handling to catch block [travis] Fix deps=low/high patching Fixed some issues of the AccessDecisionManager profiler [DoctrineBridge] fixed default parameter value in UniqueEntityValidator
2 parents 3fd00e7 + 7305a3c commit 80e9156

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 <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 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;
@@ -127,9 +129,11 @@ private function createSchema(ObjectManager $em)
127129
$schemaTool = new SchemaTool($em);
128130
$schemaTool->createSchema(array(
129131
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity'),
132+
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdNoToStringEntity'),
130133
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity'),
131134
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity'),
132135
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity'),
136+
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity2'),
133137
));
134138
}
135139

@@ -408,6 +412,42 @@ public function testAssociatedEntity()
408412
->assertRaised();
409413
}
410414

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