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\Validation;
19 class RangeValidatorTest extends AbstractConstraintValidatorTest
21 protected function getApiVersion()
23 return Validation::API_VERSION_2_5;
26 protected function createValidator()
28 return new RangeValidator();
31 public function testNullIsValid()
33 $this->validator->validate(null, new Range(array('min' => 10, 'max' => 20)));
35 $this->assertNoViolation();
38 public function getTenToTwenty()
52 public function getLessThanTen()
55 array(9.99999, '9.99999'),
56 array('9.99999', '"9.99999"'),
62 public function getMoreThanTwenty()
65 array(20.000001, '20.000001'),
66 array('20.000001', '"20.000001"'),
73 * @dataProvider getTenToTwenty
75 public function testValidValuesMin($value)
77 $constraint = new Range(array('min' => 10));
78 $this->validator->validate($value, $constraint);
80 $this->assertNoViolation();
84 * @dataProvider getTenToTwenty
86 public function testValidValuesMax($value)
88 $constraint = new Range(array('max' => 20));
89 $this->validator->validate($value, $constraint);
91 $this->assertNoViolation();
95 * @dataProvider getTenToTwenty
97 public function testValidValuesMinMax($value)
99 $constraint = new Range(array('min' => 10, 'max' => 20));
100 $this->validator->validate($value, $constraint);
102 $this->assertNoViolation();
106 * @dataProvider getLessThanTen
108 public function testInvalidValuesMin($value, $formattedValue)
110 $constraint = new Range(array(
112 'minMessage' => 'myMessage',
115 $this->validator->validate($value, $constraint);
117 $this->buildViolation('myMessage')
118 ->setParameter('{{ value }}', $formattedValue)
119 ->setParameter('{{ limit }}', 10)
120 ->setCode(Range::TOO_LOW_ERROR)
125 * @dataProvider getMoreThanTwenty
127 public function testInvalidValuesMax($value, $formattedValue)
129 $constraint = new Range(array(
131 'maxMessage' => 'myMessage',
134 $this->validator->validate($value, $constraint);
136 $this->buildViolation('myMessage')
137 ->setParameter('{{ value }}', $formattedValue)
138 ->setParameter('{{ limit }}', 20)
139 ->setCode(Range::TOO_HIGH_ERROR)
144 * @dataProvider getMoreThanTwenty
146 public function testInvalidValuesCombinedMax($value, $formattedValue)
148 $constraint = new Range(array(
151 'minMessage' => 'myMinMessage',
152 'maxMessage' => 'myMaxMessage',
155 $this->validator->validate($value, $constraint);
157 $this->buildViolation('myMaxMessage')
158 ->setParameter('{{ value }}', $formattedValue)
159 ->setParameter('{{ limit }}', 20)
160 ->setCode(Range::TOO_HIGH_ERROR)
165 * @dataProvider getLessThanTen
167 public function testInvalidValuesCombinedMin($value, $formattedValue)
169 $constraint = new Range(array(
172 'minMessage' => 'myMinMessage',
173 'maxMessage' => 'myMaxMessage',
176 $this->validator->validate($value, $constraint);
178 $this->buildViolation('myMinMessage')
179 ->setParameter('{{ value }}', $formattedValue)
180 ->setParameter('{{ limit }}', 10)
181 ->setCode(Range::TOO_LOW_ERROR)
185 public function getTenthToTwentiethMarch2014()
187 // The provider runs before setUp(), so we need to manually fix
188 // the default timezone
189 $this->setDefaultTimezone('UTC');
192 array(new \DateTime('March 10, 2014')),
193 array(new \DateTime('March 15, 2014')),
194 array(new \DateTime('March 20, 2014')),
197 if (PHP_VERSION_ID >= 50500) {
198 $tests[] = array(new \DateTimeImmutable('March 10, 2014'));
199 $tests[] = array(new \DateTimeImmutable('March 15, 2014'));
200 $tests[] = array(new \DateTimeImmutable('March 20, 2014'));
203 $this->restoreDefaultTimezone();
208 public function getSoonerThanTenthMarch2014()
210 // The provider runs before setUp(), so we need to manually fix
211 // the default timezone
212 $this->setDefaultTimezone('UTC');
215 array(new \DateTime('March 20, 2013'), 'Mar 20, 2013, 12:00 AM'),
216 array(new \DateTime('March 9, 2014'), 'Mar 9, 2014, 12:00 AM'),
219 if (PHP_VERSION_ID >= 50500) {
220 $tests[] = array(new \DateTimeImmutable('March 20, 2013'), 'Mar 20, 2013, 12:00 AM');
221 $tests[] = array(new \DateTimeImmutable('March 9, 2014'), 'Mar 9, 2014, 12:00 AM');
224 $this->restoreDefaultTimezone();
229 public function getLaterThanTwentiethMarch2014()
231 // The provider runs before setUp(), so we need to manually fix
232 // the default timezone
233 $this->setDefaultTimezone('UTC');
236 array(new \DateTime('March 21, 2014'), 'Mar 21, 2014, 12:00 AM'),
237 array(new \DateTime('March 9, 2015'), 'Mar 9, 2015, 12:00 AM'),
240 if (PHP_VERSION_ID >= 50500) {
241 $tests[] = array(new \DateTimeImmutable('March 21, 2014'), 'Mar 21, 2014, 12:00 AM');
242 $tests[] = array(new \DateTimeImmutable('March 9, 2015'), 'Mar 9, 2015, 12:00 AM');
245 $this->restoreDefaultTimezone();
251 * @dataProvider getTenthToTwentiethMarch2014
253 public function testValidDatesMin($value)
255 $constraint = new Range(array('min' => 'March 10, 2014'));
256 $this->validator->validate($value, $constraint);
258 $this->assertNoViolation();
262 * @dataProvider getTenthToTwentiethMarch2014
264 public function testValidDatesMax($value)
266 $constraint = new Range(array('max' => 'March 20, 2014'));
267 $this->validator->validate($value, $constraint);
269 $this->assertNoViolation();
273 * @dataProvider getTenthToTwentiethMarch2014
275 public function testValidDatesMinMax($value)
277 $constraint = new Range(array('min' => 'March 10, 2014', 'max' => 'March 20, 2014'));
278 $this->validator->validate($value, $constraint);
280 $this->assertNoViolation();
284 * @dataProvider getSoonerThanTenthMarch2014
286 public function testInvalidDatesMin($value, $dateTimeAsString)
288 // Conversion of dates to string differs between ICU versions
289 // Make sure we have the correct version loaded
290 IntlTestHelper::requireIntl($this, '57.1');
292 $constraint = new Range(array(
293 'min' => 'March 10, 2014',
294 'minMessage' => 'myMessage',
297 $this->validator->validate($value, $constraint);
299 $this->buildViolation('myMessage')
300 ->setParameter('{{ value }}', $dateTimeAsString)
301 ->setParameter('{{ limit }}', 'Mar 10, 2014, 12:00 AM')
302 ->setCode(Range::TOO_LOW_ERROR)
307 * @dataProvider getLaterThanTwentiethMarch2014
309 public function testInvalidDatesMax($value, $dateTimeAsString)
311 // Conversion of dates to string differs between ICU versions
312 // Make sure we have the correct version loaded
313 IntlTestHelper::requireIntl($this, '57.1');
315 $constraint = new Range(array(
316 'max' => 'March 20, 2014',
317 'maxMessage' => 'myMessage',
320 $this->validator->validate($value, $constraint);
322 $this->buildViolation('myMessage')
323 ->setParameter('{{ value }}', $dateTimeAsString)
324 ->setParameter('{{ limit }}', 'Mar 20, 2014, 12:00 AM')
325 ->setCode(Range::TOO_HIGH_ERROR)
330 * @dataProvider getLaterThanTwentiethMarch2014
332 public function testInvalidDatesCombinedMax($value, $dateTimeAsString)
334 // Conversion of dates to string differs between ICU versions
335 // Make sure we have the correct version loaded
336 IntlTestHelper::requireIntl($this, '57.1');
338 $constraint = new Range(array(
339 'min' => 'March 10, 2014',
340 'max' => 'March 20, 2014',
341 'minMessage' => 'myMinMessage',
342 'maxMessage' => 'myMaxMessage',
345 $this->validator->validate($value, $constraint);
347 $this->buildViolation('myMaxMessage')
348 ->setParameter('{{ value }}', $dateTimeAsString)
349 ->setParameter('{{ limit }}', 'Mar 20, 2014, 12:00 AM')
350 ->setCode(Range::TOO_HIGH_ERROR)
355 * @dataProvider getSoonerThanTenthMarch2014
357 public function testInvalidDatesCombinedMin($value, $dateTimeAsString)
359 // Conversion of dates to string differs between ICU versions
360 // Make sure we have the correct version loaded
361 IntlTestHelper::requireIntl($this, '57.1');
363 $constraint = new Range(array(
364 'min' => 'March 10, 2014',
365 'max' => 'March 20, 2014',
366 'minMessage' => 'myMinMessage',
367 'maxMessage' => 'myMaxMessage',
370 $this->validator->validate($value, $constraint);
372 $this->buildViolation('myMinMessage')
373 ->setParameter('{{ value }}', $dateTimeAsString)
374 ->setParameter('{{ limit }}', 'Mar 10, 2014, 12:00 AM')
375 ->setCode(Range::TOO_LOW_ERROR)
379 public function getInvalidValues()
386 array(new \stdClass()),
390 public function testNonNumeric()
392 $this->validator->validate('abcd', new Range(array(
395 'invalidMessage' => 'myMessage',
398 $this->buildViolation('myMessage')
399 ->setParameter('{{ value }}', '"abcd"')
400 ->setCode(Range::INVALID_CHARACTERS_ERROR)