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