7eb931ee92039a7ecbebc16ac7d17f96bbcd8dd0
[yaffs-website] / vendor / symfony / validator / Tests / Constraints / RangeValidatorTest.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\Intl\Util\IntlTestHelper;
15 use Symfony\Component\Validator\Constraints\Range;
16 use Symfony\Component\Validator\Constraints\RangeValidator;
17 use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
18
19 class RangeValidatorTest extends ConstraintValidatorTestCase
20 {
21     protected function createValidator()
22     {
23         return new RangeValidator();
24     }
25
26     public function testNullIsValid()
27     {
28         $this->validator->validate(null, new Range(array('min' => 10, 'max' => 20)));
29
30         $this->assertNoViolation();
31     }
32
33     public function getTenToTwenty()
34     {
35         return array(
36             array(10.00001),
37             array(19.99999),
38             array('10.00001'),
39             array('19.99999'),
40             array(10),
41             array(20),
42             array(10.0),
43             array(20.0),
44         );
45     }
46
47     public function getLessThanTen()
48     {
49         return array(
50             array(9.99999, '9.99999'),
51             array('9.99999', '"9.99999"'),
52             array(5, '5'),
53             array(1.0, '1.0'),
54         );
55     }
56
57     public function getMoreThanTwenty()
58     {
59         return array(
60             array(20.000001, '20.000001'),
61             array('20.000001', '"20.000001"'),
62             array(21, '21'),
63             array(30.0, '30.0'),
64         );
65     }
66
67     /**
68      * @dataProvider getTenToTwenty
69      */
70     public function testValidValuesMin($value)
71     {
72         $constraint = new Range(array('min' => 10));
73         $this->validator->validate($value, $constraint);
74
75         $this->assertNoViolation();
76     }
77
78     /**
79      * @dataProvider getTenToTwenty
80      */
81     public function testValidValuesMax($value)
82     {
83         $constraint = new Range(array('max' => 20));
84         $this->validator->validate($value, $constraint);
85
86         $this->assertNoViolation();
87     }
88
89     /**
90      * @dataProvider getTenToTwenty
91      */
92     public function testValidValuesMinMax($value)
93     {
94         $constraint = new Range(array('min' => 10, 'max' => 20));
95         $this->validator->validate($value, $constraint);
96
97         $this->assertNoViolation();
98     }
99
100     /**
101      * @dataProvider getLessThanTen
102      */
103     public function testInvalidValuesMin($value, $formattedValue)
104     {
105         $constraint = new Range(array(
106             'min' => 10,
107             'minMessage' => 'myMessage',
108         ));
109
110         $this->validator->validate($value, $constraint);
111
112         $this->buildViolation('myMessage')
113             ->setParameter('{{ value }}', $formattedValue)
114             ->setParameter('{{ limit }}', 10)
115             ->setCode(Range::TOO_LOW_ERROR)
116             ->assertRaised();
117     }
118
119     /**
120      * @dataProvider getMoreThanTwenty
121      */
122     public function testInvalidValuesMax($value, $formattedValue)
123     {
124         $constraint = new Range(array(
125             'max' => 20,
126             'maxMessage' => 'myMessage',
127         ));
128
129         $this->validator->validate($value, $constraint);
130
131         $this->buildViolation('myMessage')
132             ->setParameter('{{ value }}', $formattedValue)
133             ->setParameter('{{ limit }}', 20)
134             ->setCode(Range::TOO_HIGH_ERROR)
135             ->assertRaised();
136     }
137
138     /**
139      * @dataProvider getMoreThanTwenty
140      */
141     public function testInvalidValuesCombinedMax($value, $formattedValue)
142     {
143         $constraint = new Range(array(
144             'min' => 10,
145             'max' => 20,
146             'minMessage' => 'myMinMessage',
147             'maxMessage' => 'myMaxMessage',
148         ));
149
150         $this->validator->validate($value, $constraint);
151
152         $this->buildViolation('myMaxMessage')
153             ->setParameter('{{ value }}', $formattedValue)
154             ->setParameter('{{ limit }}', 20)
155             ->setCode(Range::TOO_HIGH_ERROR)
156             ->assertRaised();
157     }
158
159     /**
160      * @dataProvider getLessThanTen
161      */
162     public function testInvalidValuesCombinedMin($value, $formattedValue)
163     {
164         $constraint = new Range(array(
165             'min' => 10,
166             'max' => 20,
167             'minMessage' => 'myMinMessage',
168             'maxMessage' => 'myMaxMessage',
169         ));
170
171         $this->validator->validate($value, $constraint);
172
173         $this->buildViolation('myMinMessage')
174             ->setParameter('{{ value }}', $formattedValue)
175             ->setParameter('{{ limit }}', 10)
176             ->setCode(Range::TOO_LOW_ERROR)
177             ->assertRaised();
178     }
179
180     public function getTenthToTwentiethMarch2014()
181     {
182         // The provider runs before setUp(), so we need to manually fix
183         // the default timezone
184         $this->setDefaultTimezone('UTC');
185
186         $tests = array(
187             array(new \DateTime('March 10, 2014')),
188             array(new \DateTime('March 15, 2014')),
189             array(new \DateTime('March 20, 2014')),
190         );
191
192         $tests[] = array(new \DateTimeImmutable('March 10, 2014'));
193         $tests[] = array(new \DateTimeImmutable('March 15, 2014'));
194         $tests[] = array(new \DateTimeImmutable('March 20, 2014'));
195
196         $this->restoreDefaultTimezone();
197
198         return $tests;
199     }
200
201     public function getSoonerThanTenthMarch2014()
202     {
203         // The provider runs before setUp(), so we need to manually fix
204         // the default timezone
205         $this->setDefaultTimezone('UTC');
206
207         $tests = array(
208             array(new \DateTime('March 20, 2013'), 'Mar 20, 2013, 12:00 AM'),
209             array(new \DateTime('March 9, 2014'), 'Mar 9, 2014, 12:00 AM'),
210         );
211
212         $tests[] = array(new \DateTimeImmutable('March 20, 2013'), 'Mar 20, 2013, 12:00 AM');
213         $tests[] = array(new \DateTimeImmutable('March 9, 2014'), 'Mar 9, 2014, 12:00 AM');
214
215         $this->restoreDefaultTimezone();
216
217         return $tests;
218     }
219
220     public function getLaterThanTwentiethMarch2014()
221     {
222         // The provider runs before setUp(), so we need to manually fix
223         // the default timezone
224         $this->setDefaultTimezone('UTC');
225
226         $tests = array(
227             array(new \DateTime('March 21, 2014'), 'Mar 21, 2014, 12:00 AM'),
228             array(new \DateTime('March 9, 2015'), 'Mar 9, 2015, 12:00 AM'),
229         );
230
231         $tests[] = array(new \DateTimeImmutable('March 21, 2014'), 'Mar 21, 2014, 12:00 AM');
232         $tests[] = array(new \DateTimeImmutable('March 9, 2015'), 'Mar 9, 2015, 12:00 AM');
233
234         $this->restoreDefaultTimezone();
235
236         return $tests;
237     }
238
239     /**
240      * @dataProvider getTenthToTwentiethMarch2014
241      */
242     public function testValidDatesMin($value)
243     {
244         $constraint = new Range(array('min' => 'March 10, 2014'));
245         $this->validator->validate($value, $constraint);
246
247         $this->assertNoViolation();
248     }
249
250     /**
251      * @dataProvider getTenthToTwentiethMarch2014
252      */
253     public function testValidDatesMax($value)
254     {
255         $constraint = new Range(array('max' => 'March 20, 2014'));
256         $this->validator->validate($value, $constraint);
257
258         $this->assertNoViolation();
259     }
260
261     /**
262      * @dataProvider getTenthToTwentiethMarch2014
263      */
264     public function testValidDatesMinMax($value)
265     {
266         $constraint = new Range(array('min' => 'March 10, 2014', 'max' => 'March 20, 2014'));
267         $this->validator->validate($value, $constraint);
268
269         $this->assertNoViolation();
270     }
271
272     /**
273      * @dataProvider getSoonerThanTenthMarch2014
274      */
275     public function testInvalidDatesMin($value, $dateTimeAsString)
276     {
277         // Conversion of dates to string differs between ICU versions
278         // Make sure we have the correct version loaded
279         IntlTestHelper::requireIntl($this, '57.1');
280
281         $constraint = new Range(array(
282             'min' => 'March 10, 2014',
283             'minMessage' => 'myMessage',
284         ));
285
286         $this->validator->validate($value, $constraint);
287
288         $this->buildViolation('myMessage')
289             ->setParameter('{{ value }}', $dateTimeAsString)
290             ->setParameter('{{ limit }}', 'Mar 10, 2014, 12:00 AM')
291             ->setCode(Range::TOO_LOW_ERROR)
292             ->assertRaised();
293     }
294
295     /**
296      * @dataProvider getLaterThanTwentiethMarch2014
297      */
298     public function testInvalidDatesMax($value, $dateTimeAsString)
299     {
300         // Conversion of dates to string differs between ICU versions
301         // Make sure we have the correct version loaded
302         IntlTestHelper::requireIntl($this, '57.1');
303
304         $constraint = new Range(array(
305             'max' => 'March 20, 2014',
306             'maxMessage' => 'myMessage',
307         ));
308
309         $this->validator->validate($value, $constraint);
310
311         $this->buildViolation('myMessage')
312             ->setParameter('{{ value }}', $dateTimeAsString)
313             ->setParameter('{{ limit }}', 'Mar 20, 2014, 12:00 AM')
314             ->setCode(Range::TOO_HIGH_ERROR)
315             ->assertRaised();
316     }
317
318     /**
319      * @dataProvider getLaterThanTwentiethMarch2014
320      */
321     public function testInvalidDatesCombinedMax($value, $dateTimeAsString)
322     {
323         // Conversion of dates to string differs between ICU versions
324         // Make sure we have the correct version loaded
325         IntlTestHelper::requireIntl($this, '57.1');
326
327         $constraint = new Range(array(
328             'min' => 'March 10, 2014',
329             'max' => 'March 20, 2014',
330             'minMessage' => 'myMinMessage',
331             'maxMessage' => 'myMaxMessage',
332         ));
333
334         $this->validator->validate($value, $constraint);
335
336         $this->buildViolation('myMaxMessage')
337             ->setParameter('{{ value }}', $dateTimeAsString)
338             ->setParameter('{{ limit }}', 'Mar 20, 2014, 12:00 AM')
339             ->setCode(Range::TOO_HIGH_ERROR)
340             ->assertRaised();
341     }
342
343     /**
344      * @dataProvider getSoonerThanTenthMarch2014
345      */
346     public function testInvalidDatesCombinedMin($value, $dateTimeAsString)
347     {
348         // Conversion of dates to string differs between ICU versions
349         // Make sure we have the correct version loaded
350         IntlTestHelper::requireIntl($this, '57.1');
351
352         $constraint = new Range(array(
353             'min' => 'March 10, 2014',
354             'max' => 'March 20, 2014',
355             'minMessage' => 'myMinMessage',
356             'maxMessage' => 'myMaxMessage',
357         ));
358
359         $this->validator->validate($value, $constraint);
360
361         $this->buildViolation('myMinMessage')
362             ->setParameter('{{ value }}', $dateTimeAsString)
363             ->setParameter('{{ limit }}', 'Mar 10, 2014, 12:00 AM')
364             ->setCode(Range::TOO_LOW_ERROR)
365             ->assertRaised();
366     }
367
368     public function getInvalidValues()
369     {
370         return array(
371             array(9.999999),
372             array(20.000001),
373             array('9.999999'),
374             array('20.000001'),
375             array(new \stdClass()),
376         );
377     }
378
379     public function testNonNumeric()
380     {
381         $this->validator->validate('abcd', new Range(array(
382             'min' => 10,
383             'max' => 20,
384             'invalidMessage' => 'myMessage',
385         )));
386
387         $this->buildViolation('myMessage')
388             ->setParameter('{{ value }}', '"abcd"')
389             ->setCode(Range::INVALID_CHARACTERS_ERROR)
390             ->assertRaised();
391     }
392 }