|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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\Validator\Tests\Constraints; |
| 13 | + |
| 14 | +use Symfony\Component\Validator\Constraints\PasswordStrength; |
| 15 | +use Symfony\Component\Validator\Constraints\PasswordStrengthValidator; |
| 16 | +use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; |
| 17 | +use Symfony\Component\Validator\Tests\Constraints\Fixtures\StringableValue; |
| 18 | + |
| 19 | +class PasswordStrengthValidatorWithClosureTest extends ConstraintValidatorTestCase |
| 20 | +{ |
| 21 | + protected function createValidator(): PasswordStrengthValidator |
| 22 | + { |
| 23 | + return new PasswordStrengthValidator(static function (string $value) { |
| 24 | + $length = \strlen($value); |
| 25 | + |
| 26 | + return match (true) { |
| 27 | + $length < 6 => PasswordStrength::STRENGTH_VERY_WEAK, |
| 28 | + $length < 10 => PasswordStrength::STRENGTH_WEAK, |
| 29 | + $length < 15 => PasswordStrength::STRENGTH_MEDIUM, |
| 30 | + $length < 20 => PasswordStrength::STRENGTH_STRONG, |
| 31 | + default => PasswordStrength::STRENGTH_VERY_STRONG, |
| 32 | + }; |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @dataProvider getValidValues |
| 38 | + */ |
| 39 | + public function testValidValues(string|\Stringable $value, int $expectedStrength) |
| 40 | + { |
| 41 | + $this->validator->validate($value, new PasswordStrength(minScore: $expectedStrength)); |
| 42 | + |
| 43 | + $this->assertNoViolation(); |
| 44 | + |
| 45 | + if (PasswordStrength::STRENGTH_VERY_STRONG === $expectedStrength) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + $this->validator->validate($value, new PasswordStrength(minScore: $expectedStrength + 1)); |
| 50 | + |
| 51 | + $this->buildViolation('The password strength is too low. Please use a stronger password.') |
| 52 | + ->setCode(PasswordStrength::PASSWORD_STRENGTH_ERROR) |
| 53 | + ->setParameter('{{ strength }}', $expectedStrength) |
| 54 | + ->assertRaised(); |
| 55 | + } |
| 56 | + |
| 57 | + public static function getValidValues(): iterable |
| 58 | + { |
| 59 | + yield ['az34tyu', PasswordStrength::STRENGTH_WEAK]; |
| 60 | + yield ['A med1um one', PasswordStrength::STRENGTH_MEDIUM]; |
| 61 | + yield ['a str0ng3r one doh', PasswordStrength::STRENGTH_STRONG]; |
| 62 | + yield [new StringableValue('HeloW0rld'), PasswordStrength::STRENGTH_WEAK]; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @dataProvider provideInvalidConstraints |
| 67 | + */ |
| 68 | + public function testThePasswordIsWeak(PasswordStrength $constraint, string $password, string $expectedMessage, string $expectedCode, string $strength) |
| 69 | + { |
| 70 | + $this->validator->validate($password, $constraint); |
| 71 | + |
| 72 | + $this->buildViolation($expectedMessage) |
| 73 | + ->setCode($expectedCode) |
| 74 | + ->setParameters([ |
| 75 | + '{{ strength }}' => $strength, |
| 76 | + ]) |
| 77 | + ->assertRaised(); |
| 78 | + } |
| 79 | + |
| 80 | + public static function provideInvalidConstraints(): iterable |
| 81 | + { |
| 82 | + yield [ |
| 83 | + new PasswordStrength(), |
| 84 | + 'password', |
| 85 | + 'The password strength is too low. Please use a stronger password.', |
| 86 | + PasswordStrength::PASSWORD_STRENGTH_ERROR, |
| 87 | + (string) PasswordStrength::STRENGTH_WEAK, |
| 88 | + ]; |
| 89 | + yield [ |
| 90 | + new PasswordStrength(minScore: PasswordStrength::STRENGTH_VERY_STRONG), |
| 91 | + 'Good password?', |
| 92 | + 'The password strength is too low. Please use a stronger password.', |
| 93 | + PasswordStrength::PASSWORD_STRENGTH_ERROR, |
| 94 | + (string) PasswordStrength::STRENGTH_MEDIUM, |
| 95 | + ]; |
| 96 | + yield [ |
| 97 | + new PasswordStrength(message: 'This password should be strong.'), |
| 98 | + 'password', |
| 99 | + 'This password should be strong.', |
| 100 | + PasswordStrength::PASSWORD_STRENGTH_ERROR, |
| 101 | + (string) PasswordStrength::STRENGTH_WEAK, |
| 102 | + ]; |
| 103 | + } |
| 104 | +} |
0 commit comments