2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-15 13:29:43 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2011-05-05 17:37:03 +01:00
|
|
|
namespace Symfony\Tests\Component\Validator\Constraints;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2012-01-31 20:35:48 +00:00
|
|
|
|
2011-01-19 14:02:00 +00:00
|
|
|
use Symfony\Component\Validator\ExecutionContext;
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Validator\Constraints\Min;
|
2012-02-01 12:53:45 +00:00
|
|
|
use Symfony\Component\Validator\Constraints\Max;
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Validator\Constraints\All;
|
|
|
|
use Symfony\Component\Validator\Constraints\AllValidator;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
|
|
|
class AllValidatorTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
protected $walker;
|
|
|
|
protected $context;
|
2012-02-01 12:53:45 +00:00
|
|
|
protected $validator;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-12-06 14:45:37 +00:00
|
|
|
protected function setUp()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
2010-08-20 22:09:55 +01:00
|
|
|
$this->walker = $this->getMock('Symfony\Component\Validator\GraphWalker', array(), array(), '', false);
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false);
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->validator = new AllValidator();
|
|
|
|
$this->validator->initialize($this->context);
|
2012-02-01 12:53:45 +00:00
|
|
|
|
|
|
|
$this->context->expects($this->any())
|
|
|
|
->method('getGraphWalker')
|
|
|
|
->will($this->returnValue($this->walker));
|
|
|
|
$this->context->expects($this->any())
|
|
|
|
->method('getGroup')
|
|
|
|
->will($this->returnValue('MyGroup'));
|
|
|
|
$this->context->expects($this->any())
|
|
|
|
->method('getPropertyPath')
|
|
|
|
->will($this->returnValue('foo.bar'));
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2011-06-14 09:44:38 +01:00
|
|
|
protected function tearDown()
|
|
|
|
{
|
|
|
|
$this->validator = null;
|
|
|
|
$this->walker = null;
|
|
|
|
$this->context = null;
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testNullIsValid()
|
|
|
|
{
|
|
|
|
$this->assertTrue($this->validator->isValid(null, new All(new Min(4))));
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException
|
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testThrowsExceptionIfNotTraversable()
|
|
|
|
{
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->validator->isValid('foo.barbar', new All(new Min(4)));
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* @dataProvider getValidArguments
|
|
|
|
*/
|
|
|
|
public function testWalkSingleConstraint($array)
|
|
|
|
{
|
|
|
|
$constraint = new Min(4);
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$i = 0;
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
foreach ($array as $key => $value) {
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->walker->expects($this->at($i++))
|
|
|
|
->method('walkConstraint')
|
|
|
|
->with($constraint, $value, 'MyGroup', 'foo.bar['.$key.']');
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->never())
|
|
|
|
->method('addViolation');
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertTrue($this->validator->isValid($array, new All($constraint)));
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* @dataProvider getValidArguments
|
|
|
|
*/
|
|
|
|
public function testWalkMultipleConstraints($array)
|
|
|
|
{
|
2012-02-01 12:53:45 +00:00
|
|
|
$constraint1 = new Min(4);
|
|
|
|
$constraint2 = new Max(6);
|
|
|
|
|
|
|
|
$constraints = array($constraint1, $constraint2);
|
|
|
|
$i = 0;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
foreach ($array as $key => $value) {
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->walker->expects($this->at($i++))
|
|
|
|
->method('walkConstraint')
|
|
|
|
->with($constraint1, $value, 'MyGroup', 'foo.bar['.$key.']');
|
|
|
|
$this->walker->expects($this->at($i++))
|
|
|
|
->method('walkConstraint')
|
|
|
|
->with($constraint2, $value, 'MyGroup', 'foo.bar['.$key.']');
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->never())
|
|
|
|
->method('addViolation');
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertTrue($this->validator->isValid($array, new All($constraints)));
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function getValidArguments()
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
return array(
|
2012-02-01 12:53:45 +00:00
|
|
|
array(array(5, 6, 7)),
|
|
|
|
array(new \ArrayObject(array(5, 6, 7))),
|
2010-06-24 10:24:08 +01:00
|
|
|
);
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
2011-06-08 18:56:59 +01:00
|
|
|
}
|