123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Validator\Tests\Constraints;
- use Symfony\Component\PropertyAccess\PropertyAccess;
- use Symfony\Component\Validator\Constraints\Expression;
- use Symfony\Component\Validator\Constraints\ExpressionValidator;
- use Symfony\Component\Validator\Tests\Fixtures\Entity;
- use Symfony\Component\Validator\Tests\Fixtures\ToString;
- use Symfony\Component\Validator\Validation;
- 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')
- ->setCode(Expression::EXPRESSION_FAILED_ERROR)
- ->assertRaised();
- }
- public function testExpressionIsEvaluatedWithEmptyStringValue()
- {
- $constraint = new Expression(array(
- 'expression' => 'false',
- 'message' => 'myMessage',
- ));
- $this->validator->validate('', $constraint);
- $this->buildViolation('myMessage')
- ->setParameter('{{ value }}', '""')
- ->setCode(Expression::EXPRESSION_FAILED_ERROR)
- ->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')
- ->setCode(Expression::EXPRESSION_FAILED_ERROR)
- ->assertRaised();
- }
- public function testSucceedingExpressionAtObjectLevelWithToString()
- {
- $constraint = new Expression('this.data == 1');
- $object = new ToString();
- $object->data = '1';
- $this->setObject($object);
- $this->validator->validate($object, $constraint);
- $this->assertNoViolation();
- }
- public function testFailingExpressionAtObjectLevelWithToString()
- {
- $constraint = new Expression(array(
- 'expression' => 'this.data == 1',
- 'message' => 'myMessage',
- ));
- $object = new ToString();
- $object->data = '2';
- $this->setObject($object);
- $this->validator->validate($object, $constraint);
- $this->buildViolation('myMessage')
- ->setParameter('{{ value }}', 'toString')
- ->setCode(Expression::EXPRESSION_FAILED_ERROR)
- ->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')
- ->atPath('data')
- ->setParameter('{{ value }}', '"2"')
- ->setCode(Expression::EXPRESSION_FAILED_ERROR)
- ->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')
- ->atPath('reference.data')
- ->setParameter('{{ value }}', '"2"')
- ->setCode(Expression::EXPRESSION_FAILED_ERROR)
- ->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')
- ->atPath('')
- ->setParameter('{{ value }}', '"2"')
- ->setCode(Expression::EXPRESSION_FAILED_ERROR)
- ->assertRaised();
- }
- public function testExpressionLanguageUsage()
- {
- $constraint = new Expression(array(
- 'expression' => 'false',
- ));
- $expressionLanguage = $this->getMockBuilder('Symfony\Component\ExpressionLanguage\ExpressionLanguage')->getMock();
- $used = false;
- $expressionLanguage->method('evaluate')
- ->will($this->returnCallback(function () use (&$used) {
- $used = true;
- return true;
- }));
- $validator = new ExpressionValidator(null, $expressionLanguage);
- $validator->initialize($this->createContext());
- $validator->validate(null, $constraint);
- $this->assertTrue($used, 'Failed asserting that custom ExpressionLanguage instance is used.');
- }
- }
|