bug #21208 [Validator] Add object handling of invalid constraints in Composite (SenseException)

This PR was submitted for the master branch but it was merged into the 2.7 branch instead (closes #21208).

Discussion
----------

[Validator] Add object handling of invalid constraints in Composite

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #21206
| License       | MIT
| Doc PR        | n/a

This PR fixes a minor bug described in #21206. The constraint `Symfony\Component\Validator\Constraints\Composite` doesn't check in it's exception handling if the wrongly created instance of a nested constraint is an object, which is the expected type for a constraint.

Commits
-------

4bd2c22871 [Validator] Add object handling of invalid constraints in Composite
This commit is contained in:
Fabien Potencier 2017-03-22 12:05:14 -07:00
commit ad95227f73
2 changed files with 15 additions and 0 deletions

View File

@ -67,6 +67,10 @@ abstract class Composite extends Constraint
foreach ($nestedConstraints as $constraint) {
if (!$constraint instanceof Constraint) {
if (is_object($constraint)) {
$constraint = get_class($constraint);
}
throw new ConstraintDefinitionException(sprintf('The value %s is not an instance of Constraint in constraint %s', $constraint, get_class($this)));
}

View File

@ -125,6 +125,17 @@ class CompositeTest extends TestCase
));
}
/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*/
public function testFailIfNoConstraintObject()
{
new ConcreteComposite(array(
new NotNull(array('groups' => 'Default')),
new \ArrayObject(),
));
}
/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*/