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