From 2d7e7987be868432a2049c0ffdc57f6ca6c49ba9 Mon Sep 17 00:00:00 2001 From: Andrew Longosz Date: Fri, 5 Jul 2024 10:14:47 +0200 Subject: [PATCH] [Tests] Reduced code duplication in custom providers test cases --- .../Security/User/BaseProviderTestCase.php | 80 +++++++++++++++++++ .../Security/User/EmailProviderTest.php | 79 +----------------- .../Security/User/UsernameProviderTest.php | 77 +----------------- 3 files changed, 88 insertions(+), 148 deletions(-) diff --git a/tests/lib/MVC/Symfony/Security/User/BaseProviderTestCase.php b/tests/lib/MVC/Symfony/Security/User/BaseProviderTestCase.php index ed3e703f76..7d6b541565 100644 --- a/tests/lib/MVC/Symfony/Security/User/BaseProviderTestCase.php +++ b/tests/lib/MVC/Symfony/Security/User/BaseProviderTestCase.php @@ -12,14 +12,18 @@ use Ibexa\Contracts\Core\Repository\UserService; use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo; use Ibexa\Contracts\Core\Repository\Values\User\User as APIUser; +use Ibexa\Core\Base\Exceptions\NotFoundException; use Ibexa\Core\MVC\Symfony\Security\User as MVCUser; use Ibexa\Core\MVC\Symfony\Security\User\BaseProvider; use Ibexa\Core\MVC\Symfony\Security\UserInterface; use Ibexa\Core\Repository\Values\Content\Content; use Ibexa\Core\Repository\Values\Content\VersionInfo; use Ibexa\Core\Repository\Values\User\User; +use Ibexa\Core\Repository\Values\User\UserReference; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; +use Symfony\Component\Security\Core\Exception\UnsupportedUserException; +use Symfony\Component\Security\Core\Exception\UserNotFoundException; use Symfony\Component\Security\Core\User\UserInterface as SymfonyUserInterface; abstract class BaseProviderTestCase extends TestCase @@ -32,6 +36,10 @@ abstract class BaseProviderTestCase extends TestCase abstract protected function buildProvider(): BaseProvider; + abstract protected function getUserIdentifier(): string; + + abstract protected function getUserServiceMethod(): string; + protected function setUp(): void { parent::setUp(); @@ -73,6 +81,34 @@ public function testLoadUserByAPIUser(): void self::assertSame(['ROLE_USER'], $user->getRoles()); } + public function testRefreshUserNotFound(): void + { + $userId = 123; + $apiUser = $this->buildUserValueObjectStub($userId); + $user = $this->createMock(UserInterface::class); + $user + ->expects(self::once()) + ->method('getAPIUser') + ->willReturn($apiUser); + + $this->userService + ->expects(self::once()) + ->method('loadUser') + ->with($userId) + ->willThrowException(new NotFoundException('user', 'foo')); + + $this->expectException(UserNotFoundException::class); + $this->userProvider->refreshUser($user); + } + + public function testRefreshUserNotSupported(): void + { + $user = $this->createMock(SymfonyUserInterface::class); + + $this->expectException(UnsupportedUserException::class); + $this->userProvider->refreshUser($user); + } + protected function createUserWrapperMockFromAPIUser(User $apiUser, int $userId): UserInterface & MockObject { $refreshedAPIUser = clone $apiUser; @@ -98,6 +134,50 @@ protected function createUserWrapperMockFromAPIUser(User $apiUser, int $userId): return $user; } + public function testRefreshUser(): void + { + $userId = 123; + $apiUser = $this->buildUserValueObjectStub($userId); + $user = $this->createUserWrapperMockFromAPIUser($apiUser, $userId); + + $this->permissionResolver + ->expects(self::once()) + ->method('setCurrentUserReference') + ->with(new UserReference($apiUser->getUserId())); + + self::assertSame($user, $this->userProvider->refreshUser($user)); + } + + public function testLoadUserByUsername(): void + { + $username = $this->getUserIdentifier(); + $apiUser = $this->createMock(APIUser::class); + + $this->userService + ->expects(self::once()) + ->method($this->getUserServiceMethod()) + ->with($username) + ->willReturn($apiUser); + + $user = $this->userProvider->loadUserByIdentifier($username); + self::assertInstanceOf(UserInterface::class, $user); + self::assertSame($apiUser, $user->getAPIUser()); + self::assertSame(['ROLE_USER'], $user->getRoles()); + } + + public function testLoadUserByUsernameUserNotFound(): void + { + $username = $this->getUserIdentifier(); + $this->userService + ->expects(self::once()) + ->method($this->getUserServiceMethod()) + ->with($username) + ->willThrowException(new NotFoundException('user', $username)); + + $this->expectException(UserNotFoundException::class); + $this->userProvider->loadUserByIdentifier($username); + } + protected function buildUserValueObjectStub(int $userId): User { return new User( diff --git a/tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php b/tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php index 3f0847524f..261609b26e 100644 --- a/tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php +++ b/tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php @@ -8,16 +8,8 @@ namespace Ibexa\Tests\Core\MVC\Symfony\Security\User; -use Ibexa\Contracts\Core\Repository\Values\User\User as APIUser; -use Ibexa\Core\Base\Exceptions\NotFoundException; use Ibexa\Core\MVC\Symfony\Security\User\BaseProvider; use Ibexa\Core\MVC\Symfony\Security\User\EmailProvider; -use Ibexa\Core\MVC\Symfony\Security\UserInterface; -use Ibexa\Core\Repository\Values\User\UserReference; -use Symfony\Component\Security\Core\Exception\UnsupportedUserException; -use Symfony\Component\Security\Core\Exception\UserNotFoundException; -use Symfony\Component\Security\Core\Exception\UserNotFoundException; -use Symfony\Component\Security\Core\User\UserInterface as SymfonyUserInterface; /** * @covers \Ibexa\Core\MVC\Symfony\Security\User\EmailProvider @@ -29,76 +21,13 @@ protected function buildProvider(): BaseProvider return new EmailProvider($this->userService, $this->permissionResolver); } - public function testLoadUserByUsernameUserNotFound(): void + protected function getUserIdentifier(): string { - $username = 'foobar@example.org'; - $this->userService - ->expects(self::once()) - ->method('loadUserByEmail') - ->with($username) - ->willThrowException(new NotFoundException('user', $username)); - - $this->expectException(UserNotFoundException::class); - $this->userProvider->loadUserByIdentifier($username); - } - - public function testLoadUserByUsername(): void - { - $username = 'foobar@example.org'; - $apiUser = $this->createMock(APIUser::class); - - $this->userService - ->expects(self::once()) - ->method('loadUserByEmail') - ->with($username) - ->willReturn($apiUser); - - $user = $this->userProvider->loadUserByIdentifier($username); - self::assertInstanceOf(UserInterface::class, $user); - self::assertSame($apiUser, $user->getAPIUser()); - self::assertSame(['ROLE_USER'], $user->getRoles()); - } - - public function testRefreshUserNotSupported(): void - { - $user = $this->createMock(SymfonyUserInterface::class); - - $this->expectException(UnsupportedUserException::class); - $this->userProvider->refreshUser($user); + return 'foobar@example.org'; } - public function testRefreshUser(): void + protected function getUserServiceMethod(): string { - $userId = 123; - $apiUser = $this->buildUserValueObjectStub($userId); - $user = $this->createUserWrapperMockFromAPIUser($apiUser, $userId); - - $this->permissionResolver - ->expects(self::once()) - ->method('setCurrentUserReference') - ->with(new UserReference($apiUser->getUserId())); - - self::assertSame($user, $this->userProvider->refreshUser($user)); - } - - public function testRefreshUserNotFound(): void - { - $this->expectException(UserNotFoundException::class); - - $userId = 123; - $apiUser = $this->buildUserValueObjectStub($userId); - $user = $this->createMock(UserInterface::class); - $user - ->expects(self::once()) - ->method('getAPIUser') - ->willReturn($apiUser); - - $this->userService - ->expects(self::once()) - ->method('loadUser') - ->with($userId) - ->willThrowException(new NotFoundException('user', 'foo')); - - $this->userProvider->refreshUser($user); + return 'loadUserByEmail'; } } diff --git a/tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php b/tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php index 76ab877d7b..5886976ba4 100644 --- a/tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php +++ b/tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php @@ -8,15 +8,8 @@ namespace Ibexa\Tests\Core\MVC\Symfony\Security\User; -use Ibexa\Contracts\Core\Repository\Values\User\User as APIUser; -use Ibexa\Core\Base\Exceptions\NotFoundException; use Ibexa\Core\MVC\Symfony\Security\User\BaseProvider; use Ibexa\Core\MVC\Symfony\Security\User\UsernameProvider; -use Ibexa\Core\MVC\Symfony\Security\UserInterface; -use Ibexa\Core\Repository\Values\User\UserReference; -use Symfony\Component\Security\Core\Exception\UnsupportedUserException; -use Symfony\Component\Security\Core\Exception\UserNotFoundException; -use Symfony\Component\Security\Core\User\UserInterface as SymfonyUserInterface; /** * @covers \Ibexa\Core\MVC\Symfony\Security\User\UsernameProvider @@ -28,75 +21,13 @@ protected function buildProvider(): BaseProvider return new UsernameProvider($this->userService, $this->permissionResolver); } - public function testLoadUserByUsernameUserNotFound(): void + protected function getUserIdentifier(): string { - $username = 'foobar'; - $this->userService - ->expects(self::once()) - ->method('loadUserByLogin') - ->with($username) - ->willThrowException(new NotFoundException('user', $username)); - - $this->expectException(UserNotFoundException::class); - $this->userProvider->loadUserByIdentifier($username); + return 'foobar'; } - public function testLoadUserByUsername(): void + protected function getUserServiceMethod(): string { - $username = 'foobar'; - $apiUser = $this->createMock(APIUser::class); - - $this->userService - ->expects(self::once()) - ->method('loadUserByLogin') - ->with($username) - ->willReturn($apiUser); - - $user = $this->userProvider->loadUserByIdentifier($username); - self::assertInstanceOf(UserInterface::class, $user); - self::assertSame($apiUser, $user->getAPIUser()); - self::assertSame(['ROLE_USER'], $user->getRoles()); - } - - public function testRefreshUserNotSupported(): void - { - $user = $this->createMock(SymfonyUserInterface::class); - - $this->expectException(UnsupportedUserException::class); - $this->userProvider->refreshUser($user); - } - - public function testRefreshUser(): void - { - $userId = 123; - $apiUser = $this->buildUserValueObjectStub($userId); - $user = $this->createUserWrapperMockFromAPIUser($apiUser, $userId); - - $this->permissionResolver - ->expects(self::once()) - ->method('setCurrentUserReference') - ->with(new UserReference($apiUser->getUserId())); - - self::assertSame($user, $this->userProvider->refreshUser($user)); - } - - public function testRefreshUserNotFound(): void - { - $userId = 123; - $apiUser = $this->buildUserValueObjectStub($userId); - $user = $this->createMock(UserInterface::class); - $user - ->expects(self::once()) - ->method('getAPIUser') - ->willReturn($apiUser); - - $this->userService - ->expects(self::once()) - ->method('loadUser') - ->with($userId) - ->willThrowException(new NotFoundException('user', 'foo')); - - $this->expectException(UserNotFoundException::class); - $this->userProvider->refreshUser($user); + return 'loadUserByLogin'; } }