10000 [validator] throw an exception if isn't an instance of ConstraintVali… · symfony/symfony@fc232a5 · GitHub
[go: up one dir, main page]

Skip to content

Commit fc232a5

Browse files
aitboudadfabpot
authored andcommitted
[validator] throw an exception if isn't an instance of ConstraintValidatorInterface.
1 parent 06423a4 commit fc232a5

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/Validator/ConstraintValidatorFactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function testGetInstanceReturnsService()
4343
{
4444
$service = 'validator_constraint_service';
4545
$alias = 'validator_constraint_alias';
46-
$validator = new \stdClass();
46+
$validator = $this->getMockForAbstractClass('Symfony\\Component\\Validator\\ConstraintValidator');
4747

4848
// mock ContainerBuilder b/c it implements TaggedContainerInterface
4949
$container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerBuilder');

src/Symfony/Bundle/FrameworkBundle/Validator/ConstraintValidatorFactory.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
use Symfony\Component\DependencyInjection\ContainerInterface;
1515
use Symfony\Component\Validator\Constraint;
1616
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
17-
use Symfony\Component\Validator\ConstraintValidator;
17+
use Symfony\Component\Validator\ConstraintValidatorInterface;
18+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1819

1920
/**
2021
* Uses a service container to create constraint validators.
@@ -58,7 +59,9 @@ public function __construct(ContainerInterface $container, array $validators = a
5859
*
5960
* @param Constraint $constraint A constraint
6061
*
61-
* @return ConstraintValidator A validator for the supplied constraint
62+
* @return ConstraintValidatorInterface A validator for the supplied constraint
63+
*
64+
* @throws UnexpectedTypeException When the validator is not an instance of ConstraintValidatorInterface
6265
*/
6366
public function getInstance(Constraint $constraint)
6467
{
@@ -70,6 +73,10 @@ public function getInstance(Constraint $constraint)
7073
$this->validators[$name] = $this->container->get($this->validators[$name]);
7174
}
7275

76+
if (!$this->validators[$name] instanceof ConstraintValidatorInterface) {
77+
throw new UnexpectedTypeException($this->validators[$name], 'Symfony\Component\Validator\ConstraintValidatorInterface');
78+
}
79+
7380
return $this->validators[$name];
7481
}
7582
}

0 commit comments

Comments
 (0)
0