Version 1
[yaffs-website] / vendor / symfony / validator / Tests / Constraints / RegexValidatorTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Validator\Tests\Constraints;
13
14 use Symfony\Component\Validator\Constraints\Regex;
15 use Symfony\Component\Validator\Constraints\RegexValidator;
16 use Symfony\Component\Validator\Validation;
17
18 class RegexValidatorTest extends AbstractConstraintValidatorTest
19 {
20     protected function getApiVersion()
21     {
22         return Validation::API_VERSION_2_5;
23     }
24
25     protected function createValidator()
26     {
27         return new RegexValidator();
28     }
29
30     public function testNullIsValid()
31     {
32         $this->validator->validate(null, new Regex(array('pattern' => '/^[0-9]+$/')));
33
34         $this->assertNoViolation();
35     }
36
37     public function testEmptyStringIsValid()
38     {
39         $this->validator->validate('', new Regex(array('pattern' => '/^[0-9]+$/')));
40
41         $this->assertNoViolation();
42     }
43
44     /**
45      * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
46      */
47     public function testExpectsStringCompatibleType()
48     {
49         $this->validator->validate(new \stdClass(), new Regex(array('pattern' => '/^[0-9]+$/')));
50     }
51
52     /**
53      * @dataProvider getValidValues
54      */
55     public function testValidValues($value)
56     {
57         $constraint = new Regex(array('pattern' => '/^[0-9]+$/'));
58         $this->validator->validate($value, $constraint);
59
60         $this->assertNoViolation();
61     }
62
63     public function getValidValues()
64     {
65         return array(
66             array(0),
67             array('0'),
68             array('090909'),
69             array(90909),
70         );
71     }
72
73     /**
74      * @dataProvider getInvalidValues
75      */
76     public function testInvalidValues($value)
77     {
78         $constraint = new Regex(array(
79             'pattern' => '/^[0-9]+$/',
80             'message' => 'myMessage',
81         ));
82
83         $this->validator->validate($value, $constraint);
84
85         $this->buildViolation('myMessage')
86             ->setParameter('{{ value }}', '"'.$value.'"')
87             ->setCode(Regex::REGEX_FAILED_ERROR)
88             ->assertRaised();
89     }
90
91     public function getInvalidValues()
92     {
93         return array(
94             array('abcd'),
95             array('090foo'),
96         );
97     }
98 }