[Validator] Removed MinCount and MaxCount and replaced them by the constraint Count
This commit is contained in:
parent
741c147ce5
commit
0cdacee5be
@ -21,5 +21,5 @@ 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
|
|
||||||
* deprecated the Size constraint and renamed it to Range
|
* deprecated the Size constraint and renamed it to Range
|
||||||
|
* added Count constraint
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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');
|
|
||||||
}
|
|
||||||
}
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user