Skip to content

Commit

Permalink
[Tests] Reduced code duplication in custom providers test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
alongosz committed Jul 5, 2024
1 parent 5d23ed7 commit f3108e5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 92 deletions.
46 changes: 46 additions & 0 deletions tests/lib/MVC/Symfony/Security/User/BaseProviderTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -73,6 +77,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;
Expand All @@ -98,6 +130,20 @@ 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));
}

protected function buildUserValueObjectStub(int $userId): User
{
return new User(
Expand Down
47 changes: 0 additions & 47 deletions tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
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\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Core\User\UserInterface as SymfonyUserInterface;

/**
* @covers \Ibexa\Core\MVC\Symfony\Security\User\EmailProvider
Expand Down Expand Up @@ -58,47 +54,4 @@ public function testLoadUserByUsername(): void
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
{
$this->expectException(UsernameNotFoundException::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);
}
}
45 changes: 0 additions & 45 deletions tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
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
Expand Down Expand Up @@ -57,46 +54,4 @@ public function testLoadUserByUsername(): void
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);
}
}

0 comments on commit f3108e5

Please sign in to comment.