From 4c6ddd4737a53f94163a163be984d948cf5906c8 Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Sat, 1 Oct 2016 21:18:01 +0000 Subject: [PATCH] reset constraint options --- .../Component/Validator/Constraint.php | 3 ++ .../Validator/Tests/ConstraintTest.php | 31 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/Symfony/Component/Validator/Constraint.php b/src/Symfony/Component/Validator/Constraint.php index a6d06472c2..08041d4c7e 100644 --- a/src/Symfony/Component/Validator/Constraint.php +++ b/src/Symfony/Component/Validator/Constraint.php @@ -129,6 +129,9 @@ abstract class Constraint unset($options['value']); } + if (is_array($options)) { + reset($options); + } if (is_array($options) && count($options) > 0 && is_string(key($options))) { foreach ($options as $option => $value) { if (array_key_exists($option, $knownOptions)) { diff --git a/src/Symfony/Component/Validator/Tests/ConstraintTest.php b/src/Symfony/Component/Validator/Tests/ConstraintTest.php index f63570c5d2..d473f14f65 100644 --- a/src/Symfony/Component/Validator/Tests/ConstraintTest.php +++ b/src/Symfony/Component/Validator/Tests/ConstraintTest.php @@ -206,4 +206,35 @@ class ConstraintTest extends \PHPUnit_Framework_TestCase { Constraint::getErrorName(1); } + + public function testOptionsAsDefaultOption() + { + $constraint = new ConstraintA($options = array('value1')); + + $this->assertEquals($options, $constraint->property2); + + $constraint = new ConstraintA($options = array('value1', 'property1' => 'value2')); + + $this->assertEquals($options, $constraint->property2); + } + + /** + * @expectedException \Symfony\Component\Validator\Exception\InvalidOptionsException + * @expectedExceptionMessage The options "0", "5" do not exist + */ + public function testInvalidOptions() + { + new ConstraintA(array('property2' => 'foo', 'bar', 5 => 'baz')); + } + + public function testOptionsWithInvalidInternalPointer() + { + $options = array('property1' => 'foo'); + next($options); + next($options); + + $constraint = new ConstraintA($options); + + $this->assertEquals('foo', $constraint->property1); + } }