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
|
|
|
|
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-01-10 10:09:32 +00:00
|
|
|
use Symfony\Component\Validator\Constraints\NotBlank;
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Validator\Constraints\Collection;
|
|
|
|
use Symfony\Component\Validator\Constraints\CollectionValidator;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2012-01-10 10:09:32 +00:00
|
|
|
/**
|
|
|
|
* This class is a hand written simplified version of PHP native `ArrayObject`
|
|
|
|
* class, to show that it behaves different than PHP native implementation.
|
|
|
|
*/
|
|
|
|
class TestArrayObject implements \ArrayAccess, \IteratorAggregate, \Countable, \Serializable
|
|
|
|
{
|
|
|
|
private $array;
|
|
|
|
|
|
|
|
public function __construct(array $array = null)
|
|
|
|
{
|
|
|
|
$this->array = (array) ($array ?: array());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function offsetExists($offset)
|
|
|
|
{
|
|
|
|
return array_key_exists($offset, $this->array);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function offsetGet($offset)
|
|
|
|
{
|
|
|
|
return $this->array[$offset];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function offsetSet($offset, $value)
|
|
|
|
{
|
|
|
|
if (null === $offset) {
|
|
|
|
$this->array[] = $value;
|
|
|
|
} else {
|
|
|
|
$this->array[$offset] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function offsetUnset($offset)
|
|
|
|
{
|
|
|
|
if (array_key_exists($offset, $this->array)) {
|
|
|
|
unset($this->array[$offset]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getIterator()
|
|
|
|
{
|
|
|
|
return new \ArrayIterator($this->array);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function count()
|
|
|
|
{
|
|
|
|
return count($this->array);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function serialize()
|
|
|
|
{
|
|
|
|
return serialize($this->array);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function unserialize($serialized)
|
|
|
|
{
|
|
|
|
$this->array = (array) unserialize((string) $serialized);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-24 09:40:05 +01:00
|
|
|
class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
protected $validator;
|
|
|
|
protected $walker;
|
|
|
|
protected $context;
|
|
|
|
|
2010-12-06 14:45:37 +00:00
|
|
|
protected function setUp()
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-08-20 22:09:55 +01:00
|
|
|
$this->walker = $this->getMock('Symfony\Component\Validator\GraphWalker', array(), array(), '', false);
|
|
|
|
$metadataFactory = $this->getMock('Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface');
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2011-01-19 14:02:00 +00:00
|
|
|
$this->context = new ExecutionContext('Root', $this->walker, $metadataFactory);
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
$this->validator = new CollectionValidator();
|
|
|
|
$this->validator->initialize($this->context);
|
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()
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertTrue($this->validator->isValid(null, new Collection(array('fields' => array(
|
|
|
|
'foo' => new Min(4),
|
|
|
|
)))));
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2011-03-31 14:36:04 +01:00
|
|
|
public function testFieldsAsDefaultOption()
|
|
|
|
{
|
2012-01-10 10:09:32 +00:00
|
|
|
$this->assertTrue($this->validator->isValid(array('foo' => 'foobar'), new Collection(array(
|
2011-03-31 14:36:04 +01:00
|
|
|
'foo' => new Min(4),
|
2012-01-10 10:09:32 +00:00
|
|
|
))));
|
|
|
|
$this->assertTrue($this->validator->isValid(new \ArrayObject(array('foo' => 'foobar')), new Collection(array(
|
|
|
|
'foo' => new Min(4),
|
|
|
|
))));
|
|
|
|
$this->assertTrue($this->validator->isValid(new TestArrayObject(array('foo' => 'foobar')), new Collection(array(
|
|
|
|
'foo' => new Min(4),
|
|
|
|
))));
|
2011-03-31 14:36:04 +01:00
|
|
|
}
|
|
|
|
|
2012-01-10 10:09:32 +00:00
|
|
|
/**
|
|
|
|
* @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException
|
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testThrowsExceptionIfNotTraversable()
|
|
|
|
{
|
|
|
|
$this->validator->isValid('foobar', new Collection(array('fields' => array(
|
|
|
|
'foo' => new Min(4),
|
|
|
|
))));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getValidArguments
|
|
|
|
*/
|
|
|
|
public function testWalkSingleConstraint($array)
|
|
|
|
{
|
|
|
|
$this->context->setGroup('MyGroup');
|
|
|
|
$this->context->setPropertyPath('foo');
|
|
|
|
|
|
|
|
$constraint = new Min(4);
|
|
|
|
|
|
|
|
foreach ($array as $key => $value) {
|
|
|
|
$this->walker->expects($this->once())
|
|
|
|
->method('walkConstraint')
|
|
|
|
->with($this->equalTo($constraint), $this->equalTo($value), $this->equalTo('MyGroup'), $this->equalTo('foo['.$key.']'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertTrue($this->validator->isValid($array, new Collection(array(
|
|
|
|
'fields' => array(
|
|
|
|
'foo' => $constraint,
|
|
|
|
),
|
|
|
|
))));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getValidArguments
|
|
|
|
*/
|
|
|
|
public function testWalkMultipleConstraints($array)
|
|
|
|
{
|
|
|
|
$this->context->setGroup('MyGroup');
|
|
|
|
$this->context->setPropertyPath('foo');
|
|
|
|
|
|
|
|
$constraint = new Min(4);
|
|
|
|
// can only test for twice the same constraint because PHPUnits mocking
|
|
|
|
// can't test method calls with different arguments
|
|
|
|
$constraints = array($constraint, $constraint);
|
|
|
|
|
|
|
|
foreach ($array as $key => $value) {
|
|
|
|
$this->walker->expects($this->exactly(2))
|
|
|
|
->method('walkConstraint')
|
|
|
|
->with($this->equalTo($constraint), $this->equalTo($value), $this->equalTo('MyGroup'), $this->equalTo('foo['.$key.']'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertTrue($this->validator->isValid($array, new Collection(array(
|
|
|
|
'fields' => array(
|
|
|
|
'foo' => $constraints,
|
|
|
|
)
|
|
|
|
))));
|
|
|
|
}
|
|
|
|
|
2012-01-10 10:09:32 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider getArgumentsWithExtraFields
|
|
|
|
*/
|
|
|
|
public function testExtraFieldsDisallowed($array)
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
|
|
|
$this->assertFalse($this->validator->isValid($array, new Collection(array(
|
|
|
|
'fields' => array(
|
|
|
|
'foo' => new Min(4),
|
|
|
|
),
|
|
|
|
))));
|
|
|
|
}
|
|
|
|
|
|
|
|
// bug fix
|
|
|
|
public function testNullNotConsideredExtraField()
|
|
|
|
{
|
|
|
|
$array = array(
|
|
|
|
'foo' => null,
|
|
|
|
);
|
2012-01-10 10:09:32 +00:00
|
|
|
$collection = new Collection(array(
|
2010-06-24 10:24:08 +01:00
|
|
|
'fields' => array(
|
|
|
|
'foo' => new Min(4),
|
|
|
|
),
|
2012-01-10 10:09:32 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertTrue($this->validator->isValid($array, $collection));
|
|
|
|
$this->assertTrue($this->validator->isValid(new \ArrayObject($array), $collection));
|
|
|
|
$this->assertTrue($this->validator->isValid(new TestArrayObject($array), $collection));
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testExtraFieldsAllowed()
|
|
|
|
{
|
|
|
|
$array = array(
|
|
|
|
'foo' => 5,
|
|
|
|
'bar' => 6,
|
|
|
|
);
|
2012-01-10 10:09:32 +00:00
|
|
|
$collection = new Collection(array(
|
2010-06-24 10:24:08 +01:00
|
|
|
'fields' => array(
|
|
|
|
'foo' => new Min(4),
|
|
|
|
),
|
|
|
|
'allowExtraFields' => true,
|
2012-01-10 10:09:32 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertTrue($this->validator->isValid($array, $collection));
|
|
|
|
$this->assertTrue($this->validator->isValid(new \ArrayObject($array), $collection));
|
|
|
|
$this->assertTrue($this->validator->isValid(new TestArrayObject($array), $collection));
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMissingFieldsDisallowed()
|
|
|
|
{
|
|
|
|
$this->assertFalse($this->validator->isValid(array(), new Collection(array(
|
|
|
|
'fields' => array(
|
|
|
|
'foo' => new Min(4),
|
|
|
|
),
|
|
|
|
))));
|
2012-01-10 10:09:32 +00:00
|
|
|
$this->assertFalse($this->validator->isValid(new \ArrayObject(array()), new Collection(array(
|
|
|
|
'fields' => array(
|
|
|
|
'foo' => new Min(4),
|
|
|
|
),
|
|
|
|
))));
|
|
|
|
$this->assertFalse($this->validator->isValid(new TestArrayObject(array()), new Collection(array(
|
|
|
|
'fields' => array(
|
|
|
|
'foo' => new Min(4),
|
|
|
|
),
|
|
|
|
))));
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMissingFieldsAllowed()
|
|
|
|
{
|
|
|
|
$this->assertTrue($this->validator->isValid(array(), new Collection(array(
|
|
|
|
'fields' => array(
|
|
|
|
'foo' => new Min(4),
|
|
|
|
),
|
|
|
|
'allowMissingFields' => true,
|
|
|
|
))));
|
2012-01-10 10:09:32 +00:00
|
|
|
$this->assertTrue($this->validator->isValid(new \ArrayObject(array()), new Collection(array(
|
|
|
|
'fields' => array(
|
|
|
|
'foo' => new Min(4),
|
|
|
|
),
|
|
|
|
'allowMissingFields' => true,
|
|
|
|
))));
|
|
|
|
$this->assertTrue($this->validator->isValid(new TestArrayObject(array()), new Collection(array(
|
|
|
|
'fields' => array(
|
|
|
|
'foo' => new Min(4),
|
|
|
|
),
|
|
|
|
'allowMissingFields' => true,
|
|
|
|
))));
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2012-01-10 10:09:32 +00:00
|
|
|
public function testArrayAccessObject() {
|
|
|
|
$value = new TestArrayObject();
|
|
|
|
$value['foo'] = 12;
|
|
|
|
$value['asdf'] = 'asdfaf';
|
|
|
|
|
|
|
|
$this->assertTrue(isset($value['asdf']));
|
|
|
|
$this->assertTrue(isset($value['foo']));
|
|
|
|
$this->assertFalse(empty($value['asdf']));
|
|
|
|
$this->assertFalse(empty($value['foo']));
|
|
|
|
|
|
|
|
$result = $this->validator->isValid($value, new Collection(array(
|
|
|
|
'fields' => array(
|
|
|
|
'foo' => new NotBlank(),
|
|
|
|
'asdf' => new NotBlank()
|
|
|
|
)
|
|
|
|
)));
|
|
|
|
|
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testArrayObject() {
|
|
|
|
$value = new \ArrayObject(array());
|
|
|
|
$value['foo'] = 12;
|
|
|
|
$value['asdf'] = 'asdfaf';
|
|
|
|
|
|
|
|
$this->assertTrue(isset($value['asdf']));
|
|
|
|
$this->assertTrue(isset($value['foo']));
|
|
|
|
$this->assertFalse(empty($value['asdf']));
|
|
|
|
$this->assertFalse(empty($value['foo']));
|
|
|
|
|
|
|
|
$result = $this->validator->isValid($value, new Collection(array(
|
|
|
|
'fields' => array(
|
|
|
|
'foo' => new NotBlank(),
|
|
|
|
'asdf' => new NotBlank()
|
|
|
|
)
|
|
|
|
)));
|
|
|
|
|
|
|
|
$this->assertTrue($result);
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testObjectShouldBeLeftUnchanged()
|
|
|
|
{
|
|
|
|
$value = new \ArrayObject(array(
|
|
|
|
'foo' => 3
|
|
|
|
));
|
|
|
|
$this->validator->isValid($value, new Collection(array(
|
|
|
|
'fields' => array(
|
|
|
|
'foo' => new Min(2),
|
|
|
|
)
|
|
|
|
)));
|
|
|
|
|
|
|
|
$this->assertEquals(array(
|
|
|
|
'foo' => 3
|
|
|
|
), (array) $value);
|
|
|
|
}
|
2012-01-10 10:09:32 +00:00
|
|
|
|
|
|
|
public function getValidArguments()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
// can only test for one entry, because PHPUnits mocking does not allow
|
|
|
|
// to expect multiple method calls with different arguments
|
|
|
|
array(array('foo' => 3)),
|
|
|
|
array(new \ArrayObject(array('foo' => 3))),
|
|
|
|
array(new TestArrayObject(array('foo' => 3))),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getArgumentsWithExtraFields()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array(array(
|
|
|
|
'foo' => 5,
|
|
|
|
'bar' => 6,
|
|
|
|
)),
|
|
|
|
array(new \ArrayObject(array(
|
|
|
|
'foo' => 5,
|
|
|
|
'bar' => 6,
|
|
|
|
))),
|
|
|
|
array(new TestArrayObject(array(
|
|
|
|
'foo' => 5,
|
|
|
|
'bar' => 6,
|
|
|
|
)))
|
|
|
|
);
|
|
|
|
}
|
2011-06-08 18:56:59 +01:00
|
|
|
}
|
2012-01-10 10:09:32 +00:00
|
|
|
|