You are here

public function Abstract2Dot5ApiTest::testValidateArrayInContext 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::testValidateArrayInContext()

File

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

Class

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

Namespace

Symfony\Component\Validator\Tests\Validator

Code

public function testValidateArrayInContext() {
  $test = $this;
  $entity = new Entity();
  $entity->reference = new Reference();
  $callback1 = function ($value, ExecutionContextInterface $context) use ($test) {
    $previousValue = $context
      ->getValue();
    $previousObject = $context
      ->getObject();
    $previousMetadata = $context
      ->getMetadata();
    $previousPath = $context
      ->getPropertyPath();
    $previousGroup = $context
      ->getGroup();
    $context
      ->getValidator()
      ->inContext($context)
      ->atPath('subpath')
      ->validate(array(
      'key' => $value->reference,
    ));

    // context changes shouldn't leak out of the validate() call
    $test
      ->assertSame($previousValue, $context
      ->getValue());
    $test
      ->assertSame($previousObject, $context
      ->getObject());
    $test
      ->assertSame($previousMetadata, $context
      ->getMetadata());
    $test
      ->assertSame($previousPath, $context
      ->getPropertyPath());
    $test
      ->assertSame($previousGroup, $context
      ->getGroup());
  };
  $callback2 = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
    $test
      ->assertSame($test::REFERENCE_CLASS, $context
      ->getClassName());
    $test
      ->assertNull($context
      ->getPropertyName());
    $test
      ->assertSame('subpath[key]', $context
      ->getPropertyPath());
    $test
      ->assertSame('Group', $context
      ->getGroup());
    $test
      ->assertSame($test->referenceMetadata, $context
      ->getMetadata());
    $test
      ->assertSame($entity, $context
      ->getRoot());
    $test
      ->assertSame($entity->reference, $context
      ->getValue());
    $test
      ->assertSame($entity->reference, $value);
    $context
      ->addViolation('Message %param%', array(
      '%param%' => 'value',
    ));
  };
  $this->metadata
    ->addConstraint(new Callback(array(
    'callback' => $callback1,
    'groups' => 'Group',
  )));
  $this->referenceMetadata
    ->addConstraint(new Callback(array(
    'callback' => $callback2,
    'groups' => 'Group',
  )));
  $violations = $this->validator
    ->validate($entity, new Valid(), '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('subpath[key]', $violations[0]
    ->getPropertyPath());
  $this
    ->assertSame($entity, $violations[0]
    ->getRoot());
  $this
    ->assertSame($entity->reference, $violations[0]
    ->getInvalidValue());
  $this
    ->assertNull($violations[0]
    ->getPlural());
  $this
    ->assertNull($violations[0]
    ->getCode());
}