minor #23327 [Validator] Throw exception on Comparison constraints null options (ogizanagi)

This PR was merged into the 2.7 branch.

Discussion
----------

[Validator] Throw exception on Comparison constraints null options

| Q             | A
| ------------- | ---
| Branch?       | 2.7 <!-- see comment below -->
| Bug fix?      | no. There is no bug, but the constraint can be silently created in an invalid state.
| New feature?  | no <!-- don't forget updating src/**/CHANGELOG.md files -->
| BC breaks?    | no
| Deprecations? | no <!-- don't forget updating UPGRADE-*.md files -->
| Tests pass?   | yes (failure unrelated)
| Fixed tickets | N/A <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | N/A

Commits
-------

2de59a7381 [Validator] Throw exception on Comparison constraints null options
This commit is contained in:
Fabien Potencier 2017-07-03 08:46:34 +03:00
commit 44c3f58265
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);
}