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 9187cc0 commit 2d7e798
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 148 deletions.
80 changes: 80 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 All @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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(
Expand Down
79 changes: 4 additions & 75 deletions tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,76 +21,13 @@ protected function buildProvider(): BaseProvider
return new EmailProvider($this->userService, $this->permissionResolver);
}

public function testLoadUserByUsernameUserNotFound(): void
protected function getUserIdentifier(): string
{
$username = '[email protected]';
$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 = '[email protected]';
$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 '[email protected]';
}

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';
}
}
77 changes: 4 additions & 73 deletions tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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';
}
}

0 comments on commit 2d7e798

Please sign in to comment.