1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Admin\Orm\DataMappers; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Admin\Domain\Entities\Token; |
8
|
|
|
use AbterPhp\Admin\TestCase\Orm\DataMapperTestCase; |
9
|
|
|
use AbterPhp\Framework\Domain\Entities\IStringerEntity; |
10
|
|
|
use AbterPhp\Framework\TestDouble\Database\MockStatementFactory; |
11
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
12
|
|
|
|
13
|
|
|
class TokenSqlDataMapperTest extends DataMapperTestCase |
14
|
|
|
{ |
15
|
|
|
/** @var TokenSqlDataMapper */ |
16
|
|
|
protected $sut; |
17
|
|
|
|
18
|
|
|
public function setUp(): void |
19
|
|
|
{ |
20
|
|
|
parent::setUp(); |
21
|
|
|
|
22
|
|
|
$this->sut = new TokenSqlDataMapper($this->readConnectionMock, $this->writeConnectionMock); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testAdd() |
26
|
|
|
{ |
27
|
|
|
$nextId = '9b6ae58b-1aff-4344-a2ae-cda43a40674e'; |
28
|
|
|
$apiClientId = '33a9ef7e-3d84-4bd0-9b38-59b5cb7d5245'; |
29
|
|
|
$expiresAt = new \DateTimeImmutable(); |
30
|
|
|
$revokedAt = null; |
31
|
|
|
|
32
|
|
|
$sql0 = 'INSERT INTO tokens (id, api_client_id, expires_at, revoked_at) VALUES (?, ?, ?, ?)'; // phpcs:ignore |
33
|
|
|
$values = [ |
34
|
|
|
[$nextId, \PDO::PARAM_STR], |
35
|
|
|
[$apiClientId, \PDO::PARAM_STR], |
36
|
|
|
[$expiresAt, \PDO::PARAM_STR], |
37
|
|
|
[$revokedAt, \PDO::PARAM_NULL], |
38
|
|
|
]; |
39
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values); |
40
|
|
|
|
41
|
|
|
$this->writeConnectionMock |
42
|
|
|
->expects($this->once()) |
43
|
|
|
->method('prepare') |
44
|
|
|
->with($sql0) |
45
|
|
|
->willReturn($statement0); |
46
|
|
|
|
47
|
|
|
$entity = new Token($nextId, $apiClientId, $expiresAt, $revokedAt); |
48
|
|
|
|
49
|
|
|
$this->sut->add($entity); |
50
|
|
|
|
51
|
|
|
$this->assertSame($nextId, $entity->getId()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testAddWithRevokedAt() |
55
|
|
|
{ |
56
|
|
|
$nextId = '9b6ae58b-1aff-4344-a2ae-cda43a40674e'; |
57
|
|
|
$apiClientId = '33a9ef7e-3d84-4bd0-9b38-59b5cb7d5245'; |
58
|
|
|
$expiresAt = new \DateTimeImmutable(); |
59
|
|
|
$revokedAt = new \DateTimeImmutable(); |
60
|
|
|
|
61
|
|
|
$sql0 = 'INSERT INTO tokens (id, api_client_id, expires_at, revoked_at) VALUES (?, ?, ?, ?)'; // phpcs:ignore |
62
|
|
|
$values = [ |
63
|
|
|
[$nextId, \PDO::PARAM_STR], |
64
|
|
|
[$apiClientId, \PDO::PARAM_STR], |
65
|
|
|
[$expiresAt, \PDO::PARAM_STR], |
66
|
|
|
[$revokedAt, \PDO::PARAM_STR], |
67
|
|
|
]; |
68
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values); |
69
|
|
|
|
70
|
|
|
$this->writeConnectionMock |
71
|
|
|
->expects($this->once()) |
72
|
|
|
->method('prepare') |
73
|
|
|
->with($sql0) |
74
|
|
|
->willReturn($statement0); |
75
|
|
|
|
76
|
|
|
$entity = new Token($nextId, $apiClientId, $expiresAt, $revokedAt); |
77
|
|
|
|
78
|
|
|
$this->sut->add($entity); |
79
|
|
|
|
80
|
|
|
$this->assertSame($nextId, $entity->getId()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testDelete() |
84
|
|
|
{ |
85
|
|
|
$id = '8fe2f659-dbe5-4995-9e07-f49fb018cfe7'; |
86
|
|
|
$apiClientId = '33a9ef7e-3d84-4bd0-9b38-59b5cb7d5245'; |
87
|
|
|
$expiresAt = new \DateTimeImmutable(); |
88
|
|
|
|
89
|
|
|
$sql0 = 'UPDATE tokens AS tokens SET deleted_at = NOW() WHERE (id = ?)'; // phpcs:ignore |
90
|
|
|
$values = [[$id, \PDO::PARAM_STR]]; |
91
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values); |
92
|
|
|
|
93
|
|
|
$this->writeConnectionMock |
94
|
|
|
->expects($this->once()) |
95
|
|
|
->method('prepare') |
96
|
|
|
->with($sql0) |
97
|
|
|
->willReturn($statement0); |
98
|
|
|
|
99
|
|
|
$entity = new Token($id, $apiClientId, $expiresAt, null); |
100
|
|
|
|
101
|
|
|
$this->sut->delete($entity); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testGetAll() |
105
|
|
|
{ |
106
|
|
|
$id0 = '24bd4165-1229-4a6e-a679-76bf90743ee1'; |
107
|
|
|
$apiClientId0 = '33a9ef7e-3d84-4bd0-9b38-59b5cb7d5245'; |
108
|
|
|
$expiresAt0 = '2019-08-18 00:22:03'; |
109
|
|
|
$revokedAt0 = null; |
110
|
|
|
|
111
|
|
|
$id1 = '51eac0fc-2b26-4231-9559-469e59fae694'; |
112
|
|
|
$apiClientId1 = 'd9162b70-03e0-4969-aecc-8112adcd94c0'; |
113
|
|
|
$expiresAt1 = '2019-08-18 00:22:13'; |
114
|
|
|
$revokedAt1 = '2019-08-18 00:22:18'; |
115
|
|
|
|
116
|
|
|
$sql0 = 'SELECT tokens.id, tokens.api_client_id, tokens.expires_at, tokens.revoked_at FROM tokens WHERE (tokens.deleted_at IS NULL)'; // phpcs:ignore |
117
|
|
|
$values = []; |
118
|
|
|
$expectedData = [ |
119
|
|
|
['id' => $id0, 'api_client_id' => $apiClientId0, 'expires_at' => $expiresAt0, 'revoked_at' => $revokedAt0], |
120
|
|
|
['id' => $id1, 'api_client_id' => $apiClientId1, 'expires_at' => $expiresAt1, 'revoked_at' => $revokedAt1], |
121
|
|
|
]; |
122
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
123
|
|
|
|
124
|
|
|
$this->readConnectionMock |
125
|
|
|
->expects($this->once()) |
126
|
|
|
->method('prepare') |
127
|
|
|
->with($sql0) |
128
|
|
|
->willReturn($statement0); |
129
|
|
|
|
130
|
|
|
$actualResult = $this->sut->getAll(); |
131
|
|
|
|
132
|
|
|
$this->assertCollection($expectedData, $actualResult); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testGetByClientId() |
136
|
|
|
{ |
137
|
|
|
$apiClientId = '33a9ef7e-3d84-4bd0-9b38-59b5cb7d5245'; |
138
|
|
|
|
139
|
|
|
$id0 = '24bd4165-1229-4a6e-a679-76bf90743ee1'; |
140
|
|
|
$apiClientId0 = $apiClientId; |
141
|
|
|
$expiresAt0 = '2019-08-18 00:22:03'; |
142
|
|
|
$revokedAt0 = null; |
143
|
|
|
|
144
|
|
|
$sql0 = 'SELECT tokens.id, tokens.api_client_id, tokens.expires_at, tokens.revoked_at FROM tokens WHERE (tokens.deleted_at IS NULL) AND (tokens.api_client_id = :api_client_id)'; // phpcs:ignore |
145
|
|
|
$values = [ |
146
|
|
|
'api_client_id' => [$apiClientId, \PDO::PARAM_STR], |
147
|
|
|
]; |
148
|
|
|
$expectedData = [ |
149
|
|
|
['id' => $id0, 'api_client_id' => $apiClientId0, 'expires_at' => $expiresAt0, 'revoked_at' => $revokedAt0], |
150
|
|
|
]; |
151
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
152
|
|
|
|
153
|
|
|
$this->readConnectionMock |
154
|
|
|
->expects($this->once()) |
155
|
|
|
->method('prepare') |
156
|
|
|
->with($sql0) |
157
|
|
|
->willReturn($statement0); |
158
|
|
|
|
159
|
|
|
$actualResult = $this->sut->getByClientId($apiClientId); |
160
|
|
|
|
161
|
|
|
$this->assertEntity($expectedData[0], $actualResult); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function testGetById() |
165
|
|
|
{ |
166
|
|
|
$id = '4b72daf8-81a9-400f-b865-28306d1c1646'; |
167
|
|
|
$apiClientId = '33a9ef7e-3d84-4bd0-9b38-59b5cb7d5245'; |
168
|
|
|
$expiresAt = '2019-08-18 00:22:03'; |
169
|
|
|
$revokedAt = null; |
170
|
|
|
|
171
|
|
|
$sql0 = 'SELECT tokens.id, tokens.api_client_id, tokens.expires_at, tokens.revoked_at FROM tokens WHERE (tokens.deleted_at IS NULL) AND (tokens.id = :token_id)'; // phpcs:ignore |
172
|
|
|
$values = ['token_id' => [$id, \PDO::PARAM_STR]]; |
173
|
|
|
$expectedData = [ |
174
|
|
|
[ |
175
|
|
|
'id' => $id, |
176
|
|
|
'api_client_id' => $apiClientId, |
177
|
|
|
'expires_at' => $expiresAt, |
178
|
|
|
'revoked_at' => $revokedAt, |
179
|
|
|
], |
180
|
|
|
]; |
181
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
182
|
|
|
|
183
|
|
|
$this->readConnectionMock |
184
|
|
|
->expects($this->once()) |
185
|
|
|
->method('prepare') |
186
|
|
|
->with($sql0) |
187
|
|
|
->willReturn($statement0); |
188
|
|
|
|
189
|
|
|
$actualResult = $this->sut->getById($id); |
190
|
|
|
|
191
|
|
|
$this->assertEntity($expectedData[0], $actualResult); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function testUpdate() |
195
|
|
|
{ |
196
|
|
|
$id = '91693481-276e-495b-82a1-33209c47ca09'; |
197
|
|
|
$apiClientId = '33a9ef7e-3d84-4bd0-9b38-59b5cb7d5245'; |
198
|
|
|
$expiresAt = new \DateTimeImmutable('2019-08-18 00:22:03'); |
199
|
|
|
$revokedAt = null; |
200
|
|
|
|
201
|
|
|
$sql0 = 'UPDATE tokens AS tokens SET api_client_id = ?, expires_at = ?, revoked_at = ? WHERE (id = ?)'; // phpcs:ignore |
202
|
|
|
$values = [ |
203
|
|
|
[$apiClientId, \PDO::PARAM_STR], |
204
|
|
|
[$expiresAt, \PDO::PARAM_STR], |
205
|
|
|
[$revokedAt, \PDO::PARAM_NULL], |
206
|
|
|
[$id, \PDO::PARAM_STR], |
207
|
|
|
]; |
208
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values); |
209
|
|
|
|
210
|
|
|
$this->writeConnectionMock |
211
|
|
|
->expects($this->once()) |
212
|
|
|
->method('prepare') |
213
|
|
|
->with($sql0) |
214
|
|
|
->willReturn($statement0); |
215
|
|
|
|
216
|
|
|
$entity = new Token($id, $apiClientId, $expiresAt, $revokedAt); |
217
|
|
|
|
218
|
|
|
$this->sut->update($entity); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function testUpdateWithRevokedAt() |
222
|
|
|
{ |
223
|
|
|
$id = '91693481-276e-495b-82a1-33209c47ca09'; |
224
|
|
|
$apiClientId = '33a9ef7e-3d84-4bd0-9b38-59b5cb7d5245'; |
225
|
|
|
$expiresAt = new \DateTimeImmutable('2019-08-18 00:22:03'); |
226
|
|
|
$revokedAt = new \DateTimeImmutable('2019-08-18 00:22:13'); |
227
|
|
|
|
228
|
|
|
$sql0 = 'UPDATE tokens AS tokens SET api_client_id = ?, expires_at = ?, revoked_at = ? WHERE (id = ?)'; // phpcs:ignore |
229
|
|
|
$values = [ |
230
|
|
|
[$apiClientId, \PDO::PARAM_STR], |
231
|
|
|
[$expiresAt, \PDO::PARAM_STR], |
232
|
|
|
[$revokedAt, \PDO::PARAM_STR], |
233
|
|
|
[$id, \PDO::PARAM_STR], |
234
|
|
|
]; |
235
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values); |
236
|
|
|
|
237
|
|
|
$this->writeConnectionMock |
238
|
|
|
->expects($this->once()) |
239
|
|
|
->method('prepare') |
240
|
|
|
->with($sql0) |
241
|
|
|
->willReturn($statement0); |
242
|
|
|
|
243
|
|
|
$entity = new Token($id, $apiClientId, $expiresAt, $revokedAt); |
244
|
|
|
|
245
|
|
|
$this->sut->update($entity); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function testAddThrowsExceptionIfCalledWithInvalidEntity() |
249
|
|
|
{ |
250
|
|
|
$this->expectException(\InvalidArgumentException::class); |
251
|
|
|
|
252
|
|
|
/** @var IStringerEntity|MockObject $entity */ |
253
|
|
|
$entity = $this->createMock(IStringerEntity::class); |
254
|
|
|
|
255
|
|
|
$this->sut->add($entity); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function testDeleteThrowsExceptionIfCalledWithInvalidEntity() |
259
|
|
|
{ |
260
|
|
|
$this->expectException(\InvalidArgumentException::class); |
261
|
|
|
|
262
|
|
|
/** @var IStringerEntity|MockObject $entity */ |
263
|
|
|
$entity = $this->createMock(IStringerEntity::class); |
264
|
|
|
|
265
|
|
|
$this->sut->delete($entity); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
public function testUpdateThrowsExceptionIfCalledWithInvalidEntity() |
269
|
|
|
{ |
270
|
|
|
$this->expectException(\InvalidArgumentException::class); |
271
|
|
|
|
272
|
|
|
/** @var IStringerEntity|MockObject $entity */ |
273
|
|
|
$entity = $this->createMock(IStringerEntity::class); |
274
|
|
|
|
275
|
|
|
$this->sut->update($entity); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @param array $expectedData |
280
|
|
|
* @param Token $entity |
281
|
|
|
*/ |
282
|
|
|
protected function assertEntity(array $expectedData, $entity) |
283
|
|
|
{ |
284
|
|
|
$this->assertInstanceOf(Token::class, $entity); |
285
|
|
|
$this->assertEquals($expectedData['id'], $entity->getId()); |
286
|
|
|
$this->assertSame($expectedData['api_client_id'], $entity->getApiClientId()); |
287
|
|
|
$this->assertEquals(new \DateTimeImmutable($expectedData['expires_at']), $entity->getExpiresAt()); |
288
|
|
|
|
289
|
|
|
if (null === $expectedData['revoked_at']) { |
290
|
|
|
$this->assertNull($entity->getRevokedAt()); |
291
|
|
|
} else { |
292
|
|
|
$this->assertEquals(new \DateTimeImmutable($expectedData['revoked_at']), $entity->getRevokedAt()); |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|