01483161c9fc486117829bb8b2153dd55640188e
[yaffs-website] / vendor / symfony / validator / Tests / Constraints / ChoiceValidatorTest.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\Choice;
15 use Symfony\Component\Validator\Constraints\ChoiceValidator;
16 use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
17
18 function choice_callback()
19 {
20     return array('foo', 'bar');
21 }
22
23 class ChoiceValidatorTest extends ConstraintValidatorTestCase
24 {
25     protected function createValidator()
26     {
27         return new ChoiceValidator();
28     }
29
30     public static function staticCallback()
31     {
32         return array('foo', 'bar');
33     }
34
35     public function objectMethodCallback()
36     {
37         return array('foo', 'bar');
38     }
39
40     /**
41      * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
42      */
43     public function testExpectArrayIfMultipleIsTrue()
44     {
45         $constraint = new Choice(array(
46             'choices' => array('foo', 'bar'),
47             'multiple' => true,
48             'strict' => true,
49         ));
50
51         $this->validator->validate('asdf', $constraint);
52     }
53
54     public function testNullIsValid()
55     {
56         $this->validator->validate(
57             null,
58             new Choice(
59                 array(
60                     'choices' => array('foo', 'bar'),
61                     'strict' => true,
62                 )
63             )
64         );
65
66         $this->assertNoViolation();
67     }
68
69     /**
70      * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
71      */
72     public function testChoicesOrCallbackExpected()
73     {
74         $this->validator->validate('foobar', new Choice(array('strict' => true)));
75     }
76
77     /**
78      * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
79      */
80     public function testValidCallbackExpected()
81     {
82         $this->validator->validate('foobar', new Choice(array('callback' => 'abcd', 'strict' => true)));
83     }
84
85     public function testValidChoiceArray()
86     {
87         $constraint = new Choice(array('choices' => array('foo', 'bar'), 'strict' => true));
88
89         $this->validator->validate('bar', $constraint);
90
91         $this->assertNoViolation();
92     }
93
94     public function testValidChoiceCallbackFunction()
95     {
96         $constraint = new Choice(array('callback' => __NAMESPACE__.'\choice_callback', 'strict' => true));
97
98         $this->validator->validate('bar', $constraint);
99
100         $this->assertNoViolation();
101     }
102
103     public function testValidChoiceCallbackClosure()
104     {
105         $constraint = new Choice(
106             array(
107                 'strict' => true,
108                 'callback' => function () {
109                     return array('foo', 'bar');
110                 },
111             )
112         );
113
114         $this->validator->validate('bar', $constraint);
115
116         $this->assertNoViolation();
117     }
118
119     public function testValidChoiceCallbackStaticMethod()
120     {
121         $constraint = new Choice(array('callback' => array(__CLASS__, 'staticCallback'), 'strict' => true));
122
123         $this->validator->validate('bar', $constraint);
124
125         $this->assertNoViolation();
126     }
127
128     public function testValidChoiceCallbackContextMethod()
129     {
130         // search $this for "staticCallback"
131         $this->setObject($this);
132
133         $constraint = new Choice(array('callback' => 'staticCallback', 'strict' => true));
134
135         $this->validator->validate('bar', $constraint);
136
137         $this->assertNoViolation();
138     }
139
140     public function testValidChoiceCallbackContextObjectMethod()
141     {
142         // search $this for "objectMethodCallback"
143         $this->setObject($this);
144
145         $constraint = new Choice(array('callback' => 'objectMethodCallback', 'strict' => true));
146
147         $this->validator->validate('bar', $constraint);
148
149         $this->assertNoViolation();
150     }
151
152     public function testMultipleChoices()
153     {
154         $constraint = new Choice(array(
155             'choices' => array('foo', 'bar', 'baz'),
156             'multiple' => true,
157             'strict' => true,
158         ));
159
160         $this->validator->validate(array('baz', 'bar'), $constraint);
161
162         $this->assertNoViolation();
163     }
164
165     public function testInvalidChoice()
166     {
167         $constraint = new Choice(array(
168             'choices' => array('foo', 'bar'),
169             'message' => 'myMessage',
170             'strict' => true,
171         ));
172
173         $this->validator->validate('baz', $constraint);
174
175         $this->buildViolation('myMessage')
176             ->setParameter('{{ value }}', '"baz"')
177             ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
178             ->assertRaised();
179     }
180
181     public function testInvalidChoiceEmptyChoices()
182     {
183         $constraint = new Choice(array(
184             // May happen when the choices are provided dynamically, e.g. from
185             // the DB or the model
186             'choices' => array(),
187             'message' => 'myMessage',
188             'strict' => true,
189         ));
190
191         $this->validator->validate('baz', $constraint);
192
193         $this->buildViolation('myMessage')
194             ->setParameter('{{ value }}', '"baz"')
195             ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
196             ->assertRaised();
197     }
198
199     public function testInvalidChoiceMultiple()
200     {
201         $constraint = new Choice(array(
202             'choices' => array('foo', 'bar'),
203             'multipleMessage' => 'myMessage',
204             'multiple' => true,
205             'strict' => true,
206         ));
207
208         $this->validator->validate(array('foo', 'baz'), $constraint);
209
210         $this->buildViolation('myMessage')
211             ->setParameter('{{ value }}', '"baz"')
212             ->setInvalidValue('baz')
213             ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
214             ->assertRaised();
215     }
216
217     public function testTooFewChoices()
218     {
219         $constraint = new Choice(array(
220             'choices' => array('foo', 'bar', 'moo', 'maa'),
221             'multiple' => true,
222             'min' => 2,
223             'minMessage' => 'myMessage',
224             'strict' => true,
225         ));
226
227         $value = array('foo');
228
229         $this->setValue($value);
230
231         $this->validator->validate($value, $constraint);
232
233         $this->buildViolation('myMessage')
234             ->setParameter('{{ limit }}', 2)
235             ->setInvalidValue($value)
236             ->setPlural(2)
237             ->setCode(Choice::TOO_FEW_ERROR)
238             ->assertRaised();
239     }
240
241     public function testTooManyChoices()
242     {
243         $constraint = new Choice(array(
244             'choices' => array('foo', 'bar', 'moo', 'maa'),
245             'multiple' => true,
246             'max' => 2,
247             'maxMessage' => 'myMessage',
248             'strict' => true,
249         ));
250
251         $value = array('foo', 'bar', 'moo');
252
253         $this->setValue($value);
254
255         $this->validator->validate($value, $constraint);
256
257         $this->buildViolation('myMessage')
258             ->setParameter('{{ limit }}', 2)
259             ->setInvalidValue($value)
260             ->setPlural(2)
261             ->setCode(Choice::TOO_MANY_ERROR)
262             ->assertRaised();
263     }
264
265     /**
266      * @group legacy
267      */
268     public function testNonStrict()
269     {
270         $constraint = new Choice(array(
271             'choices' => array(1, 2),
272             'strict' => false,
273         ));
274
275         $this->validator->validate('2', $constraint);
276         $this->validator->validate(2, $constraint);
277
278         $this->assertNoViolation();
279     }
280
281     public function testStrictAllowsExactValue()
282     {
283         $constraint = new Choice(array(
284             'choices' => array(1, 2),
285             'strict' => true,
286         ));
287
288         $this->validator->validate(2, $constraint);
289
290         $this->assertNoViolation();
291     }
292
293     public function testStrictDisallowsDifferentType()
294     {
295         $constraint = new Choice(array(
296             'choices' => array(1, 2),
297             'strict' => true,
298             'message' => 'myMessage',
299         ));
300
301         $this->validator->validate('2', $constraint);
302
303         $this->buildViolation('myMessage')
304             ->setParameter('{{ value }}', '"2"')
305             ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
306             ->assertRaised();
307     }
308
309     /**
310      * @group legacy
311      */
312     public function testNonStrictWithMultipleChoices()
313     {
314         $constraint = new Choice(array(
315             'choices' => array(1, 2, 3),
316             'multiple' => true,
317             'strict' => false,
318         ));
319
320         $this->validator->validate(array('2', 3), $constraint);
321
322         $this->assertNoViolation();
323     }
324
325     public function testStrictWithMultipleChoices()
326     {
327         $constraint = new Choice(array(
328             'choices' => array(1, 2, 3),
329             'multiple' => true,
330             'strict' => true,
331             'multipleMessage' => 'myMessage',
332         ));
333
334         $this->validator->validate(array(2, '3'), $constraint);
335
336         $this->buildViolation('myMessage')
337             ->setParameter('{{ value }}', '"3"')
338             ->setInvalidValue('3')
339             ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
340             ->assertRaised();
341     }
342 }