You are here

class CallbackValidatorTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/validator/Tests/Constraints/CallbackValidatorTest.php \Symfony\Component\Validator\Tests\Constraints\CallbackValidatorTest

Hierarchy

Expanded class hierarchy of CallbackValidatorTest

File

vendor/symfony/validator/Tests/Constraints/CallbackValidatorTest.php, line 47

Namespace

Symfony\Component\Validator\Tests\Constraints
View source
class CallbackValidatorTest extends AbstractConstraintValidatorTest {
  protected function getApiVersion() {
    return Validation::API_VERSION_2_5;
  }
  protected function createValidator() {
    return new CallbackValidator();
  }
  public function testNullIsValid() {
    $this->validator
      ->validate(null, new Callback());
    $this
      ->assertNoViolation();
  }
  public function testSingleMethod() {
    $object = new CallbackValidatorTest_Object();
    $constraint = new Callback('validate');
    $this->validator
      ->validate($object, $constraint);
    $this
      ->buildViolation('My message')
      ->setParameter('{{ value }}', 'foobar')
      ->assertRaised();
  }
  public function testSingleMethodExplicitName() {
    $object = new CallbackValidatorTest_Object();
    $constraint = new Callback(array(
      'callback' => 'validate',
    ));
    $this->validator
      ->validate($object, $constraint);
    $this
      ->buildViolation('My message')
      ->setParameter('{{ value }}', 'foobar')
      ->assertRaised();
  }
  public function testSingleStaticMethod() {
    $object = new CallbackValidatorTest_Object();
    $constraint = new Callback('validateStatic');
    $this->validator
      ->validate($object, $constraint);
    $this
      ->buildViolation('Static message')
      ->setParameter('{{ value }}', 'baz')
      ->assertRaised();
  }
  public function testClosure() {
    $object = new CallbackValidatorTest_Object();
    $constraint = new Callback(function ($object, ExecutionContextInterface $context) {
      $context
        ->addViolation('My message', array(
        '{{ value }}' => 'foobar',
      ));
      return false;
    });
    $this->validator
      ->validate($object, $constraint);
    $this
      ->buildViolation('My message')
      ->setParameter('{{ value }}', 'foobar')
      ->assertRaised();
  }
  public function testClosureNullObject() {
    $constraint = new Callback(function ($object, ExecutionContextInterface $context) {
      $context
        ->addViolation('My message', array(
        '{{ value }}' => 'foobar',
      ));
      return false;
    });
    $this->validator
      ->validate(null, $constraint);
    $this
      ->buildViolation('My message')
      ->setParameter('{{ value }}', 'foobar')
      ->assertRaised();
  }
  public function testClosureExplicitName() {
    $object = new CallbackValidatorTest_Object();
    $constraint = new Callback(array(
      'callback' => function ($object, ExecutionContextInterface $context) {
        $context
          ->addViolation('My message', array(
          '{{ value }}' => 'foobar',
        ));
        return false;
      },
    ));
    $this->validator
      ->validate($object, $constraint);
    $this
      ->buildViolation('My message')
      ->setParameter('{{ value }}', 'foobar')
      ->assertRaised();
  }
  public function testArrayCallable() {
    $object = new CallbackValidatorTest_Object();
    $constraint = new Callback(array(
      __CLASS__ . '_Class',
      'validateCallback',
    ));
    $this->validator
      ->validate($object, $constraint);
    $this
      ->buildViolation('Callback message')
      ->setParameter('{{ value }}', 'foobar')
      ->assertRaised();
  }
  public function testArrayCallableNullObject() {
    $constraint = new Callback(array(
      __CLASS__ . '_Class',
      'validateCallback',
    ));
    $this->validator
      ->validate(null, $constraint);
    $this
      ->buildViolation('Callback message')
      ->setParameter('{{ value }}', 'foobar')
      ->assertRaised();
  }
  public function testArrayCallableExplicitName() {
    $object = new CallbackValidatorTest_Object();
    $constraint = new Callback(array(
      'callback' => array(
        __CLASS__ . '_Class',
        'validateCallback',
      ),
    ));
    $this->validator
      ->validate($object, $constraint);
    $this
      ->buildViolation('Callback message')
      ->setParameter('{{ value }}', 'foobar')
      ->assertRaised();
  }

