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.
|
|
|
|
*/
|
|
|
|
|
2011-05-05 17:37:03 +01:00
|
|
|
namespace Symfony\Tests\Component\Validator\Constraints;
|
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
|
|
|
|
{
|
|
|
|
public static function validateStatic($object, ExecutionContext $context)
|
|
|
|
{
|
2012-02-01 12:53:45 +00:00
|
|
|
$context->addViolation('Static message', array('{{ value }}' => 'foobar'), 'invalidValue');
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
public function validateOne(ExecutionContext $context)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public function validateTwo(ExecutionContext $context)
|
|
|
|
{
|
2012-02-01 12:53:45 +00:00
|
|
|
$context->addViolation('Other message', array('{{ value }}' => 'baz'), 'otherInvalidValue');
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
2011-03-25 00:34:57 +00:00
|
|
|
$this->assertTrue($this->validator->isValid(null, new Callback(array('foo'))));
|
2011-02-03 09:40:53 +00:00
|
|
|
}
|
|
|
|
|
2011-03-25 00:21:46 +00:00
|
|
|
public function testCallbackSingleMethod()
|
2011-02-03 09:40:53 +00:00
|
|
|
{
|
2011-03-25 00:21:46 +00:00
|
|
|
$object = new CallbackValidatorTest_Object();
|
2012-02-01 12:53:45 +00:00
|
|
|
$constraint = new Callback(array('validateOne'));
|
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-01 12:53:45 +00:00
|
|
|
$this->assertFalse($this->validator->isValid($object, $constraint));
|
2011-02-03 09:40:53 +00:00
|
|
|
}
|
|
|
|
|
2011-03-25 00:34:57 +00:00
|
|
|
public function testCallbackSingleStaticMethod()
|
|
|
|
{
|
|
|
|
$object = new CallbackValidatorTest_Object();
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->once())
|
|
|
|
->method('addViolation')
|
|
|
|
->with('Static message', array(
|
|
|
|
'{{ value }}' => 'foobar',
|
|
|
|
));
|
2011-03-25 00:34:57 +00:00
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->assertFalse($this->validator->isValid($object, new Callback(array(
|
|
|
|
array(__CLASS__.'_Class', 'validateStatic')
|
|
|
|
))));
|
2011-03-25 00:34:57 +00:00
|
|
|
}
|
|
|
|
|
2011-03-25 00:21:46 +00:00
|
|
|
public function testCallbackMultipleMethods()
|
2011-02-03 09:40:53 +00:00
|
|
|
{
|
2011-03-25 00:21:46 +00:00
|
|
|
$object = new CallbackValidatorTest_Object();
|
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')
|
|
|
|
->with('Other message', array(
|
|
|
|
'{{ value }}' => 'baz',
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertFalse($this->validator->isValid($object, new Callback(array(
|
2011-02-03 09:40:53 +00:00
|
|
|
'validateOne', 'validateTwo'
|
|
|
|
))));
|
|
|
|
}
|
2011-03-25 00:34:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException
|
|
|
|
*/
|
|
|
|
public function testExpectCallbackArray()
|
|
|
|
{
|
|
|
|
$object = new CallbackValidatorTest_Object();
|
|
|
|
|
|
|
|
$this->validator->isValid($object, new Callback('foobar'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Symfony\Component\Validator\Exception\ConstraintDefinitionException
|
|
|
|
*/
|
|
|
|
public function testExpectValidMethods()
|
|
|
|
{
|
|
|
|
$object = new CallbackValidatorTest_Object();
|
|
|
|
|
|
|
|
$this->validator->isValid($object, new Callback(array('foobar')));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Symfony\Component\Validator\Exception\ConstraintDefinitionException
|
|
|
|
*/
|
|
|
|
public function testExpectValidCallbacks()
|
|
|
|
{
|
|
|
|
$object = new CallbackValidatorTest_Object();
|
|
|
|
|
|
|
|
$this->validator->isValid($object, new Callback(array(array('foo', 'bar'))));
|
|
|
|
}
|
2011-06-14 09:40:42 +01:00
|
|
|
|
|
|
|
public function testConstraintGetTargets()
|
|
|
|
{
|
|
|
|
$constraint = new Callback(array('foo'));
|
|
|
|
|
|
|
|
$this->assertEquals('class', $constraint->getTargets());
|
|
|
|
}
|
2011-06-08 18:56:59 +01:00
|
|
|
}
|