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.
|
|
|
|
*/
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Tests\Component\Validator;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-11-16 23:51:45 +00:00
|
|
|
require_once __DIR__.'/Fixtures/Entity.php';
|
|
|
|
require_once __DIR__.'/Fixtures/FailingConstraint.php';
|
|
|
|
require_once __DIR__.'/Fixtures/FailingConstraintValidator.php';
|
|
|
|
require_once __DIR__.'/Fixtures/FakeClassMetadataFactory.php';
|
|
|
|
|
|
|
|
use Symfony\Tests\Component\Validator\Fixtures\Entity;
|
|
|
|
use Symfony\Tests\Component\Validator\Fixtures\FakeClassMetadataFactory;
|
|
|
|
use Symfony\Tests\Component\Validator\Fixtures\FailingConstraint;
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Validator\Validator;
|
|
|
|
use Symfony\Component\Validator\ConstraintViolation;
|
|
|
|
use Symfony\Component\Validator\ConstraintViolationList;
|
2010-11-16 23:51:45 +00:00
|
|
|
use Symfony\Component\Validator\ConstraintValidatorFactory;
|
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-11-16 23:51:45 +00:00
|
|
|
class ValidatorTest extends \PHPUnit_Framework_TestCase
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-11-16 23:51:45 +00:00
|
|
|
protected $factory;
|
|
|
|
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-11-16 23:51:45 +00:00
|
|
|
$this->factory = new FakeClassMetadataFactory();
|
|
|
|
$this->validator = new Validator($this->factory, new ConstraintValidatorFactory());
|
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->factory = null;
|
|
|
|
$this->validator = null;
|
|
|
|
}
|
|
|
|
|
2010-11-16 23:51:45 +00:00
|
|
|
public function testValidate_defaultGroup()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
2010-11-16 23:51:45 +00:00
|
|
|
$entity = new Entity();
|
|
|
|
$metadata = new ClassMetadata(get_class($entity));
|
|
|
|
$metadata->addPropertyConstraint('firstName', new FailingConstraint());
|
|
|
|
$metadata->addPropertyConstraint('lastName', new FailingConstraint(array(
|
|
|
|
'groups' => 'Custom',
|
|
|
|
)));
|
|
|
|
$this->factory->addClassMetadata($metadata);
|
|
|
|
|
|
|
|
// Only the constraint of group "Default" failed
|
|
|
|
$violations = new ConstraintViolationList();
|
|
|
|
$violations->add(new ConstraintViolation(
|
|
|
|
'',
|
|
|
|
array(),
|
|
|
|
$entity,
|
|
|
|
'firstName',
|
|
|
|
''
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertEquals($violations, $this->validator->validate($entity));
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-11-16 23:51:45 +00:00
|
|
|
public function testValidate_oneGroup()
|
|
|
|
{
|
|
|
|
$entity = new Entity();
|
|
|
|
$metadata = new ClassMetadata(get_class($entity));
|
|
|
|
$metadata->addPropertyConstraint('firstName', new FailingConstraint());
|
|
|
|
$metadata->addPropertyConstraint('lastName', new FailingConstraint(array(
|
|
|
|
'groups' => 'Custom',
|
|
|
|
)));
|
|
|
|
$this->factory->addClassMetadata($metadata);
|
|
|
|
|
|
|
|
// Only the constraint of group "Custom" failed
|
|
|
|
$violations = new ConstraintViolationList();
|
|
|
|
$violations->add(new ConstraintViolation(
|
|
|
|
'',
|
|
|
|
array(),
|
|
|
|
$entity,
|
|
|
|
'lastName',
|
|
|
|
''
|
|
|
|
));
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-11-16 23:51:45 +00:00
|
|
|
$this->assertEquals($violations, $this->validator->validate($entity, 'Custom'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidate_multipleGroups()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
2010-11-16 23:51:45 +00:00
|
|
|
$entity = new Entity();
|
|
|
|
$metadata = new ClassMetadata(get_class($entity));
|
|
|
|
$metadata->addPropertyConstraint('firstName', new FailingConstraint(array(
|
|
|
|
'groups' => 'First',
|
|
|
|
)));
|
|
|
|
$metadata->addPropertyConstraint('lastName', new FailingConstraint(array(
|
|
|
|
'groups' => 'Second',
|
|
|
|
)));
|
|
|
|
$this->factory->addClassMetadata($metadata);
|
|
|
|
|
|
|
|
// The constraints of both groups failed
|
|
|
|
$violations = new ConstraintViolationList();
|
|
|
|
$violations->add(new ConstraintViolation(
|
|
|
|
'',
|
|
|
|
array(),
|
|
|
|
$entity,
|
|
|
|
'firstName',
|
|
|
|
''
|
|
|
|
));
|
|
|
|
$violations->add(new ConstraintViolation(
|
|
|
|
'',
|
|
|
|
array(),
|
|
|
|
$entity,
|
|
|
|
'lastName',
|
|
|
|
''
|
2010-06-24 10:24:08 +01:00
|
|
|
));
|
|
|
|
|
2010-11-16 23:51:45 +00:00
|
|
|
$result = $this->validator->validate($entity, array('First', 'Second'));
|
|
|
|
|
|
|
|
$this->assertEquals($violations, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateProperty()
|
|
|
|
{
|
|
|
|
$entity = new Entity();
|
|
|
|
$metadata = new ClassMetadata(get_class($entity));
|
|
|
|
$metadata->addPropertyConstraint('firstName', new FailingConstraint());
|
|
|
|
$this->factory->addClassMetadata($metadata);
|
|
|
|
|
|
|
|
$result = $this->validator->validateProperty($entity, 'firstName');
|
|
|
|
|
2012-01-18 13:42:47 +00:00
|
|
|
$this->assertCount(1, $result);
|
2010-11-16 23:51:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidatePropertyValue()
|
|
|
|
{
|
|
|
|
$entity = new Entity();
|
|
|
|
$metadata = new ClassMetadata(get_class($entity));
|
|
|
|
$metadata->addPropertyConstraint('firstName', new FailingConstraint());
|
|
|
|
$this->factory->addClassMetadata($metadata);
|
|
|
|
|
|
|
|
$result = $this->validator->validatePropertyValue(get_class($entity), 'firstName', 'Bernhard');
|
|
|
|
|
2012-01-18 13:42:47 +00:00
|
|
|
$this->assertCount(1, $result);
|
2010-11-16 23:51:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateValue()
|
|
|
|
{
|
2012-01-28 14:21:08 +00:00
|
|
|
$violations = new ConstraintViolationList();
|
|
|
|
$violations->add(new ConstraintViolation(
|
|
|
|
'',
|
|
|
|
array(),
|
|
|
|
'',
|
|
|
|
'',
|
|
|
|
'Bernhard'
|
|
|
|
));
|
2010-11-16 23:51:45 +00:00
|
|
|
|
2012-01-28 14:21:08 +00:00
|
|
|
$this->assertEquals($violations, $this->validator->validateValue('Bernhard', new FailingConstraint()));
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
2011-05-08 22:52:34 +01:00
|
|
|
|
|
|
|
public function testGetMetadataFactory()
|
|
|
|
{
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
'Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface',
|
|
|
|
$this->validator->getMetadataFactory()
|
|
|
|
);
|
|
|
|
}
|
2011-06-08 18:56:59 +01:00
|
|
|
}
|