|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Tests\Service; |
| 6 | + |
| 7 | +use App\Entity\User; |
| 8 | +use App\Enum\UserTypeEnum; |
| 9 | +use App\Exceptions\BadRequestException; |
| 10 | +use App\Exceptions\NotFoundException; |
| 11 | +use App\Repository\UserActivationCodeRepository; |
| 12 | +use App\Repository\UserRepository; |
| 13 | +use App\Repository\UserRoleTenantRepository; |
| 14 | +use App\Service\UserService; |
| 15 | +use Doctrine\ORM\EntityManagerInterface; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Symfony\Component\Security\Core\Security; |
| 18 | + |
| 19 | +class UserServiceTest extends TestCase |
| 20 | +{ |
| 21 | + public function testActivateExternalUserThrowsForNonExternalUser(): void |
| 22 | + { |
| 23 | + $user = new User(); |
| 24 | + $user->setUserType(UserTypeEnum::OIDC_INTERNAL); |
| 25 | + |
| 26 | + $userService = $this->createUserService($user); |
| 27 | + |
| 28 | + $this->expectException(BadRequestException::class); |
| 29 | + $this->expectExceptionMessage('User is not of external type.'); |
| 30 | + |
| 31 | + $userService->activateExternalUser('SOMECODE'); |
| 32 | + } |
| 33 | + |
| 34 | + public function testActivateExternalUserThrowsForUnknownCode(): void |
| 35 | + { |
| 36 | + $user = new User(); |
| 37 | + $user->setUserType(UserTypeEnum::OIDC_EXTERNAL); |
| 38 | + |
| 39 | + $userService = $this->createUserService($user); |
| 40 | + |
| 41 | + $this->expectException(NotFoundException::class); |
| 42 | + |
| 43 | + $userService->activateExternalUser('UNKNOWNCODE'); |
| 44 | + } |
| 45 | + |
| 46 | + private function createUserService(User $user): UserService |
| 47 | + { |
| 48 | + $security = $this->createMock(Security::class); |
| 49 | + $security->method('getUser')->willReturn($user); |
| 50 | + |
| 51 | + $activationCodeRepository = $this->createMock(UserActivationCodeRepository::class); |
| 52 | + $activationCodeRepository->method('findOneBy')->willReturn(null); |
| 53 | + |
| 54 | + return new UserService( |
| 55 | + $activationCodeRepository, |
| 56 | + $this->createMock(EntityManagerInterface::class), |
| 57 | + $security, |
| 58 | + 'hash-salt', |
| 59 | + $this->createMock(UserRoleTenantRepository::class), |
| 60 | + $this->createMock(UserRepository::class), |
| 61 | + 'P2D', |
| 62 | + ); |
| 63 | + } |
| 64 | +} |
0 commit comments