Skip to content

Commit 8094850

Browse files
derrabusfabpot
authored andcommitted
[Validator] Upgraded constraints to enable named arguments and attributes
1 parent 9a90527 commit 8094850

File tree

4 files changed

+110
-12
lines changed

4 files changed

+110
-12
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Security\Core\Tests\Validator\Constraints;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
16+
use Symfony\Component\Validator\Mapping\ClassMetadata;
17+
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
18+
19+
class UserPasswordTest extends TestCase
20+
{
21+
public function testValidatedByStandardValidator()
22+
{
23+
$constraint = new UserPassword();
24+
25+
self::assertSame('security.validator.user_password', $constraint->validatedBy());
26+
}
27+
28+
/**
29+
* @dataProvider provideServiceValidatedConstraints
30+
*/
31+
public function testValidatedByService(UserPassword $constraint)
32+
{
33+
self::assertSame('my_service', $constraint->validatedBy());
34+
}
35+
36+
public function provideServiceValidatedConstraints(): iterable
37+
{
38+
yield 'Doctrine style' => [new UserPassword(['service' => 'my_service'])];
39+
40+
if (\PHP_VERSION_ID < 80000) {
41+
return;
42+
}
43+
44+
yield 'named arguments' => [eval('return new \Symfony\Component\Security\Core\Validator\Constraints\UserPassword(service: "my_service");')];
45+
46+
$metadata = new ClassMetadata(UserPasswordDummy::class);
47+
self::assertTrue((new AnnotationLoader())->loadClassMetadata($metadata));
48+
49+
yield 'attribute' => [$metadata->properties['b']->constraints[0]];
50+
}
51+
52+
/**
53+
* @requires PHP 8
54+
*/
55+
public function testAttributes()
56+
{
57+
$metadata = new ClassMetadata(UserPasswordDummy::class);
58+
self::assertTrue((new AnnotationLoader())->loadClassMetadata($metadata));
59+
60+
list($bConstraint) = $metadata->properties['b']->getConstraints();
61+
self::assertSame('myMessage', $bConstraint->message);
62+
self::assertSame(['Default', 'UserPasswordDummy'], $bConstraint->groups);
63+
self::assertNull($bConstraint->payload);
64+
65+
list($cConstraint) = $metadata->properties['c']->getConstraints();
66+
self::assertSame(['my_group'], $cConstraint->groups);
67+
self::assertSame('some attached data', $cConstraint->payload);
68+
}
69+
}
70+
71+
class UserPasswordDummy
72+
{
73+
#[UserPassword]
74+
private $a;
75+
76+
#[UserPassword(service: 'my_service', message: 'myMessage')]
77+
private $b;
78+
79+
#[UserPassword(groups: ['my_group'], payload: 'some attached data')]
80+
private $c;
81+
}

Tests/Validator/Constraints/UserPasswordValidatorTest.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,11 @@ protected function setUp(): void
5656
parent::setUp();
5757
}
5858

59-
public function testPasswordIsValid()
59+
/**
60+
* @dataProvider provideConstraints
61+
*/
62+
public function testPasswordIsValid(UserPassword $constraint)
6063
{
61-
$constraint = new UserPassword([
62-
'message' => 'myMessage',
63-
]);
64-
6564
$this->encoder->expects($this->once())
6665
->method('isPasswordValid')
6766
->with(static::PASSWORD, 'secret', static::SALT)
@@ -72,12 +71,11 @@ public function testPasswordIsValid()
7271
$this->assertNoViolation();
7372
}
7473

75-
public function testPasswordIsNotValid()
74+
/**
75+
* @dataProvider provideConstraints
76+
*/
77+
public function testPasswordIsNotValid(UserPassword $constraint)
7678
{
77-
$constraint = new UserPassword([
78-
'message' => 'myMessage',
79-
]);
80-
8179
$this->encoder->expects($this->once())
8280
->method('isPasswordValid')
8381
->with(static::PASSWORD, 'secret', static::SALT)
@@ -89,6 +87,15 @@ public function testPasswordIsNotValid()
8987
->assertRaised();
9088
}
9189

90+
public function provideConstraints(): iterable
91+
{
92+
yield 'Doctrine style' => [new UserPassword(['message' => 'myMessage'])];
93+
94+
if (\PHP_VERSION_ID >= 80000) {
95+
yield 'named arguments' => [eval('return new \Symfony\Component\Security\Core\Validator\Constraints\UserPassword(message: "myMessage");')];
96+
}
97+
}
98+
9299
/**
93100
* @dataProvider emptyPasswordData
94101
*/

Validator/Constraints/UserPassword.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,20 @@
1717
* @Annotation
1818
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
1919
*/
20+
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
2021
class UserPassword extends Constraint
2122
{
2223
public $message = 'This value should be the user\'s current password.';
2324
public $service = 'security.validator.user_password';
2425

26+
public function __construct(array $options = null, string $message = null, string $service = null, array $groups = null, $payload = null)
27+
{
28+
parent::__construct($options, $groups, $payload);
29+
30+
$this->message = $message ?? $this->message;
31+
$this->service = $service ?? $this->service;
32+
}
33+
2534
/**
2635
* {@inheritdoc}
2736
*/

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@
2828
"symfony/expression-language": "^4.4|^5.0",
2929
"symfony/http-foundation": "^4.4|^5.0",
3030
"symfony/ldap": "^4.4|^5.0",
31-
"symfony/validator": "^4.4|^5.0",
31+
"symfony/validator": "^5.2",
3232
"psr/log": "~1.0"
3333
},
3434
"conflict": {
3535
"symfony/event-dispatcher": "<4.4",
3636
"symfony/security-guard": "<4.4",
37-
"symfony/ldap": "<4.4"
37+
"symfony/ldap": "<4.4",
38+
"symfony/validator": "<5.2"
3839
},
3940
"suggest": {
4041
"psr/container-implementation": "To instantiate the Security class",

0 commit comments

Comments
 (0)