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\Validation;
18 class LengthValidatorTest extends AbstractConstraintValidatorTest
20 protected function getApiVersion()
22 return Validation::API_VERSION_2_5;
25 protected function createValidator()
27 return new LengthValidator();
30 public function testNullIsValid()
32 $this->validator->validate(null, new Length(6));
34 $this->assertNoViolation();
37 public function testEmptyStringIsValid()
39 $this->validator->validate('', new Length(6));
41 $this->assertNoViolation();
45 * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
47 public function testExpectsStringCompatibleType()
49 $this->validator->validate(new \stdClass(), new Length(5));
52 public function getThreeOrLessCharacters()
66 public function getFourCharacters()
76 public function getFiveOrMoreCharacters()
90 public function getOneCharset()
93 array('é', 'utf8', true),
94 array("\xE9", 'CP1252', true),
95 array("\xE9", 'XXX', false),
96 array("\xE9", 'utf8', false),
101 * @dataProvider getFiveOrMoreCharacters
103 public function testValidValuesMin($value)
105 $constraint = new Length(array('min' => 5));
106 $this->validator->validate($value, $constraint);
108 $this->assertNoViolation();
112 * @dataProvider getThreeOrLessCharacters
114 public function testValidValuesMax($value)
116 $constraint = new Length(array('max' => 3));
117 $this->validator->validate($value, $constraint);
119 $this->assertNoViolation();
123 * @dataProvider getFourCharacters
125 public function testValidValuesExact($value)
127 $constraint = new Length(4);
128 $this->validator->validate($value, $constraint);
130 $this->assertNoViolation();
134 * @dataProvider getThreeOrLessCharacters
136 public function testInvalidValuesMin($value)
138 $constraint = new Length(array(
140 'minMessage' => 'myMessage',
143 $this->validator->validate($value, $constraint);
145 $this->buildViolation('myMessage')
146 ->setParameter('{{ value }}', '"'.$value.'"')
147 ->setParameter('{{ limit }}', 4)
148 ->setInvalidValue($value)
150 ->setCode(Length::TOO_SHORT_ERROR)
155 * @dataProvider getFiveOrMoreCharacters
157 public function testInvalidValuesMax($value)
159 $constraint = new Length(array(
161 'maxMessage' => 'myMessage',
164 $this->validator->validate($value, $constraint);
166 $this->buildViolation('myMessage')
167 ->setParameter('{{ value }}', '"'.$value.'"')
168 ->setParameter('{{ limit }}', 4)
169 ->setInvalidValue($value)
171 ->setCode(Length::TOO_LONG_ERROR)
176 * @dataProvider getThreeOrLessCharacters
178 public function testInvalidValuesExactLessThanFour($value)
180 $constraint = new Length(array(
183 'exactMessage' => 'myMessage',
186 $this->validator->validate($value, $constraint);
188 $this->buildViolation('myMessage')
189 ->setParameter('{{ value }}', '"'.$value.'"')
190 ->setParameter('{{ limit }}', 4)
191 ->setInvalidValue($value)
193 ->setCode(Length::TOO_SHORT_ERROR)
198 * @dataProvider getFiveOrMoreCharacters
200 public function testInvalidValuesExactMoreThanFour($value)
202 $constraint = new Length(array(
205 'exactMessage' => 'myMessage',
208 $this->validator->validate($value, $constraint);
210 $this->buildViolation('myMessage')
211 ->setParameter('{{ value }}', '"'.$value.'"')
212 ->setParameter('{{ limit }}', 4)
213 ->setInvalidValue($value)
215 ->setCode(Length::TOO_LONG_ERROR)
220 * @dataProvider getOneCharset
222 public function testOneCharset($value, $charset, $isValid)
224 $constraint = new Length(array(
227 'charset' => $charset,
228 'charsetMessage' => 'myMessage',
231 $this->validator->validate($value, $constraint);
234 $this->assertNoViolation();
236 $this->buildViolation('myMessage')
237 ->setParameter('{{ value }}', '"'.$value.'"')
238 ->setParameter('{{ charset }}', $charset)
239 ->setInvalidValue($value)
240 ->setCode(Length::INVALID_CHARACTERS_ERROR)
245 public function testConstraintGetDefaultOption()
247 $constraint = new Length(5);
249 $this->assertEquals(5, $constraint->min);
250 $this->assertEquals(5, $constraint->max);