bug #9718 [validator] throw an exception if isn't an instance of ConstraintValidatorInterface. (aitboudad)

This PR was squashed before being merged into the 2.3 branch (closes #9718).

Discussion
----------

[validator] throw an exception if isn't an instance of ConstraintValidatorInterface.

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #9264
| License       | MIT

Commits
-------

fc232a5 [validator] throw an exception if isn't an instance of ConstraintValidatorInterface.
This commit is contained in:
Fabien Potencier 2013-12-07 09:07:05 +01:00
commit eb7df1a2d7
2 changed files with 10 additions and 3 deletions

View File

@ -43,7 +43,7 @@ class ConstraintValidatorFactoryTest extends \PHPUnit_Framework_TestCase
{
$service = 'validator_constraint_service';
$alias = 'validator_constraint_alias';
$validator = new \stdClass();
$validator = $this->getMockForAbstractClass('Symfony\\Component\\Validator\\ConstraintValidator');
// mock ContainerBuilder b/c it implements TaggedContainerInterface
$container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerBuilder');

View File

@ -14,7 +14,8 @@ namespace Symfony\Bundle\FrameworkBundle\Validator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* Uses a service container to create constraint validators.
@ -58,7 +59,9 @@ class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
*
* @param Constraint $constraint A constraint
*
* @return ConstraintValidator A validator for the supplied constraint
* @return ConstraintValidatorInterface A validator for the supplied constraint
*
* @throws UnexpectedTypeException When the validator is not an instance of ConstraintValidatorInterface
*/
public function getInstance(Constraint $constraint)
{
@ -70,6 +73,10 @@ class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
$this->validators[$name] = $this->container->get($this->validators[$name]);
}
if (!$this->validators[$name] instanceof ConstraintValidatorInterface) {
throw new UnexpectedTypeException($this->validators[$name], 'Symfony\Component\Validator\ConstraintValidatorInterface');
}
return $this->validators[$name];
}
}