. */ namespace App\Tests\Validator\Constraints; use App\Entity\UserSystem\User; use App\Validator\Constraints\ValidGoogleAuthCode; use App\Validator\Constraints\ValidGoogleAuthCodeValidator; use PHPUnit\Framework\TestCase; use Scheb\TwoFactorBundle\Model\Google\TwoFactorInterface; use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\GoogleAuthenticatorInterface; use Symfony\Bundle\SecurityBundle\Security; use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; class ValidGoogleAuthCodeValidatorTest extends ConstraintValidatorTestCase { protected function createValidator() { $googleAuth = new class implements GoogleAuthenticatorInterface { public function checkCode(TwoFactorInterface $user, string $code): bool { if ($code === '123456') { return true; } return false; } public function getQRContent(TwoFactorInterface $user): string { return 'not_needed'; } public function generateSecret(): string { return 'not_needed'; } }; $security = new class extends Security { public function __construct() { //Leave empty } public function getUser(): ?\Symfony\Component\Security\Core\User\UserInterface { return new class implements TwoFactorInterface, UserInterface { public function isGoogleAuthenticatorEnabled(): bool { return true; } public function getGoogleAuthenticatorUsername(): string { return "test"; } public function getGoogleAuthenticatorSecret(): ?string { return "not_needed"; } public function getRoles(): array { return []; } public function eraseCredentials() { } public function getUserIdentifier(): string { return 'test'; } }; } }; return new ValidGoogleAuthCodeValidator($googleAuth, $security); } public function testAllowNull(): void { $this->validator->validate(null, new ValidGoogleAuthCode()); $this->assertNoViolation(); } public function testAllowEmpty(): void { $this->validator->validate('', new ValidGoogleAuthCode()); $this->assertNoViolation(); } public function testValidCode(): void { $this->validator->validate('123456', new ValidGoogleAuthCode()); $this->assertNoViolation(); } public function testInvalidCode(): void { $this->validator->validate('111111', new ValidGoogleAuthCode()); $this->buildViolation('validator.google_code.wrong_code') ->assertRaised(); } public function testCheckNumerical(): void { $this->validator->validate('123456a', new ValidGoogleAuthCode()); $this->buildViolation('validator.google_code.only_digits_allowed') ->assertRaised(); } public function testCheckLength(): void { $this->validator->validate('12345', new ValidGoogleAuthCode()); $this->buildViolation('validator.google_code.wrong_digit_count') ->assertRaised(); } }