You are here

class ExpressionValidatorTest in Zircon Profile 8

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

Hierarchy

Expanded class hierarchy of ExpressionValidatorTest

File

vendor/symfony/validator/Tests/Constraints/ExpressionValidatorTest.php, line 20

Namespace

Symfony\Component\Validator\Tests\Constraints
View source
class ExpressionValidatorTest extends AbstractConstraintValidatorTest {
  protected function getApiVersion() {
    return Validation::API_VERSION_2_5;
  }
  protected function createValidator() {
    return new ExpressionValidator(PropertyAccess::createPropertyAccessor());
  }
  public function testExpressionIsEvaluatedWithNullValue() {
    $constraint = new Expression(array(
      'expression' => 'false',
      'message' => 'myMessage',
    ));
    $this->validator
      ->validate(null, $constraint);
    $this
      ->buildViolation('myMessage')
      ->setParameter('{{ value }}', 'null')
      ->assertRaised();
  }
  public function testExpressionIsEvaluatedWithEmptyStringValue() {
    $constraint = new Expression(array(
      'expression' => 'false',
      'message' => 'myMessage',
    ));
    $this->validator
      ->validate('', $constraint);
    $this
      ->buildViolation('myMessage')
      ->setParameter('{{ value }}', '""')
      ->assertRaised();
  }
  public function testSucceedingExpressionAtObjectLevel() {
    $constraint = new Expression('this.data == 1');
    $object = new Entity();
    $object->data = '1';
    $this
      ->setObject($object);
    $this->validator
      ->validate($object, $constraint);
    $this
      ->assertNoViolation();
  }
  public function testFailingExpressionAtObjectLevel() {
    $constraint = new Expression(array(
      'expression' => 'this.data == 1',
      'message' => 'myMessage',
    ));
    $object = new Entity();
    $object->data = '2';
    $this
      ->setObject($object);
    $this->validator
      ->validate($object, $constraint);
    $this
      ->buildViolation('myMessage')
      ->setParameter('{{ value }}', 'object')
      ->assertRaised();
  }
  public function testSucceedingExpressionAtPropertyLevel() {
    $constraint = new Expression('value == this.data');
    $object = new Entity();
    $object->data = '1';
    $this
      ->setRoot($object);
    $this
      ->setPropertyPath('data');
    $this
      ->setProperty($object, 'data');
    $this->validator
      ->validate('1', $constraint);
    $this
      ->assertNoViolation();
  }
  public function testFailingExpressionAtPropertyLevel() {
    $constraint = new Expression(array(
      'expression' => 'value == this.data',
      'message' => 'myMessage',
    ));
    $object = new Entity();
    $object->data = '1';
    $this
      ->setRoot($object);
    $this
      ->setPropertyPath('data');
    $this
      ->setProperty($object, 'data');
    $this->validator
      ->validate('2', $constraint);
    $this
      ->buildViolation('myMessage')
      ->setParameter('{{ value }}', '"2"')
      ->atPath('data')
      ->assertRaised();
  }
  public function testSucceedingExpressionAtNestedPropertyLevel() {
    $constraint = new Expression('value == this.data');
    $object = new Entity();
    $object->data = '1';
    $root = new Entity();
    $root->reference = $object;
    $this
      ->setRoot($root);
    $this
      ->setPropertyPath('reference.data');
    $this
      ->setProperty($object, 'data');
    $this->validator
      ->validate('1', $constraint);
    $this
      ->assertNoViolation();
  }
  public function testFailingExpressionAtNestedPropertyLevel() {
    $constraint = new Expression(array(
      'expression' => 'value == this.data',
      'message' => 'myMessage',
    ));
    $object = new Entity();
    $object->data = '1';
    $root = new Entity();
    $root->reference = $object;
    $this
      ->setRoot($root);
    $this
      ->setPropertyPath('reference.data');
    $this
      ->setProperty($object, 'data');
    $this->validator
      ->validate('2', $constraint);
    $this
      ->buildViolation('myMessage')
      ->setParameter('{{ value }}', '"2"')
      ->atPath('reference.data')
      ->assertRaised();
  }

  /**
   * When validatePropertyValue() is called with a class name
   * https://github.com/symfony/symfony/pull/11498.
   */
  public function testSucceedingExpressionAtPropertyLevelWithoutRoot() {
    $constraint = new Expression('value == "1"');
    $this
      ->setRoot('1');
    $this
      ->setPropertyPath('');
    $this
      ->setProperty(null, 'property');
    $this->validator
      ->validate('1', $constraint);
    $this
      ->assertNoViolation();
  }

  /**
   * When validatePropertyValue() is called with a class name
   * https://github.com/symfony/symfony/pull/11498.
   */
  public function testFailingExpressionAtPropertyLevelWithoutRoot() {
    $constraint = new Expression(array(
      'expression' => 'value == "1"',
      'message' => 'myMessage',
    ));
    $this
      ->setRoot('2');
    $this
      ->setPropertyPath('');
    $this
      ->setProperty(null, 'property');
    $this->validator
      ->validate('2', $constraint);
    $this
      ->buildViolation('myMessage')
      ->setParameter('{{ value }}', '"2"')
      ->atPath('')
      ->assertRaised();
  }

}

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
ExpressionValidatorTest::createValidator protected function Overrides AbstractConstraintValidatorTest::createValidator
ExpressionValidatorTest::getApiVersion protected function Overrides AbstractConstraintValidatorTest::getApiVersion
ExpressionValidatorTest::testExpressionIsEvaluatedWithEmptyStringValue public function
ExpressionValidatorTest::testExpressionIsEvaluatedWithNullValue public function
ExpressionValidatorTest::testFailingExpressionAtNestedPropertyLevel public function
ExpressionValidatorTest::testFailingExpressionAtObjectLevel public function
ExpressionValidatorTest::testFailingExpressionAtPropertyLevel public function
ExpressionValidatorTest::testFailingExpressionAtPropertyLevelWithoutRoot public function When validatePropertyValue() is called with a class name https://github.com/symfony/symfony/pull/11498.
ExpressionValidatorTest::testSucceedingExpressionAtNestedPropertyLevel public function
ExpressionValidatorTest::testSucceedingExpressionAtObjectLevel public function
ExpressionValidatorTest::testSucceedingExpressionAtPropertyLevel public function
ExpressionValidatorTest::testSucceedingExpressionAtPropertyLevelWithoutRoot public function When validatePropertyValue() is called with a class name https://github.com/symfony/symfony/pull/11498.