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\PropertyAccess\PropertyAccess;
15 use Symfony\Component\Validator\Constraints\Expression;
16 use Symfony\Component\Validator\Constraints\ExpressionValidator;
17 use Symfony\Component\Validator\Tests\Fixtures\Entity;
18 use Symfony\Component\Validator\Validation;
20 class ExpressionValidatorTest extends AbstractConstraintValidatorTest
22 protected function getApiVersion()
24 return Validation::API_VERSION_2_5;
27 protected function createValidator()
29 return new ExpressionValidator(PropertyAccess::createPropertyAccessor());
32 public function testExpressionIsEvaluatedWithNullValue()
34 $constraint = new Expression(array(
35 'expression' => 'false',
36 'message' => 'myMessage',
39 $this->validator->validate(null, $constraint);
41 $this->buildViolation('myMessage')
42 ->setParameter('{{ value }}', 'null')
43 ->setCode(Expression::EXPRESSION_FAILED_ERROR)
47 public function testExpressionIsEvaluatedWithEmptyStringValue()
49 $constraint = new Expression(array(
50 'expression' => 'false',
51 'message' => 'myMessage',
54 $this->validator->validate('', $constraint);
56 $this->buildViolation('myMessage')
57 ->setParameter('{{ value }}', '""')
58 ->setCode(Expression::EXPRESSION_FAILED_ERROR)
62 public function testSucceedingExpressionAtObjectLevel()
64 $constraint = new Expression('this.data == 1');
66 $object = new Entity();
69 $this->setObject($object);
71 $this->validator->validate($object, $constraint);
73 $this->assertNoViolation();
76 public function testFailingExpressionAtObjectLevel()
78 $constraint = new Expression(array(
79 'expression' => 'this.data == 1',
80 'message' => 'myMessage',
83 $object = new Entity();
86 $this->setObject($object);
88 $this->validator->validate($object, $constraint);
90 $this->buildViolation('myMessage')
91 ->setParameter('{{ value }}', 'object')
92 ->setCode(Expression::EXPRESSION_FAILED_ERROR)
96 public function testSucceedingExpressionAtPropertyLevel()
98 $constraint = new Expression('value == this.data');
100 $object = new Entity();
103 $this->setRoot($object);
104 $this->setPropertyPath('data');
105 $this->setProperty($object, 'data');
107 $this->validator->validate('1', $constraint);
109 $this->assertNoViolation();
112 public function testFailingExpressionAtPropertyLevel()
114 $constraint = new Expression(array(
115 'expression' => 'value == this.data',
116 'message' => 'myMessage',
119 $object = new Entity();
122 $this->setRoot($object);
123 $this->setPropertyPath('data');
124 $this->setProperty($object, 'data');
126 $this->validator->validate('2', $constraint);
128 $this->buildViolation('myMessage')
130 ->setParameter('{{ value }}', '"2"')
131 ->setCode(Expression::EXPRESSION_FAILED_ERROR)
135 public function testSucceedingExpressionAtNestedPropertyLevel()
137 $constraint = new Expression('value == this.data');
139 $object = new Entity();
142 $root = new Entity();
143 $root->reference = $object;
145 $this->setRoot($root);
146 $this->setPropertyPath('reference.data');
147 $this->setProperty($object, 'data');
149 $this->validator->validate('1', $constraint);
151 $this->assertNoViolation();
154 public function testFailingExpressionAtNestedPropertyLevel()
156 $constraint = new Expression(array(
157 'expression' => 'value == this.data',
158 'message' => 'myMessage',
161 $object = new Entity();
164 $root = new Entity();
165 $root->reference = $object;
167 $this->setRoot($root);
168 $this->setPropertyPath('reference.data');
169 $this->setProperty($object, 'data');
171 $this->validator->validate('2', $constraint);
173 $this->buildViolation('myMessage')
174 ->atPath('reference.data')
175 ->setParameter('{{ value }}', '"2"')
176 ->setCode(Expression::EXPRESSION_FAILED_ERROR)
181 * When validatePropertyValue() is called with a class name
182 * https://github.com/symfony/symfony/pull/11498.
184 public function testSucceedingExpressionAtPropertyLevelWithoutRoot()
186 $constraint = new Expression('value == "1"');
189 $this->setPropertyPath('');
190 $this->setProperty(null, 'property');
192 $this->validator->validate('1', $constraint);
194 $this->assertNoViolation();
198 * When validatePropertyValue() is called with a class name
199 * https://github.com/symfony/symfony/pull/11498.
201 public function testFailingExpressionAtPropertyLevelWithoutRoot()
203 $constraint = new Expression(array(
204 'expression' => 'value == "1"',
205 'message' => 'myMessage',
209 $this->setPropertyPath('');
210 $this->setProperty(null, 'property');
212 $this->validator->validate('2', $constraint);
214 $this->buildViolation('myMessage')
216 ->setParameter('{{ value }}', '"2"')
217 ->setCode(Expression::EXPRESSION_FAILED_ERROR)
221 public function testExpressionLanguageUsage()
223 $constraint = new Expression(array(
224 'expression' => 'false',
227 $expressionLanguage = $this->getMockBuilder('Symfony\Component\ExpressionLanguage\ExpressionLanguage')->getMock();
231 $expressionLanguage->method('evaluate')
232 ->will($this->returnCallback(function () use (&$used) {
238 $validator = new ExpressionValidator(null, $expressionLanguage);
239 $validator->initialize($this->createContext());
240 $validator->validate(null, $constraint);
242 $this->assertTrue($used, 'Failed asserting that custom ExpressionLanguage instance is used.');