You are here

public function Abstract2Dot5ApiTest::testAddCustomizedViolation in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/validator/Tests/Validator/Abstract2Dot5ApiTest.php \Symfony\Component\Validator\Tests\Validator\Abstract2Dot5ApiTest::testAddCustomizedViolation()

File

vendor/symfony/validator/Tests/Validator/Abstract2Dot5ApiTest.php, line 516

Class

Abstract2Dot5ApiTest
Verifies that a validator satisfies the API of Symfony 2.5+.

Namespace

Symfony\Component\Validator\Tests\Validator

Code

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]
    ->getParameters());
  $this
    ->assertSame('', $violations[0]
    ->getPropertyPath());
  $this
    ->assertSame($entity, $violations[0]
    ->getRoot());
  $this
    ->assertSame('Invalid value', $violations[0]
    ->getInvalidValue());
  $this
    ->assertSame(2, $violations[0]
    ->getPlural());
  $this
    ->assertSame(42, $violations[0]
    ->getCode());
}