You are here

public function AbstractLegacyApiTest::testInitializeObjectsOnFirstValidation in Plug 7

File

lib/Symfony/validator/Symfony/Component/Validator/Tests/Validator/AbstractLegacyApiTest.php, line 265

Class

AbstractLegacyApiTest
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
    $context
      ->validate($object);

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