You are here

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

File

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

Class

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

Namespace

Symfony\Component\Validator\Tests\Validator

Code

public function testValidateInSeparateContext() {
  $test = $this;
  $entity = new Entity();
  $entity->reference = new Reference();
  $callback1 = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
    $violations = $context
      ->getValidator()
      ->validate($value->reference, new Valid(), 'Group');

    /* @var ConstraintViolationInterface[] $violations */
    $test
      ->assertCount(1, $violations);
    $test
      ->assertSame('Message value', $violations[0]
      ->getMessage());
    $test
      ->assertSame('Message %param%', $violations[0]
      ->getMessageTemplate());
    $test
      ->assertSame(array(
      '%param%' => 'value',
    ), $violations[0]
      ->getParameters());
    $test
      ->assertSame('', $violations[0]
      ->getPropertyPath());

    // The root is different as we're in a new context
    $test
      ->assertSame($entity->reference, $violations[0]
      ->getRoot());
    $test
      ->assertSame($entity->reference, $violations[0]
      ->getInvalidValue());
    $test
      ->assertNull($violations[0]
      ->getPlural());
    $test
      ->assertNull($violations[0]
      ->getCode());

    // Verify that this method is called
    $context
      ->addViolation('Separate violation');
  };
  $callback2 = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
    $test
      ->assertSame($test::REFERENCE_CLASS, $context
      ->getClassName());
    $test
      ->assertNull($context
      ->getPropertyName());
    $test
      ->assertSame('', $context
      ->getPropertyPath());
    $test
      ->assertSame('Group', $context
      ->getGroup());
    $test
      ->assertSame($test->referenceMetadata, $context
      ->getMetadata());
    $test
      ->assertSame($entity->reference, $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);
  $test
    ->assertSame('Separate violation', $violations[0]
    ->getMessage());
}