Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[DoctrineBridge] fixed default parameter value in UniqueEntityValidator #19227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions 45 src/Symfony/Bridge/Doctrine/Tests/Fixtures/AssociationEntity2.php
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use this in the default message by including the placeholder?

}

$this->context->buildViolation($constraint->message)
->atPath($errorPath)
->setParameter('{{ value }}', $invalidValue)
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.