diff --git a/UPGRADE-2.1.md b/UPGRADE-2.1.md index ef8756ab0a..3ba03d1ea1 100644 --- a/UPGRADE-2.1.md +++ b/UPGRADE-2.1.md @@ -1136,8 +1136,8 @@ private $recursiveCollection; ``` - * The `Size` constraint was deprecated and will be removed in Symfony 2.3. You should - use the constraints `Min` and `Max` instead. + * The `Size`, `Min` and `Max` constraints were deprecated and will be removed in + Symfony 2.3. You should use the new constraint `Range` instead. Before: @@ -1149,13 +1149,41 @@ After: ``` - /** - * @Assert\Min(2) - * @Assert\Max(16) - */ + /** @Assert\Range(min = 2, max = 16) */ private $numberOfCpus; ``` + Before: + + ``` + /** @Assert\Min(2) */ + private $numberOfCpus; + ``` + + After: + + ``` + /** @Assert\Range(min = 2) */ + private $numberOfCpus; + ``` + + * The `MinLength` and `MaxLength` constraints were deprecated and will be + removed in Symfony 2.3. You should use the new constraint `Length` instead. + + Before: + + ``` + /** @Assert\MinLength(8) */ + private $password; + ``` + + After: + + ``` + /** @Assert\Length(min = 8) */ + private $password; + ``` + ### Session * Flash messages now return an array based on their type. The old method is diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index a1fd056d12..dbc3e0b0d7 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -21,5 +21,8 @@ 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 + * added Count constraint + * added Length constraint + * deprecated the Size constraint and renamed it to Range + * deprecated the Min and Max constraints + * deprecated the MinLength and MaxLength constraints 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/Length.php b/src/Symfony/Component/Validator/Constraints/Length.php new file mode 100644 index 0000000000..70554c0686 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/Length.php @@ -0,0 +1,46 @@ + + * + * 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 Length extends Constraint +{ + public $maxMessage = 'This value is too long. It should have {{ limit }} characters or less.'; + public $minMessage = 'This value is too short. It should have {{ limit }} characters or more.'; + public $exactMessage = 'This value should have exactly {{ limit }} characters.'; + public $max; + public $min; + public $charset = 'UTF-8'; + + 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/LengthValidator.php b/src/Symfony/Component/Validator/Constraints/LengthValidator.php new file mode 100644 index 0000000000..ee89e75477 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/LengthValidator.php @@ -0,0 +1,74 @@ + + * + * 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 LengthValidator 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 + */ + public function validate($value, Constraint $constraint) + { + if (null === $value || '' === $value) { + return; + } + + if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) { + throw new UnexpectedTypeException($value, 'string'); + } + + $stringValue = (string) $value; + + if (function_exists('grapheme_strlen') && 'UTF-8' === $constraint->charset) { + $length = grapheme_strlen($stringValue); + } elseif (function_exists('mb_strlen')) { + $length = mb_strlen($stringValue, $constraint->charset); + } else { + $length = strlen($stringValue); + } + + if ($constraint->min == $constraint->max && $length != $constraint->min) { + $this->context->addViolation($constraint->exactMessage, array( + '{{ value }}' => $stringValue, + '{{ limit }}' => $constraint->min, + ), $value, (int) $constraint->min); + + return; + } + + if (null !== $constraint->max && $length > $constraint->max) { + $this->context->addViolation($constraint->maxMessage, array( + '{{ value }}' => $stringValue, + '{{ limit }}' => $constraint->max, + ), $value, (int) $constraint->max); + + return; + } + + if (null !== $constraint->min && $length < $constraint->min) { + $this->context->addViolation($constraint->minMessage, array( + '{{ value }}' => $stringValue, + '{{ limit }}' => $constraint->min, + ), $value, (int) $constraint->min); + } + } +} diff --git a/src/Symfony/Component/Validator/Constraints/Max.php b/src/Symfony/Component/Validator/Constraints/Max.php index ffb958e85b..814fc1f339 100644 --- a/src/Symfony/Component/Validator/Constraints/Max.php +++ b/src/Symfony/Component/Validator/Constraints/Max.php @@ -17,6 +17,8 @@ use Symfony\Component\Validator\Constraint; * @Annotation * * @api + * + * @deprecated Deprecated since version 2.1, to be removed in 2.3. */ class Max extends Constraint { 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/MaxLength.php b/src/Symfony/Component/Validator/Constraints/MaxLength.php index c928bd5bfd..765d548060 100644 --- a/src/Symfony/Component/Validator/Constraints/MaxLength.php +++ b/src/Symfony/Component/Validator/Constraints/MaxLength.php @@ -17,6 +17,8 @@ use Symfony\Component\Validator\Constraint; * @Annotation * * @api + * + * @deprecated Deprecated since version 2.1, to be removed in 2.3. */ class MaxLength extends Constraint { diff --git a/src/Symfony/Component/Validator/Constraints/MaxLengthValidator.php b/src/Symfony/Component/Validator/Constraints/MaxLengthValidator.php index e7f8d72758..076734cd40 100644 --- a/src/Symfony/Component/Validator/Constraints/MaxLengthValidator.php +++ b/src/Symfony/Component/Validator/Constraints/MaxLengthValidator.php @@ -19,6 +19,8 @@ use Symfony\Component\Validator\Exception\UnexpectedTypeException; * @author Bernhard Schussek * * @api + * + * @deprecated Deprecated since version 2.1, to be removed in 2.3. */ class MaxLengthValidator extends ConstraintValidator { diff --git a/src/Symfony/Component/Validator/Constraints/MaxValidator.php b/src/Symfony/Component/Validator/Constraints/MaxValidator.php index 18d8e8902e..e3e9f8c462 100644 --- a/src/Symfony/Component/Validator/Constraints/MaxValidator.php +++ b/src/Symfony/Component/Validator/Constraints/MaxValidator.php @@ -18,6 +18,8 @@ use Symfony\Component\Validator\ConstraintValidator; * @author Bernhard Schussek * * @api + * + * @deprecated Deprecated since version 2.1, to be removed in 2.3. */ class MaxValidator extends ConstraintValidator { diff --git a/src/Symfony/Component/Validator/Constraints/Min.php b/src/Symfony/Component/Validator/Constraints/Min.php index 78a4833296..6e8b3649b7 100644 --- a/src/Symfony/Component/Validator/Constraints/Min.php +++ b/src/Symfony/Component/Validator/Constraints/Min.php @@ -17,6 +17,8 @@ use Symfony\Component\Validator\Constraint; * @Annotation * * @api + * + * @deprecated Deprecated since version 2.1, to be removed in 2.3. */ class Min extends Constraint { 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/Constraints/MinLength.php b/src/Symfony/Component/Validator/Constraints/MinLength.php index 1ee15906a6..4457f6c160 100644 --- a/src/Symfony/Component/Validator/Constraints/MinLength.php +++ b/src/Symfony/Component/Validator/Constraints/MinLength.php @@ -17,6 +17,8 @@ use Symfony\Component\Validator\Constraint; * @Annotation * * @api + * + * @deprecated Deprecated since version 2.1, to be removed in 2.3. */ class MinLength extends Constraint { diff --git a/src/Symfony/Component/Validator/Constraints/MinLengthValidator.php b/src/Symfony/Component/Validator/Constraints/MinLengthValidator.php index 77d833e998..fc8cef537a 100644 --- a/src/Symfony/Component/Validator/Constraints/MinLengthValidator.php +++ b/src/Symfony/Component/Validator/Constraints/MinLengthValidator.php @@ -19,6 +19,8 @@ use Symfony\Component\Validator\Exception\UnexpectedTypeException; * @author Bernhard Schussek * * @api + * + * @deprecated Deprecated since version 2.1, to be removed in 2.3. */ class MinLengthValidator extends ConstraintValidator { diff --git a/src/Symfony/Component/Validator/Constraints/MinValidator.php b/src/Symfony/Component/Validator/Constraints/MinValidator.php index a91d873864..c907793c62 100644 --- a/src/Symfony/Component/Validator/Constraints/MinValidator.php +++ b/src/Symfony/Component/Validator/Constraints/MinValidator.php @@ -18,6 +18,8 @@ use Symfony\Component\Validator\ConstraintValidator; * @author Bernhard Schussek * * @api + * + * @deprecated Deprecated since version 2.1, to be removed in 2.3. */ class MinValidator extends ConstraintValidator { diff --git a/src/Symfony/Component/Validator/Constraints/Range.php b/src/Symfony/Component/Validator/Constraints/Range.php new file mode 100644 index 0000000000..20039f5972 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/Range.php @@ -0,0 +1,38 @@ + + * + * 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 Range extends Constraint +{ + public $minMessage = 'This value should be {{ limit }} or more'; + public $maxMessage = 'This value should be {{ limit }} or less'; + public $invalidMessage = 'This value should be a valid number'; + public $min; + public $max; + + public function __construct($options = null) + { + 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/RangeValidator.php b/src/Symfony/Component/Validator/Constraints/RangeValidator.php new file mode 100644 index 0000000000..4c30388c86 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/RangeValidator.php @@ -0,0 +1,60 @@ + + * + * 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; + +/** + * @author Bernhard Schussek + */ +class RangeValidator 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 + * + * @return Boolean Whether or not the value is valid + */ + public function validate($value, Constraint $constraint) + { + if (null === $value) { + return; + } + + if (!is_numeric($value)) { + $this->context->addViolation($constraint->invalidMessage, array( + '{{ value }}' => $value, + )); + + return; + } + + if (null !== $constraint->max && $value > $constraint->max) { + $this->context->addViolation($constraint->maxMessage, array( + '{{ value }}' => $value, + '{{ limit }}' => $constraint->max, + )); + + return; + } + + if (null !== $constraint->min && $value < $constraint->min) { + $this->context->addViolation($constraint->minMessage, array( + '{{ value }}' => $value, + '{{ limit }}' => $constraint->min, + )); + } + } +} diff --git a/src/Symfony/Component/Validator/Constraints/Size.php b/src/Symfony/Component/Validator/Constraints/Size.php index b72633eef2..625f86e528 100644 --- a/src/Symfony/Component/Validator/Constraints/Size.php +++ b/src/Symfony/Component/Validator/Constraints/Size.php @@ -20,19 +20,13 @@ use Symfony\Component\Validator\Constraint; * * @deprecated Deprecated since version 2.1, to be removed in 2.3. */ -class Size extends Constraint +class Size extends Range { - public $minMessage = 'This value should be {{ limit }} or more'; - public $maxMessage = 'This value should be {{ limit }} or less'; - public $invalidMessage = 'This value should be a valid number'; - public $min; - public $max; - /** * {@inheritDoc} */ - public function getRequiredOptions() + public function validatedBy() { - return array('min', 'max'); + return get_parent_class($this).'Validator'; } } diff --git a/src/Symfony/Component/Validator/Constraints/SizeValidator.php b/src/Symfony/Component/Validator/Constraints/SizeValidator.php index f26a0ec7d9..605e74b5c1 100644 --- a/src/Symfony/Component/Validator/Constraints/SizeValidator.php +++ b/src/Symfony/Component/Validator/Constraints/SizeValidator.php @@ -21,46 +21,6 @@ use Symfony\Component\Validator\ConstraintValidator; * * @deprecated Deprecated since version 2.1, to be removed in 2.3. */ -class SizeValidator extends ConstraintValidator +class SizeValidator extends RangeValidator { - /** - * Checks if the passed value is valid. - * - * @param mixed $value The value that should be validated - * @param Constraint $constraint The constraint for the validation - * - * @return Boolean Whether or not the value is valid - * - * @api - */ - public function validate($value, Constraint $constraint) - { - if (null === $value) { - return; - } - - if (!is_numeric($value)) { - $this->context->addViolation($constraint->invalidMessage, array( - '{{ value }}' => $value, - )); - - return; - } - - if ($value > $constraint->max) { - $this->context->addViolation($constraint->maxMessage, array( - '{{ value }}' => $value, - '{{ limit }}' => $constraint->max, - )); - - return; - } - - if ($value < $constraint->min) { - $this->context->addViolation($constraint->minMessage, array( - '{{ value }}' => $value, - '{{ limit }}' => $constraint->min, - )); - } - } } 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/LengthValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php new file mode 100644 index 0000000000..dbcef694e6 --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php @@ -0,0 +1,233 @@ + + * + * 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\Length; +use Symfony\Component\Validator\Constraints\LengthValidator; + +class LengthValidatorTest 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 LengthValidator(); + $this->validator->initialize($this->context); + } + + protected function tearDown() + { + $this->context = null; + $this->validator = null; + } + + public function testNullIsValid() + { + $this->context->expects($this->never()) + ->method('addViolation'); + + $this->validator->validate(null, new Length(6)); + } + + public function testEmptyStringIsValid() + { + $this->context->expects($this->never()) + ->method('addViolation'); + + $this->validator->validate('', new Length(6)); + } + + /** + * @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException + */ + public function testExpectsStringCompatibleType() + { + $this->validator->validate(new \stdClass(), new Length(5)); + } + + public function getThreeOrLessCharacters() + { + return array( + array(12), + array('12'), + array('üü', true), + array('éé', true), + array(123), + array('123'), + array('üüü', true), + array('ééé', true), + ); + } + + public function getFourCharacters() + { + return array( + array(1234), + array('1234'), + array('üüüü', true), + array('éééé', true), + ); + } + + public function getNotFourCharacters() + { + return array_merge( + $this->getThreeOrLessCharacters(), + $this->getFiveOrMoreCharacters() + ); + } + + public function getFiveOrMoreCharacters() + { + return array( + array(12345), + array('12345'), + array('üüüüü', true), + array('ééééé', true), + array(123456), + array('123456'), + array('üüüüüü', true), + array('éééééé', true), + ); + } + + /** + * @dataProvider getFiveOrMoreCharacters + */ + public function testValidValuesMin($value, $mbOnly = false) + { + if ($mbOnly && !function_exists('mb_strlen')) { + return $this->markTestSkipped('mb_strlen does not exist'); + } + + $this->context->expects($this->never()) + ->method('addViolation'); + + $constraint = new Length(array('min' => 5)); + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getThreeOrLessCharacters + */ + public function testValidValuesMax($value, $mbOnly = false) + { + if ($mbOnly && !function_exists('mb_strlen')) { + return $this->markTestSkipped('mb_strlen does not exist'); + } + + $this->context->expects($this->never()) + ->method('addViolation'); + + $constraint = new Length(array('max' => 3)); + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getFourCharacters + */ + public function testValidValuesExact($value, $mbOnly = false) + { + if ($mbOnly && !function_exists('mb_strlen')) { + return $this->markTestSkipped('mb_strlen does not exist'); + } + + $this->context->expects($this->never()) + ->method('addViolation'); + + $constraint = new Length(4); + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getThreeOrLessCharacters + */ + public function testInvalidValuesMin($value, $mbOnly = false) + { + if ($mbOnly && !function_exists('mb_strlen')) { + return $this->markTestSkipped('mb_strlen does not exist'); + } + + $constraint = new Length(array( + 'min' => 4, + 'minMessage' => 'myMessage' + )); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMessage', $this->identicalTo(array( + '{{ value }}' => (string) $value, + '{{ limit }}' => 4, + )), $this->identicalTo($value), 4); + + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getFiveOrMoreCharacters + */ + public function testInvalidValuesMax($value, $mbOnly = false) + { + if ($mbOnly && !function_exists('mb_strlen')) { + return $this->markTestSkipped('mb_strlen does not exist'); + } + + $constraint = new Length(array( + 'max' => 4, + 'maxMessage' => 'myMessage' + )); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMessage', $this->identicalTo(array( + '{{ value }}' => (string) $value, + '{{ limit }}' => 4, + )), $this->identicalTo($value), 4); + + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getNotFourCharacters + */ + public function testInvalidValuesExact($value, $mbOnly = false) + { + if ($mbOnly && !function_exists('mb_strlen')) { + return $this->markTestSkipped('mb_strlen does not exist'); + } + + $constraint = new Length(array( + 'min' => 4, + 'max' => 4, + 'exactMessage' => 'myMessage' + )); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMessage', $this->identicalTo(array( + '{{ value }}' => (string) $value, + '{{ limit }}' => 4, + )), $this->identicalTo($value), 4); + + $this->validator->validate($value, $constraint); + } + + public function testConstraintGetDefaultOption() + { + $constraint = new Length(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); - } -} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php new file mode 100644 index 0000000000..c44b0ea6ce --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php @@ -0,0 +1,237 @@ + + * + * 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\Range; +use Symfony\Component\Validator\Constraints\RangeValidator; + +class RangeValidatorTest 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 RangeValidator(); + $this->validator->initialize($this->context); + } + + public function testNullIsValid() + { + $this->context->expects($this->never()) + ->method('addViolation'); + + $this->validator->validate(null, new Range(array('min' => 10, 'max' => 20))); + } + + public function getTenToTwenty() + { + return array( + array(10.00001), + array(19.99999), + array('10.00001'), + array('19.99999'), + array(10), + array(20), + array(10.0), + array(20.0), + ); + } + + public function getLessThanTen() + { + return array( + array(9.99999), + array('9.99999'), + array(5), + array(1.0), + ); + } + + public function getMoreThanTwenty() + { + return array( + array(20.000001), + array('20.000001'), + array(21), + array(30.0), + ); + } + + /** + * @dataProvider getTenToTwenty + */ + public function testValidValuesMin($value) + { + $this->context->expects($this->never()) + ->method('addViolation'); + + $constraint = new Range(array('min' => 10)); + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getTenToTwenty + */ + public function testValidValuesMax($value) + { + $this->context->expects($this->never()) + ->method('addViolation'); + + $constraint = new Range(array('max' => 20)); + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getTenToTwenty + */ + public function testValidValuesMinMax($value) + { + $this->context->expects($this->never()) + ->method('addViolation'); + + $constraint = new Range(array('min' => 10, 'max' => 20)); + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getLessThanTen + */ + public function testInvalidValuesMin($value) + { + $constraint = new Range(array( + 'min' => 10, + 'minMessage' => 'myMessage', + )); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMessage', $this->identicalTo(array( + '{{ value }}' => $value, + '{{ limit }}' => 10, + ))); + + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getMoreThanTwenty + */ + public function testInvalidValuesMax($value) + { + $constraint = new Range(array( + 'max' => 20, + 'maxMessage' => 'myMessage', + )); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMessage', $this->identicalTo(array( + '{{ value }}' => $value, + '{{ limit }}' => 20, + ))); + + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getMoreThanTwenty + */ + public function testInvalidValuesCombinedMax($value) + { + $constraint = new Range(array( + 'min' => 10, + 'max' => 20, + 'minMessage' => 'myMinMessage', + 'maxMessage' => 'myMaxMessage', + )); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMaxMessage', $this->identicalTo(array( + '{{ value }}' => $value, + '{{ limit }}' => 20, + ))); + + $this->validator->validate($value, $constraint); + } + + /** + * @dataProvider getLessThanTen + */ + public function testInvalidValuesCombinedMin($value) + { + $constraint = new Range(array( + 'min' => 10, + 'max' => 20, + 'minMessage' => 'myMinMessage', + 'maxMessage' => 'myMaxMessage', + )); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMinMessage', $this->identicalTo(array( + '{{ value }}' => $value, + '{{ limit }}' => 10, + ))); + + $this->validator->validate($value, $constraint); + } + + public function getInvalidValues() + { + return array( + array(9.999999), + array(20.000001), + array('9.999999'), + array('20.000001'), + array(new \stdClass()), + ); + } + + public function testMinMessageIsSet() + { + $constraint = new Range(array( + 'min' => 10, + 'max' => 20, + 'minMessage' => 'myMessage', + )); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMessage', array( + '{{ value }}' => 9, + '{{ limit }}' => 10, + )); + + $this->validator->validate(9, $constraint); + } + + public function testMaxMessageIsSet() + { + $constraint = new Range(array( + 'min' => 10, + 'max' => 20, + 'maxMessage' => 'myMessage', + )); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMessage', array( + '{{ value }}' => 21, + '{{ limit }}' => 20, + )); + + $this->validator->validate(21, $constraint); + } +} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/SizeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/SizeValidatorTest.php deleted file mode 100644 index 5cda166f48..0000000000 --- a/src/Symfony/Component/Validator/Tests/Constraints/SizeValidatorTest.php +++ /dev/null @@ -1,121 +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\Size; -use Symfony\Component\Validator\Constraints\SizeValidator; - -class SizeValidatorTest 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 SizeValidator(); - $this->validator->initialize($this->context); - } - - public function testNullIsValid() - { - $this->context->expects($this->never()) - ->method('addViolation'); - - $this->validator->validate(null, new Size(array('min' => 10, 'max' => 20))); - } - - /** - * @dataProvider getValidValues - */ - public function testValidValues($value) - { - $this->context->expects($this->never()) - ->method('addViolation'); - - $constraint = new Size(array('min' => 10, 'max' => 20)); - $this->validator->validate($value, $constraint); - } - - public function getValidValues() - { - return array( - array(10.00001), - array(19.99999), - array('10.00001'), - array('19.99999'), - array(10), - array(20), - array(10.0), - array(20.0), - ); - } - - /** - * @dataProvider getInvalidValues - */ - public function testInvalidValues($value) - { - $this->context->expects($this->once()) - ->method('addViolation'); - - $constraint = new Size(array('min' => 10, 'max' => 20)); - $this->validator->validate($value, $constraint); - } - - public function getInvalidValues() - { - return array( - array(9.999999), - array(20.000001), - array('9.999999'), - array('20.000001'), - array(new \stdClass()), - ); - } - - public function testMinMessageIsSet() - { - $constraint = new Size(array( - 'min' => 10, - 'max' => 20, - 'minMessage' => 'myMessage', - )); - - $this->context->expects($this->once()) - ->method('addViolation') - ->with('myMessage', array( - '{{ value }}' => 9, - '{{ limit }}' => 10, - )); - - $this->validator->validate(9, $constraint); - } - - public function testMaxMessageIsSet() - { - $constraint = new Size(array( - 'min' => 10, - 'max' => 20, - 'maxMessage' => 'myMessage', - )); - - $this->context->expects($this->once()) - ->method('addViolation') - ->with('myMessage', array( - '{{ value }}' => 21, - '{{ limit }}' => 20, - )); - - $this->validator->validate(21, $constraint); - } -}