You are here

public function AbstractLegacyApiTest::testAddCustomizedViolation in Plug 7

File

lib/Symfony/validator/Symfony/Component/Validator/Tests/Validator/AbstractLegacyApiTest.php, line 235

Class

AbstractLegacyApiTest
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
      ->addViolation('Message %param%', array(
      '%param%' => 'value',
    ), 'Invalid value', 2, 'Code');
  };
  $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('Code', $violations[0]
    ->getCode());
}