-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
PasswordStrengthValidatorTest.php
89 lines (76 loc) · 3.12 KB
/
PasswordStrengthValidatorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Validator\Tests\Constraints;
use Symfony\Component\Validator\Constraints\PasswordStrength;
use Symfony\Component\Validator\Constraints\PasswordStrengthValidator;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
use Symfony\Component\Validator\Tests\Constraints\Fixtures\StringableValue;
class PasswordStrengthValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator(): PasswordStrengthValidator
{
return new PasswordStrengthValidator();
}
/**
* @dataProvider getValidValues
*/
public function testValidValues(string|\Stringable $value, int $expectedStrength)
{
$this->validator->validate($value, new PasswordStrength(minScore: $expectedStrength));
$this->assertNoViolation();
if (PasswordStrength::STRENGTH_VERY_STRONG === $expectedStrength) {
return;
}
$this->validator->validate($value, new PasswordStrength(minScore: $expectedStrength + 1));
$this->buildViolation('The password strength is too low. Please use a stronger password.')
->setCode(PasswordStrength::PASSWORD_STRENGTH_ERROR)
->assertRaised();
}
public static function getValidValues(): iterable
{
yield ['How-is-this', PasswordStrength::STRENGTH_WEAK];
yield ['Reasonable-pwd', PasswordStrength::STRENGTH_MEDIUM];
yield ['This 1s a very g00d Pa55word! ;-)', PasswordStrength::STRENGTH_VERY_STRONG];
yield ['pudding-smack-👌🏼-fox-😎', PasswordStrength::STRENGTH_VERY_STRONG];
yield [new StringableValue('How-is-this'), PasswordStrength::STRENGTH_WEAK];
}
/**
* @dataProvider provideInvalidConstraints
*/
public function testThePasswordIsWeak(PasswordStrength $constraint, string $password, string $expectedMessage, string $expectedCode, array $parameters = [])
{
$this->validator->validate($password, $constraint);
$this->buildViolation($expectedMessage)
->setCode($expectedCode)
->setParameters($parameters)
->assertRaised();
}
public static function provideInvalidConstraints(): iterable
{
yield [
new PasswordStrength(),
'password',
'The password strength is too low. Please use a stronger password.',
PasswordStrength::PASSWORD_STRENGTH_ERROR,
];
yield [
new PasswordStrength(minScore: PasswordStrength::STRENGTH_VERY_STRONG),
'Good password?',
'The password strength is too low. Please use a stronger password.',
PasswordStrength::PASSWORD_STRENGTH_ERROR,
];
yield [
new PasswordStrength(message: 'This password should be strong.'),
'password',
'This password should be strong.',
PasswordStrength::PASSWORD_STRENGTH_ERROR,
];
}
}