You are here

public function Abstract2Dot5ApiTest::testInitializeObjectsOnFirstValidation in Plug 7

File

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

Class

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

Namespace

Symfony\Component\Validator\Tests\Validator

Code

public function testInitializeObjectsOnFirstValidation() {
  $test = $this;
  $entity = new Entity();
  $entity->initialized = false;

  // prepare initializers that set "initialized" to true
  $initializer1 = $this
    ->getMock('Symfony\\Component\\Validator\\ObjectInitializerInterface');
  $initializer2 = $this
    ->getMock('Symfony\\Component\\Validator\\ObjectInitializerInterface');
  $initializer1
    ->expects($this
    ->once())
    ->method('initialize')
    ->with($entity)
    ->will($this
    ->returnCallback(function ($object) {
    $object->initialized = true;
  }));
  $initializer2
    ->expects($this
    ->once())
    ->method('initialize')
    ->with($entity);
  $this->validator = $this
    ->createValidator($this->metadataFactory, array(
    $initializer1,
    $initializer2,
  ));

  // prepare constraint which
  // * checks that "initialized" is set to true
  // * validates the object again
  $callback = function ($object, ExecutionContextInterface $context) use ($test) {
    $test
      ->assertTrue($object->initialized);

    // validate again in same group
    $validator = $context
      ->getValidator()
      ->inContext($context);
    $validator
      ->validate($object);

    // validate again in other group
    $validator
      ->validate($object, null, 'SomeGroup');
  };
  $this->metadata
    ->addConstraint(new Callback($callback));
  $this
    ->validate($entity);
  $this
    ->assertTrue($entity->initialized);
}