diff --git a/UPGRADE-2.1.md b/UPGRADE-2.1.md index 2e396624f3096..ef8756ab0a69e 100644 --- a/UPGRADE-2.1.md +++ b/UPGRADE-2.1.md @@ -1136,6 +1136,26 @@ private $recursiveCollection; ``` + * The `Size` constraint was deprecated and will be removed in Symfony 2.3. You should + use the constraints `Min` and `Max` instead. + + Before: + + ``` + /** @Assert\Size(min = 2, max = 16) */ + private $numberOfCpus; + ``` + + After: + + ``` + /** + * @Assert\Min(2) + * @Assert\Max(16) + */ + private $numberOfCpus; + ``` + ### Session * Flash messages now return an array based on their type. The old method is diff --git a/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php b/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php index eeb43f1dad5da..f80b818e8f9ff 100755 --- a/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php +++ b/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php @@ -158,19 +158,14 @@ public function guessTypeForConstraint(Constraint $constraint) return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE); case 'Symfony\Component\Validator\Constraints\Size': - switch ($constraint->type) { - case 'string': - return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE); - case 'collection': - return new TypeGuess('collection', array(), Guess::MEDIUM_CONFIDENCE); - } - break; - case 'Symfony\Component\Validator\Constraints\Min': - case 'Symfony\Component\Validator\Constraints\Range': case 'Symfony\Component\Validator\Constraints\Max': return new TypeGuess('number', array(), Guess::LOW_CONFIDENCE); + case 'Symfony\Component\Validator\Constraints\MinCount': + case 'Symfony\Component\Validator\Constraints\MaxCount': + return new TypeGuess('collection', array(), Guess::LOW_CONFIDENCE); + case 'Symfony\Component\Validator\Constraints\Time': return new TypeGuess('time', array('input'=>'string'), Guess::HIGH_CONFIDENCE); @@ -208,12 +203,6 @@ public function guessMaxLengthForConstraint(Constraint $constraint) case 'Symfony\Component\Validator\Constraints\MaxLength': return new ValueGuess($constraint->limit, Guess::HIGH_CONFIDENCE); - case 'Symfony\Component\Validator\Constraints\Size': - if ('string' === $constraint->type && null !== $constraint->max) { - return new ValueGuess($constraint->max, Guess::HIGH_CONFIDENCE); - } - break; - case 'Symfony\Component\Validator\Constraints\Type': if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) { return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE); @@ -223,7 +212,7 @@ public function guessMaxLengthForConstraint(Constraint $constraint) case 'Symfony\Component\Validator\Constraints\Max': return new ValueGuess(strlen((string) $constraint->limit), Guess::LOW_CONFIDENCE); - case 'Symfony\Component\Validator\Constraints\Range': + case 'Symfony\Component\Validator\Constraints\Size': return new ValueGuess(strlen((string) $constraint->max), Guess::LOW_CONFIDENCE); } } @@ -241,25 +230,6 @@ public function guessPatternForConstraint(Constraint $constraint) case 'Symfony\Component\Validator\Constraints\MinLength': return new ValueGuess(sprintf('.{%s,}', (string) $constraint->limit), Guess::LOW_CONFIDENCE); - case 'Symfony\Component\Validator\Constraints\Size': - if ('string' !== $constraint->type) { - return; - } - - if ($constraint->min === $constraint->max) { - return new ValueGuess(sprintf('.{%s}', (string) $constraint->min), Guess::LOW_CONFIDENCE); - } - - if (null === $constraint->min) { - return new ValueGuess(sprintf('.{0,%s}', (string) $constraint->max), Guess::LOW_CONFIDENCE); - } - - if (null === $constraint->max) { - return new ValueGuess(sprintf('.{%s,}', (string) $constraint->min), Guess::LOW_CONFIDENCE); - } - - return new ValueGuess(sprintf('.{%s,%s}', (string) $constraint->min, (string) $constraint->max), Guess::LOW_CONFIDENCE); - case 'Symfony\Component\Validator\Constraints\Regex': $htmlPattern = $constraint->getHtmlPattern(); @@ -271,7 +241,7 @@ public function guessPatternForConstraint(Constraint $constraint) case 'Symfony\Component\Validator\Constraints\Min': return new ValueGuess(sprintf('.{%s,}', strlen((string) $constraint->limit)), Guess::LOW_CONFIDENCE); - case 'Symfony\Component\Validator\Constraints\Range': + case 'Symfony\Component\Validator\Constraints\Size': return new ValueGuess(sprintf('.{%s,%s}', strlen((string) $constraint->min), strlen((string) $constraint->max)), Guess::LOW_CONFIDENCE); case 'Symfony\Component\Validator\Constraints\Type': diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index 43d895d0b6c9f..a1fd056d12a6f 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -5,8 +5,6 @@ CHANGELOG ----- * added support for `ctype_*` assertions in `TypeValidator` - * added a Range validator for numeric values - * added a Size validator for string & collections * improved the ImageValidator with min width, max width, min height, and max height constraints * added support for MIME with wildcard in FileValidator * changed Collection validator to add "missing" and "extra" errors to @@ -23,3 +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 diff --git a/src/Symfony/Component/Validator/Constraints/Range.php b/src/Symfony/Component/Validator/Constraints/MaxCount.php similarity index 59% rename from src/Symfony/Component/Validator/Constraints/Range.php rename to src/Symfony/Component/Validator/Constraints/MaxCount.php index 849cd952c2072..c3286033a0b03 100644 --- a/src/Symfony/Component/Validator/Constraints/Range.php +++ b/src/Symfony/Component/Validator/Constraints/MaxCount.php @@ -18,19 +18,24 @@ * * @api */ -class Range extends Constraint +class MaxCount 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 $message = 'This collection should contain {{ limit }} elements or less.'; + public $limit; + + /** + * {@inheritDoc} + */ + public function getDefaultOption() + { + return 'limit'; + } /** * {@inheritDoc} */ public function getRequiredOptions() { - return array('min', 'max'); + return array('limit'); } } diff --git a/src/Symfony/Component/Validator/Constraints/RangeValidator.php b/src/Symfony/Component/Validator/Constraints/MaxCountValidator.php similarity index 51% rename from src/Symfony/Component/Validator/Constraints/RangeValidator.php rename to src/Symfony/Component/Validator/Constraints/MaxCountValidator.php index 32b3cf4cd9e1b..50efbb7f479cb 100644 --- a/src/Symfony/Component/Validator/Constraints/RangeValidator.php +++ b/src/Symfony/Component/Validator/Constraints/MaxCountValidator.php @@ -13,13 +13,12 @@ use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; +use Symfony\Component\Validator\Exception\UnexpectedTypeException; /** * @author Bernhard Schussek - * - * @api */ -class RangeValidator extends ConstraintValidator +class MaxCountValidator extends ConstraintValidator { /** * Checks if the passed value is valid. @@ -27,8 +26,6 @@ class RangeValidator extends ConstraintValidator * @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) @@ -37,28 +34,17 @@ public function validate($value, Constraint $constraint) return; } - if (!is_numeric($value)) { - $this->context->addViolation($constraint->invalidMessage, array( - '{{ value }}' => $value, - )); - - return; + if (!is_array($value) && !$value instanceof \Countable) { + throw new UnexpectedTypeException($value, 'array or \Countable'); } - if ($value > $constraint->max) { - $this->context->addViolation($constraint->maxMessage, array( - '{{ value }}' => $value, - '{{ limit }}' => $constraint->max, - )); - - return; - } + $count = count($value); - if ($value < $constraint->min) { - $this->context->addViolation($constraint->minMessage, array( - '{{ value }}' => $value, - '{{ limit }}' => $constraint->min, - )); + 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/MinCount.php b/src/Symfony/Component/Validator/Constraints/MinCount.php new file mode 100644 index 0000000000000..ebf1ff5d47aea --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/MinCount.php @@ -0,0 +1,41 @@ + + * + * 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 new file mode 100644 index 0000000000000..1620caa4b32cf --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/MinCountValidator.php @@ -0,0 +1,50 @@ + + * + * 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/Size.php b/src/Symfony/Component/Validator/Constraints/Size.php index d60dbaa4b9c2e..b72633eef2503 100644 --- a/src/Symfony/Component/Validator/Constraints/Size.php +++ b/src/Symfony/Component/Validator/Constraints/Size.php @@ -17,73 +17,22 @@ * @Annotation * * @api + * + * @deprecated Deprecated since version 2.1, to be removed in 2.3. */ class Size extends Constraint { - const TYPE_STRING = 'string'; - const TYPE_COLLECTION = 'collection'; - - public $minMessage; - public $maxMessage; - public $exactMessage; - public $type; + 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 $charset = 'UTF-8'; - - private $stringMinMessage = 'This value is too short. It should have {{ limit }} characters or more.'; - private $stringMaxMessage = 'This value is too long. It should have {{ limit }} characters or less.'; - private $stringExactMessage = 'This value should have exactly {{ limit }} characters.'; - - private $collectionMinMessage = 'This collection should contain {{ limit }} elements or more.'; - private $collectionMaxMessage = 'This collection should contain {{ limit }} elements or less.'; - private $collectionExactMessage = 'This collection should contain exactly {{ limit }} elements.'; - public function getMinMessage($type) + /** + * {@inheritDoc} + */ + public function getRequiredOptions() { - if (null !== $this->minMessage) { - return $this->minMessage; - } - - switch ($type) { - case static::TYPE_STRING: - return $this->stringMinMessage; - case static::TYPE_COLLECTION: - return $this->collectionMinMessage; - default: - throw new \InvalidArgumentException('Invalid type specified.'); - } - } - - public function getMaxMessage($type) - { - if (null !== $this->maxMessage) { - return $this->maxMessage; - } - - switch ($type) { - case static::TYPE_STRING: - return $this->stringMaxMessage; - case static::TYPE_COLLECTION: - return $this->collectionMaxMessage; - default: - throw new \InvalidArgumentException('Invalid type specified.'); - } - } - - public function getExactMessage($type) - { - if (null !== $this->exactMessage) { - return $this->exactMessage; - } - - switch ($type) { - case static::TYPE_STRING: - return $this->stringExactMessage; - case static::TYPE_COLLECTION: - return $this->collectionExactMessage; - default: - throw new \InvalidArgumentException('Invalid type specified.'); - } + return array('min', 'max'); } } diff --git a/src/Symfony/Component/Validator/Constraints/SizeValidator.php b/src/Symfony/Component/Validator/Constraints/SizeValidator.php index 842d3ef016e39..f26a0ec7d9676 100644 --- a/src/Symfony/Component/Validator/Constraints/SizeValidator.php +++ b/src/Symfony/Component/Validator/Constraints/SizeValidator.php @@ -13,151 +13,54 @@ use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; -use Symfony\Component\Validator\Exception\UnexpectedTypeException; -use Symfony\Component\Validator\Exception\ConstraintDefinitionException; +/** + * @author Bernhard Schussek + * + * @api + * + * @deprecated Deprecated since version 2.1, to be removed in 2.3. + */ class SizeValidator extends ConstraintValidator { /** - * {@inheritDoc} + * 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 === $constraint->min && null === $constraint->max) { - throw new ConstraintDefinitionException( - 'Either "min" or "max" must be specified on constraint Size' - ); - } - - if (null === $constraint->type) { - $type = $this->guessType($value); - } else { - $type = $constraint->type; - } - - switch ($type) { - case Size::TYPE_STRING: - return $this->validateString($value, $constraint); - case Size::TYPE_COLLECTION: - return $this->validateCollection($value, $constraint); - default: - throw new ConstraintDefinitionException(sprintf( - 'The "type" on constraint Size must be either "%s" or "%s", "%s" given.', - Size::TYPE_STRING, - Size::TYPE_COLLECTION, - $type - )); - } - - } - - private function validateString($value, Constraint $constraint) - { - if (null === $value || '' === $value) { - return; - } - - if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) { - throw new UnexpectedTypeException($value, 'string, scalar or object with __toString()'); - } - - $value = (string) $value; - - if (function_exists('grapheme_strlen') && 'UTF-8' === $constraint->charset) { - $length = grapheme_strlen($value); - } elseif (function_exists('mb_strlen')) { - $length = mb_strlen($value, $constraint->charset); - } else { - $length = strlen($value); - } - - $this->validateSize( - $constraint, - $length, - Size::TYPE_STRING, - array('{{ value }}' => $value) - ); - } - - private function validateCollection($value, Constraint $constraint) { if (null === $value) { return; } - if (!is_array($value) && !$value instanceof \Countable) { - throw new UnexpectedTypeException($value, 'array or Countable'); - } - - $count = count($value); - - $this->validateSize( - $constraint, - $count, - Size::TYPE_COLLECTION, - array('{{ count }}' => $count) - ); - } - - private function validateSize(Constraint $constraint, $size, $type, array $parameters) - { - if ($constraint->min == $constraint->max && $size != $constraint->max) { - $this->context->addViolation( - $constraint->getExactMessage($type), - array_merge(array('{{ limit }}' => $constraint->max), $parameters), - null, - (int) $constraint->max - ); + if (!is_numeric($value)) { + $this->context->addViolation($constraint->invalidMessage, array( + '{{ value }}' => $value, + )); return; } - if (null !== $constraint->max && $size > $constraint->max) { - $this->context->addViolation( - $constraint->getMaxMessage($type), - array_merge(array('{{ limit }}' => $constraint->max), $parameters), - null, - (int) $constraint->max - ); + if ($value > $constraint->max) { + $this->context->addViolation($constraint->maxMessage, array( + '{{ value }}' => $value, + '{{ limit }}' => $constraint->max, + )); return; } - if (null !== $constraint->min && $size < $constraint->min) { - $this->context->addViolation( - $constraint->getMinMessage($type), - array_merge(array('{{ limit }}' => $constraint->min), $parameters), - null, - (int) $constraint->min - ); + if ($value < $constraint->min) { + $this->context->addViolation($constraint->minMessage, array( + '{{ value }}' => $value, + '{{ limit }}' => $constraint->min, + )); } } - - private function guessType($value) - { - if (null === $value || is_scalar($value)) { - return Size::TYPE_STRING; - } - - if (is_object($value) && method_exists($value, '__toString')) { - if ($value instanceof \Countable) { - throw new \RuntimeException( - 'The "type" must be specified on constraint Size because the '. - 'validator is not able to guess it since the value is an object '. - 'implementing both the __toString() method and the Countable '. - 'interface.' - ); - } - - return Size::TYPE_STRING; - } - - if (is_array($value) || $value instanceof \Countable) { - return Size::TYPE_COLLECTION; - } - - throw new UnexpectedTypeException( - $value, 'scalar, string, array, Countable or object with __toString()' - ); - } } diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf index b95ec611fe43d..e2e6d2430078b 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf @@ -210,10 +210,6 @@ 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 1816ebeb2411a..d2e2d7815932c 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.fr.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.fr.xlf @@ -210,10 +210,6 @@ 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 b23a3cdeb945b..ee373c19585a9 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.it.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.it.xlf @@ -210,10 +210,6 @@ 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 90bde0b0d6d28..864a726d01b8f 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.nl.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.nl.xlf @@ -210,10 +210,6 @@ 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 58b19dde2d765..c44f5b932847f 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.pt.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.pt.xlf @@ -210,10 +210,6 @@ 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. - - \ No newline at end of file + 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 6195bbbb6ee14..d7822ac78906a 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.pt_BR.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.pt_BR.xlf @@ -210,10 +210,6 @@ 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 325c348ab8dc6..141a93b65ddb6 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.ru.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.ru.xlf @@ -210,10 +210,6 @@ This collection should contain {{ limit }} elements or less. Эта коллекция должна содержать {{ limit }} элемент или меньше.|Эта коллекция должна содержать {{ limit }} элемента или меньше.|Эта коллекция должна содержать {{ limit }} элементов или меньше. - - This collection should contain exactly {{ limit }} elements. - Эта коллекция должна содержать ровно {{ limit }} элемент.|Эта коллекция должна содержать ровно {{ limit }} элемента.|Эта коллекция должна содержать ровно {{ limit }} элементов. - - \ No newline at end of file + diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.sl.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.sl.xlf index fa37c289f849e..959db51fe3c69 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.sl.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.sl.xlf @@ -210,10 +210,6 @@ 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 f3461351ca2bc..ed1b1041e6984 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.sr_Cyrl.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.sr_Cyrl.xlf @@ -210,10 +210,6 @@ This collection should contain {{ limit }} elements or less. Ова колекција треба да садржи {{ limit }} или мање елемената.|Ова колекција треба да садржи {{ limit }} или мање елемената.|Ова колекција треба да садржи {{ limit }} или мање елемената. - - This collection should contain exactly {{ limit }} elements. - Ова колекција треба да садржи тачно {{ limit }} елемент.|Ова колекција треба да садржи тачно {{ limit }} елемента.|Ова колекција треба да садржи тачно {{ limit }} елемената. - - \ No newline at end of file + 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 f5d9e0b26e7ca..a956250e444dd 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.sr_Latn.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.sr_Latn.xlf @@ -210,10 +210,6 @@ 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. - - \ No newline at end of file + diff --git a/src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorArrayTest.php b/src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorArrayTest.php new file mode 100644 index 0000000000000..8453930ae7873 --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorArrayTest.php @@ -0,0 +1,23 @@ + + * + * 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/MaxCountValidatorCountableTest.php b/src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorCountableTest.php new file mode 100644 index 0000000000000..c4852e00d3f46 --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorCountableTest.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\Tests\Constraints; + +class MaxCountValidatorCountableTest_Countable implements \Countable +{ + private $content; + + public function __construct(array $content) + { + $this->content = $content; + } + + public function count() + { + return count($this->content); + } +} + +/** + * @author Bernhard Schussek + */ +class MaxCountValidatorCountableTest extends MaxCountValidatorTest +{ + protected function createCollection(array $content) + { + return new MaxCountValidatorCountableTest_Countable($content); + } +} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorTest.php new file mode 100644 index 0000000000000..f6ef305ffed56 --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Constraints/MaxCountValidatorTest.php @@ -0,0 +1,113 @@ + + * + * 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/MinCountValidatorArrayTest.php b/src/Symfony/Component/Validator/Tests/Constraints/MinCountValidatorArrayTest.php new file mode 100644 index 0000000000000..96eb63a1578ed --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Constraints/MinCountValidatorArrayTest.php @@ -0,0 +1,23 @@ + + * + * 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 MinCountValidatorArrayTest extends MinCountValidatorTest +{ + protected function createCollection(array $content) + { + return $content; + } +} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/MinCountValidatorCountableTest.php b/src/Symfony/Component/Validator/Tests/Constraints/MinCountValidatorCountableTest.php new file mode 100644 index 0000000000000..48b175a65da5e --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Constraints/MinCountValidatorCountableTest.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\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 new file mode 100644 index 0000000000000..ab0cf86eaaa8a --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Constraints/MinCountValidatorTest.php @@ -0,0 +1,114 @@ + + * + * 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 deleted file mode 100644 index 2b202782eb3e6..0000000000000 --- a/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.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\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))); - } - - /** - * @dataProvider getValidValues - */ - public function testValidValues($value) - { - $this->context->expects($this->never()) - ->method('addViolation'); - - $constraint = new Range(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 Range(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 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/SizeTest.php b/src/Symfony/Component/Validator/Tests/Constraints/SizeTest.php deleted file mode 100644 index 4fb388ca42834..0000000000000 --- a/src/Symfony/Component/Validator/Tests/Constraints/SizeTest.php +++ /dev/null @@ -1,98 +0,0 @@ -assertEquals($expected, $size->getMinMessage($type)); - } - - public function getMinMessageData() - { - $size = new Size(); - - return array( - array(array(), Size::TYPE_STRING, $this->readAttribute($size, 'stringMinMessage')), - array(array(), Size::TYPE_COLLECTION, $this->readAttribute($size, 'collectionMinMessage')), - array(array('minMessage' => 'Custom min message'), Size::TYPE_STRING, 'Custom min message'), - array(array('minMessage' => 'Custom min message'), Size::TYPE_COLLECTION, 'Custom min message'), - ); - } - - /** - * @expectedException InvalidArgumentException - */ - public function testGetMinMessageWithInvalidType() - { - $size = new Size(); - $size->getMinMessage('foo'); - } - - /** - * @dataProvider getMaxMessageData - */ - public function testGetMaxMessage($options, $type, $expected) - { - $size = new Size($options); - $this->assertEquals($expected, $size->getMaxMessage($type)); - } - - public function getMaxMessageData() - { - $size = new Size(); - - return array( - array(array(), Size::TYPE_STRING, $this->readAttribute($size, 'stringMaxMessage')), - array(array(), Size::TYPE_COLLECTION, $this->readAttribute($size, 'collectionMaxMessage')), - array(array('maxMessage' => 'Custom max message'), Size::TYPE_STRING, 'Custom max message'), - array(array('maxMessage' => 'Custom max message'), Size::TYPE_COLLECTION, 'Custom max message'), - ); - } - - /** - * @expectedException InvalidArgumentException - */ - public function testGetMaxMessageWithInvalidType() - { - $size = new Size(); - $size->getMaxMessage('foo'); - } - - /** - * @dataProvider getExactMessageData - */ - public function testGetExactMessage($options, $type, $expected) - { - $size = new Size($options); - $this->assertEquals($expected, $size->getExactMessage($type)); - } - - public function getExactMessageData() - { - $size = new Size(); - - return array( - array(array(), Size::TYPE_STRING, $this->readAttribute($size, 'stringExactMessage')), - array(array(), Size::TYPE_COLLECTION, $this->readAttribute($size, 'collectionExactMessage')), - array(array('exactMessage' => 'Custom exact message'), Size::TYPE_STRING, 'Custom exact message'), - array(array('exactMessage' => 'Custom exact message'), Size::TYPE_COLLECTION, 'Custom exact message'), - ); - } - - /** - * @expectedException InvalidArgumentException - */ - public function testGetExactMessageWithInvalidType() - { - $size = new Size(); - $size->getExactMessage('foo'); - } -} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/SizeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/SizeValidatorTest.php index 24e9d19f2319e..5cda166f48473 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/SizeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/SizeValidatorTest.php @@ -14,19 +14,6 @@ use Symfony\Component\Validator\Constraints\Size; use Symfony\Component\Validator\Constraints\SizeValidator; -class SizeAmbiguous implements \Countable -{ - public function __toString() - { - return ''; - } - - public function count() - { - return 0; - } -} - class SizeValidatorTest extends \PHPUnit_Framework_TestCase { protected $context; @@ -41,176 +28,94 @@ protected function setUp() public function testNullIsValid() { - $this->context->expects($this->never())->method('addViolation'); + $this->context->expects($this->never()) + ->method('addViolation'); $this->validator->validate(null, new Size(array('min' => 10, 'max' => 20))); } - public function testNullIsValidAsAString() - { - $this->context->expects($this->never())->method('addViolation'); - - $this->validator->validate(null, new Size(array('type' => 'string', 'min' => 10, 'max' => 20))); - } - - public function testNullIsValidAsACollectionCollection() - { - $this->context->expects($this->never())->method('addViolation'); - - $this->validator->validate(null, new Size(array('type' => 'collection', 'min' => 10, 'max' => 20))); - } - - public function testEmptyStringIsValid() - { - $this->context->expects($this->never())->method('addViolation'); - - $this->validator->validate('', new Size(array('min' => 10, 'max' => 20))); - } - - public function testEmptyStringIsValidAsAString() - { - $this->context->expects($this->never())->method('addViolation'); - - $this->validator->validate('', new Size(array('type' => 'string', 'min' => 10, 'max' => 20))); - } - /** - * @dataProvider getValidStringValues + * @dataProvider getValidValues */ - public function testValidStringValues($value, $mbOnly = false) + public function testValidValues($value) { - if ($mbOnly && !function_exists('mb_strlen')) { - return $this->markTestSkipped('mb_strlen does not exist'); - } - $this->context->expects($this->never()) ->method('addViolation'); - $constraint = new Size(array('min' => 6, 'max' => 10)); + $constraint = new Size(array('min' => 10, 'max' => 20)); $this->validator->validate($value, $constraint); } - public function getValidStringValues() - { - return array( - array(123456), - array(1234567890), - array('123456'), - array('1234567890'), - array('üüüüüü', true), - array('üüüüüüüüüü', true), - array('éééééé', true), - array('éééééééééé', true), - ); - } - - /** - * @dataProvider getInvalidStringValues - */ - public function testInvalidStringValues($value, $mbOnly = false) - { - if ($mbOnly && !function_exists('mb_strlen')) { - return $this->markTestSkipped('mb_strlen does not exist'); - } - - $this->context->expects($this->once())->method('addViolation'); - - $this->validator->validate($value, new Size(array('min' => 6, 'max' => 10))); - } - - public function getInvalidStringValues() + public function getValidValues() { return array( - array(12345), - array(12345678901), - array('12345'), - array('12345678901'), - array('üüüüü', true), - array('üüüüüüüüüüü', true), - array('ééééé', true), - array('ééééééééééé', true), + array(10.00001), + array(19.99999), + array('10.00001'), + array('19.99999'), + array(10), + array(20), + array(10.0), + array(20.0), ); } /** - * @dataProvider getValidCollectionValues + * @dataProvider getInvalidValues */ - public function testValidCollectionValue($value) + public function testInvalidValues($value) { - $this->context->expects($this->never())->method('addViolation'); + $this->context->expects($this->once()) + ->method('addViolation'); - $this->validator->validate($value, new Size(array('min' => 10, 'max' => 20))); + $constraint = new Size(array('min' => 10, 'max' => 20)); + $this->validator->validate($value, $constraint); } - public function getValidCollectionValues() + public function getInvalidValues() { - $countable = $this->getMock('Countable'); - $countable->expects($this->any())->method('count')->will($this->returnValue(15)); - return array( - array($countable), - array(range(1, 15)), + array(9.999999), + array(20.000001), + array('9.999999'), + array('20.000001'), + array(new \stdClass()), ); } - /** - * @dataProvider getInvalidCollectionValues - */ - public function testInvalidCollectionValue($value) - { - $this->context->expects($this->once())->method('addViolation'); - - $this->validator->validate($value, new Size(array('min' => 10, 'max' => 20))); - } - - public function getInvalidCollectionValues() + public function testMinMessageIsSet() { - $tooSmallCountable = $this->getMock('Countable'); - $tooSmallCountable->expects($this->any())->method('count')->will($this->returnValue(5)); + $constraint = new Size(array( + 'min' => 10, + 'max' => 20, + 'minMessage' => 'myMessage', + )); - $tooBigCountable = $this->getmock('countable'); - $tooBigCountable->expects($this->any())->method('count')->will($this->returnValue(25)); + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMessage', array( + '{{ value }}' => 9, + '{{ limit }}' => 10, + )); - return array( - array($tooSmallCountable), - array($tooBigCountable), - array(array()), - array(range(1, 5)), - array(range(1, 25)), - ); + $this->validator->validate(9, $constraint); } - /** - * @expectedException RuntimeException - */ - public function throwsAnExceptionWhenOnAmbiguousValue() - { - $this->validator->validate(new SizeAmbiguous(), new Size(array('min' => 10, 'max' => 20))); - } - - /** - * @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException - */ - public function testExpectsEitherStringOrCollectionCompatible() - { - $this->validator->validate(new \stdCLass(), new Size(array('min' => 10, 'max' => 20))); - } - - /** - * @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException - */ - public function testExpectsStringCompatibleType() + public function testMaxMessageIsSet() { - $countable = $this->getMock('Countable'); + $constraint = new Size(array( + 'min' => 10, + 'max' => 20, + 'maxMessage' => 'myMessage', + )); - $this->validator->validate($countable, new Size(array('type' => 'string', 'min' => 6, 'max' => 10))); - } + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMessage', array( + '{{ value }}' => 21, + '{{ limit }}' => 20, + )); - /** - * @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException - */ - public function testExpectsCollectionCompatibleType() - { - $this->validator->validate('string', new Size(array('type' => 'collection', 'min' => 6, 'max' => 10))); + $this->validator->validate(21, $constraint); } }