Skip to content

Commit 5c5f441

Browse files
committed
[Tests] Reduced code duplication in custom providers test cases
1 parent 5d23ed7 commit 5c5f441

File tree

3 files changed

+88
-148
lines changed

3 files changed

+88
-148
lines changed

tests/lib/MVC/Symfony/Security/User/BaseProviderTestCase.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@
1212
use Ibexa\Contracts\Core\Repository\UserService;
1313
use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo;
1414
use Ibexa\Contracts\Core\Repository\Values\User\User as APIUser;
15+
use Ibexa\Core\Base\Exceptions\NotFoundException;
1516
use Ibexa\Core\MVC\Symfony\Security\User as MVCUser;
1617
use Ibexa\Core\MVC\Symfony\Security\User\BaseProvider;
1718
use Ibexa\Core\MVC\Symfony\Security\UserInterface;
1819
use Ibexa\Core\Repository\Values\Content\Content;
1920
use Ibexa\Core\Repository\Values\Content\VersionInfo;
2021
use Ibexa\Core\Repository\Values\User\User;
22+
use Ibexa\Core\Repository\Values\User\UserReference;
2123
use PHPUnit\Framework\MockObject\MockObject;
2224
use PHPUnit\Framework\TestCase;
25+
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
26+
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
2327
use Symfony\Component\Security\Core\User\UserInterface as SymfonyUserInterface;
2428

2529
abstract class BaseProviderTestCase extends TestCase
@@ -32,6 +36,10 @@ abstract class BaseProviderTestCase extends TestCase
3236

3337
abstract protected function buildProvider(): BaseProvider;
3438

39+
abstract protected function getUserIdentifier(): string;
40+
41+
abstract protected function getUserServiceMethod(): string;
42+
3543
protected function setUp(): void
3644
{
3745
parent::setUp();
@@ -73,6 +81,34 @@ public function testLoadUserByAPIUser(): void
7381
self::assertSame(['ROLE_USER'], $user->getRoles());
7482
}
7583

84+
public function testRefreshUserNotFound(): void
85+
{
86+
$userId = 123;
87+
$apiUser = $this->buildUserValueObjectStub($userId);
88+
$user = $this->createMock(UserInterface::class);
89+
$user
90+
->expects(self::once())
91+
->method('getAPIUser')
92+
->willReturn($apiUser);
93+
94+
$this->userService
95+
->expects(self::once())
96+
->method('loadUser')
97+
->with($userId)
98+
->willThrowException(new NotFoundException('user', 'foo'));
99+
100+
$this->expectException(UserNotFoundException::class);
101+
$this->userProvider->refreshUser($user);
102+
}
103+
104+
public function testRefreshUserNotSupported(): void
105+
{
106+
$user = $this->createMock(SymfonyUserInterface::class);
107+
108+
$this->expectException(UnsupportedUserException::class);
109+
$this->userProvider->refreshUser($user);
110+
}
111+
76112
protected function createUserWrapperMockFromAPIUser(User $apiUser, int $userId): UserInterface & MockObject
77113
{
78114
$refreshedAPIUser = clone $apiUser;
@@ -98,6 +134,50 @@ protected function createUserWrapperMockFromAPIUser(User $apiUser, int $userId):
98134
return $user;
99135
}
100136

