2011-02-03 09:40:53 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-02-03 09:40:53 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2012-03-28 14:43:52 +01:00
|
|
|
namespace Symfony\Component\Validator\Tests\Constraints;
|
2011-02-03 09:40:53 +00:00
|
|
|
|
2014-08-08 09:10:43 +01:00
|
|
|
use Symfony\Component\Validator\Constraint;
|
2011-03-25 00:21:46 +00:00
|
|
|
use Symfony\Component\Validator\Constraints\Callback;
|
|
|
|
use Symfony\Component\Validator\Constraints\CallbackValidator;
|
2014-08-08 09:10:43 +01:00
|
|
|
use Symfony\Component\Validator\ExecutionContextInterface;
|
2011-02-03 09:40:53 +00:00
|
|
|
|
2011-03-25 00:34:57 +00:00
|
|
|
class CallbackValidatorTest_Class
|
|
|
|
{
|
2014-08-08 09:10:43 +01:00
|
|
|
public static function validateCallback($object, ExecutionContextInterface $context)
|
2011-03-25 00:34:57 +00:00
|
|
|
{
|
2014-08-08 09:10:43 +01:00
|
|
|
$context->addViolation('Callback message', array('{{ value }}' => 'foobar'));
|
2012-02-01 12:53:45 +00:00
|
|
|
|
|
|
|
return false;
|
2011-03-25 00:34:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-25 00:21:46 +00:00
|
|
|
class CallbackValidatorTest_Object
|
2011-02-03 09:40:53 +00:00
|
|
|
{
|
2014-08-08 09:10:43 +01:00
|
|
|
public function validate(ExecutionContextInterface $context)
|
2011-02-03 09:40:53 +00:00
|
|
|
{
|
2014-08-08 09:10:43 +01:00
|
|
|
$context->addViolation('My message', array('{{ value }}' => 'foobar'));
|
2012-02-01 12:53:45 +00:00
|
|
|
|
|
|
|
return false;
|
2011-02-03 09:40:53 +00:00
|
|
|
}
|
|
|
|
|
2014-08-08 09:10:43 +01:00
|
|
|
public static function validateStatic(ExecutionContextInterface $context)
|
2011-02-03 09:40:53 +00:00
|
|
|
{
|
2014-08-08 09:10:43 +01:00
|
|
|
$context->addViolation('Static message', array('{{ value }}' => 'baz'));
|
2012-02-01 12:53:45 +00:00
|
|
|
|
|
|
|
return false;
|
2011-02-03 09:40:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-08 09:10:43 +01:00
|
|
|
class CallbackValidatorTest extends AbstractConstraintValidatorTest
|
2011-02-03 09:40:53 +00:00
|
|
|
{
|
2014-08-08 09:10:43 +01:00
|
|
|
protected function createValidator()
|
2011-02-03 09:40:53 +00:00
|
|
|
{
|
2014-08-08 09:10:43 +01:00
|
|
|
return new CallbackValidator();
|
2011-02-03 09:40:53 +00:00
|
|
|
}
|
|
|
|
|
2014-08-08 09:10:43 +01:00
|
|
|
public function testNullIsValid()
|
2011-06-14 09:40:42 +01:00
|
|
|
{
|
2014-08-08 09:10:43 +01:00
|
|
|
$this->validator->validate(null, new Callback(array('foo')));
|
|
|
|
|
|
|
|
$this->assertNoViolation();
|
2011-06-14 09:40:42 +01:00
|
|
|
}
|
|
|
|
|
2014-08-08 09:10:43 +01:00
|
|
|
public function testSingleMethod()
|
2011-02-03 09:40:53 +00:00
|
|
|
{
|
2014-08-08 09:10:43 +01:00
|
|
|
$object = new CallbackValidatorTest_Object();
|
|
|
|
$constraint = new Callback(array('validate'));
|
2012-02-01 12:53:45 +00:00
|
|
|
|
2014-08-08 09:10:43 +01:00
|
|
|
$this->validator->validate($object, $constraint);
|
|
|
|
|
|
|
|
$this->assertViolation('My message', array(
|
|
|
|
'{{ value }}' => 'foobar',
|
|
|
|
));
|
2011-02-03 09:40:53 +00:00
|
|
|
}
|
|
|
|
|
2014-08-08 09:10:43 +01:00
|
|
|
public function testSingleMethodExplicitName()
|
2011-02-03 09:40:53 +00:00
|
|
|
{
|
2011-03-25 00:21:46 +00:00
|
|
|
$object = new CallbackValidatorTest_Object();
|
2014-08-08 09:10:43 +01:00
|
|
|
$constraint = new Callback(array('methods' => array('validate')));
|
2011-02-03 09:40:53 +00:00
|
|
|
|
2014-08-08 09:10:43 +01:00
|
|
|
$this->validator->validate($object, $constraint);
|
|
|
|
|
|
|
|
$this->assertViolation('My message', array(
|
|
|
|
'{{ value }}' => 'foobar',
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMultipleMethods()
|
|
|
|
{
|
|
|
|
$object = new CallbackValidatorTest_Object();
|
|
|
|
$constraint = new Callback(array('validate', 'validateStatic'));
|
2011-02-03 09:40:53 +00:00
|
|
|
|
2012-02-09 17:02:12 +00:00
|
|
|
$this->validator->validate($object, $constraint);
|
2014-08-08 09:10:43 +01:00
|
|
|
|
|
|
|
$this->assertViolations(array(
|
|
|
|
$this->createViolation('My message', array(
|
|
|
|
'{{ value }}' => 'foobar',
|
|
|
|
)),
|
|
|
|
$this->createViolation('Static message', array(
|
|
|
|
'{{ value }}' => 'baz',
|
|
|
|
)),
|
|
|
|
));
|
2011-02-03 09:40:53 +00:00
|
|
|
}
|
|
|
|
|
2014-08-08 09:10:43 +01:00
|
|
|
public function testMultipleMethodsExplicitName()
|
2011-03-25 00:34:57 +00:00
|
|
|
{
|
|
|
|
$object = new CallbackValidatorTest_Object();
|
2014-08-08 09:10:43 +01:00
|
|
|
$constraint = new Callback(array(
|
|
|
|
'methods' => array('validate', 'validateStatic'),
|
|
|
|
));
|
2011-03-25 00:34:57 +00:00
|
|
|
|
2014-08-08 09:10:43 +01:00
|
|
|
$this->validator->validate($object, $constraint);
|
2011-03-25 00:34:57 +00:00
|
|
|
|
2014-08-08 09:10:43 +01:00
|
|
|
$this->assertViolations(array(
|
|
|
|
$this->createViolation('My message', array(
|
|
|
|
'{{ value }}' => 'foobar',
|
|
|
|
)),
|
|
|
|
$this->createViolation('Static message', array(
|
|
|
|
'{{ value }}' => 'baz',
|
|
|
|
)),
|
|
|
|
));
|
2011-03-25 00:34:57 +00:00
|
|
|
}
|
|
|
|
|
2014-08-08 09:10:43 +01:00
|
|
|
public function testSingleStaticMethod()
|
2011-02-03 09:40:53 +00:00
|
|
|
{
|
2011-03-25 00:21:46 +00:00
|
|
|
$object = new CallbackValidatorTest_Object();
|
2014-08-08 09:10:43 +01:00
|
|
|
$constraint = new Callback(array(
|
|
|
|
array(__CLASS__.'_Class', 'validateCallback')
|
|
|
|
));
|
2011-02-03 09:40:53 +00:00
|
|
|
|
2014-08-08 09:10:43 +01:00
|
|
|
$this->validator->validate($object, $constraint);
|
2012-02-01 12:53:45 +00:00
|
|
|
|
2014-08-08 09:10:43 +01:00
|
|
|
$this->assertViolation('Callback message', array(
|
|
|
|
'{{ value }}' => 'foobar',
|
|
|
|
));
|
2011-02-03 09:40:53 +00:00
|
|
|
}
|
2011-03-25 00:34:57 +00:00
|
|
|
|
2014-08-08 09:10:43 +01:00
|
|
|
public function testSingleStaticMethodExplicitName()
|
2011-03-25 00:34:57 +00:00
|
|
|
{
|
|
|
|
$object = new CallbackValidatorTest_Object();
|
2014-08-08 09:10:43 +01:00
|
|
|
$constraint = new Callback(array(
|
|
|
|
'methods' => array(array(__CLASS__.'_Class', 'validateCallback')),
|
|
|
|
));
|
2011-03-25 00:34:57 +00:00
|
|
|
|
2014-08-08 09:10:43 +01:00
|
|
|
$this->validator->validate($object, $constraint);
|
|
|
|
|
|
|
|
$this->assertViolation('Callback message', array(
|
|
|
|
'{{ value }}' => 'foobar',
|
|
|
|
));
|
2011-03-25 00:34:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-01-05 17:52:40 +00:00
|
|
|
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
|
2011-03-25 00:34:57 +00:00
|
|
|
*/
|
|
|
|
public function testExpectValidMethods()
|
|
|
|
{
|
|
|
|
$object = new CallbackValidatorTest_Object();
|
|
|
|
|
2012-02-09 17:02:12 +00:00
|
|
|
$this->validator->validate($object, new Callback(array('foobar')));
|
2011-03-25 00:34:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-01-05 17:52:40 +00:00
|
|
|
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
|
2011-03-25 00:34:57 +00:00
|
|
|
*/
|
|
|
|
public function testExpectValidCallbacks()
|
|
|
|
{
|
|
|
|
$object = new CallbackValidatorTest_Object();
|
|
|
|
|
2012-02-09 17:02:12 +00:00
|
|
|
$this->validator->validate($object, new Callback(array(array('foo', 'bar'))));
|
2011-03-25 00:34:57 +00:00
|
|
|
}
|
2011-06-14 09:40:42 +01:00
|
|
|
|
|
|
|
public function testConstraintGetTargets()
|
|
|
|
{
|
|
|
|
$constraint = new Callback(array('foo'));
|
|
|
|
|
2014-08-08 09:10:43 +01:00
|
|
|
$this->assertEquals(Constraint::CLASS_CONSTRAINT, $constraint->getTargets());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Symfony\Component\Validator\Exception\MissingOptionsException
|
|
|
|
*/
|
|
|
|
public function testNoConstructorArguments()
|
|
|
|
{
|
|
|
|
new Callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAnnotationInvocationSingleValued()
|
|
|
|
{
|
|
|
|
$constraint = new Callback(array('value' => 'validateStatic'));
|
|
|
|
|
|
|
|
$this->assertEquals(new Callback('validateStatic'), $constraint);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAnnotationInvocationMultiValued()
|
|
|
|
{
|
|
|
|
$constraint = new Callback(array('value' => array(__CLASS__.'_Class', 'validateCallback')));
|
|
|
|
|
|
|
|
$this->assertEquals(new Callback(array(__CLASS__.'_Class', 'validateCallback')), $constraint);
|
2011-06-14 09:40:42 +01:00
|
|
|
}
|
2011-06-08 18:56:59 +01:00
|
|
|
}
|