diff --git a/Constraint.php b/Constraint.php index 44efe33fa..cf0a9f17e 100644 --- a/Constraint.php +++ b/Constraint.php @@ -108,7 +108,7 @@ public function __construct($options = null) $defaultOption = $this->getDefaultOption(); $invalidOptions = []; $missingOptions = array_flip((array) $this->getRequiredOptions()); - $knownOptions = get_object_vars($this); + $knownOptions = get_class_vars(static::class); // The "groups" option is added to the object lazily $knownOptions['groups'] = true; diff --git a/Tests/ConstraintTest.php b/Tests/ConstraintTest.php index 6c481b008..26cc460d3 100644 --- a/Tests/ConstraintTest.php +++ b/Tests/ConstraintTest.php @@ -13,10 +13,13 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\Exception\InvalidOptionsException; use Symfony\Component\Validator\Tests\Fixtures\ClassConstraint; use Symfony\Component\Validator\Tests\Fixtures\ConstraintA; use Symfony\Component\Validator\Tests\Fixtures\ConstraintB; use Symfony\Component\Validator\Tests\Fixtures\ConstraintC; +use Symfony\Component\Validator\Tests\Fixtures\ConstraintWithStaticProperty; +use Symfony\Component\Validator\Tests\Fixtures\ConstraintWithTypedProperty; use Symfony\Component\Validator\Tests\Fixtures\ConstraintWithValue; use Symfony\Component\Validator\Tests\Fixtures\ConstraintWithValueAsDefault; @@ -245,4 +248,25 @@ public function testAnnotationSetUndefinedDefaultOption() $this->expectExceptionMessage('No default option is configured for constraint "Symfony\Component\Validator\Tests\Fixtures\ConstraintB".'); new ConstraintB(['value' => 1]); } + + public function testStaticPropertiesAreNoOptions() + { + $this->expectException(InvalidOptionsException::class); + + new ConstraintWithStaticProperty([ + 'foo' => 'bar', + ]); + } + + /** + * @requires PHP 7.4 + */ + public function testSetTypedProperty() + { + $constraint = new ConstraintWithTypedProperty([ + 'foo' => 'bar', + ]); + + $this->assertSame('bar', $constraint->foo); + } } diff --git a/Tests/Fixtures/ConstraintWithStaticProperty.php b/Tests/Fixtures/ConstraintWithStaticProperty.php new file mode 100644 index 000000000..f8b216940 --- /dev/null +++ b/Tests/Fixtures/ConstraintWithStaticProperty.php @@ -0,0 +1,10 @@ +