  // BC with Symfony < 2.4

  /**
   * @group legacy
   */
  public function testLegacySingleMethodBc() {
    $object = new CallbackValidatorTest_Object();
    $constraint = new Callback(array(
      'validate',
    ));
    $this->validator
      ->validate($object, $constraint);
    $this
      ->buildViolation('My message')
      ->setParameter('{{ value }}', 'foobar')
      ->assertRaised();
  }

  // BC with Symfony < 2.4

  /**
   * @group legacy
   */
  public function testLegacySingleMethodBcExplicitName() {
    $object = new CallbackValidatorTest_Object();
    $constraint = new Callback(array(
      'methods' => array(
        'validate',
      ),
    ));
    $this->validator
      ->validate($object, $constraint);
    $this
      ->buildViolation('My message')
      ->setParameter('{{ value }}', 'foobar')
      ->assertRaised();
  }

  // BC with Symfony < 2.4

  /**
   * @group legacy
   */
  public function testLegacyMultipleMethodsBc() {
    $object = new CallbackValidatorTest_Object();
    $constraint = new Callback(array(
      'validate',
      'validateStatic',
    ));
    $this->validator
      ->validate($object, $constraint);
    $this
      ->buildViolation('My message')
      ->setParameter('{{ value }}', 'foobar')
      ->buildNextViolation('Static message')
      ->setParameter('{{ value }}', 'baz')
      ->assertRaised();
  }

  // BC with Symfony < 2.4

  /**
   * @group legacy
   */
  public function testLegacyMultipleMethodsBcExplicitName() {
    $object = new CallbackValidatorTest_Object();
    $constraint = new Callback(array(
      'methods' => array(
        'validate',
        'validateStatic',
      ),
    ));
    $this->validator
      ->validate($object, $constraint);
    $this
      ->buildViolation('My message')
      ->setParameter('{{ value }}', 'foobar')
      ->buildNextViolation('Static message')
      ->setParameter('{{ value }}', 'baz')
      ->assertRaised();
  }

  // BC with Symfony < 2.4

  /**
   * @group legacy
   */
  public function testLegacySingleStaticMethodBc() {
    $object = new CallbackValidatorTest_Object();
    $constraint = new Callback(array(
      array(
        __CLASS__ . '_Class',
        'validateCallback',
      ),
    ));
    $this->validator
      ->validate($object, $constraint);
    $this
      ->buildViolation('Callback message')
      ->setParameter('{{ value }}', 'foobar')
      ->assertRaised();
  }

  // BC with Symfony < 2.4

  /**
   * @group legacy
   */
  public function testLegacySingleStaticMethodBcExplicitName() {
    $object = new CallbackValidatorTest_Object();
    $constraint = new Callback(array(
      'methods' => array(
        array(
          __CLASS__ . '_Class',
          'validateCallback',
        ),
      ),
    ));
    $this->validator
      ->validate($object, $constraint);
    $this
      ->buildViolation('Callback message')
      ->setParameter('{{ value }}', 'foobar')
      ->assertRaised();
  }

  /**
   * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
   */
  public function testExpectValidMethods() {
    $object = new CallbackValidatorTest_Object();
    $this->validator
      ->validate($object, new Callback(array(
      'callback' => array(
        'foobar',
      ),
    )));
  }

  /**
   * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
   */
  public function testExpectValidCallbacks() {
    $object = new CallbackValidatorTest_Object();
    $this->validator
      ->validate($object, new Callback(array(
      'callback' => array(
        'foo',
        'bar',
      ),
    )));
  }

  /**
   * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
   * @group legacy
   */
  public function testLegacyExpectEitherCallbackOrMethods() {
    $object = new CallbackValidatorTest_Object();
    $this->validator
      ->validate($object, new Callback(array(
      'callback' => 'validate',
      'methods' => array(
        'validateStatic',
      ),
    )));
  }
  public function testConstraintGetTargets() {
    $constraint = new Callback(array());
    $targets = array(
      Constraint::CLASS_CONSTRAINT,
      Constraint::PROPERTY_CONSTRAINT,
    );
    $this
      ->assertEquals($targets, $constraint
      ->getTargets());
  }

