[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)
{
if (null === $options) {
$options = array();
}
if (is_array($options) && !isset($options['value'])) {
throw new ConstraintDefinitionException(sprintf(
'The %s constraint requires the "value" option to be set.',

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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