4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Tests\Constraints;
14 use Symfony\Component\Validator\Constraints\Expression;
15 use Symfony\Component\Validator\Constraints\ExpressionValidator;
16 use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
17 use Symfony\Component\Validator\Tests\Fixtures\Entity;
19 class ExpressionValidatorTest extends ConstraintValidatorTestCase
21 protected function createValidator()
23 return new ExpressionValidator();
26 public function testExpressionIsEvaluatedWithNullValue()
28 $constraint = new Expression(array(
29 'expression' => 'false',
30 'message' => 'myMessage',
33 $this->validator->validate(null, $constraint);
35 $this->buildViolation('myMessage')
36 ->setParameter('{{ value }}', 'null')
37 ->setCode(Expression::EXPRESSION_FAILED_ERROR)
41 public function testExpressionIsEvaluatedWithEmptyStringValue()
43 $constraint = new Expression(array(
44 'expression' => 'false',
45 'message' => 'myMessage',
48 $this->validator->validate('', $constraint);
50 $this->buildViolation('myMessage')
51 ->setParameter('{{ value }}', '""')
52 ->setCode(Expression::EXPRESSION_FAILED_ERROR)
56 public function testSucceedingExpressionAtObjectLevel()
58 $constraint = new Expression('this.data == 1');
60 $object = new Entity();
63 $this->setObject($object);
65 $this->validator->validate($object, $constraint);
67 $this->assertNoViolation();
70 public function testFailingExpressionAtObjectLevel()
72 $constraint = new Expression(array(
73 'expression' => 'this.data == 1',
74 'message' => 'myMessage',
77 $object = new Entity();
80 $this->setObject($object);
82 $this->validator->validate($object, $constraint);
84 $this->buildViolation('myMessage')
85 ->setParameter('{{ value }}', 'object')
86 ->setCode(Expression::EXPRESSION_FAILED_ERROR)
90 public function testSucceedingExpressionAtPropertyLevel()
92 $constraint = new Expression('value == this.data');
94 $object = new Entity();
97 $this->setRoot($object);
98 $this->setPropertyPath('data');
99 $this->setProperty($object, 'data');
101 $this->validator->validate('1', $constraint);
103 $this->assertNoViolation();
106 public function testFailingExpressionAtPropertyLevel()
108 $constraint = new Expression(array(
109 'expression' => 'value == this.data',
110 'message' => 'myMessage',
113 $object = new Entity();
116 $this->setRoot($object);
117 $this->setPropertyPath('data');
118 $this->setProperty($object, 'data');
120 $this->validator->validate('2', $constraint);
122 $this->buildViolation('myMessage')
124 ->setParameter('{{ value }}', '"2"')
125 ->setCode(Expression::EXPRESSION_FAILED_ERROR)
129 public function testSucceedingExpressionAtNestedPropertyLevel()
131 $constraint = new Expression('value == this.data');
133 $object = new Entity();
136 $root = new Entity();
137 $root->reference = $object;
139 $this->setRoot($root);
140 $this->setPropertyPath('reference.data');
141 $this->setProperty($object, 'data');
143 $this->validator->validate('1', $constraint);
145 $this->assertNoViolation();
148 public function testFailingExpressionAtNestedPropertyLevel()
150 $constraint = new Expression(array(
151 'expression' => 'value == this.data',
152 'message' => 'myMessage',
155 $object = new Entity();
158 $root = new Entity();
159 $root->reference = $object;
161 $this->setRoot($root);
162 $this->setPropertyPath('reference.data');
163 $this->setProperty($object, 'data');
165 $this->validator->validate('2', $constraint);
167 $this->buildViolation('myMessage')
168 ->atPath('reference.data')
169 ->setParameter('{{ value }}', '"2"')
170 ->setCode(Expression::EXPRESSION_FAILED_ERROR)
175 * When validatePropertyValue() is called with a class name
176 * https://github.com/symfony/symfony/pull/11498.
178 public function testSucceedingExpressionAtPropertyLevelWithoutRoot()
180 $constraint = new Expression('value == "1"');
183 $this->setPropertyPath('');
184 $this->setProperty(null, 'property');
186 $this->validator->validate('1', $constraint);
188 $this->assertNoViolation();
192 * When validatePropertyValue() is called with a class name
193 * https://github.com/symfony/symfony/pull/11498.
195 public function testFailingExpressionAtPropertyLevelWithoutRoot()
197 $constraint = new Expression(array(
198 'expression' => 'value == "1"',
199 'message' => 'myMessage',
203 $this->setPropertyPath('');
204 $this->setProperty(null, 'property');
206 $this->validator->validate('2', $constraint);
208 $this->buildViolation('myMessage')
210 ->setParameter('{{ value }}', '"2"')
211 ->setCode(Expression::EXPRESSION_FAILED_ERROR)
215 public function testExpressionLanguageUsage()
217 $constraint = new Expression(array(
218 'expression' => 'false',
221 $expressionLanguage = $this->getMockBuilder('Symfony\Component\ExpressionLanguage\ExpressionLanguage')->getMock();
225 $expressionLanguage->method('evaluate')
226 ->will($this->returnCallback(function () use (&$used) {
232 $validator = new ExpressionValidator(null, $expressionLanguage);
233 $validator->initialize($this->createContext());
234 $validator->validate(null, $constraint);
236 $this->assertTrue($used, 'Failed asserting that custom ExpressionLanguage instance is used.');