2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-15 13:29:43 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Tests\Component\Validator;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2011-01-19 14:02:00 +00:00
|
|
|
use Symfony\Component\Validator\ExecutionContext;
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Validator\Constraints\Choice;
|
|
|
|
use Symfony\Component\Validator\Constraints\ChoiceValidator;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
|
|
|
function choice_callback()
|
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
return array('foo', 'bar');
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class ChoiceValidatorTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
protected $validator;
|
|
|
|
|
|
|
|
public static function staticCallback()
|
|
|
|
{
|
|
|
|
return array('foo', 'bar');
|
|
|
|
}
|
|
|
|
|
2010-12-06 14:45:37 +00:00
|
|
|
protected function setUp()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
2010-08-20 22:09:55 +01:00
|
|
|
$walker = $this->getMock('Symfony\Component\Validator\GraphWalker', array(), array(), '', false);
|
|
|
|
$factory = $this->getMock('Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface');
|
2011-01-19 14:02:00 +00:00
|
|
|
$context = new ExecutionContext('root', $walker, $factory);
|
2010-06-24 10:24:08 +01:00
|
|
|
$context->setCurrentClass(__CLASS__);
|
|
|
|
$this->validator = new ChoiceValidator();
|
|
|
|
$this->validator->initialize($context);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExpectArrayIfMultipleIsTrue()
|
|
|
|
{
|
|
|
|
$constraint = new Choice(array(
|
|
|
|
'choices' => array('foo', 'bar'),
|
|
|
|
'multiple' => true,
|
|
|
|
));
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
$this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
$this->validator->isValid('asdf', $constraint);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNullIsValid()
|
|
|
|
{
|
|
|
|
$this->assertTrue($this->validator->isValid(null, new Choice(array('choices' => array('foo', 'bar')))));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testChoicesOrCallbackExpected()
|
|
|
|
{
|
2010-08-20 22:09:55 +01:00
|
|
|
$this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
$this->validator->isValid('foobar', new Choice());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidCallbackExpected()
|
|
|
|
{
|
2010-08-20 22:09:55 +01:00
|
|
|
$this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
$this->validator->isValid('foobar', new Choice(array('callback' => 'abcd')));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidChoiceArray()
|
|
|
|
{
|
|
|
|
$constraint = new Choice(array('choices' => array('foo', 'bar')));
|
|
|
|
|
|
|
|
$this->assertTrue($this->validator->isValid('bar', $constraint));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidChoiceCallbackFunction()
|
|
|
|
{
|
|
|
|
$constraint = new Choice(array('callback' => __NAMESPACE__.'\choice_callback'));
|
|
|
|
|
|
|
|
$this->assertTrue($this->validator->isValid('bar', $constraint));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidChoiceCallbackClosure()
|
|
|
|
{
|
|
|
|
$constraint = new Choice(array('callback' => function() {
|
|
|
|
return array('foo', 'bar');
|
|
|
|
}));
|
|
|
|
|
|
|
|
$this->assertTrue($this->validator->isValid('bar', $constraint));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidChoiceCallbackStaticMethod()
|
|
|
|
{
|
|
|
|
$constraint = new Choice(array('callback' => array(__CLASS__, 'staticCallback')));
|
|
|
|
|
|
|
|
$this->assertTrue($this->validator->isValid('bar', $constraint));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidChoiceCallbackContextMethod()
|
|
|
|
{
|
|
|
|
$constraint = new Choice(array('callback' => 'staticCallback'));
|
|
|
|
|
|
|
|
$this->assertTrue($this->validator->isValid('bar', $constraint));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMultipleChoices()
|
|
|
|
{
|
|
|
|
$constraint = new Choice(array(
|
|
|
|
'choices' => array('foo', 'bar', 'baz'),
|
|
|
|
'multiple' => true,
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertTrue($this->validator->isValid(array('baz', 'bar'), $constraint));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalidChoice()
|
|
|
|
{
|
|
|
|
$constraint = new Choice(array(
|
|
|
|
'choices' => array('foo', 'bar'),
|
|
|
|
'message' => 'myMessage',
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertFalse($this->validator->isValid('baz', $constraint));
|
|
|
|
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
|
|
|
$this->assertEquals($this->validator->getMessageParameters(), array(
|
2010-09-29 06:59:45 +01:00
|
|
|
'{{ value }}' => 'baz',
|
2010-06-24 10:24:08 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalidChoiceMultiple()
|
|
|
|
{
|
|
|
|
$constraint = new Choice(array(
|
|
|
|
'choices' => array('foo', 'bar'),
|
|
|
|
'message' => 'myMessage',
|
|
|
|
'multiple' => true,
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertFalse($this->validator->isValid(array('foo', 'baz'), $constraint));
|
|
|
|
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
|
|
|
$this->assertEquals($this->validator->getMessageParameters(), array(
|
2010-09-29 06:59:45 +01:00
|
|
|
'{{ value }}' => 'baz',
|
2010-06-24 10:24:08 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTooFewChoices()
|
|
|
|
{
|
|
|
|
$constraint = new Choice(array(
|
|
|
|
'choices' => array('foo', 'bar', 'moo', 'maa'),
|
|
|
|
'multiple' => true,
|
|
|
|
'min' => 2,
|
|
|
|
'minMessage' => 'myMessage',
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertFalse($this->validator->isValid(array('foo'), $constraint));
|
|
|
|
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
|
|
|
$this->assertEquals($this->validator->getMessageParameters(), array(
|
2010-09-29 06:59:45 +01:00
|
|
|
'{{ limit }}' => 2,
|
2010-06-24 10:24:08 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTooManyChoices()
|
|
|
|
{
|
|
|
|
$constraint = new Choice(array(
|
|
|
|
'choices' => array('foo', 'bar', 'moo', 'maa'),
|
|
|
|
'multiple' => true,
|
|
|
|
'max' => 2,
|
|
|
|
'maxMessage' => 'myMessage',
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertFalse($this->validator->isValid(array('foo', 'bar', 'moo'), $constraint));
|
|
|
|
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
|
|
|
$this->assertEquals($this->validator->getMessageParameters(), array(
|
2010-09-29 06:59:45 +01:00
|
|
|
'{{ limit }}' => 2,
|
2010-06-24 10:24:08 +01:00
|
|
|
));
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|