[Validator] Throw exception on Comparison constraints null options

This commit is contained in:
Maxime Steinhausser 2017-06-29 19:50:07 +02:00
parent 7acc34537b
commit 2de59a7381
10 changed files with 25 additions and 14 deletions

View File

@ -29,6 +29,10 @@ abstract class AbstractComparison extends Constraint
*/ */
public function __construct($options = null) public function __construct($options = null)
{ {
if (null === $options) {
$options = array();
}
if (is_array($options) && !isset($options['value'])) { if (is_array($options) && !isset($options['value'])) {
throw new ConstraintDefinitionException(sprintf( throw new ConstraintDefinitionException(sprintf(
'The %s constraint requires the "value" option to be set.', 'The %s constraint requires the "value" option to be set.',

View File

@ -68,14 +68,21 @@ abstract class AbstractComparisonValidatorTestCase extends AbstractConstraintVal
return $result; return $result;
} }
public function provideInvalidConstraintOptions()
{
return array(
array(null),
array(array()),
);
}
/** /**
* @dataProvider provideInvalidConstraintOptions
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*/ */
public function testThrowsConstraintExceptionIfNoValueOrProperty() public function testThrowsConstraintExceptionIfNoValueOrProperty($options)
{ {
$comparison = $this->createConstraint(array()); $this->createConstraint($options);
$this->validator->validate('some value', $comparison);
} }
/** /**
@ -169,9 +176,9 @@ abstract class AbstractComparisonValidatorTestCase extends AbstractConstraintVal
abstract public function provideInvalidComparisons(); abstract public function provideInvalidComparisons();
/** /**
* @param array $options Options for the constraint * @param array|null $options Options for the constraint
* *
* @return Constraint * @return Constraint
*/ */
abstract protected function createConstraint(array $options); abstract protected function createConstraint(array $options = null);
} }

View File

@ -30,7 +30,7 @@ class EqualToValidatorTest extends AbstractComparisonValidatorTestCase
return new EqualToValidator(); return new EqualToValidator();
} }
protected function createConstraint(array $options) protected function createConstraint(array $options = null)
{ {
return new EqualTo($options); return new EqualTo($options);
} }

View File

@ -30,7 +30,7 @@ class GreaterThanOrEqualValidatorTest extends AbstractComparisonValidatorTestCas
return new GreaterThanOrEqualValidator(); return new GreaterThanOrEqualValidator();
} }
protected function createConstraint(array $options) protected function createConstraint(array $options = null)
{ {
return new GreaterThanOrEqual($options); return new GreaterThanOrEqual($options);
} }

View File

@ -30,7 +30,7 @@ class GreaterThanValidatorTest extends AbstractComparisonValidatorTestCase
return new GreaterThanValidator(); return new GreaterThanValidator();
} }
protected function createConstraint(array $options) protected function createConstraint(array $options = null)
{ {
return new GreaterThan($options); return new GreaterThan($options);
} }

View File

@ -30,7 +30,7 @@ class IdenticalToValidatorTest extends AbstractComparisonValidatorTestCase
return new IdenticalToValidator(); return new IdenticalToValidator();
} }
protected function createConstraint(array $options) protected function createConstraint(array $options = null)
{ {
return new IdenticalTo($options); return new IdenticalTo($options);
} }

View File

@ -30,7 +30,7 @@ class LessThanOrEqualValidatorTest extends AbstractComparisonValidatorTestCase
return new LessThanOrEqualValidator(); return new LessThanOrEqualValidator();
} }
protected function createConstraint(array $options) protected function createConstraint(array $options = null)
{ {
return new LessThanOrEqual($options); return new LessThanOrEqual($options);
} }

View File

@ -30,7 +30,7 @@ class LessThanValidatorTest extends AbstractComparisonValidatorTestCase
return new LessThanValidator(); return new LessThanValidator();
} }
protected function createConstraint(array $options) protected function createConstraint(array $options = null)
{ {
return new LessThan($options); return new LessThan($options);
} }

View File

@ -30,7 +30,7 @@ class NotEqualToValidatorTest extends AbstractComparisonValidatorTestCase
return new NotEqualToValidator(); return new NotEqualToValidator();
} }
protected function createConstraint(array $options) protected function createConstraint(array $options = null)
{ {
return new NotEqualTo($options); return new NotEqualTo($options);
} }

View File

@ -30,7 +30,7 @@ class NotIdenticalToValidatorTest extends AbstractComparisonValidatorTestCase
return new NotIdenticalToValidator(); return new NotIdenticalToValidator();
} }
protected function createConstraint(array $options) protected function createConstraint(array $options = null)
{ {
return new NotIdenticalTo($options); return new NotIdenticalTo($options);
} }