PasswordStrength example


    protected function createValidator(): PasswordStrengthValidator
    {
        return new PasswordStrengthValidator();
    }

    /** * @dataProvider getValidValues */
    public function testValidValues(string $value, int $expectedStrength)
    {
        $this->validator->validate($valuenew PasswordStrength(minScore: $expectedStrength));

        $this->assertNoViolation();

        if (PasswordStrength::STRENGTH_VERY_STRONG === $expectedStrength) {
            return;
        }

        $this->validator->validate($valuenew PasswordStrength(minScore: $expectedStrength + 1));

        $this->buildViolation('The password strength is too low. Please use a stronger password.')
            ->setCode(PasswordStrength::PASSWORD_STRENGTH_ERROR)
            
namespace Symfony\Component\Validator\Tests\Constraints;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\PasswordStrength;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;

class PasswordStrengthTest extends TestCase
{
    public function testConstructor()
    {
        $constraint = new PasswordStrength();
        $this->assertSame(2, $constraint->minScore);
    }

    public function testConstructorWithParameters()
    {
        $constraint = new PasswordStrength(minScore: PasswordStrength::STRENGTH_STRONG, message: 'This password should be strong.');

        $this->assertSame(PasswordStrength::STRENGTH_STRONG, $constraint->minScore);
        $this->assertSame('This password should be strong.', $constraint->message);
    }

    
Home | Imprint | This part of the site doesn't use cookies.