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;
18 use Symfony\Component\Validator\Tests\Fixtures\ToString;
20 class ExpressionValidatorTest extends ConstraintValidatorTestCase
22 protected function createValidator()
24 return new ExpressionValidator();
27 public function testExpressionIsEvaluatedWithNullValue()
29 $constraint = new Expression(array(
30 'expression' => 'false',
31 'message' => 'myMessage',
34 $this->validator->validate(null, $constraint);
36 $this->buildViolation('myMessage')
37 ->setParameter('{{ value }}', 'null')
38 ->setCode(Expression::EXPRESSION_FAILED_ERROR)
42 public function testExpressionIsEvaluatedWithEmptyStringValue()
44 $constraint = new Expression(array(
45 'expression' => 'false',
46 'message' => 'myMessage',
49 $this->validator->validate('', $constraint);
51 $this->buildViolation('myMessage')
52 ->setParameter('{{ value }}', '""')
53 ->setCode(Expression::EXPRESSION_FAILED_ERROR)
57 public function testSucceedingExpressionAtObjectLevel()
59 $constraint = new Expression('this.data == 1');
61 $object = new Entity();
64 $this->setObject($object);
66 $this->validator->validate($object, $constraint);
68 $this->assertNoViolation();
71 public function testFailingExpressionAtObjectLevel()
73 $constraint = new Expression(array(
74 'expression' => 'this.data == 1',
75 'message' => 'myMessage',
78 $object = new Entity();
81 $this->setObject($object);
83 $this->validator->validate($object, $constraint);
85 $this->buildViolation('myMessage')
86 ->setParameter('{{ value }}', 'object')
87 ->setCode(Expression::EXPRESSION_FAILED_ERROR)
91 public function testSucceedingExpressionAtObjectLevelWithToString()
93 $constraint = new Expression('this.data == 1');
95 $object = new ToString();
98 $this->setObject($object);
100 $this->validator->validate($object, $constraint);
102 $this->assertNoViolation();
105 public function testFailingExpressionAtObjectLevelWithToString()
107 $constraint = new Expression(array(
108 'expression' => 'this.data == 1',
109 'message' => 'myMessage',
112 $object = new ToString();
115 $this->setObject($object);
117 $this->validator->validate($object, $constraint);
119 $this->buildViolation('myMessage')
120 ->setParameter('{{ value }}', 'toString')
121 ->setCode(Expression::EXPRESSION_FAILED_ERROR)
125 public function testSucceedingExpressionAtPropertyLevel()
127 $constraint = new Expression('value == this.data');
129 $object = new Entity();
132 $this->setRoot($object);
133 $this->setPropertyPath('data');
134 $this->setProperty($object, 'data');
136 $this->validator->validate('1', $constraint);
138 $this->assertNoViolation();
141 public function testFailingExpressionAtPropertyLevel()
143 $constraint = new Expression(array(
144 'expression' => 'value == this.data',
145 'message' => 'myMessage',
148 $object = new Entity();
151 $this->setRoot($object);
152 $this->setPropertyPath('data');
153 $this->setProperty($object, 'data');
155 $this->validator->validate('2', $constraint);
157 $this->buildViolation('myMessage')
159 ->setParameter('{{ value }}', '"2"')
160 ->setCode(Expression::EXPRESSION_FAILED_ERROR)
164 public function testSucceedingExpressionAtNestedPropertyLevel()
166 $constraint = new Expression('value == this.data');
168 $object = new Entity();
171 $root = new Entity();
172 $root->reference = $object;
174 $this->setRoot($root);
175 $this->setPropertyPath('reference.data');
176 $this->setProperty($object, 'data');
178 $this->validator->validate('1', $constraint);
180 $this->assertNoViolation();
183 public function testFailingExpressionAtNestedPropertyLevel()
185 $constraint = new Expression(array(
186 'expression' => 'value == this.data',
187 'message' => 'myMessage',
190 $object = new Entity();
193 $root = new Entity();
194 $root->reference = $object;
196 $this->setRoot($root);
197 $this->setPropertyPath('reference.data');
198 $this->setProperty($object, 'data');
200 $this->validator->validate('2', $constraint);
202 $this->buildViolation('myMessage')
203 ->atPath('reference.data')
204 ->setParameter('{{ value }}', '"2"')
205 ->setCode(Expression::EXPRESSION_FAILED_ERROR)
210 * When validatePropertyValue() is called with a class name
211 * https://github.com/symfony/symfony/pull/11498.
213 public function testSucceedingExpressionAtPropertyLevelWithoutRoot()
215 $constraint = new Expression('value == "1"');
218 $this->setPropertyPath('');
219 $this->setProperty(null, 'property');
221 $this->validator->validate('1', $constraint);
223 $this->assertNoViolation();
227 * When validatePropertyValue() is called with a class name
228 * https://github.com/symfony/symfony/pull/11498.
230 public function testFailingExpressionAtPropertyLevelWithoutRoot()
232 $constraint = new Expression(array(
233 'expression' => 'value == "1"',
234 'message' => 'myMessage',
238 $this->setPropertyPath('');
239 $this->setProperty(null, 'property');
241 $this->validator->validate('2', $constraint);
243 $this->buildViolation('myMessage')
245 ->setParameter('{{ value }}', '"2"')
246 ->setCode(Expression::EXPRESSION_FAILED_ERROR)
250 public function testExpressionLanguageUsage()
252 $constraint = new Expression(array(
253 'expression' => 'false',
256 $expressionLanguage = $this->getMockBuilder('Symfony\Component\ExpressionLanguage\ExpressionLanguage')->getMock();
260 $expressionLanguage->method('evaluate')
261 ->will($this->returnCallback(function () use (&$used) {
267 $validator = new ExpressionValidator(null, $expressionLanguage);
268 $validator->initialize($this->createContext());
269 $validator->validate(null, $constraint);
271 $this->assertTrue($used, 'Failed asserting that custom ExpressionLanguage instance is used.');