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\Choice;
15 use Symfony\Component\Validator\Constraints\ChoiceValidator;
16 use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
18 function choice_callback()
20 return array('foo', 'bar');
23 class ChoiceValidatorTest extends ConstraintValidatorTestCase
25 protected function createValidator()
27 return new ChoiceValidator();
30 public static function staticCallback()
32 return array('foo', 'bar');
35 public function objectMethodCallback()
37 return array('foo', 'bar');
41 * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
43 public function testExpectArrayIfMultipleIsTrue()
45 $constraint = new Choice(array(
46 'choices' => array('foo', 'bar'),
51 $this->validator->validate('asdf', $constraint);
54 public function testNullIsValid()
56 $this->validator->validate(
60 'choices' => array('foo', 'bar'),
66 $this->assertNoViolation();
70 * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
72 public function testChoicesOrCallbackExpected()
74 $this->validator->validate('foobar', new Choice(array('strict' => true)));
78 * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
80 public function testValidCallbackExpected()
82 $this->validator->validate('foobar', new Choice(array('callback' => 'abcd', 'strict' => true)));
85 public function testValidChoiceArray()
87 $constraint = new Choice(array('choices' => array('foo', 'bar'), 'strict' => true));
89 $this->validator->validate('bar', $constraint);
91 $this->assertNoViolation();
94 public function testValidChoiceCallbackFunction()
96 $constraint = new Choice(array('callback' => __NAMESPACE__.'\choice_callback', 'strict' => true));
98 $this->validator->validate('bar', $constraint);
100 $this->assertNoViolation();
103 public function testValidChoiceCallbackClosure()
105 $constraint = new Choice(
108 'callback' => function () {
109 return array('foo', 'bar');
114 $this->validator->validate('bar', $constraint);
116 $this->assertNoViolation();
119 public function testValidChoiceCallbackStaticMethod()
121 $constraint = new Choice(array('callback' => array(__CLASS__, 'staticCallback'), 'strict' => true));
123 $this->validator->validate('bar', $constraint);
125 $this->assertNoViolation();
128 public function testValidChoiceCallbackContextMethod()
130 // search $this for "staticCallback"
131 $this->setObject($this);
133 $constraint = new Choice(array('callback' => 'staticCallback', 'strict' => true));
135 $this->validator->validate('bar', $constraint);
137 $this->assertNoViolation();
140 public function testValidChoiceCallbackContextObjectMethod()
142 // search $this for "objectMethodCallback"
143 $this->setObject($this);
145 $constraint = new Choice(array('callback' => 'objectMethodCallback', 'strict' => true));
147 $this->validator->validate('bar', $constraint);
149 $this->assertNoViolation();
152 public function testMultipleChoices()
154 $constraint = new Choice(array(
155 'choices' => array('foo', 'bar', 'baz'),
160 $this->validator->validate(array('baz', 'bar'), $constraint);
162 $this->assertNoViolation();
165 public function testInvalidChoice()
167 $constraint = new Choice(array(
168 'choices' => array('foo', 'bar'),
169 'message' => 'myMessage',
173 $this->validator->validate('baz', $constraint);
175 $this->buildViolation('myMessage')
176 ->setParameter('{{ value }}', '"baz"')
177 ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
181 public function testInvalidChoiceEmptyChoices()
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',
191 $this->validator->validate('baz', $constraint);
193 $this->buildViolation('myMessage')
194 ->setParameter('{{ value }}', '"baz"')
195 ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
199 public function testInvalidChoiceMultiple()
201 $constraint = new Choice(array(
202 'choices' => array('foo', 'bar'),
203 'multipleMessage' => 'myMessage',
208 $this->validator->validate(array('foo', 'baz'), $constraint);
210 $this->buildViolation('myMessage')
211 ->setParameter('{{ value }}', '"baz"')
212 ->setInvalidValue('baz')
213 ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
217 public function testTooFewChoices()
219 $constraint = new Choice(array(
220 'choices' => array('foo', 'bar', 'moo', 'maa'),
223 'minMessage' => 'myMessage',
227 $value = array('foo');
229 $this->setValue($value);
231 $this->validator->validate($value, $constraint);
233 $this->buildViolation('myMessage')
234 ->setParameter('{{ limit }}', 2)
235 ->setInvalidValue($value)
237 ->setCode(Choice::TOO_FEW_ERROR)
241 public function testTooManyChoices()
243 $constraint = new Choice(array(
244 'choices' => array('foo', 'bar', 'moo', 'maa'),
247 'maxMessage' => 'myMessage',
251 $value = array('foo', 'bar', 'moo');
253 $this->setValue($value);
255 $this->validator->validate($value, $constraint);
257 $this->buildViolation('myMessage')
258 ->setParameter('{{ limit }}', 2)
259 ->setInvalidValue($value)
261 ->setCode(Choice::TOO_MANY_ERROR)
268 public function testNonStrict()
270 $constraint = new Choice(array(
271 'choices' => array(1, 2),
275 $this->validator->validate('2', $constraint);
276 $this->validator->validate(2, $constraint);
278 $this->assertNoViolation();
281 public function testStrictAllowsExactValue()
283 $constraint = new Choice(array(
284 'choices' => array(1, 2),
288 $this->validator->validate(2, $constraint);
290 $this->assertNoViolation();
293 public function testStrictDisallowsDifferentType()
295 $constraint = new Choice(array(
296 'choices' => array(1, 2),
298 'message' => 'myMessage',
301 $this->validator->validate('2', $constraint);
303 $this->buildViolation('myMessage')
304 ->setParameter('{{ value }}', '"2"')
305 ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
312 public function testNonStrictWithMultipleChoices()
314 $constraint = new Choice(array(
315 'choices' => array(1, 2, 3),
320 $this->validator->validate(array('2', 3), $constraint);
322 $this->assertNoViolation();
325 public function testStrictWithMultipleChoices()
327 $constraint = new Choice(array(
328 'choices' => array(1, 2, 3),
331 'multipleMessage' => 'myMessage',
334 $this->validator->validate(array(2, '3'), $constraint);
336 $this->buildViolation('myMessage')
337 ->setParameter('{{ value }}', '"3"')
338 ->setInvalidValue('3')
339 ->setCode(Choice::NO_SUCH_CHOICE_ERROR)