1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Files\Orm\DataMappers; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Admin\Domain\Entities\User; |
8
|
|
|
use AbterPhp\Admin\Domain\Entities\UserLanguage; |
9
|
|
|
use AbterPhp\Admin\TestCase\Orm\DataMapperTestCase; |
10
|
|
|
use AbterPhp\Files\Domain\Entities\File; |
11
|
|
|
use AbterPhp\Files\Domain\Entities\FileDownload; |
12
|
|
|
use AbterPhp\Framework\TestDouble\Database\MockStatementFactory; |
13
|
|
|
|
14
|
|
|
class FileDownloadSqlDataMapperTest extends DataMapperTestCase |
15
|
|
|
{ |
16
|
|
|
/** @var FileDownloadSqlDataMapper */ |
17
|
|
|
protected $sut; |
18
|
|
|
|
19
|
|
|
public function setUp(): void |
20
|
|
|
{ |
21
|
|
|
parent::setUp(); |
22
|
|
|
|
23
|
|
|
$this->sut = new FileDownloadSqlDataMapper($this->readConnectionMock, $this->writeConnectionMock); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testAdd() |
27
|
|
|
{ |
28
|
|
|
$nextId = '97fe8788-b98c-483d-ab8a-e0fea4d4c54c'; |
29
|
|
|
$fileId = 'ea65ff5a-8a6d-430f-9ee1-ba96e3dcea65'; |
30
|
|
|
$userId = 'bd5edd79-4dac-4de3-b670-d93ea2b8b14e'; |
31
|
|
|
$downloadedAt = new \DateTime(); |
32
|
|
|
|
33
|
|
|
$sql0 = 'INSERT INTO file_downloads (id, file_id, user_id, downloaded_at) VALUES (?, ?, ?, ?)'; // phpcs:ignore |
34
|
|
|
$values = [ |
35
|
|
|
[$nextId, \PDO::PARAM_STR], |
36
|
|
|
[$fileId, \PDO::PARAM_STR], |
37
|
|
|
[$userId, \PDO::PARAM_STR], |
38
|
|
|
[$downloadedAt->format(FileDownload::DATE_FORMAT), \PDO::PARAM_STR], |
39
|
|
|
]; |
40
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values); |
41
|
|
|
|
42
|
|
|
$this->writeConnectionMock |
43
|
|
|
->expects($this->once()) |
44
|
|
|
->method('prepare') |
45
|
|
|
->with($sql0) |
46
|
|
|
->willReturn($statement0); |
47
|
|
|
|
48
|
|
|
$entity = $this->createEntity($nextId, $fileId, $userId, $downloadedAt); |
49
|
|
|
|
50
|
|
|
$this->sut->add($entity); |
51
|
|
|
|
52
|
|
|
$this->assertSame($nextId, $entity->getId()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testDelete() |
56
|
|
|
{ |
57
|
|
|
$id = '4ec776f2-dcb1-421a-9073-226c01cfe28d'; |
58
|
|
|
$fileId = '13bcd299-12a9-484a-8a4c-0b745a11d726'; |
59
|
|
|
$userId = '4c178710-2c4a-4658-90ea-491aadc3c32b'; |
60
|
|
|
$downloadedAt = new \DateTime(); |
61
|
|
|
|
62
|
|
|
$sql0 = 'UPDATE file_downloads AS file_downloads SET deleted_at = NOW() WHERE (id = ?)'; // phpcs:ignore |
63
|
|
|
$values = [[$id, \PDO::PARAM_STR]]; |
64
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values); |
65
|
|
|
|
66
|
|
|
$this->writeConnectionMock |
67
|
|
|
->expects($this->once()) |
68
|
|
|
->method('prepare') |
69
|
|
|
->with($sql0) |
70
|
|
|
->willReturn($statement0); |
71
|
|
|
|
72
|
|
|
$entity = $this->createEntity($id, $fileId, $userId, $downloadedAt); |
73
|
|
|
|
74
|
|
|
$this->sut->delete($entity); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testGetAll() |
78
|
|
|
{ |
79
|
|
|
$id = '0302b961-700f-4bbf-8d33-74c7b679cbf5'; |
80
|
|
|
$fileId = '658ee9e7-9a0e-44a3-832c-9dd2acebcd64'; |
81
|
|
|
$userId = '80f00d82-4832-4764-b74e-8ad90b7af2f1'; |
82
|
|
|
$downloadedAt = new \DateTime(); |
83
|
|
|
$filesystemName = 'foo'; |
84
|
|
|
$publicName = 'bar'; |
85
|
|
|
$mime = 'text/yax'; |
86
|
|
|
$userName = 'baz'; |
87
|
|
|
|
88
|
|
|
$sql0 = 'SELECT file_downloads.id, file_downloads.file_id, file_downloads.user_id, file_downloads.downloaded_at, files.filesystem_name AS filesystem_name, files.public_name AS public_name, files.mime AS mime, users.username AS username FROM file_downloads INNER JOIN files AS files ON files.id=file_downloads.file_id INNER JOIN users AS users ON users.id=file_downloads.user_id WHERE (file_downloads.deleted_at IS NULL)'; // phpcs:ignore |
89
|
|
|
$values = []; |
90
|
|
|
$expectedData = [ |
91
|
|
|
[ |
92
|
|
|
'id' => $id, |
93
|
|
|
'file_id' => $fileId, |
94
|
|
|
'user_id' => $userId, |
95
|
|
|
'downloaded_at' => $downloadedAt->format(FileDownload::DATE_FORMAT), |
96
|
|
|
'filesystem_name' => $filesystemName, |
97
|
|
|
'public_name' => $publicName, |
98
|
|
|
'mime' => $mime, |
99
|
|
|
'username' => $userName, |
100
|
|
|
], |
101
|
|
|
]; |
102
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
103
|
|
|
|
104
|
|
|
$this->readConnectionMock |
105
|
|
|
->expects($this->once()) |
106
|
|
|
->method('prepare') |
107
|
|
|
->with($sql0) |
108
|
|
|
->willReturn($statement0); |
109
|
|
|
|
110
|
|
|
$actualResult = $this->sut->getAll(); |
111
|
|
|
|
112
|
|
|
$this->assertCollection($expectedData, $actualResult); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testGetPage() |
116
|
|
|
{ |
117
|
|
|
$id = '0302b961-700f-4bbf-8d33-74c7b679cbf5'; |
118
|
|
|
$fileId = '658ee9e7-9a0e-44a3-832c-9dd2acebcd64'; |
119
|
|
|
$userId = '80f00d82-4832-4764-b74e-8ad90b7af2f1'; |
120
|
|
|
$downloadedAt = new \DateTime(); |
121
|
|
|
$filesystemName = 'foo'; |
122
|
|
|
$publicName = 'bar'; |
123
|
|
|
$mime = 'text/yax'; |
124
|
|
|
$userName = 'baz'; |
125
|
|
|
|
126
|
|
|
$sql0 = 'SELECT SQL_CALC_FOUND_ROWS file_downloads.id, file_downloads.file_id, file_downloads.user_id, file_downloads.downloaded_at, files.filesystem_name AS filesystem_name, files.public_name AS public_name, files.mime AS mime, users.username AS username FROM file_downloads INNER JOIN files AS files ON files.id=file_downloads.file_id INNER JOIN users AS users ON users.id=file_downloads.user_id WHERE (file_downloads.deleted_at IS NULL) ORDER BY downloaded_at DESC LIMIT 10 OFFSET 0'; // phpcs:ignore |
127
|
|
|
$values = []; |
128
|
|
|
$expectedData = [ |
129
|
|
|
[ |
130
|
|
|
'id' => $id, |
131
|
|
|
'file_id' => $fileId, |
132
|
|
|
'user_id' => $userId, |
133
|
|
|
'downloaded_at' => $downloadedAt->format(FileDownload::DATE_FORMAT), |
134
|
|
|
'filesystem_name' => $filesystemName, |
135
|
|
|
'public_name' => $publicName, |
136
|
|
|
'mime' => $mime, |
137
|
|
|
'username' => $userName, |
138
|
|
|
], |
139
|
|
|
]; |
140
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
141
|
|
|
|
142
|
|
|
$this->readConnectionMock |
143
|
|
|
->expects($this->once()) |
144
|
|
|
->method('prepare') |
145
|
|
|
->with($sql0) |
146
|
|
|
->willReturn($statement0); |
147
|
|
|
|
148
|
|
|
$actualResult = $this->sut->getPage(0, 10, [], [], []); |
149
|
|
|
|
150
|
|
|
$this->assertCollection($expectedData, $actualResult); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function testGetPageWithOrdersAndConditions() |
154
|
|
|
{ |
155
|
|
|
$id = '0302b961-700f-4bbf-8d33-74c7b679cbf5'; |
156
|
|
|
$fileId = '658ee9e7-9a0e-44a3-832c-9dd2acebcd64'; |
157
|
|
|
$userId = '80f00d82-4832-4764-b74e-8ad90b7af2f1'; |
158
|
|
|
$downloadedAt = new \DateTime(); |
159
|
|
|
$filesystemName = 'foo'; |
160
|
|
|
$publicName = 'bar'; |
161
|
|
|
$mime = 'text/yax'; |
162
|
|
|
$userName = 'baz'; |
163
|
|
|
|
164
|
|
|
$orders = ['files.filesystem_name ASC']; |
165
|
|
|
$conditions = ['files.filesystem_name LIKE \'abc%\'', 'files.filesystem_name LIKE \'%bca\'']; |
166
|
|
|
|
167
|
|
|
$sql0 = "SELECT SQL_CALC_FOUND_ROWS file_downloads.id, file_downloads.file_id, file_downloads.user_id, file_downloads.downloaded_at, files.filesystem_name AS filesystem_name, files.public_name AS public_name, files.mime AS mime, users.username AS username FROM file_downloads INNER JOIN files AS files ON files.id=file_downloads.file_id INNER JOIN users AS users ON users.id=file_downloads.user_id WHERE (file_downloads.deleted_at IS NULL) AND (files.filesystem_name LIKE 'abc%') AND (files.filesystem_name LIKE '%bca') ORDER BY files.filesystem_name ASC LIMIT 10 OFFSET 0"; // phpcs:ignore |
168
|
|
|
$values = []; |
169
|
|
|
$expectedData = [ |
170
|
|
|
[ |
171
|
|
|
'id' => $id, |
172
|
|
|
'file_id' => $fileId, |
173
|
|
|
'user_id' => $userId, |
174
|
|
|
'downloaded_at' => $downloadedAt->format(FileDownload::DATE_FORMAT), |
175
|
|
|
'filesystem_name' => $filesystemName, |
176
|
|
|
'public_name' => $publicName, |
177
|
|
|
'mime' => $mime, |
178
|
|
|
'username' => $userName, |
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->getPage(0, 10, $orders, $conditions, []); |
190
|
|
|
|
191
|
|
|
$this->assertCollection($expectedData, $actualResult); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function testGetById() |
195
|
|
|
{ |
196
|
|
|
$id = '2a73fd3f-bb4d-4a13-b762-fbe36944170d'; |
197
|
|
|
$fileId = '87366ea2-3bb2-41ef-b716-1817636074ba'; |
198
|
|
|
$userId = '3aac6c0d-f327-4645-b322-85a7d0fbd86c'; |
199
|
|
|
$downloadedAt = new \DateTime(); |
200
|
|
|
$filesystemName = 'foo'; |
201
|
|
|
$publicName = 'bar'; |
202
|
|
|
$mime = 'text/yax'; |
203
|
|
|
$userName = 'baz'; |
204
|
|
|
|
205
|
|
|
$sql0 = 'SELECT file_downloads.id, file_downloads.file_id, file_downloads.user_id, file_downloads.downloaded_at, files.filesystem_name AS filesystem_name, files.public_name AS public_name, files.mime AS mime, users.username AS username FROM file_downloads INNER JOIN files AS files ON files.id=file_downloads.file_id INNER JOIN users AS users ON users.id=file_downloads.user_id WHERE (file_downloads.deleted_at IS NULL) AND (file_downloads.id = :file_download_id)'; // phpcs:ignore |
206
|
|
|
$values = ['file_download_id' => [$id, \PDO::PARAM_STR]]; |
207
|
|
|
$expectedData = [ |
208
|
|
|
[ |
209
|
|
|
'id' => $id, |
210
|
|
|
'file_id' => $fileId, |
211
|
|
|
'user_id' => $userId, |
212
|
|
|
'downloaded_at' => $downloadedAt->format(FileDownload::DATE_FORMAT), |
213
|
|
|
'filesystem_name' => $filesystemName, |
214
|
|
|
'public_name' => $publicName, |
215
|
|
|
'mime' => $mime, |
216
|
|
|
'username' => $userName, |
217
|
|
|
], |
218
|
|
|
]; |
219
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
220
|
|
|
|
221
|
|
|
$this->readConnectionMock |
222
|
|
|
->expects($this->once()) |
223
|
|
|
->method('prepare') |
224
|
|
|
->with($sql0) |
225
|
|
|
->willReturn($statement0); |
226
|
|
|
|
227
|
|
|
$actualResult = $this->sut->getById($id); |
228
|
|
|
|
229
|
|
|
$this->assertEntity($expectedData[0], $actualResult); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function testUpdate() |
233
|
|
|
{ |
234
|
|
|
$id = '78683847-4e00-423c-8e47-e0026bc83e85'; |
235
|
|
|
$fileId = '45ffd14e-69cc-4d40-a97a-a7504a481521'; |
236
|
|
|
$userId = '0515c6bb-0ab4-4375-a990-bfa103ba9447'; |
237
|
|
|
$downloadedAt = new \DateTime(); |
238
|
|
|
|
239
|
|
|
$sql0 = 'UPDATE file_downloads AS file_downloads SET file_id = ?, user_id = ?, downloaded_at = ? WHERE (id = ?)'; // phpcs:ignore |
240
|
|
|
$values = [ |
241
|
|
|
[$fileId, \PDO::PARAM_STR], |
242
|
|
|
[$userId, \PDO::PARAM_STR], |
243
|
|
|
[$downloadedAt->format(FileDownload::DATE_FORMAT), \PDO::PARAM_STR], |
244
|
|
|
[$id, \PDO::PARAM_STR], |
245
|
|
|
]; |
246
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values); |
247
|
|
|
|
248
|
|
|
$this->writeConnectionMock |
249
|
|
|
->expects($this->once()) |
250
|
|
|
->method('prepare') |
251
|
|
|
->with($sql0) |
252
|
|
|
->willReturn($statement0); |
253
|
|
|
|
254
|
|
|
$entity = $this->createEntity($id, $fileId, $userId, $downloadedAt); |
255
|
|
|
|
256
|
|
|
$this->sut->update($entity); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public function testGetByFileId() |
260
|
|
|
{ |
261
|
|
|
$id = '7756106a-f150-4328-9bfe-c807ab6189a5'; |
262
|
|
|
$fileId = '31efab70-fa54-4feb-9855-a25cdadd2143'; |
263
|
|
|
$userId = '50946095-b41a-4c2d-8091-fd4bc740a28c'; |
264
|
|
|
$downloadedAt = new \DateTime(); |
265
|
|
|
$filesystemName = 'foo'; |
266
|
|
|
$publicName = 'bar'; |
267
|
|
|
$mime = 'text/yax'; |
268
|
|
|
$userName = 'baz'; |
269
|
|
|
|
270
|
|
|
$sql0 = 'SELECT file_downloads.id, file_downloads.file_id, file_downloads.user_id, file_downloads.downloaded_at, files.filesystem_name AS filesystem_name, files.public_name AS public_name, files.mime AS mime, users.username AS username FROM file_downloads INNER JOIN files AS files ON files.id=file_downloads.file_id INNER JOIN users AS users ON users.id=file_downloads.user_id WHERE (file_downloads.deleted_at IS NULL) AND (file_id = :file_id)'; // phpcs:ignore |
271
|
|
|
$values = ['file_id' => [$fileId, \PDO::PARAM_STR]]; |
272
|
|
|
$expectedData = [ |
273
|
|
|
[ |
274
|
|
|
'id' => $id, |
275
|
|
|
'file_id' => $fileId, |
276
|
|
|
'user_id' => $userId, |
277
|
|
|
'downloaded_at' => $downloadedAt->format(FileDownload::DATE_FORMAT), |
278
|
|
|
'filesystem_name' => $filesystemName, |
279
|
|
|
'public_name' => $publicName, |
280
|
|
|
'mime' => $mime, |
281
|
|
|
'username' => $userName, |
282
|
|
|
], |
283
|
|
|
]; |
284
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
285
|
|
|
|
286
|
|
|
$this->readConnectionMock |
287
|
|
|
->expects($this->once()) |
288
|
|
|
->method('prepare') |
289
|
|
|
->with($sql0) |
290
|
|
|
->willReturn($statement0); |
291
|
|
|
|
292
|
|
|
$actualResult = $this->sut->getByFileId($fileId); |
293
|
|
|
|
294
|
|
|
$this->assertCollection($expectedData, $actualResult); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
public function testGetByUserId() |
298
|
|
|
{ |
299
|
|
|
$id = 'f222f33a-2fbd-4552-abdf-577f95731e36'; |
300
|
|
|
$fileId = '80fda718-4285-48a3-b8d6-86902a275b5f'; |
301
|
|
|
$userId = '1bde0bc1-175e-45bf-a64a-80dab37ed760'; |
302
|
|
|
$downloadedAt = new \DateTime(); |
303
|
|
|
$filesystemName = 'foo'; |
304
|
|
|
$publicName = 'bar'; |
305
|
|
|
$mime = 'text/yax'; |
306
|
|
|
$userName = 'baz'; |
307
|
|
|
|
308
|
|
|
$sql0 = 'SELECT file_downloads.id, file_downloads.file_id, file_downloads.user_id, file_downloads.downloaded_at, files.filesystem_name AS filesystem_name, files.public_name AS public_name, files.mime AS mime, users.username AS username FROM file_downloads INNER JOIN files AS files ON files.id=file_downloads.file_id INNER JOIN users AS users ON users.id=file_downloads.user_id WHERE (file_downloads.deleted_at IS NULL) AND (user_id = :user_id)'; // phpcs:ignore |
309
|
|
|
$values = ['user_id' => [$userId, \PDO::PARAM_STR]]; |
310
|
|
|
$expectedData = [ |
311
|
|
|
[ |
312
|
|
|
'id' => $id, |
313
|
|
|
'file_id' => $fileId, |
314
|
|
|
'user_id' => $userId, |
315
|
|
|
'downloaded_at' => $downloadedAt->format(FileDownload::DATE_FORMAT), |
316
|
|
|
'filesystem_name' => $filesystemName, |
317
|
|
|
'public_name' => $publicName, |
318
|
|
|
'mime' => $mime, |
319
|
|
|
'username' => $userName, |
320
|
|
|
], |
321
|
|
|
]; |
322
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
323
|
|
|
|
324
|
|
|
$this->readConnectionMock |
325
|
|
|
->expects($this->once()) |
326
|
|
|
->method('prepare') |
327
|
|
|
->with($sql0) |
328
|
|
|
->willReturn($statement0); |
329
|
|
|
|
330
|
|
|
$actualResult = $this->sut->getByUserId($userId); |
331
|
|
|
|
332
|
|
|
$this->assertCollection($expectedData, $actualResult); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* @param string $id |
337
|
|
|
* @param string $fileId |
338
|
|
|
* @param string $userId |
339
|
|
|
* @param \DateTime $downloadedAt |
340
|
|
|
* |
341
|
|
|
* @return FileDownload |
342
|
|
|
* @throws \Exception |
343
|
|
|
*/ |
344
|
|
|
protected function createEntity(string $id, string $fileId, string $userId, \DateTime $downloadedAt) |
345
|
|
|
{ |
346
|
|
|
$file = new File($fileId, '', '', '', ''); |
347
|
|
|
$userLanguage = new UserLanguage('', '', ''); |
348
|
|
|
$user = new User($userId, '', '', '', true, true, $userLanguage); |
349
|
|
|
|
350
|
|
|
return new FileDownload($id, $file, $user, $downloadedAt); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @param array $expectedData |
355
|
|
|
* @param FileDownload $entity |
356
|
|
|
*/ |
357
|
|
|
protected function assertEntity(array $expectedData, $entity) |
358
|
|
|
{ |
359
|
|
|
$downloadedAt = $entity->getDownloadedAt()->format(FileDownload::DATE_FORMAT); |
360
|
|
|
|
361
|
|
|
$this->assertInstanceOf(FileDownload::class, $entity); |
362
|
|
|
$this->assertSame($expectedData['id'], $entity->getId()); |
363
|
|
|
$this->assertSame($expectedData['file_id'], $entity->getFile()->getId()); |
364
|
|
|
$this->assertSame($expectedData['user_id'], $entity->getUser()->getId()); |
365
|
|
|
$this->assertSame($expectedData['downloaded_at'], $downloadedAt); |
366
|
|
|
$this->assertSame($expectedData['filesystem_name'], $entity->getFile()->getFilesystemName()); |
367
|
|
|
$this->assertSame($expectedData['public_name'], $entity->getFile()->getPublicName()); |
368
|
|
|
$this->assertSame($expectedData['mime'], $entity->getFile()->getMime()); |
369
|
|
|
$this->assertSame($expectedData['username'], $entity->getUser()->getUsername()); |
370
|
|
|
} |
371
|
|
|
} |
372
|
|
|
|