|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright © OXID eSales AG. All rights reserved. |
| 5 | + * See LICENSE file for license details. |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace OxidEsales\SecurityModule\Tests\Unit\Authentication\TwoFactorAuth\Factory; |
| 11 | + |
| 12 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Factory\TwoFAServiceFactory; |
| 13 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Factory\TwoFAServiceFactoryInterface; |
| 14 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\ModuleSettingsServiceInterface; |
| 15 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Exception\AuthenticationTypeNotFoundException; |
| 16 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\TwoFAServiceInterface; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | + |
| 19 | +class TwoFAServiceFactoryTest extends TestCase |
| 20 | +{ |
| 21 | + public function testCreateReturnsServiceMatchingConfiguredType(): void |
| 22 | + { |
| 23 | + $otpServiceStub = $this->createStub(TwoFAServiceInterface::class); |
| 24 | + $totpServiceStub = $this->createStub(TwoFAServiceInterface::class); |
| 25 | + |
| 26 | + $settingsStub = $this->createStub(ModuleSettingsServiceInterface::class); |
| 27 | + $settingsStub->method('getTwoFactorAuthType')->willReturn('otp'); |
| 28 | + |
| 29 | + $sut = $this->getSut( |
| 30 | + settings: $settingsStub, |
| 31 | + implementations: ['otp' => $otpServiceStub, 'totp' => $totpServiceStub], |
| 32 | + ); |
| 33 | + |
| 34 | + $this->assertSame($otpServiceStub, $sut->create()); |
| 35 | + } |
| 36 | + |
| 37 | + public function testCreateThrowsForUnknownType(): void |
| 38 | + { |
| 39 | + $settingsStub = $this->createStub(ModuleSettingsServiceInterface::class); |
| 40 | + $settingsStub->method('getTwoFactorAuthType')->willReturn('unknown'); |
| 41 | + |
| 42 | + $sut = $this->getSut( |
| 43 | + settings: $settingsStub, |
| 44 | + ); |
| 45 | + |
| 46 | + $this->expectException(AuthenticationTypeNotFoundException::class); |
| 47 | + $sut->create(); |
| 48 | + } |
| 49 | + |
| 50 | + private function getSut( |
| 51 | + ModuleSettingsServiceInterface $settings = null, |
| 52 | + array $implementations = [], |
| 53 | + ): TwoFAServiceFactoryInterface { |
| 54 | + return new TwoFAServiceFactory( |
| 55 | + settings: $settings ?? $this->createStub(ModuleSettingsServiceInterface::class), |
| 56 | + implementations: $implementations, |
| 57 | + ); |
| 58 | + } |
| 59 | +} |
0 commit comments