From 78c145196bf1a32f3c7c3d6e9027e844fbbddd35 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Sun, 5 Feb 2012 10:03:22 +0100 Subject: [PATCH] [Validator] Throwing exception in ConstraintValidator::setMessage() if initialize() wasn't called --- .../Validator/ConstraintValidator.php | 6 ++ .../Validator/ConstraintValidatorTest.php | 60 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 tests/Symfony/Tests/Component/Validator/ConstraintValidatorTest.php diff --git a/src/Symfony/Component/Validator/ConstraintValidator.php b/src/Symfony/Component/Validator/ConstraintValidator.php index e7c045dfa8..5a90846527 100644 --- a/src/Symfony/Component/Validator/ConstraintValidator.php +++ b/src/Symfony/Component/Validator/ConstraintValidator.php @@ -18,6 +18,8 @@ namespace Symfony\Component\Validator; * * @api */ +use Symfony\Component\Validator\Exception\ValidatorException; + abstract class ConstraintValidator implements ConstraintValidatorInterface { /** @@ -79,6 +81,10 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface $this->messageTemplate = $template; $this->messageParameters = $parameters; + if (!$this->context instanceof ExecutionContext) { + throw new ValidatorException('ConstraintValidator::initialize() must be called before setting violation messages'); + } + $this->context->addViolation($template, $parameters); } } diff --git a/tests/Symfony/Tests/Component/Validator/ConstraintValidatorTest.php b/tests/Symfony/Tests/Component/Validator/ConstraintValidatorTest.php new file mode 100644 index 0000000000..df5f6f9a35 --- /dev/null +++ b/tests/Symfony/Tests/Component/Validator/ConstraintValidatorTest.php @@ -0,0 +1,60 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Component\Validator; + +use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\ConstraintValidator; + +class ConstraintValidatorTest_Validator extends ConstraintValidator +{ + private $message; + private $params; + + public function __construct($message, array $params = array()) + { + $this->message = $message; + $this->params = $params; + } + + public function isValid($value, Constraint $constraint) + { + $this->setMessage($this->message, $this->params); + } +} + +class ConstraintValidatorTest extends \PHPUnit_Framework_TestCase +{ + public function testSetMessage() + { + $context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false); + $constraint = $this->getMock('Symfony\Component\Validator\Constraint', array(), array(), '', false); + $validator = new ConstraintValidatorTest_Validator('error message', array('foo' => 'bar')); + $validator->initialize($context); + + $context->expects($this->once()) + ->method('addViolation') + ->with('error message', array('foo' => 'bar')); + + $validator->isValid('bam', $constraint); + } + + /** + * @expectedException Symfony\Component\Validator\Exception\ValidatorException + */ + public function testSetMessageFailsIfNoContextSet() + { + $constraint = $this->getMock('Symfony\Component\Validator\Constraint', array(), array(), '', false); + $validator = new ConstraintValidatorTest_Validator('error message', array('foo' => 'bar')); + + $validator->isValid('bam', $constraint); + } +}