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
|
|
|
use Symfony\Component\Validator\GlobalExecutionContext;
|
|
|
|
|
2012-01-30 19:57:20 +00:00
|
|
|
use Symfony\Component\Validator\ConstraintViolation;
|
|
|
|
use Symfony\Component\Validator\ConstraintViolationList;
|
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\NotNull;
|
2012-01-15 19:31:13 +00:00
|
|
|
use Symfony\Component\Validator\Constraints\Collection\Required;
|
|
|
|
use Symfony\Component\Validator\Constraints\Collection\Optional;
|
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-16 12:03:38 +00:00
|
|
|
abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
protected $validator;
|
|
|
|
protected $walker;
|
2012-01-31 20:35:48 +00:00
|
|
|
protected $globalContext;
|
2010-06-24 10:24:08 +01:00
|
|
|
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
|
|
|
|
2012-01-31 20:35:48 +00:00
|
|
|
$this->globalContext = new GlobalExecutionContext('Root', $this->walker, $metadataFactory);
|
|
|
|
$this->context = new ExecutionContext($this->globalContext, 'value', 'bar', 'MyGroup', 'ClassName', 'propertyName');
|
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;
|
2012-01-31 20:35:48 +00:00
|
|
|
$this->globalContext = null;
|
2011-06-14 09:44:38 +01:00
|
|
|
$this->context = null;
|
|
|
|
}
|
|
|
|
|
2012-01-16 12:03:38 +00:00
|
|
|
abstract protected function prepareTestData(array $contents);
|
|
|
|
|
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-16 12:03:38 +00:00
|
|
|
$data = $this->prepareTestData(array('foo' => 'foobar'));
|
|
|
|
|
|
|
|
$this->assertTrue($this->validator->isValid($data, new Collection(array(
|
2012-01-10 10:09:32 +00:00
|
|
|
'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),
|
|
|
|
))));
|
|
|
|
}
|
|
|
|
|
2012-01-16 12:03:38 +00:00
|
|
|
public function testWalkSingleConstraint()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
|
|
|
$constraint = new Min(4);
|
|
|
|
|
2012-01-16 12:03:38 +00:00
|
|
|
$array = array('foo' => 3);
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
foreach ($array as $key => $value) {
|
|
|
|
$this->walker->expects($this->once())
|
2012-01-16 12:03:38 +00:00
|
|
|
->method('walkConstraint')
|
2012-01-31 20:35:48 +00:00
|
|
|
->with($this->equalTo($constraint), $this->equalTo($value), $this->equalTo('MyGroup'), $this->equalTo('bar['.$key.']'));
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2012-01-16 12:03:38 +00:00
|
|
|
$data = $this->prepareTestData($array);
|
|
|
|
|
|
|
|
$this->assertTrue($this->validator->isValid($data, new Collection(array(
|
2010-06-24 10:24:08 +01:00
|
|
|
'fields' => array(
|
|
|
|
'foo' => $constraint,
|
|
|
|
),
|
|
|
|
))));
|
|
|
|
}
|
|
|
|
|
2012-01-16 12:03:38 +00:00
|
|
|
public function testWalkMultipleConstraints()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
2012-01-16 12:03:38 +00:00
|
|
|
$constraints = array(
|
|
|
|
new Min(4),
|
|
|
|
new NotNull(),
|
|
|
|
);
|
|
|
|
$array = array('foo' => 3);
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
foreach ($array as $key => $value) {
|
2012-01-16 12:03:38 +00:00
|
|
|
foreach ($constraints as $i => $constraint) {
|
|
|
|
$this->walker->expects($this->at($i))
|
|
|
|
->method('walkConstraint')
|
2012-01-31 20:35:48 +00:00
|
|
|
->with($this->equalTo($constraint), $this->equalTo($value), $this->equalTo('MyGroup'), $this->equalTo('bar['.$key.']'));
|
2012-01-16 12:03:38 +00:00
|
|
|
}
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2012-01-16 12:03:38 +00:00
|
|
|
$data = $this->prepareTestData($array);
|
|
|
|
|
|
|
|
$this->assertTrue($this->validator->isValid($data, new Collection(array(
|
2010-06-24 10:24:08 +01:00
|
|
|
'fields' => array(
|
|
|
|
'foo' => $constraints,
|
|
|
|
)
|
|
|
|
))));
|
|
|
|
}
|
|
|
|
|
2012-01-16 12:03:38 +00:00
|
|
|
public function testExtraFieldsDisallowed()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
2012-01-16 12:03:38 +00:00
|
|
|
$data = $this->prepareTestData(array(
|
|
|
|
'foo' => 5,
|
2012-01-30 19:57:20 +00:00
|
|
|
'baz' => 6,
|
2012-01-16 12:03:38 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertFalse($this->validator->isValid($data, new Collection(array(
|
2010-06-24 10:24:08 +01:00
|
|
|
'fields' => array(
|
|
|
|
'foo' => new Min(4),
|
|
|
|
),
|
|
|
|
))));
|
2012-01-30 19:57:20 +00:00
|
|
|
|
|
|
|
$this->assertEquals(new ConstraintViolationList(array(
|
|
|
|
new ConstraintViolation(
|
|
|
|
'This field was not expected',
|
|
|
|
array('{{ field }}' => '"baz"'),
|
|
|
|
'Root',
|
|
|
|
'bar[baz]',
|
2012-01-31 17:42:14 +00:00
|
|
|
6
|
2012-01-30 19:57:20 +00:00
|
|
|
),
|
|
|
|
)), $this->context->getViolations());
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// bug fix
|
|
|
|
public function testNullNotConsideredExtraField()
|
|
|
|
{
|
2012-01-16 12:03:38 +00:00
|
|
|
$data = $this->prepareTestData(array(
|
2010-06-24 10:24:08 +01:00
|
|
|
'foo' => null,
|
2012-01-16 12:03:38 +00:00
|
|
|
));
|
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
|
|
|
));
|
|
|
|
|
2012-01-16 12:03:38 +00:00
|
|
|
$this->assertTrue($this->validator->isValid($data, $collection));
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testExtraFieldsAllowed()
|
|
|
|
{
|
2012-01-16 12:03:38 +00:00
|
|
|
$data = $this->prepareTestData(array(
|
2010-06-24 10:24:08 +01:00
|
|
|
'foo' => 5,
|
|
|
|
'bar' => 6,
|
2012-01-16 12:03:38 +00:00
|
|
|
));
|
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
|
|
|
));
|
|
|
|
|
2012-01-16 12:03:38 +00:00
|
|
|
$this->assertTrue($this->validator->isValid($data, $collection));
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMissingFieldsDisallowed()
|
|
|
|
{
|
2012-01-16 12:03:38 +00:00
|
|
|
$data = $this->prepareTestData(array());
|
|
|
|
|
|
|
|
$this->assertFalse($this->validator->isValid($data, new Collection(array(
|
2012-01-10 10:09:32 +00:00
|
|
|
'fields' => array(
|
|
|
|
'foo' => new Min(4),
|
|
|
|
),
|
|
|
|
))));
|
2012-01-30 19:57:20 +00:00
|
|
|
|
|
|
|
$this->assertEquals(new ConstraintViolationList(array(
|
|
|
|
new ConstraintViolation(
|
|
|
|
'This field is missing',
|
|
|
|
array('{{ field }}' => '"foo"'),
|
|
|
|
'Root',
|
|
|
|
'bar[foo]',
|
2012-01-31 17:42:14 +00:00
|
|
|
null
|
2012-01-30 19:57:20 +00:00
|
|
|
),
|
|
|
|
)), $this->context->getViolations());
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMissingFieldsAllowed()
|
|
|
|
{
|
2012-01-16 12:03:38 +00:00
|
|
|
$data = $this->prepareTestData(array());
|
|
|
|
|
|
|
|
$this->assertTrue($this->validator->isValid($data, new Collection(array(
|
2012-01-10 10:09:32 +00:00
|
|
|
'fields' => array(
|
|
|
|
'foo' => new Min(4),
|
|
|
|
),
|
|
|
|
'allowMissingFields' => true,
|
|
|
|
))));
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2011-12-13 22:40:32 +00:00
|
|
|
public function testOptionalFieldPresent()
|
|
|
|
{
|
2012-01-16 12:03:38 +00:00
|
|
|
$data = $this->prepareTestData(array(
|
2011-12-13 22:40:32 +00:00
|
|
|
'foo' => null,
|
2012-01-16 12:03:38 +00:00
|
|
|
));
|
2011-12-13 22:40:32 +00:00
|
|
|
|
2012-01-16 12:03:38 +00:00
|
|
|
$this->assertTrue($this->validator->isValid($data, new Collection(array(
|
2011-12-13 22:40:32 +00:00
|
|
|
'foo' => new Optional(),
|
|
|
|
))));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testOptionalFieldNotPresent()
|
|
|
|
{
|
2012-01-16 12:03:38 +00:00
|
|
|
$data = $this->prepareTestData(array());
|
2011-12-13 22:40:32 +00:00
|
|
|
|
2012-01-16 12:03:38 +00:00
|
|
|
$this->assertTrue($this->validator->isValid($data, new Collection(array(
|
2011-12-13 22:40:32 +00:00
|
|
|
'foo' => new Optional(),
|
|
|
|
))));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testOptionalFieldSingleConstraint()
|
|
|
|
{
|
|
|
|
$array = array(
|
|
|
|
'foo' => 5,
|
|
|
|
);
|
|
|
|
|
|
|
|
$constraint = new Min(4);
|
|
|
|
|
|
|
|
$this->walker->expects($this->once())
|
|
|
|
->method('walkConstraint')
|
|
|
|
->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]'));
|
|
|
|
|
2012-01-16 12:03:38 +00:00
|
|
|
$data = $this->prepareTestData($array);
|
|
|
|
|
|
|
|
$this->assertTrue($this->validator->isValid($data, new Collection(array(
|
2011-12-13 22:40:32 +00:00
|
|
|
'foo' => new Optional($constraint),
|
|
|
|
))));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testOptionalFieldMultipleConstraints()
|
|
|
|
{
|
|
|
|
$array = array(
|
|
|
|
'foo' => 5,
|
|
|
|
);
|
|
|
|
|
|
|
|
$constraints = array(
|
|
|
|
new NotNull(),
|
|
|
|
new Min(4),
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($constraints as $i => $constraint) {
|
|
|
|
$this->walker->expects($this->at($i))
|
|
|
|
->method('walkConstraint')
|
|
|
|
->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]'));
|
|
|
|
}
|
|
|
|
|
2012-01-16 12:03:38 +00:00
|
|
|
$data = $this->prepareTestData($array);
|
|
|
|
|
|
|
|
$this->assertTrue($this->validator->isValid($data, new Collection(array(
|
2011-12-13 22:40:32 +00:00
|
|
|
'foo' => new Optional($constraints),
|
|
|
|
))));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRequiredFieldPresent()
|
|
|
|
{
|
2012-01-16 12:03:38 +00:00
|
|
|
$data = $this->prepareTestData(array(
|
2011-12-13 22:40:32 +00:00
|
|
|
'foo' => null,
|
2012-01-16 12:03:38 +00:00
|
|
|
));
|
2011-12-13 22:40:32 +00:00
|
|
|
|
2012-01-16 12:03:38 +00:00
|
|
|
$this->assertTrue($this->validator->isValid($data, new Collection(array(
|
2011-12-13 22:40:32 +00:00
|
|
|
'foo' => new Required(),
|
|
|
|
))));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRequiredFieldNotPresent()
|
|
|
|
{
|
2012-01-16 12:03:38 +00:00
|
|
|
$data = $this->prepareTestData(array());
|
2011-12-13 22:40:32 +00:00
|
|
|
|
2012-01-16 12:03:38 +00:00
|
|
|
$this->assertFalse($this->validator->isValid($data, new Collection(array(
|
2011-12-13 22:40:32 +00:00
|
|
|
'foo' => new Required(),
|
|
|
|
))));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRequiredFieldSingleConstraint()
|
|
|
|
{
|
|
|
|
$array = array(
|
|
|
|
'foo' => 5,
|
|
|
|
);
|
|
|
|
|
|
|
|
$constraint = new Min(4);
|
|
|
|
|
|
|
|
$this->walker->expects($this->once())
|
2012-01-16 12:03:38 +00:00
|
|
|
->method('walkConstraint')
|
|
|
|
->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]'));
|
2011-12-13 22:40:32 +00:00
|
|
|
|
2012-01-16 12:03:38 +00:00
|
|
|
$data = $this->prepareTestData($array);
|
|
|
|
|
|
|
|
$this->assertTrue($this->validator->isValid($data, new Collection(array(
|
2011-12-13 22:40:32 +00:00
|
|
|
'foo' => new Required($constraint),
|
|
|
|
))));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRequiredFieldMultipleConstraints()
|
|
|
|
{
|
|
|
|
$array = array(
|
|
|
|
'foo' => 5,
|
|
|
|
);
|
|
|
|
|
|
|
|
$constraints = array(
|
|
|
|
new NotNull(),
|
|
|
|
new Min(4),
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($constraints as $i => $constraint) {
|
|
|
|
$this->walker->expects($this->at($i))
|
2012-01-16 12:03:38 +00:00
|
|
|
->method('walkConstraint')
|
|
|
|
->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]'));
|
2011-12-13 22:40:32 +00:00
|
|
|
}
|
|
|
|
|
2012-01-16 12:03:38 +00:00
|
|
|
$data = $this->prepareTestData($array);
|
|
|
|
|
2011-12-13 22:40:32 +00:00
|
|
|
$this->assertTrue($this->validator->isValid($array, new Collection(array(
|
|
|
|
'foo' => new Required($constraints),
|
|
|
|
))));
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
2011-06-08 18:56:59 +01:00
|
|
|
}
|