. */ declare(strict_types=1); namespace App\Tests\Services\UserSystem\TFA; use App\Services\UserSystem\TFA\BackupCodeGenerator; use PHPUnit\Framework\TestCase; use RuntimeException; class BackupCodeGeneratorTest extends TestCase { /** * Test if an exception is thrown if you are using a too high code length. */ public function testLengthUpperLimit(): void { $this->expectException(RuntimeException::class); new BackupCodeGenerator(33, 10); } /** * Test if an exception is thrown if you are using a too high code length. */ public function testLengthLowerLimit(): void { $this->expectException(RuntimeException::class); new BackupCodeGenerator(4, 10); } public function codeLengthDataProvider(): \Iterator { yield [6]; yield [8]; yield [10]; yield [16]; } /** * @dataProvider codeLengthDataProvider */ public function testGenerateSingleCode(int $code_length): void { $generator = new BackupCodeGenerator($code_length, 10); $this->assertMatchesRegularExpression("/^([a-f0-9]){{$code_length}}\$/", $generator->generateSingleCode()); } public function codeCountDataProvider(): \Iterator { yield [2]; yield [8]; yield [10]; } /** * @dataProvider codeCountDataProvider */ public function testGenerateCodeSet(int $code_count): void { $generator = new BackupCodeGenerator(8, $code_count); $this->assertCount($code_count, $generator->generateCodeSet()); } }