4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Tests\Constraints;
14 use Symfony\Component\Validator\Constraint;
15 use Symfony\Component\Validator\Constraints\Callback;
16 use Symfony\Component\Validator\Constraints\CallbackValidator;
17 use Symfony\Component\Validator\Context\ExecutionContextInterface;
18 use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
20 class CallbackValidatorTest_Class
22 public static function validateCallback($object, ExecutionContextInterface $context)
24 $context->addViolation('Callback message', array('{{ value }}' => 'foobar'));
30 class CallbackValidatorTest_Object
32 public function validate(ExecutionContextInterface $context)
34 $context->addViolation('My message', array('{{ value }}' => 'foobar'));
39 public static function validateStatic($object, ExecutionContextInterface $context)
41 $context->addViolation('Static message', array('{{ value }}' => 'baz'));
47 class CallbackValidatorTest extends ConstraintValidatorTestCase
49 protected function createValidator()
51 return new CallbackValidator();
54 public function testNullIsValid()
56 $this->validator->validate(null, new Callback());
58 $this->assertNoViolation();
61 public function testSingleMethod()
63 $object = new CallbackValidatorTest_Object();
64 $constraint = new Callback('validate');
66 $this->validator->validate($object, $constraint);
68 $this->buildViolation('My message')
69 ->setParameter('{{ value }}', 'foobar')
73 public function testSingleMethodExplicitName()
75 $object = new CallbackValidatorTest_Object();
76 $constraint = new Callback(array('callback' => 'validate'));
78 $this->validator->validate($object, $constraint);
80 $this->buildViolation('My message')
81 ->setParameter('{{ value }}', 'foobar')
85 public function testSingleStaticMethod()
87 $object = new CallbackValidatorTest_Object();
88 $constraint = new Callback('validateStatic');
90 $this->validator->validate($object, $constraint);
92 $this->buildViolation('Static message')
93 ->setParameter('{{ value }}', 'baz')
97 public function testClosure()
99 $object = new CallbackValidatorTest_Object();
100 $constraint = new Callback(function ($object, ExecutionContextInterface $context) {
101 $context->addViolation('My message', array('{{ value }}' => 'foobar'));
106 $this->validator->validate($object, $constraint);
108 $this->buildViolation('My message')
109 ->setParameter('{{ value }}', 'foobar')
113 public function testClosureNullObject()
115 $constraint = new Callback(function ($object, ExecutionContextInterface $context) {
116 $context->addViolation('My message', array('{{ value }}' => 'foobar'));
121 $this->validator->validate(null, $constraint);
123 $this->buildViolation('My message')
124 ->setParameter('{{ value }}', 'foobar')
128 public function testClosureExplicitName()
130 $object = new CallbackValidatorTest_Object();
131 $constraint = new Callback(array(
132 'callback' => function ($object, ExecutionContextInterface $context) {
133 $context->addViolation('My message', array('{{ value }}' => 'foobar'));
139 $this->validator->validate($object, $constraint);
141 $this->buildViolation('My message')
142 ->setParameter('{{ value }}', 'foobar')
146 public function testArrayCallable()
148 $object = new CallbackValidatorTest_Object();
149 $constraint = new Callback(array(__CLASS__.'_Class', 'validateCallback'));
151 $this->validator->validate($object, $constraint);
153 $this->buildViolation('Callback message')
154 ->setParameter('{{ value }}', 'foobar')
158 public function testArrayCallableNullObject()
160 $constraint = new Callback(array(__CLASS__.'_Class', 'validateCallback'));
162 $this->validator->validate(null, $constraint);
164 $this->buildViolation('Callback message')
165 ->setParameter('{{ value }}', 'foobar')
169 public function testArrayCallableExplicitName()
171 $object = new CallbackValidatorTest_Object();
172 $constraint = new Callback(array(
173 'callback' => array(__CLASS__.'_Class', 'validateCallback'),
176 $this->validator->validate($object, $constraint);
178 $this->buildViolation('Callback message')
179 ->setParameter('{{ value }}', 'foobar')
184 * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
186 public function testExpectValidMethods()
188 $object = new CallbackValidatorTest_Object();
190 $this->validator->validate($object, new Callback(array('callback' => array('foobar'))));
194 * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
196 public function testExpectValidCallbacks()
198 $object = new CallbackValidatorTest_Object();
200 $this->validator->validate($object, new Callback(array('callback' => array('foo', 'bar'))));
203 public function testConstraintGetTargets()
205 $constraint = new Callback(array());
206 $targets = array(Constraint::CLASS_CONSTRAINT, Constraint::PROPERTY_CONSTRAINT);
208 $this->assertEquals($targets, $constraint->getTargets());
211 // Should succeed. Needed when defining constraints as annotations.
212 public function testNoConstructorArguments()
214 $constraint = new Callback();
216 $this->assertSame(array(Constraint::CLASS_CONSTRAINT, Constraint::PROPERTY_CONSTRAINT), $constraint->getTargets());
219 public function testAnnotationInvocationSingleValued()
221 $constraint = new Callback(array('value' => 'validateStatic'));
223 $this->assertEquals(new Callback('validateStatic'), $constraint);
226 public function testAnnotationInvocationMultiValued()
228 $constraint = new Callback(array('value' => array(__CLASS__.'_Class', 'validateCallback')));
230 $this->assertEquals(new Callback(array(__CLASS__.'_Class', 'validateCallback')), $constraint);
233 public function testPayloadIsPassedToCallback()
235 $object = new \stdClass();
238 $constraint = new Callback(array(
239 'callback' => function ($object, ExecutionContextInterface $constraint, $payload) use (&$payloadCopy) {
240 $payloadCopy = $payload;
242 'payload' => 'Hello world!',
244 $this->validator->validate($object, $constraint);
245 $this->assertEquals('Hello world!', $payloadCopy);
248 $constraint = new Callback(array(
249 'callback' => function ($object, ExecutionContextInterface $constraint, $payload) use (&$payloadCopy) {
250 $payloadCopy = $payload;
253 $this->validator->validate($object, $constraint);
254 $this->assertNull($payloadCopy);