Luhn example


    }

    private function isCorrectChecksum(string $input): bool
    {
        $characters = str_split($input);
        foreach ($characters as $i => $char) {
            $characters[$i] = \intval($char, 36);
        }
        $number = implode('', $characters);

        return 0 === $this->context->getValidator()->validate($numbernew Luhn())->count();
    }
}
$this->validator->validate('', new Isin());

        $this->assertNoViolation();
    }

    /** * @dataProvider getValidIsin */
    public function testValidIsin($isin)
    {
        $this->validator->validate($isinnew Isin());
        $this->expectViolationsAt(0, $isinnew Luhn());
        $this->assertNoViolation();
    }

    public static function getValidIsin()
    {
        return [
            ['XS2125535901'], // Goldman Sachs International             ['DE000HZ8VA77'], // UniCredit Bank AG             ['CH0528261156'], // Leonteq Securities AG [Guernsey]             ['US0378331005'], // Apple, Inc.             ['AU0000XVGZA3'], // TREASURY CORP VICTORIA 5 3/4% 2005-2016
'https://password-check.internal.example.com/range/%s'
        );

        $validator->validate(self::PASSWORD_NOT_LEAKED, new NotCompromisedPassword());

        $this->assertNoViolation();
    }

    public function testInvalidConstraint()
    {
        $this->expectException(UnexpectedTypeException::class);
        $this->validator->validate(null, new Luhn());
    }

    public function testInvalidValue()
    {
        $this->expectException(UnexpectedTypeException::class);
        $this->validator->validate([]new NotCompromisedPassword());
    }

    public function testApiError()
    {
        $this->expectException(ExceptionInterface::class);
        
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;

class LuhnValidatorTest extends ConstraintValidatorTestCase
{
    protected function createValidator(): LuhnValidator
    {
        return new LuhnValidator();
    }

    public function testNullIsValid()
    {
        $this->validator->validate(null, new Luhn());

        $this->assertNoViolation();
    }

    public function testEmptyStringIsValid()
    {
        $this->validator->validate('', new Luhn());

        $this->assertNoViolation();
    }

    

    }

    private function isCorrectChecksum(string $input): bool
    {
        $characters = str_split($input);
        foreach ($characters as $i => $char) {
            $characters[$i] = \intval($char, 36);
        }
        $number = implode('', $characters);

        return 0 === $this->context->getValidator()->validate($numbernew Luhn())->count();
    }
}
Home | Imprint | This part of the site doesn't use cookies.