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\Validation;
18 function choice_callback()
20 return array('foo', 'bar');
23 class ChoiceValidatorTest extends AbstractConstraintValidatorTest
25 protected function getApiVersion()
27 return Validation::API_VERSION_2_5;
30 protected function createValidator()
32 return new ChoiceValidator();
35 public static function staticCallback()
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'),
50 $this->validator->validate('asdf', $constraint);
53 public function testNullIsValid()
55 $this->validator->validate(null, new Choice(array('choices' => array('foo', 'bar'))));
57 $this->assertNoViolation();
61 * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
63 public function testChoicesOrCallbackExpected()
65 $this->validator->validate('foobar', new Choice());
69 * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
71 public function testValidCallbackExpected()
73 $this->validator->validate('foobar', new Choice(array('callback' => 'abcd')));
76 public function testValidChoiceArray()
78 $constraint = new Choice(array('choices' => array('foo', 'bar')));
80 $this->validator->validate('bar', $constraint);
82 $this->assertNoViolation();
85 public function testValidChoiceCallbackFunction()
87 $constraint = new Choice(array('callback' => __NAMESPACE__.'\choice_callback'));
89 $this->validator->validate('bar', $constraint);
91 $this->assertNoViolation();
94 public function testValidChoiceCallbackClosure()
96 $constraint = new Choice(array('callback' => function () {
97 return array('foo', 'bar');
100 $this->validator->validate('bar', $constraint);
102 $this->assertNoViolation();
105 public function testValidChoiceCallbackStaticMethod()
107 $constraint = new Choice(array('callback' => array(__CLASS__, 'staticCallback')));
109 $this->validator->validate('bar', $constraint);
111 $this->assertNoViolation();
114 public function testValidChoiceCallbackContextMethod()
116 // search $this for "staticCallback"
117 $this->setObject($this);
119 $constraint = new Choice(array('callback' => 'staticCallback'));
121 $this->validator->validate('bar', $constraint);
123 $this->assertNoViolation();
126 public function testMultipleChoices()
128 $constraint = new Choice(array(
129 'choices' => array('foo', 'bar', 'baz'),
133 $this->validator->validate(array('baz', 'bar'), $constraint);
135 $this->assertNoViolation();
138 public function testInvalidChoice()
140 $constraint = new Choice(array(
141 'choices' => array('foo', 'bar'),
142 'message' => 'myMessage',
145 $this->validator->validate('baz', $constraint);
147 $this->buildViolation('myMessage')
148 ->setParameter('{{ value }}', '"baz"')
149 ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
153 public function testInvalidChoiceEmptyChoices()
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',
162 $this->validator->validate('baz', $constraint);
164 $this->buildViolation('myMessage')
165 ->setParameter('{{ value }}', '"baz"')
166 ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
170 public function testInvalidChoiceMultiple()
172 $constraint = new Choice(array(
173 'choices' => array('foo', 'bar'),
174 'multipleMessage' => 'myMessage',
178 $this->validator->validate(array('foo', 'baz'), $constraint);
180 $this->buildViolation('myMessage')
181 ->setParameter('{{ value }}', '"baz"')
182 ->setInvalidValue('baz')
183 ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
187 public function testTooFewChoices()
189 $constraint = new Choice(array(
190 'choices' => array('foo', 'bar', 'moo', 'maa'),
193 'minMessage' => 'myMessage',
196 $value = array('foo');
198 $this->setValue($value);
200 $this->validator->validate($value, $constraint);
202 $this->buildViolation('myMessage')
203 ->setParameter('{{ limit }}', 2)
204 ->setInvalidValue($value)
206 ->setCode(Choice::TOO_FEW_ERROR)
210 public function testTooManyChoices()
212 $constraint = new Choice(array(
213 'choices' => array('foo', 'bar', 'moo', 'maa'),
216 'maxMessage' => 'myMessage',
219 $value = array('foo', 'bar', 'moo');
221 $this->setValue($value);
223 $this->validator->validate($value, $constraint);
225 $this->buildViolation('myMessage')
226 ->setParameter('{{ limit }}', 2)
227 ->setInvalidValue($value)
229 ->setCode(Choice::TOO_MANY_ERROR)
233 public function testNonStrict()
235 $constraint = new Choice(array(
236 'choices' => array(1, 2),
240 $this->validator->validate('2', $constraint);
241 $this->validator->validate(2, $constraint);
243 $this->assertNoViolation();
246 public function testStrictAllowsExactValue()
248 $constraint = new Choice(array(
249 'choices' => array(1, 2),
253 $this->validator->validate(2, $constraint);
255 $this->assertNoViolation();
258 public function testStrictDisallowsDifferentType()
260 $constraint = new Choice(array(
261 'choices' => array(1, 2),
263 'message' => 'myMessage',
266 $this->validator->validate('2', $constraint);
268 $this->buildViolation('myMessage')
269 ->setParameter('{{ value }}', '"2"')
270 ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
274 public function testNonStrictWithMultipleChoices()
276 $constraint = new Choice(array(
277 'choices' => array(1, 2, 3),
282 $this->validator->validate(array('2', 3), $constraint);
284 $this->assertNoViolation();
287 public function testStrictWithMultipleChoices()
289 $constraint = new Choice(array(
290 'choices' => array(1, 2, 3),
293 'multipleMessage' => 'myMessage',
296 $this->validator->validate(array(2, '3'), $constraint);
298 $this->buildViolation('myMessage')
299 ->setParameter('{{ value }}', '"3"')
300 ->setInvalidValue('3')
301 ->setCode(Choice::NO_SUCH_CHOICE_ERROR)