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.
|
|
|
|
*/
|
|
|
|
|
2011-05-05 17:37:03 +01:00
|
|
|
namespace Symfony\Tests\Component\Validator\Constraints;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2012-01-31 20:35:48 +00: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
|
|
|
|
{
|
2012-02-01 12:53:45 +00:00
|
|
|
protected $context;
|
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
|
|
|
{
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false);
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->validator = new ChoiceValidator();
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->validator->initialize($this->context);
|
|
|
|
|
|
|
|
$this->context->expects($this->any())
|
|
|
|
->method('getCurrentClass')
|
|
|
|
->will($this->returnValue(__CLASS__));
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2011-06-14 09:44:38 +01:00
|
|
|
protected function tearDown()
|
|
|
|
{
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context = null;
|
2011-06-14 09:44:38 +01:00
|
|
|
$this->validator = null;
|
|
|
|
}
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
/**
|
|
|
|
* @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException
|
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testExpectArrayIfMultipleIsTrue()
|
|
|
|
{
|
|
|
|
$constraint = new Choice(array(
|
|
|
|
'choices' => array('foo', 'bar'),
|
|
|
|
'multiple' => true,
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->validator->isValid('asdf', $constraint);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNullIsValid()
|
|
|
|
{
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->never())
|
|
|
|
->method('addViolation');
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertTrue($this->validator->isValid(null, new Choice(array('choices' => array('foo', 'bar')))));
|
|
|
|
}
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
/**
|
|
|
|
* @expectedException Symfony\Component\Validator\Exception\ConstraintDefinitionException
|
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testChoicesOrCallbackExpected()
|
|
|
|
{
|
|
|
|
$this->validator->isValid('foobar', new Choice());
|
|
|
|
}
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
/**
|
|
|
|
* @expectedException Symfony\Component\Validator\Exception\ConstraintDefinitionException
|
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testValidCallbackExpected()
|
|
|
|
{
|
|
|
|
$this->validator->isValid('foobar', new Choice(array('callback' => 'abcd')));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidChoiceArray()
|
|
|
|
{
|
|
|
|
$constraint = new Choice(array('choices' => array('foo', 'bar')));
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->never())
|
|
|
|
->method('addViolation');
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertTrue($this->validator->isValid('bar', $constraint));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidChoiceCallbackFunction()
|
|
|
|
{
|
|
|
|
$constraint = new Choice(array('callback' => __NAMESPACE__.'\choice_callback'));
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->never())
|
|
|
|
->method('addViolation');
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertTrue($this->validator->isValid('bar', $constraint));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidChoiceCallbackClosure()
|
|
|
|
{
|
|
|
|
$constraint = new Choice(array('callback' => function() {
|
|
|
|
return array('foo', 'bar');
|
|
|
|
}));
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->never())
|
|
|
|
->method('addViolation');
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertTrue($this->validator->isValid('bar', $constraint));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidChoiceCallbackStaticMethod()
|
|
|
|
{
|
|
|
|
$constraint = new Choice(array('callback' => array(__CLASS__, 'staticCallback')));
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->never())
|
|
|
|
->method('addViolation');
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertTrue($this->validator->isValid('bar', $constraint));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidChoiceCallbackContextMethod()
|
|
|
|
{
|
|
|
|
$constraint = new Choice(array('callback' => 'staticCallback'));
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->never())
|
|
|
|
->method('addViolation');
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertTrue($this->validator->isValid('bar', $constraint));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMultipleChoices()
|
|
|
|
{
|
|
|
|
$constraint = new Choice(array(
|
|
|
|
'choices' => array('foo', 'bar', 'baz'),
|
|
|
|
'multiple' => true,
|
|
|
|
));
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->never())
|
|
|
|
->method('addViolation');
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertTrue($this->validator->isValid(array('baz', 'bar'), $constraint));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalidChoice()
|
|
|
|
{
|
|
|
|
$constraint = new Choice(array(
|
|
|
|
'choices' => array('foo', 'bar'),
|
|
|
|
'message' => 'myMessage',
|
|
|
|
));
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->once())
|
|
|
|
->method('addViolation')
|
|
|
|
->with('myMessage', array(
|
|
|
|
'{{ value }}' => 'baz',
|
|
|
|
));
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertFalse($this->validator->isValid('baz', $constraint));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalidChoiceMultiple()
|
|
|
|
{
|
|
|
|
$constraint = new Choice(array(
|
|
|
|
'choices' => array('foo', 'bar'),
|
2011-04-22 07:41:58 +01:00
|
|
|
'multipleMessage' => 'myMessage',
|
2010-06-24 10:24:08 +01:00
|
|
|
'multiple' => true,
|
|
|
|
));
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->once())
|
|
|
|
->method('addViolation')
|
|
|
|
->with('myMessage', array(
|
|
|
|
'{{ value }}' => 'baz',
|
|
|
|
));
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertFalse($this->validator->isValid(array('foo', 'baz'), $constraint));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTooFewChoices()
|
|
|
|
{
|
|
|
|
$constraint = new Choice(array(
|
|
|
|
'choices' => array('foo', 'bar', 'moo', 'maa'),
|
|
|
|
'multiple' => true,
|
|
|
|
'min' => 2,
|
|
|
|
'minMessage' => 'myMessage',
|
|
|
|
));
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->once())
|
|
|
|
->method('addViolation')
|
|
|
|
->with('myMessage', array(
|
|
|
|
'{{ limit }}' => 2,
|
|
|
|
));
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertFalse($this->validator->isValid(array('foo'), $constraint));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTooManyChoices()
|
|
|
|
{
|
|
|
|
$constraint = new Choice(array(
|
|
|
|
'choices' => array('foo', 'bar', 'moo', 'maa'),
|
|
|
|
'multiple' => true,
|
|
|
|
'max' => 2,
|
|
|
|
'maxMessage' => 'myMessage',
|
|
|
|
));
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->once())
|
|
|
|
->method('addViolation')
|
|
|
|
->with('myMessage', array(
|
|
|
|
'{{ limit }}' => 2,
|
|
|
|
));
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertFalse($this->validator->isValid(array('foo', 'bar', 'moo'), $constraint));
|
|
|
|
}
|
2011-07-07 14:11:36 +01:00
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
public function testNonStrict()
|
2011-07-07 14:11:36 +01:00
|
|
|
{
|
|
|
|
$constraint = new Choice(array(
|
|
|
|
'choices' => array(1, 2),
|
|
|
|
'strict' => false,
|
|
|
|
));
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->never())
|
|
|
|
->method('addViolation');
|
|
|
|
|
2011-07-07 14:11:36 +01:00
|
|
|
$this->assertTrue($this->validator->isValid('2', $constraint));
|
|
|
|
$this->assertTrue($this->validator->isValid(2, $constraint));
|
|
|
|
}
|
2011-07-16 18:10:04 +01:00
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
public function testStrictAllowsExactValue()
|
2011-07-16 18:10:04 +01:00
|
|
|
{
|
|
|
|
$constraint = new Choice(array(
|
|
|
|
'choices' => array(1, 2),
|
|
|
|
'strict' => true,
|
|
|
|
));
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->never())
|
|
|
|
->method('addViolation');
|
|
|
|
|
2011-07-16 18:10:04 +01:00
|
|
|
$this->assertTrue($this->validator->isValid(2, $constraint));
|
2012-02-01 12:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testStrictDisallowsDifferentType()
|
|
|
|
{
|
|
|
|
$constraint = new Choice(array(
|
|
|
|
'choices' => array(1, 2),
|
|
|
|
'strict' => true,
|
|
|
|
'message' => 'myMessage'
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->context->expects($this->once())
|
|
|
|
->method('addViolation')
|
|
|
|
->with('myMessage', array(
|
|
|
|
'{{ value }}' => '2',
|
|
|
|
));
|
|
|
|
|
2011-07-16 18:10:04 +01:00
|
|
|
$this->assertFalse($this->validator->isValid('2', $constraint));
|
|
|
|
}
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
public function testNonStrictWithMultipleChoices()
|
2011-07-16 18:10:04 +01:00
|
|
|
{
|
|
|
|
$constraint = new Choice(array(
|
|
|
|
'choices' => array(1, 2, 3),
|
|
|
|
'multiple' => true,
|
|
|
|
'strict' => false
|
|
|
|
));
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->never())
|
|
|
|
->method('addViolation');
|
|
|
|
|
2011-07-16 18:10:04 +01:00
|
|
|
$this->assertTrue($this->validator->isValid(array('2', 3), $constraint));
|
|
|
|
}
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
public function testStrictWithMultipleChoices()
|
2011-07-16 18:10:04 +01:00
|
|
|
{
|
|
|
|
$constraint = new Choice(array(
|
|
|
|
'choices' => array(1, 2, 3),
|
|
|
|
'multiple' => true,
|
2012-02-01 12:53:45 +00:00
|
|
|
'strict' => true,
|
|
|
|
'multipleMessage' => 'myMessage',
|
2011-07-16 18:10:04 +01:00
|
|
|
));
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->expects($this->once())
|
|
|
|
->method('addViolation')
|
|
|
|
->with('myMessage', array(
|
|
|
|
'{{ value }}' => '3',
|
|
|
|
));
|
|
|
|
|
2011-07-16 18:10:04 +01:00
|
|
|
$this->assertFalse($this->validator->isValid(array(2, '3'), $constraint));
|
|
|
|
}
|
2011-06-08 18:56:59 +01:00
|
|
|
}
|