Yaffs site version 1.1
[yaffs-website] / vendor / symfony / validator / Tests / Constraints / LengthValidatorTest.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\Length;
15 use Symfony\Component\Validator\Constraints\LengthValidator;
16 use Symfony\Component\Validator\Validation;
17
18 class LengthValidatorTest 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 LengthValidator();
28     }
29
30     public function testNullIsValid()
31     {
32         $this->validator->validate(null, new Length(6));
33
34         $this->assertNoViolation();
35     }
36
37     public function testEmptyStringIsValid()
38     {
39         $this->validator->validate('', new Length(6));
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 Length(5));
50     }
51
52     public function getThreeOrLessCharacters()
53     {
54         return array(
55             array(12),
56             array('12'),
57             array('üü'),
58             array('éé'),
59             array(123),
60             array('123'),
61             array('üüü'),
62             array('ééé'),
63         );
64     }
65
66     public function getFourCharacters()
67     {
68         return array(
69             array(1234),
70             array('1234'),
71             array('üüüü'),
72             array('éééé'),
73         );
74     }
75
76     public function getFiveOrMoreCharacters()
77     {
78         return array(
79             array(12345),
80             array('12345'),
81             array('üüüüü'),
82             array('ééééé'),
83             array(123456),
84             array('123456'),
85             array('üüüüüü'),
86             array('éééééé'),
87         );
88     }
89
90     public function getOneCharset()
91     {
92         return array(
93             array('é', 'utf8', true),
94             array("\xE9", 'CP1252', true),
95             array("\xE9", 'XXX', false),
96             array("\xE9", 'utf8', false),
97         );
98     }
99
100     /**
101      * @dataProvider getFiveOrMoreCharacters
102      */
103     public function testValidValuesMin($value)
104     {
105         $constraint = new Length(array('min' => 5));
106         $this->validator->validate($value, $constraint);
107
108         $this->assertNoViolation();
109     }
110
111     /**
112      * @dataProvider getThreeOrLessCharacters
113      */
114     public function testValidValuesMax($value)
115     {
116         $constraint = new Length(array('max' => 3));
117         $this->validator->validate($value, $constraint);
118
119         $this->assertNoViolation();
120     }
121
122     /**
123      * @dataProvider getFourCharacters
124      */
125     public function testValidValuesExact($value)
126     {
127         $constraint = new Length(4);
128         $this->validator->validate($value, $constraint);
129
130         $this->assertNoViolation();
131     }
132
133     /**
134      * @dataProvider getThreeOrLessCharacters
135      */
136     public function testInvalidValuesMin($value)
137     {
138         $constraint = new Length(array(
139             'min' => 4,
140             'minMessage' => 'myMessage',
141         ));
142
143         $this->validator->validate($value, $constraint);
144
145         $this->buildViolation('myMessage')
146             ->setParameter('{{ value }}', '"'.$value.'"')
147             ->setParameter('{{ limit }}', 4)
148             ->setInvalidValue($value)
149             ->setPlural(4)
150             ->setCode(Length::TOO_SHORT_ERROR)
151             ->assertRaised();
152     }
153
154     /**
155      * @dataProvider getFiveOrMoreCharacters
156      */
157     public function testInvalidValuesMax($value)
158     {
159         $constraint = new Length(array(
160             'max' => 4,
161             'maxMessage' => 'myMessage',
162         ));
163
164         $this->validator->validate($value, $constraint);
165
166         $this->buildViolation('myMessage')
167             ->setParameter('{{ value }}', '"'.$value.'"')
168             ->setParameter('{{ limit }}', 4)
169             ->setInvalidValue($value)
170             ->setPlural(4)
171             ->setCode(Length::TOO_LONG_ERROR)
172             ->assertRaised();
173     }
174
175     /**
176      * @dataProvider getThreeOrLessCharacters
177      */
178     public function testInvalidValuesExactLessThanFour($value)
179     {
180         $constraint = new Length(array(
181             'min' => 4,
182             'max' => 4,
183             'exactMessage' => 'myMessage',
184         ));
185
186         $this->validator->validate($value, $constraint);
187
188         $this->buildViolation('myMessage')
189             ->setParameter('{{ value }}', '"'.$value.'"')
190             ->setParameter('{{ limit }}', 4)
191             ->setInvalidValue($value)
192             ->setPlural(4)
193             ->setCode(Length::TOO_SHORT_ERROR)
194             ->assertRaised();
195     }
196
197     /**
198      * @dataProvider getFiveOrMoreCharacters
199      */
200     public function testInvalidValuesExactMoreThanFour($value)
201     {
202         $constraint = new Length(array(
203             'min' => 4,
204             'max' => 4,
205             'exactMessage' => 'myMessage',
206         ));
207
208         $this->validator->validate($value, $constraint);
209
210         $this->buildViolation('myMessage')
211             ->setParameter('{{ value }}', '"'.$value.'"')
212             ->setParameter('{{ limit }}', 4)
213             ->setInvalidValue($value)
214             ->setPlural(4)
215             ->setCode(Length::TOO_LONG_ERROR)
216             ->assertRaised();
217     }
218
219     /**
220      * @dataProvider getOneCharset
221      */
222     public function testOneCharset($value, $charset, $isValid)
223     {
224         $constraint = new Length(array(
225             'min' => 1,
226             'max' => 1,
227             'charset' => $charset,
228             'charsetMessage' => 'myMessage',
229         ));
230
231         $this->validator->validate($value, $constraint);
232
233         if ($isValid) {
234             $this->assertNoViolation();
235         } else {
236             $this->buildViolation('myMessage')
237                 ->setParameter('{{ value }}', '"'.$value.'"')
238                 ->setParameter('{{ charset }}', $charset)
239                 ->setInvalidValue($value)
240                 ->setCode(Length::INVALID_CHARACTERS_ERROR)
241                 ->assertRaised();
242         }
243     }
244
245     public function testConstraintGetDefaultOption()
246     {
247         $constraint = new Length(5);
248
249         $this->assertEquals(5, $constraint->min);
250         $this->assertEquals(5, $constraint->max);
251     }
252 }