From 0cdacee5be5e9931fa86d0bb795e0069a6450e56 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Wed, 11 Jul 2012 19:08:03 +0200 Subject: [PATCH] [Validator] Removed MinCount and MaxCount and replaced them by the constraint Count --- src/Symfony/Component/Validator/CHANGELOG.md | 2 +- .../Component/Validator/Constraints/Count.php | 45 ++++ ...xCountValidator.php => CountValidator.php} | 30 ++- .../Validator/Constraints/MaxCount.php | 41 ---- .../Validator/Constraints/MinCount.php | 41 ---- .../Constraints/MinCountValidator.php | 50 ----- .../Resources/translations/validators.en.xlf | 4 + .../Resources/translations/validators.fr.xlf | 4 + .../Resources/translations/validators.it.xlf | 4 + .../Resources/translations/validators.nl.xlf | 4 + .../Resources/translations/validators.pt.xlf | 4 + .../translations/validators.pt_BR.xlf | 4 + .../Resources/translations/validators.ru.xlf | 4 + .../Resources/translations/validators.sl.xlf | 4 + .../translations/validators.sr_Cyrl.xlf | 4 + .../translations/validators.sr_Latn.xlf | 4 + ...ayTest.php => CountValidatorArrayTest.php} | 2 +- ...st.php => CountValidatorCountableTest.php} | 6 +- .../Tests/Constraints/CountValidatorTest.php | 195 ++++++++++++++++++ .../MaxCountValidatorArrayTest.php | 23 --- .../Constraints/MaxCountValidatorTest.php | 113 ---------- .../MinCountValidatorCountableTest.php | 38 ---- .../Constraints/MinCountValidatorTest.php | 114 ---------- 23 files changed, 308 insertions(+), 432 deletions(-) create mode 100644 src/Symfony/Component/Validator/Constraints/Count.php rename src/Symfony/Component/Validator/Constraints/{MaxCountValidator.php => CountValidator.php} (54%) delete mode 100644 src/Symfony/Component/Validator/Constraints/MaxCount.php delete mode 100644 src/Symfony/Component/Validator/Constraints/MinCount.php delete mode 100644 src/Symfony/Component/Validator/Constraints/MinCountValidator.php rename src/Symfony/Component/Validator/Tests/Constraints/{MinCountValidatorArrayTest.php => CountValidatorArrayTest.php} (87%) rename src/Symfony/Component/Validator/Tests/Constraints/{MaxCountValidatorCountableTest.php => CountValidatorCountableTest.php} (75%) create mode 100644 src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTest.php delete mode 100644 src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorArrayTest.php delete mode 100644 src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorTest.php delete mode 100644 src/Symfony/Component/Validator/Tests/Constraints/MinCountValidatorCountableTest.php delete mode 100644 src/Symfony/Component/Validator/Tests/Constraints/MinCountValidatorTest.php diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index 58296fa424..f869321899 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -21,5 +21,5 @@ CHANGELOG * [BC BREAK] collections in fields annotated with `Valid` are not traversed recursively anymore by default. `Valid` contains a new property `deep` which enables the BC behavior. - * added MinCount and MaxCount constraint * deprecated the Size constraint and renamed it to Range + * added Count constraint diff --git a/src/Symfony/Component/Validator/Constraints/Count.php b/src/Symfony/Component/Validator/Constraints/Count.php new file mode 100644 index 0000000000..db0661d0d1 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/Count.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\Exception\MissingOptionsException; + +/** + * @Annotation + * + * @api + */ +class Count extends Constraint +{ + public $minMessage = 'This collection should contain {{ limit }} elements or more.'; + public $maxMessage = 'This collection should contain {{ limit }} elements or less.'; + public $exactMessage = 'This collection should contain exactly {{ limit }} elements.'; + public $min; + public $max; + + public function __construct($options = null) + { + if (null !== $options && !is_array($options)) { + $options = array( + 'min' => $options, + 'max' => $options, + ); + } + + parent::__construct($options); + + if (null === $this->min && null === $this->max) { + throw new MissingOptionsException('Either option "min" or "max" must be given for constraint ' . __CLASS__, array('min', 'max')); + } + } +} diff --git a/src/Symfony/Component/Validator/Constraints/MaxCountValidator.php b/src/Symfony/Component/Validator/Constraints/CountValidator.php similarity index 54% rename from src/Symfony/Component/Validator/Constraints/MaxCountValidator.php rename to src/Symfony/Component/Validator/Constraints/CountValidator.php index 50efbb7f47..d4fd3ed6aa 100644 --- a/src/Symfony/Component/Validator/Constraints/MaxCountValidator.php +++ b/src/Symfony/Component/Validator/Constraints/CountValidator.php @@ -18,15 +18,13 @@ use Symfony\Component\Validator\Exception\UnexpectedTypeException; /** * @author Bernhard Schussek */ -class MaxCountValidator extends ConstraintValidator +class CountValidator extends ConstraintValidator { /** * Checks if the passed value is valid. * * @param mixed $value The value that should be validated * @param Constraint $constraint The constraint for the validation - * - * @api */ public function validate($value, Constraint $constraint) { @@ -40,11 +38,29 @@ class MaxCountValidator extends ConstraintValidator $count = count($value); - if ($count > $constraint->limit) { - $this->context->addViolation($constraint->message, array( + if ($constraint->min == $constraint->max && $count != $constraint->min) { + $this->context->addViolation($constraint->exactMessage, array( '{{ count }}' => $count, - '{{ limit }}' => $constraint->limit, - ), $value, (int) $constraint->limit); + '{{ limit }}' => $constraint->min, + ), $value, (int) $constraint->min); + + return; + } + + if (null !== $constraint->max && $count > $constraint->max) { + $this->context->addViolation($constraint->maxMessage, array( + '{{ count }}' => $count, + '{{ limit }}' => $constraint->max, + ), $value, (int) $constraint->max); + + return; + } + + if (null !== $constraint->min && $count < $constraint->min) { + $this->context->addViolation($constraint->minMessage, array( + '{{ count }}' => $count, + '{{ limit }}' => $constraint->min, + ), $value, (int) $constraint->min); } } } diff --git a/src/Symfony/Component/Validator/Constraints/MaxCount.php b/src/Symfony/Component/Validator/Constraints/MaxCount.php deleted file mode 100644 index c3286033a0..0000000000 --- a/src/Symfony/Component/Validator/Constraints/MaxCount.php +++ /dev/null @@ -1,41 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Validator\Constraints; - -use Symfony\Component\Validator\Constraint; - -/** - * @Annotation - * - * @api - */ -class MaxCount extends Constraint -{ - public $message = 'This collection should contain {{ limit }} elements or less.'; - public $limit; - - /** - * {@inheritDoc} - */ - public function getDefaultOption() - { - return 'limit'; - } - - /** - * {@inheritDoc} - */ - public function getRequiredOptions() - { - return array('limit'); - } -} diff --git a/src/Symfony/Component/Validator/Constraints/MinCount.php b/src/Symfony/Component/Validator/Constraints/MinCount.php deleted file mode 100644 index ebf1ff5d47..0000000000 --- a/src/Symfony/Component/Validator/Constraints/MinCount.php +++ /dev/null @@ -1,41 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Validator\Constraints; - -use Symfony\Component\Validator\Constraint; - -/** - * @Annotation - * - * @api - */ -class MinCount extends Constraint -{ - public $message = 'This collection should contain {{ limit }} elements or more.'; - public $limit; - - /** - * {@inheritDoc} - */ - public function getDefaultOption() - { - return 'limit'; - } - - /** - * {@inheritDoc} - */ - public function getRequiredOptions() - { - return array('limit'); - } -} diff --git a/src/Symfony/Component/Validator/Constraints/MinCountValidator.php b/src/Symfony/Component/Validator/Constraints/MinCountValidator.php deleted file mode 100644 index 1620caa4b3..0000000000 --- a/src/Symfony/Component/Validator/Constraints/MinCountValidator.php +++ /dev/null @@ -1,50 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Validator\Constraints; - -use Symfony\Component\Validator\Constraint; -use Symfony\Component\Validator\ConstraintValidator; -use Symfony\Component\Validator\Exception\UnexpectedTypeException; - -/** - * @author Bernhard Schussek - */ -class MinCountValidator extends ConstraintValidator -{ - /** - * Checks if the passed value is valid. - * - * @param mixed $value The value that should be validated - * @param Constraint $constraint The constraint for the validation - * - * @throws UnexpectedTypeException If the given value is no array or \Countable. - */ - public function validate($value, Constraint $constraint) - { - if (null === $value) { - return; - } - - if (!is_array($value) && !$value instanceof \Countable) { - throw new UnexpectedTypeException($value, 'array or \Countable'); - } - - $count = count($value); - - if ($count < $constraint->limit) { - $this->context->addViolation($constraint->message, array( - '{{ count }}' => $count, - '{{ limit }}' => $constraint->limit, - ), $value, (int) $constraint->limit); - } - } -} diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf index e2e6d24300..b95ec611fe 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf @@ -210,6 +210,10 @@ This collection should contain {{ limit }} elements or less. This collection should contain {{ limit }} element or less.|This collection should contain {{ limit }} elements or less. + + This collection should contain exactly {{ limit }} elements. + This collection should contain exactly {{ limit }} element.|This collection should contain exactly {{ limit }} elements. + diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.fr.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.fr.xlf index d2e2d78159..1816ebeb24 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.fr.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.fr.xlf @@ -210,6 +210,10 @@ This collection should contain {{ limit }} elements or less. Cette collection doit contenir {{ limit }} élément ou moins.|Cette collection doit contenir {{ limit }} éléments ou moins. + + This collection should contain exactly {{ limit }} elements. + Cette collection doit contenir exactement {{ limit }} élément.|Cette collection doit contenir exactement {{ limit }} éléments. + diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.it.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.it.xlf index ee373c1958..b23a3cdeb9 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.it.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.it.xlf @@ -210,6 +210,10 @@ This collection should contain {{ limit }} elements or less. Questa collezione dovrebbe contenere massimo {{ limit }} elemento.|Questa collezione dovrebbe contenere massimo {{ limit }} elementi. + + This collection should contain exactly {{ limit }} elements. + Questa collezione dovrebbe contenere esattamente {{ limit }} elemento.|Questa collezione dovrebbe contenere esattamente {{ limit }} elementi. + diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.nl.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.nl.xlf index 864a726d01..90bde0b0d6 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.nl.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.nl.xlf @@ -210,6 +210,10 @@ This collection should contain {{ limit }} elements or less. Deze collectie moet {{ limit }} element of minder bevatten.|Deze collectie moet {{ limit }} elementen of minder bevatten. + + This collection should contain exactly {{ limit }} elements. + Deze collectie moet exact {{ limit }} element bevatten.|Deze collectie moet exact {{ limit }} elementen bevatten. + diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.pt.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.pt.xlf index c44f5b9328..bbe344756f 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.pt.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.pt.xlf @@ -210,6 +210,10 @@ This collection should contain {{ limit }} elements or less. Esta coleção deve conter {{ limit }} elemento ou menos.|Esta coleção deve conter {{ limit }} elementos ou menos. + + This collection should contain exactly {{ limit }} elements. + Esta coleção deve conter exatamente {{ limit }} elemento.|Esta coleção deve conter exatamente {{ limit }} elementos. + diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.pt_BR.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.pt_BR.xlf index d7822ac789..6195bbbb6e 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.pt_BR.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.pt_BR.xlf @@ -210,6 +210,10 @@ This collection should contain {{ limit }} elements or less. Esta coleção deve conter {{ limit }} elemento ou menos.|Esta coleção deve conter {{ limit }} elementos ou menos. + + This collection should contain exactly {{ limit }} elements. + Esta coleção deve conter exatamente {{ limit }} elemento.|Esta coleção deve conter exatamente {{ limit }} elementos. + diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.ru.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.ru.xlf index 141a93b65d..351760041a 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.ru.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.ru.xlf @@ -210,6 +210,10 @@ This collection should contain {{ limit }} elements or less. Эта коллекция должна содержать {{ limit }} элемент или меньше.|Эта коллекция должна содержать {{ limit }} элемента или меньше.|Эта коллекция должна содержать {{ limit }} элементов или меньше. + + This collection should contain exactly {{ limit }} elements. + Эта коллекция должна содержать ровно {{ limit }} элемент.|Эта коллекция должна содержать ровно {{ limit }} элемента.|Эта коллекция должна содержать ровно {{ limit }} элементов. + diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.sl.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.sl.xlf index 959db51fe3..fa37c289f8 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.sl.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.sl.xlf @@ -210,6 +210,10 @@ This collection should contain {{ limit }} elements or less. Ta zbirka bi morala vsebovati {{ limit }} ali manj elementov. + + This collection should contain exactly {{ limit }} elements. + Ta zbirka bi morala vsebovati točno {{ limit }} element.|Ta zbirka bi morala vsebovati točno {{ limit }} elementa.|Ta zbirka bi morala vsebovati točno {{ limit }} elemente.|Ta zbirka bi morala vsebovati točno {{ limit }} elementov. + diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.sr_Cyrl.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.sr_Cyrl.xlf index ed1b1041e6..1636b330a9 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.sr_Cyrl.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.sr_Cyrl.xlf @@ -210,6 +210,10 @@ This collection should contain {{ limit }} elements or less. Ова колекција треба да садржи {{ limit }} или мање елемената.|Ова колекција треба да садржи {{ limit }} или мање елемената.|Ова колекција треба да садржи {{ limit }} или мање елемената. + + This collection should contain exactly {{ limit }} elements. + Ова колекција треба да садржи тачно {{ limit }} елемент.|Ова колекција треба да садржи тачно {{ limit }} елемента.|Ова колекција треба да садржи тачно {{ limit }} елемената. + diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.sr_Latn.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.sr_Latn.xlf index a956250e44..cf4666de30 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.sr_Latn.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.sr_Latn.xlf @@ -210,6 +210,10 @@ This collection should contain {{ limit }} elements or less. Ova kolekcija treba da sadrži {{ limit }} ili manje elemenata.|Ova kolekcija treba da sadrži {{ limit }} ili manje elemenata.|Ova kolekcija treba da sadrži {{ limit }} ili manje elemenata. + + This collection should contain exactly {{ limit }} elements. + Ova kolekcija treba da sadrži tačno {{ limit }} element.|Ova kolekcija treba da sadrži tačno {{ limit }} elementa.|Ova kolekcija treba da sadrži tačno {{ limit }} elemenata. + diff --git a/src/Symfony/Component/Validator/Tests/Constraints/MinCountValidatorArrayTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorArrayTest.php similarity index 87% rename from src/Symfony/Component/Validator/Tests/Constraints/MinCountValidatorArrayTest.php rename to src/Symfony/Component/Validator/Tests/Constraints/CountValidatorArrayTest.php index 96eb63a157..5f562e7445 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/MinCountValidatorArrayTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorArrayTest.php @@ -14,7 +14,7 @@ namespace Symfony\Component\Validator\Tests\Constraints; /** * @author Bernhard Schussek */ -class MinCountValidatorArrayTest extends MinCountValidatorTest +class CountValidatorArrayTest extends CountValidatorTest { protected function createCollection(array $content) { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorCountableTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorCountableTest.php similarity index 75% rename from src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorCountableTest.php rename to src/Symfony/Component/Validator/Tests/Constraints/CountValidatorCountableTest.php index c4852e00d3..ec4d8dec71 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorCountableTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorCountableTest.php @@ -11,7 +11,7 @@ namespace Symfony\Component\Validator\Tests\Constraints; -class MaxCountValidatorCountableTest_Countable implements \Countable +class CountValidatorCountableTest_Countable implements \Countable { private $content; @@ -29,10 +29,10 @@ class MaxCountValidatorCountableTest_Countable implements \Countable /** * @author Bernhard Schussek */ -class MaxCountValidatorCountableTest extends MaxCountValidatorTest +class CountValidatorCountableTest extends CountValidatorTest { protected function createCollection(array $content) { - return new MaxCountValidatorCountableTest_Countable($content); + return new CountValidatorCountableTest_Countable($content); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTest.php new file mode 100644 index 0000000000..c7d29c6d55 --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTest.php @@ -0,0 +1,195 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Tests\Constraints; + +use Symfony\Component\Validator\Constraints\Count; +use Symfony\Component\Validator\Constraints\CountValidator; + +/** + * @author Bernhard Schussek + */ +abstract class CountValidatorTest extends \PHPUnit_Framework_TestCase +{ + protected $context; + protected $validator; + + protected function setUp() + { + $this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false); + $this->validator = new CountValidator(); + $this->validator->initialize($this->context); + } + + protected function tearDown() + { + $this->context = null; + $this->validator = null; + } + + abstract protected function createCollection(array $content); + + public function testNullIsValid() + { + $this->context->expects($this->never()) + ->method('addViolation'); + + $this->validator->validate(null, new Count(6)); + } + + /** + * @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException + */ + public function testExpectsCountableType() + { + $this->validator->validate(new \stdClass(), new Count(5)); + } + + public function getThreeOrLessElements() + { + return array( + array($this->createCollection(array(1))), + array($this->createCollection(array(1, 2))), + array($this->createCollection(array(1, 2, 3))), + array($this->createCollection(array('a' => 1, 'b' => 2, 'c' => 3))), + ); + } + + public function getFourElements() + { + return array( + array($this->createCollection(array(1, 2, 3, 4))), + array($this->createCollection(array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4))), + ); + } + + public function getNotFourElements() + { + return array_merge( + $this->getThreeOrLessElements(), + $this->getFiveOrMoreElements() + ); + } + + public function getFiveOrMoreElements() + { + return array( + array($this->createCollection(array(1, 2, 3, 4, 5))), + array($this->createCollection(array(1, 2, 3, 4, 5, 6))), + array($this->createCollection(array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5))), + ); + } + + /** + * @dataProvider getThreeOrLessElements + */ + public function testValidValuesMax($value) + { + $this->context->expects($this->never()) + ->method('addViolation'); + + $constraint = new Count(array('max' => 3)); + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getFiveOrMoreElements + */ + public function testValidValuesMin($value) + { + $this->context->expects($this->never()) + ->method('addViolation'); + + $constraint = new Count(array('min' => 5)); + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getFourElements + */ + public function testValidValuesExact($value) + { + $this->context->expects($this->never()) + ->method('addViolation'); + + $constraint = new Count(4); + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getFiveOrMoreElements + */ + public function testInvalidValuesMax($value) + { + $constraint = new Count(array( + 'max' => 4, + 'maxMessage' => 'myMessage' + )); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMessage', $this->identicalTo(array( + '{{ count }}' => count($value), + '{{ limit }}' => 4, + )), $value, 4); + + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getThreeOrLessElements + */ + public function testInvalidValuesMin($value) + { + $constraint = new Count(array( + 'min' => 4, + 'minMessage' => 'myMessage' + )); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMessage', $this->identicalTo(array( + '{{ count }}' => count($value), + '{{ limit }}' => 4, + )), $value, 4); + + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getNotFourElements + */ + public function testInvalidValuesExact($value) + { + $constraint = new Count(array( + 'min' => 4, + 'max' => 4, + 'exactMessage' => 'myMessage' + )); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMessage', $this->identicalTo(array( + '{{ count }}' => count($value), + '{{ limit }}' => 4, + )), $value, 4); + + $this->validator->validate($value, $constraint); + } + + public function testDefaultOption() + { + $constraint = new Count(5); + + $this->assertEquals(5, $constraint->min); + $this->assertEquals(5, $constraint->max); + } +} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorArrayTest.php b/src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorArrayTest.php deleted file mode 100644 index 8453930ae7..0000000000 --- a/src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorArrayTest.php +++ /dev/null @@ -1,23 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Validator\Tests\Constraints; - -/** - * @author Bernhard Schussek - */ -class MaxCountValidatorArrayTest extends MaxCountValidatorTest -{ - protected function createCollection(array $content) - { - return $content; - } -} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorTest.php deleted file mode 100644 index f6ef305ffe..0000000000 --- a/src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorTest.php +++ /dev/null @@ -1,113 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Validator\Tests\Constraints; - -use Symfony\Component\Validator\Constraints\MaxCount; -use Symfony\Component\Validator\Constraints\MaxCountValidator; - -/** - * @author Bernhard Schussek - */ -abstract class MaxCountValidatorTest extends \PHPUnit_Framework_TestCase -{ - protected $context; - protected $validator; - - protected function setUp() - { - $this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false); - $this->validator = new MaxCountValidator(); - $this->validator->initialize($this->context); - } - - protected function tearDown() - { - $this->context = null; - $this->validator = null; - } - - abstract protected function createCollection(array $content); - - public function testNullIsValid() - { - $this->context->expects($this->never()) - ->method('addViolation'); - - $this->validator->validate(null, new MaxCount(6)); - } - - /** - * @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException - */ - public function testExpectsCountableType() - { - $this->validator->validate(new \stdClass(), new MaxCount(5)); - } - - /** - * @dataProvider getValidValues - */ - public function testValidValues($value) - { - $this->context->expects($this->never()) - ->method('addViolation'); - - $constraint = new MaxCount(3); - $this->validator->validate($value, $constraint); - } - - public function getValidValues() - { - return array( - array($this->createCollection(array(1))), - array($this->createCollection(array(1, 2))), - array($this->createCollection(array(1, 2, 3))), - array($this->createCollection(array('a' => 1, 'b' => 2, 'c' => 3))), - ); - } - - /** - * @dataProvider getInvalidValues - */ - public function testInvalidValues($value) - { - $constraint = new MaxCount(array( - 'limit' => 3, - 'message' => 'myMessage' - )); - - $this->context->expects($this->once()) - ->method('addViolation') - ->with('myMessage', $this->identicalTo(array( - '{{ count }}' => count($value), - '{{ limit }}' => 3, - )), $value, 3); - - $this->validator->validate($value, $constraint); - } - - public function getInvalidValues() - { - return array( - array($this->createCollection(array(1, 2, 3, 4))), - array($this->createCollection(array(1, 2, 3, 4, 5))), - array($this->createCollection(array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4))), - ); - } - - public function testDefaultOption() - { - $constraint = new MaxCount(5); - - $this->assertEquals(5, $constraint->limit); - } -} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/MinCountValidatorCountableTest.php b/src/Symfony/Component/Validator/Tests/Constraints/MinCountValidatorCountableTest.php deleted file mode 100644 index 48b175a65d..0000000000 --- a/src/Symfony/Component/Validator/Tests/Constraints/MinCountValidatorCountableTest.php +++ /dev/null @@ -1,38 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Validator\Tests\Constraints; - -class MinCountValidatorCountableTest_Countable implements \Countable -{ - private $content; - - public function __construct(array $content) - { - $this->content = $content; - } - - public function count() - { - return count($this->content); - } -} - -/** - * @author Bernhard Schussek - */ -class MinCountValidatorCountableTest extends MinCountValidatorTest -{ - protected function createCollection(array $content) - { - return new MinCountValidatorCountableTest_Countable($content); - } -} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/MinCountValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/MinCountValidatorTest.php deleted file mode 100644 index ab0cf86eaa..0000000000 --- a/src/Symfony/Component/Validator/Tests/Constraints/MinCountValidatorTest.php +++ /dev/null @@ -1,114 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Validator\Tests\Constraints; - -use Symfony\Component\Validator\Constraints\MinCount; -use Symfony\Component\Validator\Constraints\MinCountValidator; - -/** - * @author Bernhard Schussek - */ -abstract class MinCountValidatorTest extends \PHPUnit_Framework_TestCase -{ - protected $context; - protected $validator; - - protected function setUp() - { - $this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false); - $this->validator = new MinCountValidator(); - $this->validator->initialize($this->context); - } - - protected function tearDown() - { - $this->context = null; - $this->validator = null; - } - - abstract protected function createCollection(array $content); - - public function testNullIsValid() - { - $this->context->expects($this->never()) - ->method('addViolation'); - - $this->validator->validate(null, new MinCount(6)); - } - - /** - * @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException - */ - public function testExpectsCountableType() - { - $this->validator->validate(new \stdClass(), new MinCount(5)); - } - - /** - * @dataProvider getValidValues - */ - public function testValidValues($value) - { - $this->context->expects($this->never()) - ->method('addViolation'); - - $constraint = new MinCount(3); - $this->validator->validate($value, $constraint); - } - - public function getValidValues() - { - return array( - array($this->createCollection(array(1, 2, 3))), - array($this->createCollection(array(1, 2, 3, 4))), - array($this->createCollection(array(1, 2, 3, 4, 5))), - array($this->createCollection(array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4))), - ); - } - - /** - * @dataProvider getInvalidValues - */ - public function testInvalidValues($value) - { - $constraint = new MinCount(array( - 'limit' => 4, - 'message' => 'myMessage' - )); - - $this->context->expects($this->once()) - ->method('addViolation') - ->with('myMessage', $this->identicalTo(array( - '{{ count }}' => count($value), - '{{ limit }}' => 4, - )), $value, 4); - - $this->validator->validate($value, $constraint); - } - - public function getInvalidValues() - { - return array( - array($this->createCollection(array(1))), - array($this->createCollection(array(1, 2))), - array($this->createCollection(array(1, 2, 3))), - array($this->createCollection(array('a' => 1, 'b' => 2, 'c' => 3))), - ); - } - - public function testDefaultOption() - { - $constraint = new MinCount(5); - - $this->assertEquals(5, $constraint->limit); - } -}