  // Should succeed. Needed when defining constraints as annotations.
  public function testNoConstructorArguments() {
    new Callback();
  }
  public function testAnnotationInvocationSingleValued() {
    $constraint = new Callback(array(
      'value' => 'validateStatic',
    ));
    $this
      ->assertEquals(new Callback('validateStatic'), $constraint);
  }
  public function testAnnotationInvocationMultiValued() {
    $constraint = new Callback(array(
      'value' => array(
        __CLASS__ . '_Class',
        'validateCallback',
      ),
    ));
    $this
      ->assertEquals(new Callback(array(
      __CLASS__ . '_Class',
      'validateCallback',
    )), $constraint);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AbstractConstraintValidatorTest::$constraint protected property
AbstractConstraintValidatorTest::$context protected property 1
AbstractConstraintValidatorTest::$defaultTimezone protected property
AbstractConstraintValidatorTest::$group protected property
AbstractConstraintValidatorTest::$metadata protected property
AbstractConstraintValidatorTest::$object protected property
AbstractConstraintValidatorTest::$propertyPath protected property
AbstractConstraintValidatorTest::$root protected property
AbstractConstraintValidatorTest::$validator protected property 1
AbstractConstraintValidatorTest::$value protected property
AbstractConstraintValidatorTest::assertNoViolation protected function
AbstractConstraintValidatorTest::assertViolation Deprecated protected function
AbstractConstraintValidatorTest::assertViolations Deprecated protected function
AbstractConstraintValidatorTest::buildViolation protected function
AbstractConstraintValidatorTest::createContext protected function
AbstractConstraintValidatorTest::createViolation Deprecated protected function
AbstractConstraintValidatorTest::expectNoValidate protected function
AbstractConstraintValidatorTest::expectValidateAt protected function
AbstractConstraintValidatorTest::expectValidateValueAt protected function
AbstractConstraintValidatorTest::restoreDefaultTimezone protected function
AbstractConstraintValidatorTest::setDefaultTimezone protected function
AbstractConstraintValidatorTest::setGroup protected function
AbstractConstraintValidatorTest::setObject protected function
AbstractConstraintValidatorTest::setProperty protected function
AbstractConstraintValidatorTest::setPropertyPath protected function
AbstractConstraintValidatorTest::setRoot protected function
AbstractConstraintValidatorTest::setUp protected function 2
AbstractConstraintValidatorTest::setValue protected function
AbstractConstraintValidatorTest::tearDown protected function 1
CallbackValidatorTest::createValidator protected function Overrides AbstractConstraintValidatorTest::createValidator
CallbackValidatorTest::getApiVersion protected function Overrides AbstractConstraintValidatorTest::getApiVersion
CallbackValidatorTest::testAnnotationInvocationMultiValued public function
CallbackValidatorTest::testAnnotationInvocationSingleValued public function
CallbackValidatorTest::testArrayCallable public function
CallbackValidatorTest::testArrayCallableExplicitName public function
CallbackValidatorTest::testArrayCallableNullObject public function
CallbackValidatorTest::testClosure public function
CallbackValidatorTest::testClosureExplicitName public function
CallbackValidatorTest::testClosureNullObject public function
CallbackValidatorTest::testConstraintGetTargets public function
CallbackValidatorTest::testExpectValidCallbacks public function @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
CallbackValidatorTest::testExpectValidMethods public function @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
CallbackValidatorTest::testLegacyExpectEitherCallbackOrMethods public function @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException @group legacy
CallbackValidatorTest::testLegacyMultipleMethodsBc public function @group legacy
CallbackValidatorTest::testLegacyMultipleMethodsBcExplicitName public function @group legacy
CallbackValidatorTest::testLegacySingleMethodBc public function @group legacy
CallbackValidatorTest::testLegacySingleMethodBcExplicitName public function @group legacy
CallbackValidatorTest::testLegacySingleStaticMethodBc public function @group legacy
CallbackValidatorTest::testLegacySingleStaticMethodBcExplicitName public function @group legacy
CallbackValidatorTest::testNoConstructorArguments public function
CallbackValidatorTest::testNullIsValid public function
CallbackValidatorTest::testSingleMethod public function
CallbackValidatorTest::testSingleMethodExplicitName public function
CallbackValidatorTest::testSingleStaticMethod public function