16
16
use Symfony \Component \Validator \Test \ConstraintValidatorTestCase ;
17
17
use Symfony \Component \Validator \Tests \Constraints \Fixtures \StringableValue ;
18
18
19
- /**
20
- * @group legacy
21
- */
22
19
class PasswordStrengthValidatorWithClosureTest extends ConstraintValidatorTestCase
23
20
{
24
21
protected function createValidator (): PasswordStrengthValidator
25
22
{
26
- return new PasswordStrengthValidator (PasswordStrengthValidator::estimateStrength (...));
23
+ return new PasswordStrengthValidator (function (string $ value ) {
24
+ $ length = strlen ($ value );
25
+ if ($ length < 6 ) {
26
+ return PasswordStrength::STRENGTH_VERY_WEAK ;
27
+ }
28
+ if ($ length < 10 ) {
29
+ return PasswordStrength::STRENGTH_WEAK ;
30
+ }
31
+ if ($ length < 15 ) {
32
+ return PasswordStrength::STRENGTH_MEDIUM ;
33
+ }
34
+ if ($ length < 20 ) {
35
+ return PasswordStrength::STRENGTH_STRONG ;
36
+ }
37
+ return PasswordStrength::STRENGTH_VERY_STRONG ;
38
+ });
27
39
}
28
40
29
41
/**
@@ -49,11 +61,10 @@ public function testValidValues(string|\Stringable $value, int $expectedStrength
49
61
50
62
public static function getValidValues (): iterable
51
63
{
52
- yield ['How-is-this ' , PasswordStrength::STRENGTH_WEAK ];
53
- yield ['Reasonable-pwd ' , PasswordStrength::STRENGTH_MEDIUM ];
54
- yield ['This 1s a very g00d Pa55word! ;-) ' , PasswordStrength::STRENGTH_VERY_STRONG ];
55
- yield ['pudding-smack-👌🏼-fox-😎 ' , PasswordStrength::STRENGTH_VERY_STRONG ];
56
- yield [new StringableValue ('How-is-this ' ), PasswordStrength::STRENGTH_WEAK ];
64
+ yield ['az34tyu ' , PasswordStrength::STRENGTH_WEAK ];
65
+ yield ['A med1um one ' , PasswordStrength::STRENGTH_MEDIUM ];
66
+ yield ['a str0ng3r one doh ' , PasswordStrength::STRENGTH_STRONG ];
67
+ yield [new StringableValue ('HeloW0rld ' ), PasswordStrength::STRENGTH_WEAK ];
57
68
}
58
69
59
70
/**
@@ -78,21 +89,21 @@ public static function provideInvalidConstraints(): iterable
78
89
'password ' ,
79
90
'The password strength is too low. Please use a stronger password. ' ,
80
91
PasswordStrength::PASSWORD_STRENGTH_ERROR ,
81
- ' 0 ' ,
92
+ ( string ) PasswordStrength:: STRENGTH_WEAK ,
82
93
];
83
94
yield [
84
95
new PasswordStrength (minScore: PasswordStrength::STRENGTH_VERY_STRONG ),
85
96
'Good password? ' ,
86
97
'The password strength is too low. Please use a stronger password. ' ,
87
98
PasswordStrength::PASSWORD_STRENGTH_ERROR ,
88
- ' 1 ' ,
99
+ ( string ) PasswordStrength:: STRENGTH_MEDIUM ,
89
100
];
90
101
yield [
91
102
new PasswordStrength (message: 'This password should be strong. ' ),
92
103
'password ' ,
93
104
'This password should be strong. ' ,
94
105
PasswordStrength::PASSWORD_STRENGTH_ERROR ,
95
- ' 0 ' ,
106
+ ( string ) PasswordStrength:: STRENGTH_WEAK ,
96
107
];
97
108
}
98
109
}
0 commit comments