merged branch bschussek/range_count_length (PR #4863)
Commits -------a92f80b
[Validator] Added Length constraint and deprecated MinLength and MaxLength83a3f75
[Validator] Deprecated the constraints Min and Max in favor of Range0cdacee
[Validator] Removed MinCount and MaxCount and replaced them by the constraint Count741c147
[Validator] Renamed deprecated Size constraint to Range Discussion ---------- [Validator] Reintroduced Range constraint and created Count and Length constraints Bug fix: no Feature addition: no Backwards compatibility break: no Symfony2 tests pass: yes Fixes the following tickets: - Todo: - After @Tobion's comment to #4851, this is the next try to streamline the constraints and reduce duplication of logic. The downside of the current MinLength/MaxLength and MinCount/MaxCount pairs is that they cannot output a fitting error message if a value should have an *exact* length/count. So this PR introduces * Range (formerly Size) to replace Min/Max * Count to replace MinCount/MaxCount * Length to replace MinLength/MaxLength Feedback is appreciated. --------------------------------------------------------------------------- by Tobion at 2012-07-11T20:40:08Z The `choice` constraint also cannot handle `min = max`. Or maybe we don't need these options on choice anymore as we can achieve the same with the new `count` constraint?! --------------------------------------------------------------------------- by beberlei at 2012-07-12T08:59:44Z Dude, nobody has time to fix the BC breaks you introduce :-) --------------------------------------------------------------------------- by TomAdam at 2012-07-12T12:38:49Z The changes to the `Size` validator yesterday broke my project, and I started rewriting to use `MaxLength / MinLength` validators today, until I spotted this. It would be good if this PR could have a reasonably high priority (whether or not it is accepted) as it will change how I fix my issues. I suspect a lot of people using the master branch will be in the same situation.
This commit is contained in:
commit
b3d1958209
@ -1136,8 +1136,8 @@
|
|||||||
private $recursiveCollection;
|
private $recursiveCollection;
|
||||||
```
|
```
|
||||||
|
|
||||||
* The `Size` constraint was deprecated and will be removed in Symfony 2.3. You should
|
* The `Size`, `Min` and `Max` constraints were deprecated and will be removed in
|
||||||
use the constraints `Min` and `Max` instead.
|
Symfony 2.3. You should use the new constraint `Range` instead.
|
||||||
|
|
||||||
Before:
|
Before:
|
||||||
|
|
||||||
@ -1149,13 +1149,41 @@
|
|||||||
After:
|
After:
|
||||||
|
|
||||||
```
|
```
|
||||||
/**
|
/** @Assert\Range(min = 2, max = 16) */
|
||||||
* @Assert\Min(2)
|
|
||||||
* @Assert\Max(16)
|
|
||||||
*/
|
|
||||||
private $numberOfCpus;
|
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
|
### Session
|
||||||
|
|
||||||
* Flash messages now return an array based on their type. The old method is
|
* Flash messages now return an array based on their type. The old method is
|
||||||
|
@ -21,5 +21,8 @@ CHANGELOG
|
|||||||
* [BC BREAK] collections in fields annotated with `Valid` are not traversed
|
* [BC BREAK] collections in fields annotated with `Valid` are not traversed
|
||||||
recursively anymore by default. `Valid` contains a new property `deep`
|
recursively anymore by default. `Valid` contains a new property `deep`
|
||||||
which enables the BC behavior.
|
which enables the BC behavior.
|
||||||
* added MinCount and MaxCount constraint
|
* added Count constraint
|
||||||
* deprecated the Size 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
|
||||||
|
45
src/Symfony/Component/Validator/Constraints/Count.php
Normal file
45
src/Symfony/Component/Validator/Constraints/Count.php
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* 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'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -18,15 +18,13 @@ use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
|||||||
/**
|
/**
|
||||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||||
*/
|
*/
|
||||||
class MaxCountValidator extends ConstraintValidator
|
class CountValidator extends ConstraintValidator
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Checks if the passed value is valid.
|
* Checks if the passed value is valid.
|
||||||
*
|
*
|
||||||
* @param mixed $value The value that should be validated
|
* @param mixed $value The value that should be validated
|
||||||
* @param Constraint $constraint The constraint for the validation
|
* @param Constraint $constraint The constraint for the validation
|
||||||
*
|
|
||||||
* @api
|
|
||||||
*/
|
*/
|
||||||
public function validate($value, Constraint $constraint)
|
public function validate($value, Constraint $constraint)
|
||||||
{
|
{
|
||||||
@ -40,11 +38,29 @@ class MaxCountValidator extends ConstraintValidator
|
|||||||
|
|
||||||
$count = count($value);
|
$count = count($value);
|
||||||
|
|
||||||
if ($count > $constraint->limit) {
|
if ($constraint->min == $constraint->max && $count != $constraint->min) {
|
||||||
$this->context->addViolation($constraint->message, array(
|
$this->context->addViolation($constraint->exactMessage, array(
|
||||||
'{{ count }}' => $count,
|
'{{ count }}' => $count,
|
||||||
'{{ limit }}' => $constraint->limit,
|
'{{ limit }}' => $constraint->min,
|
||||||
), $value, (int) $constraint->limit);
|
), $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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
46
src/Symfony/Component/Validator/Constraints/Length.php
Normal file
46
src/Symfony/Component/Validator/Constraints/Length.php
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* 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'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,74 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* 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 <bschussek@gmail.com>
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -17,6 +17,8 @@ use Symfony\Component\Validator\Constraint;
|
|||||||
* @Annotation
|
* @Annotation
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
|
*
|
||||||
|
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
|
||||||
*/
|
*/
|
||||||
class Max extends Constraint
|
class Max extends Constraint
|
||||||
{
|
{
|
||||||
|
@ -1,41 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
/*
|
|
||||||
* This file is part of the Symfony package.
|
|
||||||
*
|
|
||||||
* (c) Fabien Potencier <fabien@symfony.com>
|
|
||||||
*
|
|
||||||
* 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');
|
|
||||||
}
|
|
||||||
}
|
|
@ -17,6 +17,8 @@ use Symfony\Component\Validator\Constraint;
|
|||||||
* @Annotation
|
* @Annotation
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
|
*
|
||||||
|
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
|
||||||
*/
|
*/
|
||||||
class MaxLength extends Constraint
|
class MaxLength extends Constraint
|
||||||
{
|
{
|
||||||
|
@ -19,6 +19,8 @@ use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
|||||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
|
*
|
||||||
|
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
|
||||||
*/
|
*/
|
||||||
class MaxLengthValidator extends ConstraintValidator
|
class MaxLengthValidator extends ConstraintValidator
|
||||||
{
|
{
|
||||||
|
@ -18,6 +18,8 @@ use Symfony\Component\Validator\ConstraintValidator;
|
|||||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
|
*
|
||||||
|
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
|
||||||
*/
|
*/
|
||||||
class MaxValidator extends ConstraintValidator
|
class MaxValidator extends ConstraintValidator
|
||||||
{
|
{
|
||||||
|
@ -17,6 +17,8 @@ use Symfony\Component\Validator\Constraint;
|
|||||||
* @Annotation
|
* @Annotation
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
|
*
|
||||||
|
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
|
||||||
*/
|
*/
|
||||||
class Min extends Constraint
|
class Min extends Constraint
|
||||||
{
|
{
|
||||||
|
@ -1,41 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
/*
|
|
||||||
* This file is part of the Symfony package.
|
|
||||||
*
|
|
||||||
* (c) Fabien Potencier <fabien@symfony.com>
|
|
||||||
*
|
|
||||||
* 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');
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,50 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
/*
|
|
||||||
* This file is part of the Symfony package.
|
|
||||||
*
|
|
||||||
* (c) Fabien Potencier <fabien@symfony.com>
|
|
||||||
*
|
|
||||||
* 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 <bschussek@gmail.com>
|
|
||||||
*/
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -17,6 +17,8 @@ use Symfony\Component\Validator\Constraint;
|
|||||||
* @Annotation
|
* @Annotation
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
|
*
|
||||||
|
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
|
||||||
*/
|
*/
|
||||||
class MinLength extends Constraint
|
class MinLength extends Constraint
|
||||||
{
|
{
|
||||||
|
@ -19,6 +19,8 @@ use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
|||||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
|
*
|
||||||
|
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
|
||||||
*/
|
*/
|
||||||
class MinLengthValidator extends ConstraintValidator
|
class MinLengthValidator extends ConstraintValidator
|
||||||
{
|
{
|
||||||
|
@ -18,6 +18,8 @@ use Symfony\Component\Validator\ConstraintValidator;
|
|||||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
|
*
|
||||||
|
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
|
||||||
*/
|
*/
|
||||||
class MinValidator extends ConstraintValidator
|
class MinValidator extends ConstraintValidator
|
||||||
{
|
{
|
||||||
|
38
src/Symfony/Component/Validator/Constraints/Range.php
Normal file
38
src/Symfony/Component/Validator/Constraints/Range.php
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* 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'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* 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 <bschussek@gmail.com>
|
||||||
|
*/
|
||||||
|
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,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -20,19 +20,13 @@ use Symfony\Component\Validator\Constraint;
|
|||||||
*
|
*
|
||||||
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
|
* @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}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
public function getRequiredOptions()
|
public function validatedBy()
|
||||||
{
|
{
|
||||||
return array('min', 'max');
|
return get_parent_class($this).'Validator';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,46 +21,6 @@ use Symfony\Component\Validator\ConstraintValidator;
|
|||||||
*
|
*
|
||||||
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
|
* @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,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -210,6 +210,10 @@
|
|||||||
<source>This collection should contain {{ limit }} elements or less.</source>
|
<source>This collection should contain {{ limit }} elements or less.</source>
|
||||||
<target>This collection should contain {{ limit }} element or less.|This collection should contain {{ limit }} elements or less.</target>
|
<target>This collection should contain {{ limit }} element or less.|This collection should contain {{ limit }} elements or less.</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
|
<trans-unit id="56">
|
||||||
|
<source>This collection should contain exactly {{ limit }} elements.</source>
|
||||||
|
<target>This collection should contain exactly {{ limit }} element.|This collection should contain exactly {{ limit }} elements.</target>
|
||||||
|
</trans-unit>
|
||||||
</body>
|
</body>
|
||||||
</file>
|
</file>
|
||||||
</xliff>
|
</xliff>
|
||||||
|
@ -210,6 +210,10 @@
|
|||||||
<source>This collection should contain {{ limit }} elements or less.</source>
|
<source>This collection should contain {{ limit }} elements or less.</source>
|
||||||
<target>Cette collection doit contenir {{ limit }} élément ou moins.|Cette collection doit contenir {{ limit }} éléments ou moins.</target>
|
<target>Cette collection doit contenir {{ limit }} élément ou moins.|Cette collection doit contenir {{ limit }} éléments ou moins.</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
|
<trans-unit id="56">
|
||||||
|
<source>This collection should contain exactly {{ limit }} elements.</source>
|
||||||
|
<target>Cette collection doit contenir exactement {{ limit }} élément.|Cette collection doit contenir exactement {{ limit }} éléments.</target>
|
||||||
|
</trans-unit>
|
||||||
</body>
|
</body>
|
||||||
</file>
|
</file>
|
||||||
</xliff>
|
</xliff>
|
||||||
|
@ -210,6 +210,10 @@
|
|||||||
<source>This collection should contain {{ limit }} elements or less.</source>
|
<source>This collection should contain {{ limit }} elements or less.</source>
|
||||||
<target>Questa collezione dovrebbe contenere massimo {{ limit }} elemento.|Questa collezione dovrebbe contenere massimo {{ limit }} elementi.</target>
|
<target>Questa collezione dovrebbe contenere massimo {{ limit }} elemento.|Questa collezione dovrebbe contenere massimo {{ limit }} elementi.</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
|
<trans-unit id="56">
|
||||||
|
<source>This collection should contain exactly {{ limit }} elements.</source>
|
||||||
|
<target>Questa collezione dovrebbe contenere esattamente {{ limit }} elemento.|Questa collezione dovrebbe contenere esattamente {{ limit }} elementi.</target>
|
||||||
|
</trans-unit>
|
||||||
</body>
|
</body>
|
||||||
</file>
|
</file>
|
||||||
</xliff>
|
</xliff>
|
||||||
|
@ -210,6 +210,10 @@
|
|||||||
<source>This collection should contain {{ limit }} elements or less.</source>
|
<source>This collection should contain {{ limit }} elements or less.</source>
|
||||||
<target>Deze collectie moet {{ limit }} element of minder bevatten.|Deze collectie moet {{ limit }} elementen of minder bevatten.</target>
|
<target>Deze collectie moet {{ limit }} element of minder bevatten.|Deze collectie moet {{ limit }} elementen of minder bevatten.</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
|
<trans-unit id="56">
|
||||||
|
<source>This collection should contain exactly {{ limit }} elements.</source>
|
||||||
|
<target>Deze collectie moet exact {{ limit }} element bevatten.|Deze collectie moet exact {{ limit }} elementen bevatten.</target>
|
||||||
|
</trans-unit>
|
||||||
</body>
|
</body>
|
||||||
</file>
|
</file>
|
||||||
</xliff>
|
</xliff>
|
||||||
|
@ -210,6 +210,10 @@
|
|||||||
<source>This collection should contain {{ limit }} elements or less.</source>
|
<source>This collection should contain {{ limit }} elements or less.</source>
|
||||||
<target>Esta coleção deve conter {{ limit }} elemento ou menos.|Esta coleção deve conter {{ limit }} elementos ou menos.</target>
|
<target>Esta coleção deve conter {{ limit }} elemento ou menos.|Esta coleção deve conter {{ limit }} elementos ou menos.</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
|
<trans-unit id="56">
|
||||||
|
<source>This collection should contain exactly {{ limit }} elements.</source>
|
||||||
|
<target>Esta coleção deve conter exatamente {{ limit }} elemento.|Esta coleção deve conter exatamente {{ limit }} elementos.</target>
|
||||||
|
</trans-unit>
|
||||||
</body>
|
</body>
|
||||||
</file>
|
</file>
|
||||||
</xliff>
|
</xliff>
|
||||||
|
@ -210,6 +210,10 @@
|
|||||||
<source>This collection should contain {{ limit }} elements or less.</source>
|
<source>This collection should contain {{ limit }} elements or less.</source>
|
||||||
<target>Esta coleção deve conter {{ limit }} elemento ou menos.|Esta coleção deve conter {{ limit }} elementos ou menos.</target>
|
<target>Esta coleção deve conter {{ limit }} elemento ou menos.|Esta coleção deve conter {{ limit }} elementos ou menos.</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
|
<trans-unit id="56">
|
||||||
|
<source>This collection should contain exactly {{ limit }} elements.</source>
|
||||||
|
<target>Esta coleção deve conter exatamente {{ limit }} elemento.|Esta coleção deve conter exatamente {{ limit }} elementos.</target>
|
||||||
|
</trans-unit>
|
||||||
</body>
|
</body>
|
||||||
</file>
|
</file>
|
||||||
</xliff>
|
</xliff>
|
||||||
|
@ -210,6 +210,10 @@
|
|||||||
<source>This collection should contain {{ limit }} elements or less.</source>
|
<source>This collection should contain {{ limit }} elements or less.</source>
|
||||||
<target>Эта коллекция должна содержать {{ limit }} элемент или меньше.|Эта коллекция должна содержать {{ limit }} элемента или меньше.|Эта коллекция должна содержать {{ limit }} элементов или меньше.</target>
|
<target>Эта коллекция должна содержать {{ limit }} элемент или меньше.|Эта коллекция должна содержать {{ limit }} элемента или меньше.|Эта коллекция должна содержать {{ limit }} элементов или меньше.</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
|
<trans-unit id="56">
|
||||||
|
<source>This collection should contain exactly {{ limit }} elements.</source>
|
||||||
|
<target>Эта коллекция должна содержать ровно {{ limit }} элемент.|Эта коллекция должна содержать ровно {{ limit }} элемента.|Эта коллекция должна содержать ровно {{ limit }} элементов.</target>
|
||||||
|
</trans-unit>
|
||||||
</body>
|
</body>
|
||||||
</file>
|
</file>
|
||||||
</xliff>
|
</xliff>
|
||||||
|
@ -210,6 +210,10 @@
|
|||||||
<source>This collection should contain {{ limit }} elements or less.</source>
|
<source>This collection should contain {{ limit }} elements or less.</source>
|
||||||
<target>Ta zbirka bi morala vsebovati {{ limit }} ali manj elementov.</target>
|
<target>Ta zbirka bi morala vsebovati {{ limit }} ali manj elementov.</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
|
<trans-unit id="56">
|
||||||
|
<source>This collection should contain exactly {{ limit }} elements.</source>
|
||||||
|
<target>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.</target>
|
||||||
|
</trans-unit>
|
||||||
</body>
|
</body>
|
||||||
</file>
|
</file>
|
||||||
</xliff>
|
</xliff>
|
||||||
|
@ -210,6 +210,10 @@
|
|||||||
<source>This collection should contain {{ limit }} elements or less.</source>
|
<source>This collection should contain {{ limit }} elements or less.</source>
|
||||||
<target>Ова колекција треба да садржи {{ limit }} или мање елемената.|Ова колекција треба да садржи {{ limit }} или мање елемената.|Ова колекција треба да садржи {{ limit }} или мање елемената.</target>
|
<target>Ова колекција треба да садржи {{ limit }} или мање елемената.|Ова колекција треба да садржи {{ limit }} или мање елемената.|Ова колекција треба да садржи {{ limit }} или мање елемената.</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
|
<trans-unit id="56">
|
||||||
|
<source>This collection should contain exactly {{ limit }} elements.</source>
|
||||||
|
<target>Ова колекција треба да садржи тачно {{ limit }} елемент.|Ова колекција треба да садржи тачно {{ limit }} елемента.|Ова колекција треба да садржи тачно {{ limit }} елемената.</target>
|
||||||
|
</trans-unit>
|
||||||
</body>
|
</body>
|
||||||
</file>
|
</file>
|
||||||
</xliff>
|
</xliff>
|
||||||
|
@ -210,6 +210,10 @@
|
|||||||
<source>This collection should contain {{ limit }} elements or less.</source>
|
<source>This collection should contain {{ limit }} elements or less.</source>
|
||||||
<target>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.</target>
|
<target>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.</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
|
<trans-unit id="56">
|
||||||
|
<source>This collection should contain exactly {{ limit }} elements.</source>
|
||||||
|
<target>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.</target>
|
||||||
|
</trans-unit>
|
||||||
</body>
|
</body>
|
||||||
</file>
|
</file>
|
||||||
</xliff>
|
</xliff>
|
||||||
|
@ -14,7 +14,7 @@ namespace Symfony\Component\Validator\Tests\Constraints;
|
|||||||
/**
|
/**
|
||||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||||
*/
|
*/
|
||||||
class MinCountValidatorArrayTest extends MinCountValidatorTest
|
class CountValidatorArrayTest extends CountValidatorTest
|
||||||
{
|
{
|
||||||
protected function createCollection(array $content)
|
protected function createCollection(array $content)
|
||||||
{
|
{
|
@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Validator\Tests\Constraints;
|
namespace Symfony\Component\Validator\Tests\Constraints;
|
||||||
|
|
||||||
class MaxCountValidatorCountableTest_Countable implements \Countable
|
class CountValidatorCountableTest_Countable implements \Countable
|
||||||
{
|
{
|
||||||
private $content;
|
private $content;
|
||||||
|
|
||||||
@ -29,10 +29,10 @@ class MaxCountValidatorCountableTest_Countable implements \Countable
|
|||||||
/**
|
/**
|
||||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||||
*/
|
*/
|
||||||
class MaxCountValidatorCountableTest extends MaxCountValidatorTest
|
class CountValidatorCountableTest extends CountValidatorTest
|
||||||
{
|
{
|
||||||
protected function createCollection(array $content)
|
protected function createCollection(array $content)
|
||||||
{
|
{
|
||||||
return new MaxCountValidatorCountableTest_Countable($content);
|
return new CountValidatorCountableTest_Countable($content);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,195 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* 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 <bschussek@gmail.com>
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,233 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
}
|
@ -1,23 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
/*
|
|
||||||
* This file is part of the Symfony package.
|
|
||||||
*
|
|
||||||
* (c) Fabien Potencier <fabien@symfony.com>
|
|
||||||
*
|
|
||||||
* 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 <bschussek@gmail.com>
|
|
||||||
*/
|
|
||||||
class MaxCountValidatorArrayTest extends MaxCountValidatorTest
|
|
||||||
{
|
|
||||||
protected function createCollection(array $content)
|
|
||||||
{
|
|
||||||
return $content;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,113 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
/*
|
|
||||||
* This file is part of the Symfony package.
|
|
||||||
*
|
|
||||||
* (c) Fabien Potencier <fabien@symfony.com>
|
|
||||||
*
|
|
||||||
* 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 <bschussek@gmail.com>
|
|
||||||
*/
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,38 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
/*
|
|
||||||
* This file is part of the Symfony package.
|
|
||||||
*
|
|
||||||
* (c) Fabien Potencier <fabien@symfony.com>
|
|
||||||
*
|
|
||||||
* 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 <bschussek@gmail.com>
|
|
||||||
*/
|
|
||||||
class MinCountValidatorCountableTest extends MinCountValidatorTest
|
|
||||||
{
|
|
||||||
protected function createCollection(array $content)
|
|
||||||
{
|
|
||||||
return new MinCountValidatorCountableTest_Countable($content);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,114 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
/*
|
|
||||||
* This file is part of the Symfony package.
|
|
||||||
*
|
|
||||||
* (c) Fabien Potencier <fabien@symfony.com>
|
|
||||||
*
|
|
||||||
* 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 <bschussek@gmail.com>
|
|
||||||
*/
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,237 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
}
|
@ -1,121 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
/*
|
|
||||||
* This file is part of the Symfony package.
|
|
||||||
*
|
|
||||||
* (c) Fabien Potencier <fabien@symfony.com>
|
|
||||||
*
|
|
||||||
* 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);
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user