|
10 | 10 | namespace OxidEsales\SecurityModule\Tests\Unit\Authentication\TwoFactorAuth\Transput; |
11 | 11 |
|
12 | 12 | use OxidEsales\EshopCommunity\Internal\Framework\Request\RequestInterface; |
13 | | -use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Exception\InvalidCodeException; |
| 13 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Exception\MalformedRequestException; |
14 | 14 | use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Transput\AuthCodeRequest; |
15 | 15 | use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Transput\AuthCodeRequestInterface; |
| 16 | +use PHPUnit\Framework\Attributes\DataProvider; |
16 | 17 | use PHPUnit\Framework\TestCase; |
17 | 18 |
|
18 | 19 | class AuthCodeRequestTest extends TestCase |
19 | 20 | { |
20 | | - public function testGetCodeReturnsValidCode(): void |
| 21 | + public static function validCodeDataProvider(): \Generator |
21 | 22 | { |
22 | 23 | $code = uniqid(); |
23 | | - |
24 | | - $requestStub = $this->createStub(RequestInterface::class); |
25 | | - $requestStub->method('get')->with('auth_code')->willReturn($code); |
26 | | - |
27 | | - $sut = $this->getSut( |
28 | | - request: $requestStub, |
29 | | - ); |
30 | | - |
31 | | - $this->assertSame($code, $sut->getCode()); |
32 | | - } |
33 | | - |
34 | | - public function testGetCodeThrowsExceptionWhenCodeIsEmpty(): void |
35 | | - { |
36 | | - $requestStub = $this->createStub(RequestInterface::class); |
37 | | - $requestStub->method('get')->with('auth_code')->willReturn(''); |
38 | | - |
39 | | - $sut = $this->getSut( |
40 | | - request: $requestStub, |
41 | | - ); |
42 | | - |
43 | | - $this->expectException(InvalidCodeException::class); |
44 | | - |
45 | | - $sut->getCode(); |
| 24 | + yield 'string code' => ['input' => $code, 'expected' => $code]; |
| 25 | + yield 'empty string' => ['input' => '', 'expected' => '']; |
| 26 | + yield 'integer code' => ['input' => 123456, 'expected' => '123456']; |
46 | 27 | } |
47 | 28 |
|
48 | | - public function testGetCodeThrowsExceptionWhenCodeIsNull(): void |
| 29 | + #[DataProvider('validCodeDataProvider')] |
| 30 | + public function testGetCodeReturnsCode(mixed $input, string $expected): void |
49 | 31 | { |
50 | 32 | $requestStub = $this->createStub(RequestInterface::class); |
51 | | - $requestStub->method('get')->with('auth_code')->willReturn(null); |
52 | | - |
53 | | - $sut = $this->getSut( |
54 | | - request: $requestStub, |
55 | | - ); |
| 33 | + $requestStub->method('get')->with('auth_code')->willReturn($input); |
56 | 34 |
|
57 | | - $this->expectException(InvalidCodeException::class); |
| 35 | + $sut = $this->getSut(request: $requestStub); |
58 | 36 |
|
59 | | - $sut->getCode(); |
| 37 | + $this->assertSame($expected, $sut->getCode()); |
60 | 38 | } |
61 | 39 |
|
62 | | - public function testGetCodeThrowsExceptionWhenCodeIsArray(): void |
| 40 | + public static function invalidCodeDataProvider(): \Generator |
63 | 41 | { |
64 | | - $requestStub = $this->createStub(RequestInterface::class); |
65 | | - $requestStub->method('get')->with('auth_code')->willReturn(['code']); |
66 | | - |
67 | | - $sut = $this->getSut( |
68 | | - request: $requestStub, |
69 | | - ); |
70 | | - |
71 | | - $this->expectException(InvalidCodeException::class); |
72 | | - |
73 | | - $sut->getCode(); |
| 42 | + yield 'null' => [null]; |
| 43 | + yield 'array' => [['code']]; |
74 | 44 | } |
75 | 45 |
|
76 | | - public function testGetCodeThrowsExceptionWhenCodeIsInteger(): void |
| 46 | + #[DataProvider('invalidCodeDataProvider')] |
| 47 | + public function testGetCodeThrowsExceptionForInvalidInput(mixed $value): void |
77 | 48 | { |
78 | 49 | $requestStub = $this->createStub(RequestInterface::class); |
79 | | - $requestStub->method('get')->with('auth_code')->willReturn(123456); |
| 50 | + $requestStub->method('get')->with('auth_code')->willReturn($value); |
80 | 51 |
|
81 | | - $sut = $this->getSut( |
82 | | - request: $requestStub, |
83 | | - ); |
| 52 | + $sut = $this->getSut(request: $requestStub); |
84 | 53 |
|
85 | | - $this->expectException(InvalidCodeException::class); |
| 54 | + $this->expectException(MalformedRequestException::class); |
86 | 55 |
|
87 | 56 | $sut->getCode(); |
88 | 57 | } |
|
0 commit comments