Version 1
[yaffs-website] / vendor / symfony / validator / Tests / Constraints / CountValidatorTest.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\Count;
15 use Symfony\Component\Validator\Constraints\CountValidator;
16 use Symfony\Component\Validator\Validation;
17
18 /**
19  * @author Bernhard Schussek <bschussek@gmail.com>
20  */
21 abstract class CountValidatorTest extends AbstractConstraintValidatorTest
22 {
23     protected function getApiVersion()
24     {
25         return Validation::API_VERSION_2_5;
26     }
27
28     protected function createValidator()
29     {
30         return new CountValidator();
31     }
32
33     abstract protected function createCollection(array $content);
34
35     public function testNullIsValid()
36     {
37         $this->validator->validate(null, new Count(6));
38
39         $this->assertNoViolation();
40     }
41
42     /**
43      * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
44      */
45     public function testExpectsCountableType()
46     {
47         $this->validator->validate(new \stdClass(), new Count(5));
48     }
49
50     public function getThreeOrLessElements()
51     {
52         return array(
53             array($this->createCollection(array(1))),
54             array($this->createCollection(array(1, 2))),
55             array($this->createCollection(array(1, 2, 3))),
56             array($this->createCollection(array('a' => 1, 'b' => 2, 'c' => 3))),
57         );
58     }
59
60     public function getFourElements()
61     {
62         return array(
63             array($this->createCollection(array(1, 2, 3, 4))),
64             array($this->createCollection(array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4))),
65         );
66     }
67
68     public function getFiveOrMoreElements()
69     {
70         return array(
71             array($this->createCollection(array(1, 2, 3, 4, 5))),
72             array($this->createCollection(array(1, 2, 3, 4, 5, 6))),
73             array($this->createCollection(array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5))),
74         );
75     }
76
77     /**
78      * @dataProvider getThreeOrLessElements
79      */
80     public function testValidValuesMax($value)
81     {
82         $constraint = new Count(array('max' => 3));
83         $this->validator->validate($value, $constraint);
84
85         $this->assertNoViolation();
86     }
87
88     /**
89      * @dataProvider getFiveOrMoreElements
90      */
91     public function testValidValuesMin($value)
92     {
93         $constraint = new Count(array('min' => 5));
94         $this->validator->validate($value, $constraint);
95
96         $this->assertNoViolation();
97     }
98
99     /**
100      * @dataProvider getFourElements
101      */
102     public function testValidValuesExact($value)
103     {
104         $constraint = new Count(4);
105         $this->validator->validate($value, $constraint);
106
107         $this->assertNoViolation();
108     }
109
110     /**
111      * @dataProvider getFiveOrMoreElements
112      */
113     public function testTooManyValues($value)
114     {
115         $constraint = new Count(array(
116             'max' => 4,
117             'maxMessage' => 'myMessage',
118         ));
119
120         $this->validator->validate($value, $constraint);
121
122         $this->buildViolation('myMessage')
123             ->setParameter('{{ count }}', count($value))
124             ->setParameter('{{ limit }}', 4)
125             ->setInvalidValue($value)
126             ->setPlural(4)
127             ->setCode(Count::TOO_MANY_ERROR)
128             ->assertRaised();
129     }
130
131     /**
132      * @dataProvider getThreeOrLessElements
133      */
134     public function testTooFewValues($value)
135     {
136         $constraint = new Count(array(
137             'min' => 4,
138             'minMessage' => 'myMessage',
139         ));
140
141         $this->validator->validate($value, $constraint);
142
143         $this->buildViolation('myMessage')
144             ->setParameter('{{ count }}', count($value))
145             ->setParameter('{{ limit }}', 4)
146             ->setInvalidValue($value)
147             ->setPlural(4)
148             ->setCode(Count::TOO_FEW_ERROR)
149             ->assertRaised();
150     }
151
152     /**
153      * @dataProvider getFiveOrMoreElements
154      */
155     public function testTooManyValuesExact($value)
156     {
157         $constraint = new Count(array(
158             'min' => 4,
159             'max' => 4,
160             'exactMessage' => 'myMessage',
161         ));
162
163         $this->validator->validate($value, $constraint);
164
165         $this->buildViolation('myMessage')
166             ->setParameter('{{ count }}', count($value))
167             ->setParameter('{{ limit }}', 4)
168             ->setInvalidValue($value)
169             ->setPlural(4)
170             ->setCode(Count::TOO_MANY_ERROR)
171             ->assertRaised();
172     }
173
174     /**
175      * @dataProvider getThreeOrLessElements
176      */
177     public function testTooFewValuesExact($value)
178     {
179         $constraint = new Count(array(
180             'min' => 4,
181             'max' => 4,
182             'exactMessage' => 'myMessage',
183         ));
184
185         $this->validator->validate($value, $constraint);
186
187         $this->buildViolation('myMessage')
188             ->setParameter('{{ count }}', count($value))
189             ->setParameter('{{ limit }}', 4)
190             ->setInvalidValue($value)
191             ->setPlural(4)
192             ->setCode(Count::TOO_FEW_ERROR)
193             ->assertRaised();
194     }
195
196     public function testDefaultOption()
197     {
198         $constraint = new Count(5);
199
200         $this->assertEquals(5, $constraint->min);
201         $this->assertEquals(5, $constraint->max);
202     }
203 }