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
This commit is contained in:
commit
4ac9f72884
|
@ -0,0 +1,45 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Entity
|
||||||
|
*/
|
||||||
|
class AssociationEntity2
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var int
|
||||||
|
* @ORM\Id @ORM\GeneratedValue
|
||||||
|
* @ORM\Column(type="integer")
|
||||||
|
*/
|
||||||
|
private $id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\ManyToOne(targetEntity="SingleIntIdNoToStringEntity")
|
||||||
|
*
|
||||||
|
* @var \Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdNoToStringEntity
|
||||||
|
*/
|
||||||
|
public $single;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\ManyToOne(targetEntity="CompositeIntIdEntity")
|
||||||
|
* @ORM\JoinColumns({
|
||||||
|
* @ORM\JoinColumn(name="composite_id1", referencedColumnName="id1"),
|
||||||
|
* @ORM\JoinColumn(name="composite_id2", referencedColumnName="id2")
|
||||||
|
* })
|
||||||
|
*
|
||||||
|
* @var \Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity
|
||||||
|
*/
|
||||||
|
public $composite;
|
||||||
|
}
|
|
@ -19,6 +19,8 @@ use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
|
||||||
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity;
|
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity;
|
||||||
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity;
|
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity;
|
||||||
use Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity;
|
use Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity;
|
||||||
|
use Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity2;
|
||||||
|
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdNoToStringEntity;
|
||||||
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
||||||
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator;
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator;
|
||||||
use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest;
|
use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest;
|
||||||
|
@ -125,9 +127,11 @@ class UniqueEntityValidatorTest extends AbstractConstraintValidatorTest
|
||||||
$schemaTool = new SchemaTool($em);
|
$schemaTool = new SchemaTool($em);
|
||||||
$schemaTool->createSchema(array(
|
$schemaTool->createSchema(array(
|
||||||
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity'),
|
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity'),
|
||||||
|
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdNoToStringEntity'),
|
||||||
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity'),
|
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity'),
|
||||||
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity'),
|
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity'),
|
||||||
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity'),
|
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity'),
|
||||||
|
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity2'),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -406,6 +410,42 @@ class UniqueEntityValidatorTest extends AbstractConstraintValidatorTest
|
||||||
->assertRaised();
|
->assertRaised();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testValidateUniquenessNotToStringEntityWithAssociatedEntity()
|
||||||
|
{
|
||||||
|
$constraint = new UniqueEntity(array(
|
||||||
|
'message' => 'myMessage',
|
||||||
|
'fields' => array('single'),
|
||||||
|
'em' => self::EM_NAME,
|
||||||
|
));
|
||||||
|
|
||||||
|
$entity1 = new SingleIntIdNoToStringEntity(1, 'foo');
|
||||||
|
$associated = new AssociationEntity2();
|
||||||
|
$associated->single = $entity1;
|
||||||
|
$associated2 = new AssociationEntity2();
|
||||||
|
$associated2->single = $entity1;
|
||||||
|
|
||||||
|
$this->em->persist($entity1);
|
||||||
|
$this->em->persist($associated);
|
||||||
|
$this->em->flush();
|
||||||
|
|
||||||
|
$this->validator->validate($associated, $constraint);
|
||||||
|
|
||||||
|
$this->assertNoViolation();
|
||||||
|
|
||||||
|
$this->em->persist($associated2);
|
||||||
|
$this->em->flush();
|
||||||
|
|
||||||
|
$this->validator->validate($associated2, $constraint);
|
||||||
|
|
||||||
|
$expectedValue = 'Object of class "Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity2" identified by "2"';
|
||||||
|
|
||||||
|
$this->buildViolation('myMessage')
|
||||||
|
->atPath('property.path.single')
|
||||||
|
->setParameter('{{ value }}', $expectedValue)
|
||||||
|
->setInvalidValue($expectedValue)
|
||||||
|
->assertRaised();
|
||||||
|
}
|
||||||
|
|
||||||
public function testAssociatedEntityWithNull()
|
public function testAssociatedEntityWithNull()
|
||||||
{
|
{
|
||||||
$constraint = new UniqueEntity(array(
|
$constraint = new UniqueEntity(array(
|
||||||
|
|
|
@ -127,6 +127,10 @@ class UniqueEntityValidator extends ConstraintValidator
|
||||||
$errorPath = null !== $constraint->errorPath ? $constraint->errorPath : $fields[0];
|
$errorPath = null !== $constraint->errorPath ? $constraint->errorPath : $fields[0];
|
||||||
$invalidValue = isset($criteria[$errorPath]) ? $criteria[$errorPath] : $criteria[$fields[0]];
|
$invalidValue = isset($criteria[$errorPath]) ? $criteria[$errorPath] : $criteria[$fields[0]];
|
||||||
|
|
||||||
|
if (is_object($invalidValue) && !method_exists($invalidValue, '__toString')) {
|
||||||
|
$invalidValue = sprintf('Object of class "%s" identified by "%s"', get_class($entity), implode(', ', $class->getIdentifierValues($entity)));
|
||||||
|
}
|
||||||
|
|
||||||
$this->context->buildViolation($constraint->message)
|
$this->context->buildViolation($constraint->message)
|
||||||
->atPath($errorPath)
|
->atPath($errorPath)
|
||||||
->setParameter('{{ value }}', $invalidValue)
|
->setParameter('{{ value }}', $invalidValue)
|
||||||
|
|
Reference in New Issue