You are here

public function Abstract2Dot5ApiTest::testValidateInContext in Plug 7

File

lib/Symfony/validator/Symfony/Component/Validator/Tests/Validator/Abstract2Dot5ApiTest.php, line 199

Class

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

Namespace

Symfony\Component\Validator\Tests\Validator

Code

public function testValidateInContext() {
  $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($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', $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]
    ->getMessageParameters());
  $this
    ->assertSame('subpath', $violations[0]
    ->getPropertyPath());
  $this
    ->assertSame($entity, $violations[0]
    ->getRoot());
  $this
    ->assertSame($entity->reference, $violations[0]
    ->getInvalidValue());
  $this
    ->assertNull($violations[0]
    ->getMessagePluralization());
  $this
    ->assertNull($violations[0]
    ->getCode());
}