feature #23845 [Validator] Add unique entity violation cause (Ilya Vertakov)

This PR was squashed before being merged into the 3.4 branch (closes #23845).

Discussion
----------

[Validator] Add unique entity violation cause

| Q             | A
| ------------- | ---
| Branch?       | 3.4 or master
| Bug fix?      | no
| New feature?  | yes/no <!-- don't forget updating src/**/CHANGELOG.md files -->
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | n/a

This PR adds unique entity validation error cause which can be array of entities to constraint violation

Commits
-------

7ae06dc617 [Validator] Add unique entity violation cause
This commit is contained in:
Fabien Potencier 2017-10-01 11:06:53 -07:00
commit 09dbed766d
3 changed files with 48 additions and 0 deletions

View File

@ -5,6 +5,7 @@ CHANGELOG
-----
* added support for doctrine/dbal v2.6 types
* added cause of UniqueEntity constraint violation
3.1.0
-----

View File

@ -190,6 +190,7 @@ class UniqueEntityValidatorTest extends ConstraintValidatorTestCase
->atPath('property.path.name')
->setParameter('{{ value }}', '"Foo"')
->setInvalidValue($entity2)
->setCause(array($entity1))
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
->assertRaised();
}
@ -215,6 +216,7 @@ class UniqueEntityValidatorTest extends ConstraintValidatorTestCase
->atPath('property.path.bar')
->setParameter('{{ value }}', '"Foo"')
->setInvalidValue($entity2)
->setCause(array($entity1))
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
->assertRaised();
}
@ -268,6 +270,7 @@ class UniqueEntityValidatorTest extends ConstraintValidatorTestCase
->atPath('property.path.name')
->setParameter('{{ value }}', '"Foo"')
->setInvalidValue('Foo')
->setCause(array($entity1))
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
->assertRaised();
}
@ -346,6 +349,7 @@ class UniqueEntityValidatorTest extends ConstraintValidatorTestCase
->atPath('property.path.name2')
->setParameter('{{ value }}', '"Bar"')
->setInvalidValue('Bar')
->setCause(array($entity1))
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
->assertRaised();
}
@ -481,6 +485,7 @@ class UniqueEntityValidatorTest extends ConstraintValidatorTestCase
->setParameter('{{ value }}', 'object("Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity") identified by (id => 1)')
->setInvalidValue($entity1)
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
->setCause(array($associated, $associated2))
->assertRaised();
}
@ -517,6 +522,7 @@ class UniqueEntityValidatorTest extends ConstraintValidatorTestCase
->atPath('property.path.single')
->setParameter('{{ value }}', $expectedValue)
->setInvalidValue($entity1)
->setCause(array($associated, $associated2))
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
->assertRaised();
}
@ -575,6 +581,7 @@ class UniqueEntityValidatorTest extends ConstraintValidatorTestCase
->atPath('property.path.phoneNumbers')
->setParameter('{{ value }}', 'array')
->setInvalidValue(array(123))
->setCause(array($entity1))
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
->assertRaised();
}
@ -652,6 +659,7 @@ class UniqueEntityValidatorTest extends ConstraintValidatorTestCase
->atPath('property.path.name')
->setInvalidValue('Foo')
->setCode('23bd9dbf-6b9b-41cd-a99e-4844bcf3077f')
->setCause(array($entity1))
->setParameters(array('{{ value }}' => '"Foo"'))
->assertRaised();
}
@ -703,6 +711,7 @@ class UniqueEntityValidatorTest extends ConstraintValidatorTestCase
->atPath('property.path.objectOne')
->setParameter('{{ value }}', $expectedValue)
->setInvalidValue($objectOne)
->setCause(array($entity))
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
->assertRaised();
}
@ -730,6 +739,43 @@ class UniqueEntityValidatorTest extends ConstraintValidatorTestCase
->atPath('property.path.name')
->setParameter('{{ value }}', $expectedValue)
->setInvalidValue($existingEntity->name)
->setCause(array($existingEntity))
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
->assertRaised();
}
/**
* This is a functional test as there is a large integration necessary to get the validator working.
*/
public function testValidateUniquenessCause()
{
$constraint = new UniqueEntity(array(
'message' => 'myMessage',
'fields' => array('name'),
'em' => self::EM_NAME,
));
$entity1 = new SingleIntIdEntity(1, 'Foo');
$entity2 = new SingleIntIdEntity(2, 'Foo');
$this->validator->validate($entity1, $constraint);
$this->assertNoViolation();
$this->em->persist($entity1);
$this->em->flush();
$this->validator->validate($entity1, $constraint);
$this->assertNoViolation();
$this->validator->validate($entity2, $constraint);
$this->buildViolation('myMessage')
->atPath('property.path.name')
->setParameter('{{ value }}', '"Foo"')
->setInvalidValue($entity2)
->setCause(array($entity1))
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
->assertRaised();
}

View File

@ -171,6 +171,7 @@ class UniqueEntityValidator extends ConstraintValidator
->setParameter('{{ value }}', $this->formatWithIdentifiers($em, $class, $invalidValue))
->setInvalidValue($invalidValue)
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
->setCause($result)
->addViolation();
}