123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Validator\Tests\Constraints;
- use Symfony\Component\Intl\Util\IntlTestHelper;
- use Symfony\Component\Validator\Constraints\Range;
- use Symfony\Component\Validator\Constraints\RangeValidator;
- use Symfony\Component\Validator\Validation;
- class RangeValidatorTest extends AbstractConstraintValidatorTest
- {
- protected function getApiVersion()
- {
- return Validation::API_VERSION_2_5;
- }
- protected function createValidator()
- {
- return new RangeValidator();
- }
- public function testNullIsValid()
- {
- $this->validator->validate(null, new Range(array('min' => 10, 'max' => 20)));
- $this->assertNoViolation();
- }
- public function getTenToTwenty()
- {
- return array(
- array(10.00001),
- array(19.99999),
- array('10.00001'),
- array('19.99999'),
- array(10),
- array(20),
- array(10.0),
- array(20.0),
- );
- }
- public function getLessThanTen()
- {
- return array(
- array(9.99999, '9.99999'),
- array('9.99999', '"9.99999"'),
- array(5, '5'),
- array(1.0, '1.0'),
- );
- }
- public function getMoreThanTwenty()
- {
- return array(
- array(20.000001, '20.000001'),
- array('20.000001', '"20.000001"'),
- array(21, '21'),
- array(30.0, '30.0'),
- );
- }
- /**
- * @dataProvider getTenToTwenty
- */
- public function testValidValuesMin($value)
- {
- $constraint = new Range(array('min' => 10));
- $this->validator->validate($value, $constraint);
- $this->assertNoViolation();
- }
- /**
- * @dataProvider getTenToTwenty
- */
- public function testValidValuesMax($value)
- {
- $constraint = new Range(array('max' => 20));
- $this->validator->validate($value, $constraint);
- $this->assertNoViolation();
- }
- /**
- * @dataProvider getTenToTwenty
- */
- public function testValidValuesMinMax($value)
- {
- $constraint = new Range(array('min' => 10, 'max' => 20));
- $this->validator->validate($value, $constraint);
- $this->assertNoViolation();
- }
- /**
- * @dataProvider getLessThanTen
- */
- public function testInvalidValuesMin($value, $formattedValue)
- {
- $constraint = new Range(array(
- 'min' => 10,
- 'minMessage' => 'myMessage',
- ));
- $this->validator->validate($value, $constraint);
- $this->buildViolation('myMessage')
- ->setParameter('{{ value }}', $formattedValue)
- ->setParameter('{{ limit }}', 10)
- ->setCode(Range::TOO_LOW_ERROR)
- ->assertRaised();
- }
- /**
- * @dataProvider getMoreThanTwenty
- */
- public function testInvalidValuesMax($value, $formattedValue)
- {
- $constraint = new Range(array(
- 'max' => 20,
- 'maxMessage' => 'myMessage',
- ));
- $this->validator->validate($value, $constraint);
- $this->buildViolation('myMessage')
- ->setParameter('{{ value }}', $formattedValue)
- ->setParameter('{{ limit }}', 20)
- ->setCode(Range::TOO_HIGH_ERROR)
- ->assertRaised();
- }
- /**
- * @dataProvider getMoreThanTwenty
- */
- public function testInvalidValuesCombinedMax($value, $formattedValue)
- {
- $constraint = new Range(array(
- 'min' => 10,
- 'max' => 20,
- 'minMessage' => 'myMinMessage',
- 'maxMessage' => 'myMaxMessage',
- ));
- $this->validator->validate($value, $constraint);
- $this->buildViolation('myMaxMessage')
- ->setParameter('{{ value }}', $formattedValue)
- ->setParameter('{{ limit }}', 20)
- ->setCode(Range::TOO_HIGH_ERROR)
- ->assertRaised();
- }
- /**
- * @dataProvider getLessThanTen
- */
- public function testInvalidValuesCombinedMin($value, $formattedValue)
- {
- $constraint = new Range(array(
- 'min' => 10,
- 'max' => 20,
- 'minMessage' => 'myMinMessage',
- 'maxMessage' => 'myMaxMessage',
- ));
- $this->validator->validate($value, $constraint);
- $this->buildViolation('myMinMessage')
- ->setParameter('{{ value }}', $formattedValue)
- ->setParameter('{{ limit }}', 10)
- ->setCode(Range::TOO_LOW_ERROR)
- ->assertRaised();
- }
- public function getTenthToTwentiethMarch2014()
- {
- // The provider runs before setUp(), so we need to manually fix
- // the default timezone
- $this->setDefaultTimezone('UTC');
- $tests = array(
- array(new \DateTime('March 10, 2014')),
- array(new \DateTime('March 15, 2014')),
- array(new \DateTime('March 20, 2014')),
- );
- if (\PHP_VERSION_ID >= 50500) {
- $tests[] = array(new \DateTimeImmutable('March 10, 2014'));
- $tests[] = array(new \DateTimeImmutable('March 15, 2014'));
- $tests[] = array(new \DateTimeImmutable('March 20, 2014'));
- }
- $this->restoreDefaultTimezone();
- return $tests;
- }
- public function getSoonerThanTenthMarch2014()
- {
- // The provider runs before setUp(), so we need to manually fix
- // the default timezone
- $this->setDefaultTimezone('UTC');
- $tests = array(
- array(new \DateTime('March 20, 2013'), 'Mar 20, 2013, 12:00 AM'),
- array(new \DateTime('March 9, 2014'), 'Mar 9, 2014, 12:00 AM'),
- );
- if (\PHP_VERSION_ID >= 50500) {
- $tests[] = array(new \DateTimeImmutable('March 20, 2013'), 'Mar 20, 2013, 12:00 AM');
- $tests[] = array(new \DateTimeImmutable('March 9, 2014'), 'Mar 9, 2014, 12:00 AM');
- }
- $this->restoreDefaultTimezone();
- return $tests;
- }
- public function getLaterThanTwentiethMarch2014()
- {
- // The provider runs before setUp(), so we need to manually fix
- // the default timezone
- $this->setDefaultTimezone('UTC');
- $tests = array(
- array(new \DateTime('March 21, 2014'), 'Mar 21, 2014, 12:00 AM'),
- array(new \DateTime('March 9, 2015'), 'Mar 9, 2015, 12:00 AM'),
- );
- if (\PHP_VERSION_ID >= 50500) {
- $tests[] = array(new \DateTimeImmutable('March 21, 2014'), 'Mar 21, 2014, 12:00 AM');
- $tests[] = array(new \DateTimeImmutable('March 9, 2015'), 'Mar 9, 2015, 12:00 AM');
- }
- $this->restoreDefaultTimezone();
- return $tests;
- }
- /**
- * @dataProvider getTenthToTwentiethMarch2014
- */
- public function testValidDatesMin($value)
- {
- $constraint = new Range(array('min' => 'March 10, 2014'));
- $this->validator->validate($value, $constraint);
- $this->assertNoViolation();
- }
- /**
- * @dataProvider getTenthToTwentiethMarch2014
- */
- public function testValidDatesMax($value)
- {
- $constraint = new Range(array('max' => 'March 20, 2014'));
- $this->validator->validate($value, $constraint);
- $this->assertNoViolation();
- }
- /**
- * @dataProvider getTenthToTwentiethMarch2014
- */
- public function testValidDatesMinMax($value)
- {
- $constraint = new Range(array('min' => 'March 10, 2014', 'max' => 'March 20, 2014'));
- $this->validator->validate($value, $constraint);
- $this->assertNoViolation();
- }
- /**
- * @dataProvider getSoonerThanTenthMarch2014
- */
- public function testInvalidDatesMin($value, $dateTimeAsString)
- {
- // Conversion of dates to string differs between ICU versions
- // Make sure we have the correct version loaded
- IntlTestHelper::requireIntl($this, '57.1');
- $constraint = new Range(array(
- 'min' => 'March 10, 2014',
- 'minMessage' => 'myMessage',
- ));
- $this->validator->validate($value, $constraint);
- $this->buildViolation('myMessage')
- ->setParameter('{{ value }}', $dateTimeAsString)
- ->setParameter('{{ limit }}', 'Mar 10, 2014, 12:00 AM')
- ->setCode(Range::TOO_LOW_ERROR)
- ->assertRaised();
- }
- /**
- * @dataProvider getLaterThanTwentiethMarch2014
- */
- public function testInvalidDatesMax($value, $dateTimeAsString)
- {
- // Conversion of dates to string differs between ICU versions
- // Make sure we have the correct version loaded
- IntlTestHelper::requireIntl($this, '57.1');
- $constraint = new Range(array(
- 'max' => 'March 20, 2014',
- 'maxMessage' => 'myMessage',
- ));
- $this->validator->validate($value, $constraint);
- $this->buildViolation('myMessage')
- ->setParameter('{{ value }}', $dateTimeAsString)
- ->setParameter('{{ limit }}', 'Mar 20, 2014, 12:00 AM')
- ->setCode(Range::TOO_HIGH_ERROR)
- ->assertRaised();
- }
- /**
- * @dataProvider getLaterThanTwentiethMarch2014
- */
- public function testInvalidDatesCombinedMax($value, $dateTimeAsString)
- {
- // Conversion of dates to string differs between ICU versions
- // Make sure we have the correct version loaded
- IntlTestHelper::requireIntl($this, '57.1');
- $constraint = new Range(array(
- 'min' => 'March 10, 2014',
- 'max' => 'March 20, 2014',
- 'minMessage' => 'myMinMessage',
- 'maxMessage' => 'myMaxMessage',
- ));
- $this->validator->validate($value, $constraint);
- $this->buildViolation('myMaxMessage')
- ->setParameter('{{ value }}', $dateTimeAsString)
- ->setParameter('{{ limit }}', 'Mar 20, 2014, 12:00 AM')
- ->setCode(Range::TOO_HIGH_ERROR)
- ->assertRaised();
- }
- /**
- * @dataProvider getSoonerThanTenthMarch2014
- */
- public function testInvalidDatesCombinedMin($value, $dateTimeAsString)
- {
- // Conversion of dates to string differs between ICU versions
- // Make sure we have the correct version loaded
- IntlTestHelper::requireIntl($this, '57.1');
- $constraint = new Range(array(
- 'min' => 'March 10, 2014',
- 'max' => 'March 20, 2014',
- 'minMessage' => 'myMinMessage',
- 'maxMessage' => 'myMaxMessage',
- ));
- $this->validator->validate($value, $constraint);
- $this->buildViolation('myMinMessage')
- ->setParameter('{{ value }}', $dateTimeAsString)
- ->setParameter('{{ limit }}', 'Mar 10, 2014, 12:00 AM')
- ->setCode(Range::TOO_LOW_ERROR)
- ->assertRaised();
- }
- public function getInvalidValues()
- {
- return array(
- array(9.999999),
- array(20.000001),
- array('9.999999'),
- array('20.000001'),
- array(new \stdClass()),
- );
- }
- public function testNonNumeric()
- {
- $this->validator->validate('abcd', new Range(array(
- 'min' => 10,
- 'max' => 20,
- 'invalidMessage' => 'myMessage',
- )));
- $this->buildViolation('myMessage')
- ->setParameter('{{ value }}', '"abcd"')
- ->setCode(Range::INVALID_CHARACTERS_ERROR)
- ->assertRaised();
- }
- }
|