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