bug #37392 [Validator] fix handling typed properties as constraint options (xabbuh)

This PR was merged into the 3.4 branch.

Discussion
----------

[Validator] fix handling typed properties as constraint options

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #37387
| License       | MIT
| Doc PR        |

Commits
-------

4a66a6098f fix handling typed properties as constraint options
This commit is contained in:
Fabien Potencier 2020-06-25 12:06:33 +02:00
commit 9660e6b1e2
4 changed files with 45 additions and 1 deletions

View File

@ -108,7 +108,7 @@ abstract class Constraint
$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;

View File

@ -13,10 +13,13 @@ namespace Symfony\Component\Validator\Tests;
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 @@ class ConstraintTest extends TestCase
$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);
}
}

View File

@ -0,0 +1,10 @@
<?php
namespace Symfony\Component\Validator\Tests\Fixtures;
use Symfony\Component\Validator\Constraint;
class ConstraintWithStaticProperty extends Constraint
{
public static $foo;
}

View File

@ -0,0 +1,10 @@
<?php
namespace Symfony\Component\Validator\Tests\Fixtures;
use Symfony\Component\Validator\Constraint;
class ConstraintWithTypedProperty extends Constraint
{
public string $foo;
}