137+
public function testRefreshUser(): void
138+
{
139+
$userId = 123;
140+
$apiUser = $this->buildUserValueObjectStub($userId);
141+
$user = $this->createUserWrapperMockFromAPIUser($apiUser, $userId);
142+
143+
$this->permissionResolver
144+
->expects(self::once())
145+
->method('setCurrentUserReference')
146+
->with(new UserReference($apiUser->getUserId()));
147+
148+
self::assertSame($user, $this->userProvider->refreshUser($user));
149+
}
150+
151+
public function testLoadUserByUsername(): void
152+
{
153+
$username = $this->getUserIdentifier();
154+
$apiUser = $this->createMock(APIUser::class);
155+
156+
$this->userService
157+
->expects(self::once())
158+
->method($this->getUserServiceMethod())
159+
->with($username)
160+
->willReturn($apiUser);
161+
162+
$user = $this->userProvider->loadUserByIdentifier($username);
163+
self::assertInstanceOf(UserInterface::class, $user);
164+
self::assertSame($apiUser, $user->getAPIUser());
165+
self::assertSame(['ROLE_USER'], $user->getRoles());
166+
}
167+
168+
public function testLoadUserByUsernameUserNotFound(): void
169+
{
170+
$username = $this->getUserIdentifier();
171+
$this->userService
172+
->expects(self::once())
173+
->method($this->getUserServiceMethod())
174+
->with($username)
175+
->willThrowException(new NotFoundException('user', $username));
176+
177+
$this->expectException(UserNotFoundException::class);
178+
$this->userProvider->loadUserByIdentifier($username);
179+
}
180+
101181
protected function buildUserValueObjectStub(int $userId): User
102182
{
103183
return new User(

tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php

Lines changed: 4 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,8 @@
88

99
namespace Ibexa\Tests\Core\MVC\Symfony\Security\User;
1010

11-
use Ibexa\Contracts\Core\Repository\Values\User\User as APIUser;
12-
use Ibexa\Core\Base\Exceptions\NotFoundException;
1311
use Ibexa\Core\MVC\Symfony\Security\User\BaseProvider;
1412
use Ibexa\Core\MVC\Symfony\Security\User\EmailProvider;
15-
use Ibexa\Core\MVC\Symfony\Security\UserInterface;
16-
use Ibexa\Core\Repository\Values\User\UserReference;
17-
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
18-
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
19-
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
20-
use Symfony\Component\Security\Core\User\UserInterface as SymfonyUserInterface;
2113

2214
/**
2315
* @covers \Ibexa\Core\MVC\Symfony\Security\User\EmailProvider
@@ -29,76 +21,13 @@ protected function buildProvider(): BaseProvider
2921
return new EmailProvider($this->userService, $this->permissionResolver);
3022
}
3123

32-
public function testLoadUserByUsernameUserNotFound(): void
24+
protected function getUserIdentifier(): string
3325
{
34-
$username = '[email protected]';
35-
$this->userService
36-
->expects(self::once())
37-
->method('loadUserByEmail')
38-
->with($username)
39-
->willThrowException(new NotFoundException('user', $username));
40-
41-
$this->expectException(UserNotFoundException::class);
42-
$this->userProvider->loadUserByIdentifier($username);
43-
}
44-
45-
public function testLoadUserByUsername(): void
46-
{
47-
$username = '[email protected]';
48-
$apiUser = $this->createMock(APIUser::class);
49-
50-
$this->userService
51-
->expects(self::once())
52-
->method('loadUserByEmail')
53-
->with($username)
54-
->willReturn($apiUser);
55-
56-
$user = $this->userProvider->loadUserByIdentifier($username);
57-
self::assertInstanceOf(UserInterface::class, $user);
58-
self::assertSame($apiUser, $user->getAPIUser());
59-
self::assertSame(['ROLE_USER'], $user->getRoles());
60-
}
61-
62-
public function testRefreshUserNotSupported(): void
63-
{
64-
$user = $this->createMock(SymfonyUserInterface::class);
65-
66-
$this->expectException(UnsupportedUserException::class);
67-
$this->userProvider->refreshUser($user);
26+
6827
}
6928

70-
public function testRefreshUser(): void
29+
protected function getUserServiceMethod(): string
7130
{
72-
$userId = 123;
73-
$apiUser = $this->buildUserValueObjectStub($userId);
74-
$user = $this->createUserWrapperMockFromAPIUser($apiUser, $userId);
75-
76-
$this->permissionResolver
77-
->expects(self::once())
78-
->method('setCurrentUserReference')
79-
->with(new UserReference($apiUser->getUserId()));
80-
81-
self::assertSame($user, $this->userProvider->refreshUser($user));
82-
}
83-
84-
public function testRefreshUserNotFound(): void
85-
{
86-
$this->expectException(UsernameNotFoundException::class);
87-
88-
$userId = 123;
89-
$apiUser = $this->buildUserValueObjectStub($userId);
90-
$user = $this->createMock(UserInterface::class);
91-
$user
92-
->expects(self::once())
93-
->method('getAPIUser')
94-
->willReturn($apiUser);
95-
96-
$this->userService
97-
->expects(self::once())
98-
->method('loadUser')
99-
->with($userId)
100-
->willThrowException(new NotFoundException('user', 'foo'));
101-
102-
$this->userProvider->refreshUser($user);
31+
return 'loadUserByEmail';
10332
}
10433
}

tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php

Lines changed: 4 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,8 @@
88

99
namespace Ibexa\Tests\Core\MVC\Symfony\Security\User;
1010

11-
use Ibexa\Contracts\Core\Repository\Values\User\User as APIUser;
12-
use Ibexa\Core\Base\Exceptions\NotFoundException;
1311
use Ibexa\Core\MVC\Symfony\Security\User\BaseProvider;
1412
use Ibexa\Core\MVC\Symfony\Security\User\UsernameProvider;
15-
use Ibexa\Core\MVC\Symfony\Security\UserInterface;
16-
use Ibexa\Core\Repository\Values\User\UserReference;
17-
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
18-
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
19-
use Symfony\Component\Security\Core\User\UserInterface as SymfonyUserInterface;
2013

2114
/**
2215
* @covers \Ibexa\Core\MVC\Symfony\Security\User\UsernameProvider
@@ -28,75 +21,13 @@ protected function buildProvider(): BaseProvider
2821
return new UsernameProvider($this->userService, $this->permissionResolver);
2922
}
3023

31-
public function testLoadUserByUsernameUserNotFound(): void
24+
protected function getUserIdentifier(): string
3225
{
33-
$username = 'foobar';
34-
$this->userService
35-
->expects(self::once())
36-
->method('loadUserByLogin')
37-
->with($username)
38-
->willThrowException(new NotFoundException('user', $username));
39-
40-
$this->expectException(UserNotFoundException::class);
41-
$this->userProvider->loadUserByIdentifier($username);
26+
return 'foobar';
4227
}
4328

44-
public function testLoadUserByUsername(): void
29+
protected function getUserServiceMethod(): string
4530
{
46-
$username = 'foobar';
47-
$apiUser = $this->createMock(APIUser::class);
48-
49-
$this->userService
50-
->expects(self::once())
51-
->method('loadUserByLogin')
52-
->with($username)
53-
->willReturn($apiUser);
54-
55-
$user = $this->userProvider->loadUserByIdentifier($username);
56-
self::assertInstanceOf(UserInterface::class, $user);
57-
self::assertSame($apiUser, $user->getAPIUser());
58-
self::assertSame(['ROLE_USER'], $user->getRoles());
59-
}
60-
61-
public function testRefreshUserNotSupported(): void
62-
{
63-
$user = $this->createMock(SymfonyUserInterface::class);
64-
65-
$this->expectException(UnsupportedUserException::class);
66-
$this->userProvider->refreshUser($user);
67-
}
68-
69-
public function testRefreshUser(): void
70-
{
71-
$userId = 123;
72-
$apiUser = $this->buildUserValueObjectStub($userId);
73-
$user = $this->createUserWrapperMockFromAPIUser($apiUser, $userId);
74-
75-
$this->permissionResolver
76-
->expects(self::once())
77-
->method('setCurrentUserReference')
78-
->with(new UserReference($apiUser->getUserId()));
79-
80-
self::assertSame($user, $this->userProvider->refreshUser($user));
81-
}
82-
83-
public function testRefreshUserNotFound(): void
84-
{
85-
$userId = 123;
86-
$apiUser = $this->buildUserValueObjectStub($userId);
87-
$user = $this->createMock(UserInterface::class);
88-
$user
89-
->expects(self::once())
90-
->method('getAPIUser')
91-
->willReturn($apiUser);
92-
93-
$this->userService
94-
->expects(self::once())
95-
->method('loadUser')
96-
->with($userId)
97-
->willThrowException(new NotFoundException('user', 'foo'));
98-
99-
$this->expectException(UserNotFoundException::class);
100-
$this->userProvider->refreshUser($user);
31+
return 'loadUserByLogin';
10132
}
10233
}

0 commit comments

Comments
 (0)