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-02-12 11:38:50 +00:00
|
|
|
use Symfony\Component\Validator\Constraint;
|
2011-02-03 09:40:53 +00:00
|
|
|
use Symfony\Component\Validator\ExecutionContext;
|
2011-03-25 00:21:46 +00:00
|
|
|
use Symfony\Component\Validator\Constraints\Callback;
|
|
|
|
use Symfony\Component\Validator\Constraints\CallbackValidator;
|
2011-02-03 09:40:53 +00:00
|
|
|
|
2011-03-25 00:34:57 +00:00
|
|
|
class CallbackValidatorTest_Class
|
|
|
|
{
|
2013-09-26 10:03:13 +01:00
|
|
|
public static function validateCallback($object, ExecutionContext $context)
|
2011-03-25 00:34:57 +00:00
|
|
|
{
|
2013-09-26 10:03:13 +01:00
|
|
|
$context->addViolation('Callback message', array('{{ value }}' => 'foobar'), 'invalidValue');
|
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
|
|
|
{
|
2013-09-26 10:03:13 +01:00
|
|
|
public function validate(ExecutionContext $context)
|
2011-02-03 09:40:53 +00:00
|
|
|
{
|
2012-02-01 12:53:45 +00:00
|
|
|
$context->addViolation('My message', array('{{ value }}' => 'foobar'), 'invalidValue');
|
|
|
|
|
|
|
|
return false;
|
2011-02-03 09:40:53 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 10:03:13 +01:00
|
|
|
public static function validateStatic($object, ExecutionContext $context)
|
2011-02-03 09:40:53 +00:00
|
|
|
{
|
2013-09-26 10:03:13 +01:00
|
|
|
$context->addViolation('Static message', array('{{ value }}' => 'baz'), 'otherInvalidValue');
|
2012-02-01 12:53:45 +00:00
|
|
|
|
|
|
|
return false;
|
2011-02-03 09:40:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-25 00:21:46 +00:00
|
|
|
class CallbackValidatorTest extends \PHPUnit_Framework_TestCase
|
2011-02-03 09:40:53 +00:00
|
|
|
{
|
|
|
|
protected $context;
|
2012-02-01 12:53:45 +00:00
|
|
|
protected $validator;
|
2011-02-03 09:40:53 +00:00
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false);
|
2011-03-25 00:21:46 +00:00
|
|
|
$this->validator = new CallbackValidator();
|
2011-02-03 09:40:53 +00:00
|
|
|
$this->validator->initialize($this->context);
|
|
|
|
}
|
|
|
|
|
2011-06-14 09:40:42 +01:00
|
|
|
protected function tearDown()
|
|
|
|
{
|
|
|
|
$this->context = null;
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->validator = null;
|
2011-06-14 09:40:42 +01:00
|
|
|
}
|
|
|
|
|
2011-02-03 09:40:53 +00:00
|
|
|
public function testNullIsValid()
|
|
|
|
{
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->never())
|
|
|
|
->method('addViolation');
|
|
|
|
|
2012-02-09 17:02:12 +00:00
|
|
|
$this->validator->validate(null, new Callback(array('foo')));
|
2011-02-03 09:40:53 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 10:03:13 +01:00
|
|
|
public function testSingleMethod()
|
2011-02-03 09:40:53 +00:00
|
|
|
{
|
2011-03-25 00:21:46 +00:00
|
|
|
$object = new CallbackValidatorTest_Object();
|
2013-09-26 10:03:13 +01:00
|
|
|
$constraint = new Callback('validate');
|
2011-02-03 09:40:53 +00:00
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->once())
|
|
|
|
->method('addViolation')
|
|
|
|
->with('My message', array(
|
|
|
|
'{{ value }}' => 'foobar',
|
|
|
|
));
|
2011-02-03 09:40:53 +00:00
|
|
|
|
2012-02-09 17:02:12 +00:00
|
|
|
$this->validator->validate($object, $constraint);
|
2011-02-03 09:40:53 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 10:03:13 +01:00
|
|
|
public function testSingleMethodExplicitName()
|
2011-03-25 00:34:57 +00:00
|
|
|
{
|
|
|
|
$object = new CallbackValidatorTest_Object();
|
2013-09-26 10:03:13 +01:00
|
|
|
$constraint = new Callback(array('callback' => 'validate'));
|
|
|
|
|
|
|
|
$this->context->expects($this->once())
|
|
|
|
->method('addViolation')
|
|
|
|
->with('My message', array(
|
|
|
|
'{{ value }}' => 'foobar',
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->validator->validate($object, $constraint);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSingleStaticMethod()
|
|
|
|
{
|
|
|
|
$object = new CallbackValidatorTest_Object();
|
|
|
|
$constraint = new Callback('validateStatic');
|
2011-03-25 00:34:57 +00:00
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->once())
|
|
|
|
->method('addViolation')
|
|
|
|
->with('Static message', array(
|
2013-09-26 10:03:13 +01:00
|
|
|
'{{ value }}' => 'baz',
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->validator->validate($object, $constraint);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testClosure()
|
|
|
|
{
|
|
|
|
$object = new CallbackValidatorTest_Object();
|
|
|
|
$constraint = new Callback(function ($object, ExecutionContext $context) {
|
|
|
|
$context->addViolation('My message', array('{{ value }}' => 'foobar'), 'invalidValue');
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->context->expects($this->once())
|
|
|
|
->method('addViolation')
|
|
|
|
->with('My message', array(
|
2012-02-01 12:53:45 +00:00
|
|
|
'{{ value }}' => 'foobar',
|
|
|
|
));
|
2011-03-25 00:34:57 +00:00
|
|
|
|
2013-09-26 10:03:13 +01:00
|
|
|
$this->validator->validate($object, $constraint);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testClosureExplicitName()
|
|
|
|
{
|
|
|
|
$object = new CallbackValidatorTest_Object();
|
|
|
|
$constraint = new Callback(array(
|
|
|
|
'callback' => function ($object, ExecutionContext $context) {
|
|
|
|
$context->addViolation('My message', array('{{ value }}' => 'foobar'), 'invalidValue');
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->context->expects($this->once())
|
|
|
|
->method('addViolation')
|
|
|
|
->with('My message', array(
|
|
|
|
'{{ value }}' => 'foobar',
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->validator->validate($object, $constraint);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testArrayCallable()
|
|
|
|
{
|
|
|
|
$object = new CallbackValidatorTest_Object();
|
|
|
|
$constraint = new Callback(array(__CLASS__.'_Class', 'validateCallback'));
|
|
|
|
|
|
|
|
$this->context->expects($this->once())
|
|
|
|
->method('addViolation')
|
|
|
|
->with('Callback message', array(
|
|
|
|
'{{ value }}' => 'foobar',
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->validator->validate($object, $constraint);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testArrayCallableExplicitName()
|
|
|
|
{
|
|
|
|
$object = new CallbackValidatorTest_Object();
|
|
|
|
$constraint = new Callback(array(
|
|
|
|
'callback' => array(__CLASS__.'_Class', 'validateCallback'),
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->context->expects($this->once())
|
|
|
|
->method('addViolation')
|
|
|
|
->with('Callback message', array(
|
|
|
|
'{{ value }}' => 'foobar',
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->validator->validate($object, $constraint);
|
|
|
|
}
|
|
|
|
|
|
|
|
// BC with Symfony < 2.4
|
|
|
|
public function testSingleMethodBc()
|
|
|
|
{
|
|
|
|
$object = new CallbackValidatorTest_Object();
|
|
|
|
$constraint = new Callback(array('validate'));
|
|
|
|
|
|
|
|
$this->context->expects($this->once())
|
|
|
|
->method('addViolation')
|
|
|
|
->with('My message', array(
|
|
|
|
'{{ value }}' => 'foobar',
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->validator->validate($object, $constraint);
|
2011-03-25 00:34:57 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 10:03:13 +01:00
|
|
|
// BC with Symfony < 2.4
|
|
|
|
public function testSingleMethodBcExplicitName()
|
2011-02-03 09:40:53 +00:00
|
|
|
{
|
2011-03-25 00:21:46 +00:00
|
|
|
$object = new CallbackValidatorTest_Object();
|
2013-09-26 10:03:13 +01:00
|
|
|
$constraint = new Callback(array('methods' => array('validate')));
|
|
|
|
|
|
|
|
$this->context->expects($this->once())
|
|
|
|
->method('addViolation')
|
|
|
|
->with('My message', array(
|
|
|
|
'{{ value }}' => 'foobar',
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->validator->validate($object, $constraint);
|
|
|
|
}
|
|
|
|
|
|
|
|
// BC with Symfony < 2.4
|
|
|
|
public function testMultipleMethodsBc()
|
|
|
|
{
|
|
|
|
$object = new CallbackValidatorTest_Object();
|
|
|
|
$constraint = new Callback(array('validate', 'validateStatic'));
|
2011-02-03 09:40:53 +00:00
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->at(0))
|
|
|
|
->method('addViolation')
|
|
|
|
->with('My message', array(
|
|
|
|
'{{ value }}' => 'foobar',
|
|
|
|
));
|
|
|
|
$this->context->expects($this->at(1))
|
|
|
|
->method('addViolation')
|
2013-09-26 10:03:13 +01:00
|
|
|
->with('Static message', array(
|
2012-02-01 12:53:45 +00:00
|
|
|
'{{ value }}' => 'baz',
|
|
|
|
));
|
|
|
|
|
2013-09-26 10:03:13 +01:00
|
|
|
$this->validator->validate($object, $constraint);
|
2011-02-03 09:40:53 +00:00
|
|
|
}
|
2011-03-25 00:34:57 +00:00
|
|
|
|
2013-09-26 10:03:13 +01:00
|
|
|
// BC with Symfony < 2.4
|
|
|
|
public function testMultipleMethodsBcExplicitName()
|
2011-03-25 00:34:57 +00:00
|
|
|
{
|
|
|
|
$object = new CallbackValidatorTest_Object();
|
2013-09-26 10:03:13 +01:00
|
|
|
$constraint = new Callback(array(
|
|
|
|
'methods' => array('validate', 'validateStatic'),
|
|
|
|
));
|
2011-03-25 00:34:57 +00:00
|
|
|
|
2013-09-26 10:03:13 +01:00
|
|
|
$this->context->expects($this->at(0))
|
|
|
|
->method('addViolation')
|
|
|
|
->with('My message', array(
|
|
|
|
'{{ value }}' => 'foobar',
|
|
|
|
));
|
|
|
|
$this->context->expects($this->at(1))
|
|
|
|
->method('addViolation')
|
|
|
|
->with('Static message', array(
|
|
|
|
'{{ value }}' => 'baz',
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->validator->validate($object, $constraint);
|
|
|
|
}
|
|
|
|
|
|
|
|
// BC with Symfony < 2.4
|
|
|
|
public function testSingleStaticMethodBc()
|
|
|
|
{
|
|
|
|
$object = new CallbackValidatorTest_Object();
|
|
|
|
$constraint = new Callback(array(
|
|
|
|
array(__CLASS__.'_Class', 'validateCallback')
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->context->expects($this->once())
|
|
|
|
->method('addViolation')
|
|
|
|
->with('Callback message', array(
|
|
|
|
'{{ value }}' => 'foobar',
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->validator->validate($object, $constraint);
|
|
|
|
}
|
|
|
|
|
|
|
|
// BC with Symfony < 2.4
|
|
|
|
public function testSingleStaticMethodBcExplicitName()
|
|
|
|
{
|
|
|
|
$object = new CallbackValidatorTest_Object();
|
|
|
|
$constraint = new Callback(array(
|
|
|
|
'methods' => array(array(__CLASS__.'_Class', 'validateCallback')),
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->context->expects($this->once())
|
|
|
|
->method('addViolation')
|
|
|
|
->with('Callback message', array(
|
|
|
|
'{{ value }}' => 'foobar',
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->validator->validate($object, $constraint);
|
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
|
|
|
|
2013-09-26 10:03:13 +01:00
|
|
|
/**
|
|
|
|
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
|
|
|
|
*/
|
|
|
|
public function testExpectEitherCallbackOrMethods()
|
|
|
|
{
|
|
|
|
$object = new CallbackValidatorTest_Object();
|
|
|
|
|
|
|
|
$this->validator->validate($object, new Callback(array(
|
|
|
|
'callback' => 'validate',
|
|
|
|
'methods' => array('validateStatic'),
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
2011-06-14 09:40:42 +01:00
|
|
|
public function testConstraintGetTargets()
|
|
|
|
{
|
|
|
|
$constraint = new Callback(array('foo'));
|
2014-02-12 11:38:50 +00:00
|
|
|
$targets = array(Constraint::CLASS_CONSTRAINT, Constraint::PROPERTY_CONSTRAINT);
|
2011-06-14 09:40:42 +01:00
|
|
|
|
2014-02-12 11:38:50 +00:00
|
|
|
$this->assertEquals($targets, $constraint->getTargets());
|
2011-06-14 09:40:42 +01:00
|
|
|
}
|
2013-09-26 10:03:13 +01:00
|
|
|
|
|
|
|
// Should succeed. Needed when defining constraints as annotations.
|
|
|
|
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-08 18:56:59 +01:00
|
|
|
}
|