/
UserProviderTest.php
164 lines (142 loc) · 5.11 KB
/
UserProviderTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
declare(strict_types=1);
namespace App\Tests\Security\Core;
use App\Entity\User;
use App\Repository\UserRepository;
use App\Security\Core\UserProvider;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Uid\Uuid;
final class UserProviderTest extends TestCase
{
private ManagerRegistry|MockObject $registryMock;
private MockObject|ObjectManager $managerMock;
private MockObject|UserRepository $repositoryMock;
private MockObject|User $userMock;
private UserProvider $provider;
protected function setUp(): void
{
$this->registryMock = $this->createMock(ManagerRegistry::class);
$this->managerMock = $this->createMock(ObjectManager::class);
$this->repositoryMock = $this->createMock(UserRepository::class);
$this->userMock = $this->createMock(User::class);
$this->provider = new UserProvider($this->registryMock, $this->repositoryMock);
}
#[Test]
public function itDoesNotSupportAnInvalidClass(): void
{
$this->assertFalse($this->provider->supportsClass(\stdClass::class));
}
#[Test]
public function itSupportsAValidClass(): void
{
$this->assertTrue($this->provider->supportsClass(User::class));
}
#[Test]
public function itCannotRefreshAnInvalidObject(): void
{
$this->expectException(UnsupportedUserException::class);
$objectMock = $this->createMock(UserInterface::class);
$this->registryMock
->expects($this->once())
->method('getManagerForClass')
->with($objectMock::class)
->willReturn(null)
;
$this->provider->refreshUser($objectMock);
}
#[Test]
public function itRefreshesAValidObject(): void
{
$objectMock = $this->createMock(UserInterface::class);
$this->registryMock
->expects($this->once())
->method('getManagerForClass')
->with($objectMock::class)
->willReturn($this->managerMock)
;
$this->managerMock
->expects($this->once())
->method('refresh')
->with($objectMock)
->willReturn($this->managerMock)
;
$this->assertSame($objectMock, $this->provider->refreshUser($objectMock));
}
#[Test]
#[DataProvider(methodName: 'getInvalidAttributes')]
public function itCannotLoadUserIfAttributeIsMissing(array $attributes): void
{
$this->expectException(UnsupportedUserException::class);
$this->repositoryMock
->expects($this->once())
->method('findOneBy')
->with(['email' => 'john.doe@example.com'])
->willReturn($this->userMock)
;
$this->repositoryMock->expects($this->never())->method('save');
$this->provider->loadUserByIdentifier('john.doe@example.com', $attributes);
}
public static function getInvalidAttributes(): iterable
{
yield 'missing sub' => [[]];
yield 'missing given_name' => [[
'sub' => 'ba86c94b-efeb-4452-a0b4-93ed3c889156',
]];
yield 'missing family_name' => [[
'sub' => 'ba86c94b-efeb-4452-a0b4-93ed3c889156',
'given_name' => 'John',
]];
}
#[Test]
public function itLoadsUserFromAttributes(): void
{
$this->repositoryMock
->expects($this->once())
->method('findOneBy')
->with(['email' => 'john.doe@example.com'])
->willReturn($this->userMock)
;
$this->repositoryMock
->expects($this->once())
->method('save')
->with($this->userMock)
;
$this->assertSame($this->userMock, $this->provider->loadUserByIdentifier('john.doe@example.com', [
'sub' => 'ba86c94b-efeb-4452-a0b4-93ed3c889156',
'given_name' => 'John',
'family_name' => 'DOE',
]));
}
#[Test]
public function itCreatesAUserFromAttributes(): void
{
$expectedUser = new User();
$expectedUser->firstName = 'John';
$expectedUser->lastName = 'DOE';
$expectedUser->sub = Uuid::fromString('ba86c94b-efeb-4452-a0b4-93ed3c889156');
$expectedUser->email = 'john.doe@example.com';
$this->repositoryMock
->expects($this->once())
->method('findOneBy')
->with(['email' => 'john.doe@example.com'])
->willReturn(null)
;
$this->repositoryMock
->expects($this->once())
->method('save')
->with($expectedUser)
;
$this->assertEquals($expectedUser, $this->provider->loadUserByIdentifier('john.doe@example.com', [
'sub' => 'ba86c94b-efeb-4452-a0b4-93ed3c889156',
'given_name' => 'John',
'family_name' => 'DOE',
]));
}
}