You are here

public function AbstractValidatorTest::testGetterConstraint in Zircon Profile 8

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

File

vendor/symfony/validator/Tests/Validator/AbstractValidatorTest.php, line 185

Class

AbstractValidatorTest
@since 2.5

Namespace

Symfony\Component\Validator\Tests\Validator

Code

public function testGetterConstraint() {
  $test = $this;
  $entity = new Entity();
  $entity
    ->setLastName('Schussek');
  $callback = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
    $propertyMetadatas = $test->metadata
      ->getPropertyMetadata('lastName');
    $test
      ->assertSame($test::ENTITY_CLASS, $context
      ->getClassName());
    $test
      ->assertSame('lastName', $context
      ->getPropertyName());
    $test
      ->assertSame('lastName', $context
      ->getPropertyPath());
    $test
      ->assertSame('Group', $context
      ->getGroup());
    $test
      ->assertSame($propertyMetadatas[0], $context
      ->getMetadata());
    $test
      ->assertSame($entity, $context
      ->getRoot());
    $test
      ->assertSame('Schussek', $context
      ->getValue());
    $test
      ->assertSame('Schussek', $value);
    $context
      ->addViolation('Message %param%', array(
      '%param%' => 'value',
    ));
  };
  $this->metadata
    ->addGetterConstraint('lastName', new Callback(array(
    'callback' => $callback,
    'groups' => 'Group',
  )));
  $violations = $this
    ->validate($entity, null, 'Group');

  /* @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('lastName', $violations[0]
    ->getPropertyPath());
  $this
    ->assertSame($entity, $violations[0]
    ->getRoot());
  $this
    ->assertSame('Schussek', $violations[0]
    ->getInvalidValue());
  $this
    ->assertNull($violations[0]
    ->getPlural());
  $this
    ->assertNull($violations[0]
    ->getCode());
}