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\Length;
15 use Symfony\Component\Validator\Constraints\LengthValidator;
16 use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
18 class LengthValidatorTest extends ConstraintValidatorTestCase
20 protected function createValidator()
22 return new LengthValidator();
25 public function testNullIsValid()
27 $this->validator->validate(null, new Length(6));
29 $this->assertNoViolation();
32 public function testEmptyStringIsValid()
34 $this->validator->validate('', new Length(6));
36 $this->assertNoViolation();
40 * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
42 public function testExpectsStringCompatibleType()
44 $this->validator->validate(new \stdClass(), new Length(5));
47 public function getThreeOrLessCharacters()
61 public function getFourCharacters()
71 public function getFiveOrMoreCharacters()
85 public function getOneCharset()
88 array('é', 'utf8', true),
89 array("\xE9", 'CP1252', true),
90 array("\xE9", 'XXX', false),
91 array("\xE9", 'utf8', false),
96 * @dataProvider getFiveOrMoreCharacters
98 public function testValidValuesMin($value)
100 $constraint = new Length(array('min' => 5));
101 $this->validator->validate($value, $constraint);
103 $this->assertNoViolation();
107 * @dataProvider getThreeOrLessCharacters
109 public function testValidValuesMax($value)
111 $constraint = new Length(array('max' => 3));
112 $this->validator->validate($value, $constraint);
114 $this->assertNoViolation();
118 * @dataProvider getFourCharacters
120 public function testValidValuesExact($value)
122 $constraint = new Length(4);
123 $this->validator->validate($value, $constraint);
125 $this->assertNoViolation();
129 * @dataProvider getThreeOrLessCharacters
131 public function testInvalidValuesMin($value)
133 $constraint = new Length(array(
135 'minMessage' => 'myMessage',
138 $this->validator->validate($value, $constraint);
140 $this->buildViolation('myMessage')
141 ->setParameter('{{ value }}', '"'.$value.'"')
142 ->setParameter('{{ limit }}', 4)
143 ->setInvalidValue($value)
145 ->setCode(Length::TOO_SHORT_ERROR)
150 * @dataProvider getFiveOrMoreCharacters
152 public function testInvalidValuesMax($value)
154 $constraint = new Length(array(
156 'maxMessage' => 'myMessage',
159 $this->validator->validate($value, $constraint);
161 $this->buildViolation('myMessage')
162 ->setParameter('{{ value }}', '"'.$value.'"')
163 ->setParameter('{{ limit }}', 4)
164 ->setInvalidValue($value)
166 ->setCode(Length::TOO_LONG_ERROR)
171 * @dataProvider getThreeOrLessCharacters
173 public function testInvalidValuesExactLessThanFour($value)
175 $constraint = new Length(array(
178 'exactMessage' => 'myMessage',
181 $this->validator->validate($value, $constraint);
183 $this->buildViolation('myMessage')
184 ->setParameter('{{ value }}', '"'.$value.'"')
185 ->setParameter('{{ limit }}', 4)
186 ->setInvalidValue($value)
188 ->setCode(Length::TOO_SHORT_ERROR)
193 * @dataProvider getFiveOrMoreCharacters
195 public function testInvalidValuesExactMoreThanFour($value)
197 $constraint = new Length(array(
200 'exactMessage' => 'myMessage',
203 $this->validator->validate($value, $constraint);
205 $this->buildViolation('myMessage')
206 ->setParameter('{{ value }}', '"'.$value.'"')
207 ->setParameter('{{ limit }}', 4)
208 ->setInvalidValue($value)
210 ->setCode(Length::TOO_LONG_ERROR)
215 * @dataProvider getOneCharset
217 public function testOneCharset($value, $charset, $isValid)
219 $constraint = new Length(array(
222 'charset' => $charset,
223 'charsetMessage' => 'myMessage',
226 $this->validator->validate($value, $constraint);
229 $this->assertNoViolation();
231 $this->buildViolation('myMessage')
232 ->setParameter('{{ value }}', '"'.$value.'"')
233 ->setParameter('{{ charset }}', $charset)
234 ->setInvalidValue($value)
235 ->setCode(Length::INVALID_CHARACTERS_ERROR)
240 public function testConstraintGetDefaultOption()
242 $constraint = new Length(5);
244 $this->assertEquals(5, $constraint->min);
245 $this->assertEquals(5, $constraint->max);