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\Intl\Util\IntlTestHelper;
15 use Symfony\Component\Validator\Constraints\Range;
16 use Symfony\Component\Validator\Constraints\RangeValidator;
17 use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
19 class RangeValidatorTest extends ConstraintValidatorTestCase
21 protected function createValidator()
23 return new RangeValidator();
26 public function testNullIsValid()
28 $this->validator->validate(null, new Range(array('min' => 10, 'max' => 20)));
30 $this->assertNoViolation();
33 public function getTenToTwenty()
47 public function getLessThanTen()
50 array(9.99999, '9.99999'),
51 array('9.99999', '"9.99999"'),
57 public function getMoreThanTwenty()
60 array(20.000001, '20.000001'),
61 array('20.000001', '"20.000001"'),
68 * @dataProvider getTenToTwenty
70 public function testValidValuesMin($value)
72 $constraint = new Range(array('min' => 10));
73 $this->validator->validate($value, $constraint);
75 $this->assertNoViolation();
79 * @dataProvider getTenToTwenty
81 public function testValidValuesMax($value)
83 $constraint = new Range(array('max' => 20));
84 $this->validator->validate($value, $constraint);
86 $this->assertNoViolation();
90 * @dataProvider getTenToTwenty
92 public function testValidValuesMinMax($value)
94 $constraint = new Range(array('min' => 10, 'max' => 20));
95 $this->validator->validate($value, $constraint);
97 $this->assertNoViolation();
101 * @dataProvider getLessThanTen
103 public function testInvalidValuesMin($value, $formattedValue)
105 $constraint = new Range(array(
107 'minMessage' => 'myMessage',
110 $this->validator->validate($value, $constraint);
112 $this->buildViolation('myMessage')
113 ->setParameter('{{ value }}', $formattedValue)
114 ->setParameter('{{ limit }}', 10)
115 ->setCode(Range::TOO_LOW_ERROR)
120 * @dataProvider getMoreThanTwenty
122 public function testInvalidValuesMax($value, $formattedValue)
124 $constraint = new Range(array(
126 'maxMessage' => 'myMessage',
129 $this->validator->validate($value, $constraint);
131 $this->buildViolation('myMessage')
132 ->setParameter('{{ value }}', $formattedValue)
133 ->setParameter('{{ limit }}', 20)
134 ->setCode(Range::TOO_HIGH_ERROR)
139 * @dataProvider getMoreThanTwenty
141 public function testInvalidValuesCombinedMax($value, $formattedValue)
143 $constraint = new Range(array(
146 'minMessage' => 'myMinMessage',
147 'maxMessage' => 'myMaxMessage',
150 $this->validator->validate($value, $constraint);
152 $this->buildViolation('myMaxMessage')
153 ->setParameter('{{ value }}', $formattedValue)
154 ->setParameter('{{ limit }}', 20)
155 ->setCode(Range::TOO_HIGH_ERROR)
160 * @dataProvider getLessThanTen
162 public function testInvalidValuesCombinedMin($value, $formattedValue)
164 $constraint = new Range(array(
167 'minMessage' => 'myMinMessage',
168 'maxMessage' => 'myMaxMessage',
171 $this->validator->validate($value, $constraint);
173 $this->buildViolation('myMinMessage')
174 ->setParameter('{{ value }}', $formattedValue)
175 ->setParameter('{{ limit }}', 10)
176 ->setCode(Range::TOO_LOW_ERROR)
180 public function getTenthToTwentiethMarch2014()
182 // The provider runs before setUp(), so we need to manually fix
183 // the default timezone
184 $this->setDefaultTimezone('UTC');
187 array(new \DateTime('March 10, 2014')),
188 array(new \DateTime('March 15, 2014')),
189 array(new \DateTime('March 20, 2014')),
192 $tests[] = array(new \DateTimeImmutable('March 10, 2014'));
193 $tests[] = array(new \DateTimeImmutable('March 15, 2014'));
194 $tests[] = array(new \DateTimeImmutable('March 20, 2014'));
196 $this->restoreDefaultTimezone();
201 public function getSoonerThanTenthMarch2014()
203 // The provider runs before setUp(), so we need to manually fix
204 // the default timezone
205 $this->setDefaultTimezone('UTC');
208 array(new \DateTime('March 20, 2013'), 'Mar 20, 2013, 12:00 AM'),
209 array(new \DateTime('March 9, 2014'), 'Mar 9, 2014, 12:00 AM'),
212 $tests[] = array(new \DateTimeImmutable('March 20, 2013'), 'Mar 20, 2013, 12:00 AM');
213 $tests[] = array(new \DateTimeImmutable('March 9, 2014'), 'Mar 9, 2014, 12:00 AM');
215 $this->restoreDefaultTimezone();
220 public function getLaterThanTwentiethMarch2014()
222 // The provider runs before setUp(), so we need to manually fix
223 // the default timezone
224 $this->setDefaultTimezone('UTC');
227 array(new \DateTime('March 21, 2014'), 'Mar 21, 2014, 12:00 AM'),
228 array(new \DateTime('March 9, 2015'), 'Mar 9, 2015, 12:00 AM'),
231 $tests[] = array(new \DateTimeImmutable('March 21, 2014'), 'Mar 21, 2014, 12:00 AM');
232 $tests[] = array(new \DateTimeImmutable('March 9, 2015'), 'Mar 9, 2015, 12:00 AM');
234 $this->restoreDefaultTimezone();
240 * @dataProvider getTenthToTwentiethMarch2014
242 public function testValidDatesMin($value)
244 $constraint = new Range(array('min' => 'March 10, 2014'));
245 $this->validator->validate($value, $constraint);
247 $this->assertNoViolation();
251 * @dataProvider getTenthToTwentiethMarch2014
253 public function testValidDatesMax($value)
255 $constraint = new Range(array('max' => 'March 20, 2014'));
256 $this->validator->validate($value, $constraint);
258 $this->assertNoViolation();
262 * @dataProvider getTenthToTwentiethMarch2014
264 public function testValidDatesMinMax($value)
266 $constraint = new Range(array('min' => 'March 10, 2014', 'max' => 'March 20, 2014'));
267 $this->validator->validate($value, $constraint);
269 $this->assertNoViolation();
273 * @dataProvider getSoonerThanTenthMarch2014
275 public function testInvalidDatesMin($value, $dateTimeAsString)
277 // Conversion of dates to string differs between ICU versions
278 // Make sure we have the correct version loaded
279 IntlTestHelper::requireIntl($this, '57.1');
281 $constraint = new Range(array(
282 'min' => 'March 10, 2014',
283 'minMessage' => 'myMessage',
286 $this->validator->validate($value, $constraint);
288 $this->buildViolation('myMessage')
289 ->setParameter('{{ value }}', $dateTimeAsString)
290 ->setParameter('{{ limit }}', 'Mar 10, 2014, 12:00 AM')
291 ->setCode(Range::TOO_LOW_ERROR)
296 * @dataProvider getLaterThanTwentiethMarch2014
298 public function testInvalidDatesMax($value, $dateTimeAsString)
300 // Conversion of dates to string differs between ICU versions
301 // Make sure we have the correct version loaded
302 IntlTestHelper::requireIntl($this, '57.1');
304 $constraint = new Range(array(
305 'max' => 'March 20, 2014',
306 'maxMessage' => 'myMessage',
309 $this->validator->validate($value, $constraint);
311 $this->buildViolation('myMessage')
312 ->setParameter('{{ value }}', $dateTimeAsString)
313 ->setParameter('{{ limit }}', 'Mar 20, 2014, 12:00 AM')
314 ->setCode(Range::TOO_HIGH_ERROR)
319 * @dataProvider getLaterThanTwentiethMarch2014
321 public function testInvalidDatesCombinedMax($value, $dateTimeAsString)
323 // Conversion of dates to string differs between ICU versions
324 // Make sure we have the correct version loaded
325 IntlTestHelper::requireIntl($this, '57.1');
327 $constraint = new Range(array(
328 'min' => 'March 10, 2014',
329 'max' => 'March 20, 2014',
330 'minMessage' => 'myMinMessage',
331 'maxMessage' => 'myMaxMessage',
334 $this->validator->validate($value, $constraint);
336 $this->buildViolation('myMaxMessage')
337 ->setParameter('{{ value }}', $dateTimeAsString)
338 ->setParameter('{{ limit }}', 'Mar 20, 2014, 12:00 AM')
339 ->setCode(Range::TOO_HIGH_ERROR)
344 * @dataProvider getSoonerThanTenthMarch2014
346 public function testInvalidDatesCombinedMin($value, $dateTimeAsString)
348 // Conversion of dates to string differs between ICU versions
349 // Make sure we have the correct version loaded
350 IntlTestHelper::requireIntl($this, '57.1');
352 $constraint = new Range(array(
353 'min' => 'March 10, 2014',
354 'max' => 'March 20, 2014',
355 'minMessage' => 'myMinMessage',
356 'maxMessage' => 'myMaxMessage',
359 $this->validator->validate($value, $constraint);
361 $this->buildViolation('myMinMessage')
362 ->setParameter('{{ value }}', $dateTimeAsString)
363 ->setParameter('{{ limit }}', 'Mar 10, 2014, 12:00 AM')
364 ->setCode(Range::TOO_LOW_ERROR)
368 public function getInvalidValues()
375 array(new \stdClass()),
379 public function testNonNumeric()
381 $this->validator->validate('abcd', new Range(array(
384 'invalidMessage' => 'myMessage',
387 $this->buildViolation('myMessage')
388 ->setParameter('{{ value }}', '"abcd"')
389 ->setCode(Range::INVALID_CHARACTERS_ERROR)