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\Test\ConstraintValidatorTestCase;
19 * @author Bernhard Schussek <bschussek@gmail.com>
21 abstract class CountValidatorTest extends ConstraintValidatorTestCase
23 protected function createValidator()
25 return new CountValidator();
28 abstract protected function createCollection(array $content);
30 public function testNullIsValid()
32 $this->validator->validate(null, new Count(6));
34 $this->assertNoViolation();
38 * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
40 public function testExpectsCountableType()
42 $this->validator->validate(new \stdClass(), new Count(5));
45 public function getThreeOrLessElements()
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))),
55 public function getFourElements()
58 array($this->createCollection(array(1, 2, 3, 4))),
59 array($this->createCollection(array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4))),
63 public function getFiveOrMoreElements()
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))),
73 * @dataProvider getThreeOrLessElements
75 public function testValidValuesMax($value)
77 $constraint = new Count(array('max' => 3));
78 $this->validator->validate($value, $constraint);
80 $this->assertNoViolation();
84 * @dataProvider getFiveOrMoreElements
86 public function testValidValuesMin($value)
88 $constraint = new Count(array('min' => 5));
89 $this->validator->validate($value, $constraint);
91 $this->assertNoViolation();
95 * @dataProvider getFourElements
97 public function testValidValuesExact($value)
99 $constraint = new Count(4);
100 $this->validator->validate($value, $constraint);
102 $this->assertNoViolation();
106 * @dataProvider getFiveOrMoreElements
108 public function testTooManyValues($value)
110 $constraint = new Count(array(
112 'maxMessage' => 'myMessage',
115 $this->validator->validate($value, $constraint);
117 $this->buildViolation('myMessage')
118 ->setParameter('{{ count }}', \count($value))
119 ->setParameter('{{ limit }}', 4)
120 ->setInvalidValue($value)
122 ->setCode(Count::TOO_MANY_ERROR)
127 * @dataProvider getThreeOrLessElements
129 public function testTooFewValues($value)
131 $constraint = new Count(array(
133 'minMessage' => 'myMessage',
136 $this->validator->validate($value, $constraint);
138 $this->buildViolation('myMessage')
139 ->setParameter('{{ count }}', \count($value))
140 ->setParameter('{{ limit }}', 4)
141 ->setInvalidValue($value)
143 ->setCode(Count::TOO_FEW_ERROR)
148 * @dataProvider getFiveOrMoreElements
150 public function testTooManyValuesExact($value)
152 $constraint = new Count(array(
155 'exactMessage' => 'myMessage',
158 $this->validator->validate($value, $constraint);
160 $this->buildViolation('myMessage')
161 ->setParameter('{{ count }}', \count($value))
162 ->setParameter('{{ limit }}', 4)
163 ->setInvalidValue($value)
165 ->setCode(Count::TOO_MANY_ERROR)
170 * @dataProvider getThreeOrLessElements
172 public function testTooFewValuesExact($value)
174 $constraint = new Count(array(
177 'exactMessage' => 'myMessage',
180 $this->validator->validate($value, $constraint);
182 $this->buildViolation('myMessage')
183 ->setParameter('{{ count }}', \count($value))
184 ->setParameter('{{ limit }}', 4)
185 ->setInvalidValue($value)
187 ->setCode(Count::TOO_FEW_ERROR)
191 public function testDefaultOption()
193 $constraint = new Count(5);
195 $this->assertEquals(5, $constraint->min);
196 $this->assertEquals(5, $constraint->max);