Skip to content

Commit

Permalink
[FrameworkBundle] updated constraint validator factory to work with n…
Browse files Browse the repository at this point in the history
…on-DIC validators
  • Loading branch information
kriswallsmith authored and fabpot committed Oct 16, 2010
1 parent e1f8423 commit 7639fde
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 21 deletions.
Expand Up @@ -13,10 +13,33 @@

use Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Validator\Constraints\Blank as BlankConstraint;

class ConstraintValidatorFactoryTest extends \PHPUnit_Framework_TestCase
{
public function testLoadTaggedServiceIdsSetsValidators()
public function testGetInstanceCreatesValidator()
{
$class = get_class($this->getMockForAbstractClass('Symfony\\Component\\Validator\\ConstraintValidator'));

$constraint = $this->getMock('Symfony\\Component\\Validator\\Constraint');
$constraint
->expects($this->once())
->method('validatedBy')
->will($this->returnValue($class));

$factory = new ConstraintValidatorFactory(new Container());
$this->assertInstanceOf($class, $factory->getInstance($constraint));
}

public function testGetInstanceReturnsExistingValidator()
{
$factory = new ConstraintValidatorFactory(new Container());
$v1 = $factory->getInstance(new BlankConstraint());
$v2 = $factory->getInstance(new BlankConstraint());
$this->assertSame($v1, $v2);
}

public function testGetInstanceReturnsService()
{
$service = 'validator_constraint_service';
$alias = 'validator_constraint_alias';
Expand Down Expand Up @@ -47,18 +70,4 @@ public function testLoadTaggedServiceIdsSetsValidators()
$factory->loadTaggedServiceIds($container);
$this->assertSame($validator, $factory->getInstance($constraint));
}

public function testGetInstanceException()
{
$this->setExpectedException('InvalidArgumentException');

$constraint = $this->getMock('Symfony\\Component\\Validator\\Constraint');
$constraint
->expects($this->once())
->method('validatedBy')
->will($this->returnValue('foo'));

$factory = new ConstraintValidatorFactory(new Container());
$factory->getInstance($constraint);
}
}
Expand Up @@ -19,6 +19,21 @@
/**
* Uses a service container to create constraint validators.
*
* A constraint validator should be tagged as "validator.constraint_validator"
* in the service container and include an "alias" attribute:
*
* <service id="some_doctrine_validator">
* <argument type="service" id="doctrine.orm.some_entity_manager" />
* <tag name="validator.constraint_validator" alias="some_alias" />
* </service>
*
* A constraint may then return this alias in its validatedBy() method:
*
* public function validatedBy()
* {
* return 'some_alias';
* }
*
* @author Kris Wallsmith <kris.wallsmith@symfony-project.com>
*/
class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
Expand Down Expand Up @@ -56,18 +71,14 @@ public function loadTaggedServiceIds(TaggedContainerInterface $container)
* @param Constraint $constraint A constraint
*
* @return Symfony\Component\Validator\ConstraintValidator A validator for the supplied constraint
*
* @throws InvalidArgumentException If no validator for the supplied constraint is found
*/
public function getInstance(Constraint $constraint)
{
$name = $constraint->validatedBy();

if (!isset($this->validators[$name])) {
throw new \InvalidArgumentException(sprintf('There is no "%s" constraint validator.', $name));
}

if (is_string($this->validators[$name])) {
$this->validators[$name] = new $name();
} elseif (is_string($this->validators[$name])) {
$this->validators[$name] = $this->container->get($this->validators[$name]);
}

Expand Down

0 comments on commit 7639fde

Please sign in to comment.