You are here

public function AbstractLegacyApiTest::testValidateArrayInContext in Zircon Profile 8

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

File

vendor/symfony/validator/Tests/Validator/AbstractLegacyApiTest.php, line 177

Class

AbstractLegacyApiTest
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();
    $previousMetadata = $context
      ->getMetadata();
    $previousPath = $context
      ->getPropertyPath();
    $previousGroup = $context
      ->getGroup();
    $context
      ->validate(array(
      'key' => $value->reference,
    ), 'subpath');

    // context changes shouldn't leak out of the validate() call
    $test
      ->assertSame($previousValue, $context
      ->getValue());
    $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($test->metadataFactory, $context
      ->getMetadataFactory());
    $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, '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());
}