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\Regex;
15 use Symfony\Component\Validator\Constraints\RegexValidator;
16 use Symfony\Component\Validator\Validation;
18 class RegexValidatorTest extends AbstractConstraintValidatorTest
20 protected function getApiVersion()
22 return Validation::API_VERSION_2_5;
25 protected function createValidator()
27 return new RegexValidator();
30 public function testNullIsValid()
32 $this->validator->validate(null, new Regex(array('pattern' => '/^[0-9]+$/')));
34 $this->assertNoViolation();
37 public function testEmptyStringIsValid()
39 $this->validator->validate('', new Regex(array('pattern' => '/^[0-9]+$/')));
41 $this->assertNoViolation();
45 * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
47 public function testExpectsStringCompatibleType()
49 $this->validator->validate(new \stdClass(), new Regex(array('pattern' => '/^[0-9]+$/')));
53 * @dataProvider getValidValues
55 public function testValidValues($value)
57 $constraint = new Regex(array('pattern' => '/^[0-9]+$/'));
58 $this->validator->validate($value, $constraint);
60 $this->assertNoViolation();
63 public function getValidValues()
74 * @dataProvider getInvalidValues
76 public function testInvalidValues($value)
78 $constraint = new Regex(array(
79 'pattern' => '/^[0-9]+$/',
80 'message' => 'myMessage',
83 $this->validator->validate($value, $constraint);
85 $this->buildViolation('myMessage')
86 ->setParameter('{{ value }}', '"'.$value.'"')
87 ->setCode(Regex::REGEX_FAILED_ERROR)
91 public function getInvalidValues()