Skip to content

Commit

Permalink
bug #19227 [DoctrineBridge] fixed default parameter value in UniqueEn…
Browse files Browse the repository at this point in the history
…tityValidator (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
  • Loading branch information
fabpot committed Jun 30, 2016
2 parents fbeb299 + 40c0c52 commit 4ac9f72
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/Symfony/Bridge/Doctrine/Tests/Fixtures/AssociationEntity2.php
@@ -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;
}
Expand Up @@ -19,6 +19,8 @@
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity;
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity;
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\UniqueEntityValidator;
use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest;
Expand Down Expand Up @@ -125,9 +127,11 @@ private function createSchema(ObjectManager $em)
$schemaTool = new SchemaTool($em);
$schemaTool->createSchema(array(
$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\CompositeIntIdEntity'),
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity'),
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity2'),
));
}

Expand Down Expand Up @@ -406,6 +410,42 @@ public function testAssociatedEntity()
->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()
{
$constraint = new UniqueEntity(array(
Expand Down
Expand Up @@ -127,6 +127,10 @@ public function validate($entity, Constraint $constraint)
$errorPath = null !== $constraint->errorPath ? $constraint->errorPath : $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)
->atPath($errorPath)
->setParameter('{{ value }}', $invalidValue)
Expand Down

0 comments on commit 4ac9f72

Please sign in to comment.