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