e4316243e558081930ad4f1c200e961f672a413b
[yaffs-website] / vendor / symfony / validator / Tests / Constraints / ExpressionValidatorTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Validator\Tests\Constraints;
13
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
19 class ExpressionValidatorTest extends ConstraintValidatorTestCase
20 {
21     protected function createValidator()
22     {
23         return new ExpressionValidator();
24     }
25
26     public function testExpressionIsEvaluatedWithNullValue()
27     {
28         $constraint = new Expression(array(
29             'expression' => 'false',
30             'message' => 'myMessage',
31         ));
32
33         $this->validator->validate(null, $constraint);
34
35         $this->buildViolation('myMessage')
36             ->setParameter('{{ value }}', 'null')
37             ->setCode(Expression::EXPRESSION_FAILED_ERROR)
38             ->assertRaised();
39     }
40
41     public function testExpressionIsEvaluatedWithEmptyStringValue()
42     {
43         $constraint = new Expression(array(
44             'expression' => 'false',
45             'message' => 'myMessage',
46         ));
47
48         $this->validator->validate('', $constraint);
49
50         $this->buildViolation('myMessage')
51             ->setParameter('{{ value }}', '""')
52             ->setCode(Expression::EXPRESSION_FAILED_ERROR)
53             ->assertRaised();
54     }
55
56     public function testSucceedingExpressionAtObjectLevel()
57     {
58         $constraint = new Expression('this.data == 1');
59
60         $object = new Entity();
61         $object->data = '1';
62
63         $this->setObject($object);
64
65         $this->validator->validate($object, $constraint);
66
67         $this->assertNoViolation();
68     }
69
70     public function testFailingExpressionAtObjectLevel()
71     {
72         $constraint = new Expression(array(
73             'expression' => 'this.data == 1',
74             'message' => 'myMessage',
75         ));
76
77         $object = new Entity();
78         $object->data = '2';
79
80         $this->setObject($object);
81
82         $this->validator->validate($object, $constraint);
83
84         $this->buildViolation('myMessage')
85             ->setParameter('{{ value }}', 'object')
86             ->setCode(Expression::EXPRESSION_FAILED_ERROR)
87             ->assertRaised();
88     }
89
90     public function testSucceedingExpressionAtPropertyLevel()
91     {
92         $constraint = new Expression('value == this.data');
93
94         $object = new Entity();
95         $object->data = '1';
96
97         $this->setRoot($object);
98         $this->setPropertyPath('data');
99         $this->setProperty($object, 'data');
100
101         $this->validator->validate('1', $constraint);
102
103         $this->assertNoViolation();
104     }
105
106     public function testFailingExpressionAtPropertyLevel()
107     {
108         $constraint = new Expression(array(
109             'expression' => 'value == this.data',
110             'message' => 'myMessage',
111         ));
112
113         $object = new Entity();
114         $object->data = '1';
115
116         $this->setRoot($object);
117         $this->setPropertyPath('data');
118         $this->setProperty($object, 'data');
119
120         $this->validator->validate('2', $constraint);
121
122         $this->buildViolation('myMessage')
123             ->atPath('data')
124             ->setParameter('{{ value }}', '"2"')
125             ->setCode(Expression::EXPRESSION_FAILED_ERROR)
126             ->assertRaised();
127     }
128
129     public function testSucceedingExpressionAtNestedPropertyLevel()
130     {
131         $constraint = new Expression('value == this.data');
132
133         $object = new Entity();
134         $object->data = '1';
135
136         $root = new Entity();
137         $root->reference = $object;
138
139         $this->setRoot($root);
140         $this->setPropertyPath('reference.data');
141         $this->setProperty($object, 'data');
142
143         $this->validator->validate('1', $constraint);
144
145         $this->assertNoViolation();
146     }
147
148     public function testFailingExpressionAtNestedPropertyLevel()
149     {
150         $constraint = new Expression(array(
151             'expression' => 'value == this.data',
152             'message' => 'myMessage',
153         ));
154
155         $object = new Entity();
156         $object->data = '1';
157
158         $root = new Entity();
159         $root->reference = $object;
160
161         $this->setRoot($root);
162         $this->setPropertyPath('reference.data');
163         $this->setProperty($object, 'data');
164
165         $this->validator->validate('2', $constraint);
166
167         $this->buildViolation('myMessage')
168             ->atPath('reference.data')
169             ->setParameter('{{ value }}', '"2"')
170             ->setCode(Expression::EXPRESSION_FAILED_ERROR)
171             ->assertRaised();
172     }
173
174     /**
175      * When validatePropertyValue() is called with a class name
176      * https://github.com/symfony/symfony/pull/11498.
177      */
178     public function testSucceedingExpressionAtPropertyLevelWithoutRoot()
179     {
180         $constraint = new Expression('value == "1"');
181
182         $this->setRoot('1');
183         $this->setPropertyPath('');
184         $this->setProperty(null, 'property');
185
186         $this->validator->validate('1', $constraint);
187
188         $this->assertNoViolation();
189     }
190
191     /**
192      * When validatePropertyValue() is called with a class name
193      * https://github.com/symfony/symfony/pull/11498.
194      */
195     public function testFailingExpressionAtPropertyLevelWithoutRoot()
196     {
197         $constraint = new Expression(array(
198             'expression' => 'value == "1"',
199             'message' => 'myMessage',
200         ));
201
202         $this->setRoot('2');
203         $this->setPropertyPath('');
204         $this->setProperty(null, 'property');
205
206         $this->validator->validate('2', $constraint);
207
208         $this->buildViolation('myMessage')
209             ->atPath('')
210             ->setParameter('{{ value }}', '"2"')
211             ->setCode(Expression::EXPRESSION_FAILED_ERROR)
212             ->assertRaised();
213     }
214
215     public function testExpressionLanguageUsage()
216     {
217         $constraint = new Expression(array(
218             'expression' => 'false',
219         ));
220
221         $expressionLanguage = $this->getMockBuilder('Symfony\Component\ExpressionLanguage\ExpressionLanguage')->getMock();
222
223         $used = false;
224
225         $expressionLanguage->method('evaluate')
226             ->will($this->returnCallback(function () use (&$used) {
227                 $used = true;
228
229                 return true;
230             }));
231
232         $validator = new ExpressionValidator(null, $expressionLanguage);
233         $validator->initialize($this->createContext());
234         $validator->validate(null, $constraint);
235
236         $this->assertTrue($used, 'Failed asserting that custom ExpressionLanguage instance is used.');
237     }
238 }