public function Abstract2Dot5ApiTest::testAddCustomizedViolation in Plug 7
File
- lib/
Symfony/ validator/ Symfony/ Component/ Validator/ Tests/ Validator/ Abstract2Dot5ApiTest.php, line 568
Class
- Abstract2Dot5ApiTest
- Verifies that a validator satisfies the API of Symfony 2.5+.
Namespace
Symfony\Component\Validator\Tests\ValidatorCode
public function testAddCustomizedViolation() {
$entity = new Entity();
$callback = function ($value, ExecutionContextInterface $context) {
$context
->buildViolation('Message %param%')
->setParameter('%param%', 'value')
->setInvalidValue('Invalid value')
->setPlural(2)
->setCode(42)
->addViolation();
};
$this->metadata
->addConstraint(new Callback($callback));
$violations = $this->validator
->validate($entity);
/** @var ConstraintViolationInterface[] $violations */
$this
->assertCount(1, $violations);
$this
->assertSame('Message value', $violations[0]
->getMessage());
$this
->assertSame('Message %param%', $violations[0]
->getMessageTemplate());
$this
->assertSame(array(
'%param%' => 'value',
), $violations[0]
->getMessageParameters());
$this
->assertSame('', $violations[0]
->getPropertyPath());
$this
->assertSame($entity, $violations[0]
->getRoot());
$this
->assertSame('Invalid value', $violations[0]
->getInvalidValue());
$this
->assertSame(2, $violations[0]
->getMessagePluralization());
$this
->assertSame(42, $violations[0]
->getCode());
}