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