use Symfony\Component\Validator\Constraints\Choice;
use Symfony\Component\Validator\Constraints\ChoiceValidator;
-use Symfony\Component\Validator\Validation;
+use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
function choice_callback()
{
return array('foo', 'bar');
}
-class ChoiceValidatorTest extends AbstractConstraintValidatorTest
+class ChoiceValidatorTest extends ConstraintValidatorTestCase
{
- protected function getApiVersion()
- {
- return Validation::API_VERSION_2_5;
- }
-
protected function createValidator()
{
return new ChoiceValidator();
return array('foo', 'bar');
}
+ public function objectMethodCallback()
+ {
+ return array('foo', 'bar');
+ }
+
/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
*/
$constraint = new Choice(array(
'choices' => array('foo', 'bar'),
'multiple' => true,
+ 'strict' => true,
));
$this->validator->validate('asdf', $constraint);
public function testNullIsValid()
{
- $this->validator->validate(null, new Choice(array('choices' => array('foo', 'bar'))));
+ $this->validator->validate(
+ null,
+ new Choice(
+ array(
+ 'choices' => array('foo', 'bar'),
+ 'strict' => true,
+ )
+ )
+ );
$this->assertNoViolation();
}
*/
public function testChoicesOrCallbackExpected()
{
- $this->validator->validate('foobar', new Choice());
+ $this->validator->validate('foobar', new Choice(array('strict' => true)));
}
/**
*/
public function testValidCallbackExpected()
{
- $this->validator->validate('foobar', new Choice(array('callback' => 'abcd')));
+ $this->validator->validate('foobar', new Choice(array('callback' => 'abcd', 'strict' => true)));
}
public function testValidChoiceArray()
{
- $constraint = new Choice(array('choices' => array('foo', 'bar')));
+ $constraint = new Choice(array('choices' => array('foo', 'bar'), 'strict' => true));
$this->validator->validate('bar', $constraint);
public function testValidChoiceCallbackFunction()
{
- $constraint = new Choice(array('callback' => __NAMESPACE__.'\choice_callback'));
+ $constraint = new Choice(array('callback' => __NAMESPACE__.'\choice_callback', 'strict' => true));
$this->validator->validate('bar', $constraint);
public function testValidChoiceCallbackClosure()
{
- $constraint = new Choice(array('callback' => function () {
- return array('foo', 'bar');
- }));
+ $constraint = new Choice(
+ array(
+ 'strict' => true,
+ 'callback' => function () {
+ return array('foo', 'bar');
+ },
+ )
+ );
$this->validator->validate('bar', $constraint);
public function testValidChoiceCallbackStaticMethod()
{
- $constraint = new Choice(array('callback' => array(__CLASS__, 'staticCallback')));
+ $constraint = new Choice(array('callback' => array(__CLASS__, 'staticCallback'), 'strict' => true));
$this->validator->validate('bar', $constraint);
// search $this for "staticCallback"
$this->setObject($this);
- $constraint = new Choice(array('callback' => 'staticCallback'));
+ $constraint = new Choice(array('callback' => 'staticCallback', 'strict' => true));
+
+ $this->validator->validate('bar', $constraint);
+
+ $this->assertNoViolation();
+ }
+
+ public function testValidChoiceCallbackContextObjectMethod()
+ {
+ // search $this for "objectMethodCallback"
+ $this->setObject($this);
+
+ $constraint = new Choice(array('callback' => 'objectMethodCallback', 'strict' => true));
$this->validator->validate('bar', $constraint);
$constraint = new Choice(array(
'choices' => array('foo', 'bar', 'baz'),
'multiple' => true,
+ 'strict' => true,
));
$this->validator->validate(array('baz', 'bar'), $constraint);
$constraint = new Choice(array(
'choices' => array('foo', 'bar'),
'message' => 'myMessage',
+ 'strict' => true,
));
$this->validator->validate('baz', $constraint);
// the DB or the model
'choices' => array(),
'message' => 'myMessage',
+ 'strict' => true,
));
$this->validator->validate('baz', $constraint);
'choices' => array('foo', 'bar'),
'multipleMessage' => 'myMessage',
'multiple' => true,
+ 'strict' => true,
));
$this->validator->validate(array('foo', 'baz'), $constraint);
'multiple' => true,
'min' => 2,
'minMessage' => 'myMessage',
+ 'strict' => true,
));
$value = array('foo');
'multiple' => true,
'max' => 2,
'maxMessage' => 'myMessage',
+ 'strict' => true,
));
$value = array('foo', 'bar', 'moo');
->assertRaised();
}
+ /**
+ * @group legacy
+ */
public function testNonStrict()
{
$constraint = new Choice(array(
->assertRaised();
}
+ /**
+ * @group legacy
+ */
public function testNonStrictWithMultipleChoices()
{
$constraint = new Choice(array(