4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Tests\Constraints;
14 use Symfony\Component\Validator\Constraints\Count;
15 use Symfony\Component\Validator\Constraints\CountValidator;
16 use Symfony\Component\Validator\Validation;
19 * @author Bernhard Schussek <bschussek@gmail.com>
21 abstract class CountValidatorTest extends AbstractConstraintValidatorTest
23 protected function getApiVersion()
25 return Validation::API_VERSION_2_5;
28 protected function createValidator()
30 return new CountValidator();
33 abstract protected function createCollection(array $content);
35 public function testNullIsValid()
37 $this->validator->validate(null, new Count(6));
39 $this->assertNoViolation();
43 * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
45 public function testExpectsCountableType()
47 $this->validator->validate(new \stdClass(), new Count(5));
50 public function getThreeOrLessElements()
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))),
60 public function getFourElements()
63 array($this->createCollection(array(1, 2, 3, 4))),
64 array($this->createCollection(array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4))),
68 public function getFiveOrMoreElements()
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))),
78 * @dataProvider getThreeOrLessElements
80 public function testValidValuesMax($value)
82 $constraint = new Count(array('max' => 3));
83 $this->validator->validate($value, $constraint);
85 $this->assertNoViolation();
89 * @dataProvider getFiveOrMoreElements
91 public function testValidValuesMin($value)
93 $constraint = new Count(array('min' => 5));
94 $this->validator->validate($value, $constraint);
96 $this->assertNoViolation();
100 * @dataProvider getFourElements
102 public function testValidValuesExact($value)
104 $constraint = new Count(4);
105 $this->validator->validate($value, $constraint);
107 $this->assertNoViolation();
111 * @dataProvider getFiveOrMoreElements
113 public function testTooManyValues($value)
115 $constraint = new Count(array(
117 'maxMessage' => 'myMessage',
120 $this->validator->validate($value, $constraint);
122 $this->buildViolation('myMessage')
123 ->setParameter('{{ count }}', count($value))
124 ->setParameter('{{ limit }}', 4)
125 ->setInvalidValue($value)
127 ->setCode(Count::TOO_MANY_ERROR)
132 * @dataProvider getThreeOrLessElements
134 public function testTooFewValues($value)
136 $constraint = new Count(array(
138 'minMessage' => 'myMessage',
141 $this->validator->validate($value, $constraint);
143 $this->buildViolation('myMessage')
144 ->setParameter('{{ count }}', count($value))
145 ->setParameter('{{ limit }}', 4)
146 ->setInvalidValue($value)
148 ->setCode(Count::TOO_FEW_ERROR)
153 * @dataProvider getFiveOrMoreElements
155 public function testTooManyValuesExact($value)
157 $constraint = new Count(array(
160 'exactMessage' => 'myMessage',
163 $this->validator->validate($value, $constraint);
165 $this->buildViolation('myMessage')
166 ->setParameter('{{ count }}', count($value))
167 ->setParameter('{{ limit }}', 4)
168 ->setInvalidValue($value)
170 ->setCode(Count::TOO_MANY_ERROR)
175 * @dataProvider getThreeOrLessElements
177 public function testTooFewValuesExact($value)
179 $constraint = new Count(array(
182 'exactMessage' => 'myMessage',
185 $this->validator->validate($value, $constraint);
187 $this->buildViolation('myMessage')
188 ->setParameter('{{ count }}', count($value))
189 ->setParameter('{{ limit }}', 4)
190 ->setInvalidValue($value)
192 ->setCode(Count::TOO_FEW_ERROR)
196 public function testDefaultOption()
198 $constraint = new Count(5);
200 $this->assertEquals(5, $constraint->min);
201 $this->assertEquals(5, $constraint->max);