FileSqlDataMapperTest::testDelete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 21
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 27
rs 9.584
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Files\Orm\DataMappers;
6
7
use AbterPhp\Admin\TestCase\Orm\DataMapperTestCase;
8
use AbterPhp\Files\Domain\Entities\File;
9
use AbterPhp\Files\Domain\Entities\FileCategory;
10
use AbterPhp\Framework\TestDouble\Database\MockStatementFactory;
11
12
class FileSqlDataMapperTest extends DataMapperTestCase
13
{
14
    /** @var FileSqlDataMapper */
15
    protected $sut;
16
17
    public function setUp(): void
18
    {
19
        parent::setUp();
20
21
        $this->sut = new FileSqlDataMapper($this->readConnectionMock, $this->writeConnectionMock);
22
    }
23
24
    public function testAdd()
25
    {
26
        $nextId         = 'c2883287-ae5d-42d1-ab0c-7d3da2846452';
27
        $filesystemName = 'foo';
28
        $publicName     = 'bar';
29
        $mime           = 'baz';
30
        $description    = 'qux';
31
        $fileCategoryId = '5df656e1-f2b2-4bff-8999-b90b041b696a';
32
        $uploadedAt     = new \DateTime();
33
34
        $sql0       = 'INSERT INTO files (id, filesystem_name, public_name, mime, description, file_category_id, uploaded_at) VALUES (?, ?, ?, ?, ?, ?, ?)'; // phpcs:ignore
35
        $values     = [
36
            [$nextId, \PDO::PARAM_STR],
37
            [$filesystemName, \PDO::PARAM_STR],
38
            [$publicName, \PDO::PARAM_STR],
39
            [$mime, \PDO::PARAM_STR],
40
            [$description, \PDO::PARAM_STR],
41
            [$fileCategoryId, \PDO::PARAM_STR],
42
            [$uploadedAt->format('Y-m-d'), \PDO::PARAM_STR],
43
        ];
44
        $statement0 = MockStatementFactory::createWriteStatement($this, $values);
45
46
        $this->writeConnectionMock
47
            ->expects($this->once())
48
            ->method('prepare')
49
            ->with($sql0)
50
            ->willReturn($statement0);
51
52
        $category = new FileCategory($fileCategoryId, '', '', false);
53
        $entity   = new File($nextId, $filesystemName, $publicName, $mime, $description, $category, $uploadedAt);
54
55
        $this->sut->add($entity);
56
57
        $this->assertSame($nextId, $entity->getId());
58
    }
59
60
    public function testDelete()
61
    {
62
        $id                 = '5bc63ac6-b3cd-41f0-bbc6-81a4568179db';
63
        $filesystemName     = 'foo';
64
        $publicName         = 'bar';
65
        $mime               = 'text/yax';
66
        $description        = 'baz';
67
        $categoryId         = 'aa961686-d042-4b43-8b0a-163d80a29166';
68
        $categoryName       = 'qux';
69
        $categoryIdentifier = 'quuux';
70
        $categoryIsPublic   = false;
71
        $uploadedAt         = new \DateTime();
72
73
        $sql0       = 'UPDATE files AS files SET deleted_at = NOW() WHERE (id = ?)'; // phpcs:ignore
74
        $values     = [[$id, \PDO::PARAM_STR]];
75
        $statement0 = MockStatementFactory::createWriteStatement($this, $values);
76
77
        $this->writeConnectionMock
78
            ->expects($this->once())
79
            ->method('prepare')
80
            ->with($sql0)
81
            ->willReturn($statement0);
82
83
        $category = new FileCategory($categoryId, $categoryIdentifier, $categoryName, $categoryIsPublic);
84
        $entity   = new File($id, $filesystemName, $publicName, $mime, $description, $category, $uploadedAt);
85
86
        $this->sut->delete($entity);
87
    }
88
89
    public function testGetAll()
90
    {
91
        $id                 = '54d0ff01-f6b7-4058-9fcd-40f847cf2aef';
92
        $filesystemName     = 'foo';
93
        $publicName         = 'bar';
94
        $mime               = 'text/yax';
95
        $description        = 'baz';
96
        $categoryId         = 'fc14a949-03cc-4d7a-8a71-7ee31d4d3be2';
97
        $categoryName       = 'qux';
98
        $categoryIdentifier = 'quuux';
99
        $uploadedAt         = new \DateTime();
100
101
        $sql0         = 'SELECT files.id, files.filesystem_name, files.public_name, files.mime, files.file_category_id, files.description, files.uploaded_at, file_categories.name AS file_category_name, file_categories.identifier AS file_category_identifier FROM files INNER JOIN file_categories AS file_categories ON file_categories.id = files.file_category_id AND file_categories.deleted_at IS NULL WHERE (files.deleted_at IS NULL) GROUP BY files.id'; // phpcs:ignore
102
        $values       = [];
103
        $expectedData = [
104
            [
105
                'id'                       => $id,
106
                'filesystem_name'          => $filesystemName,
107
                'public_name'              => $publicName,
108
                'file_category_id'         => $categoryId,
109
                'mime'                     => $mime,
110
                'description'              => $description,
111
                'uploaded_at'              => $uploadedAt->format(File::DATE_FORMAT),
112
                'file_category_name'       => $categoryName,
113
                'file_category_identifier' => $categoryIdentifier,
114
            ],
115
        ];
116
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
117
118
        $this->readConnectionMock
119
            ->expects($this->once())
120
            ->method('prepare')
121
            ->with($sql0)
122
            ->willReturn($statement0);
123
124
        $actualResult = $this->sut->getAll();
125
126
        $this->assertCollection($expectedData, $actualResult);
127
    }
128
129
    public function testGetPage()
130
    {
131
        $id                 = '54d0ff01-f6b7-4058-9fcd-40f847cf2aef';
132
        $filesystemName     = 'foo';
133
        $publicName         = 'bar';
134
        $mime               = 'text/yax';
135
        $description        = 'baz';
136
        $categoryId         = 'fc14a949-03cc-4d7a-8a71-7ee31d4d3be2';
137
        $categoryName       = 'qux';
138
        $categoryIdentifier = 'quuux';
139
        $uploadedAt         = new \DateTime();
140
141
        $sql0         = 'SELECT SQL_CALC_FOUND_ROWS files.id, files.filesystem_name, files.public_name, files.mime, files.file_category_id, files.description, files.uploaded_at, file_categories.name AS file_category_name, file_categories.identifier AS file_category_identifier FROM files INNER JOIN file_categories AS file_categories ON file_categories.id = files.file_category_id AND file_categories.deleted_at IS NULL WHERE (files.deleted_at IS NULL) GROUP BY files.id ORDER BY files.public_name ASC LIMIT 10 OFFSET 0'; // phpcs:ignore
142
        $values       = [];
143
        $expectedData = [
144
            [
145
                'id'                       => $id,
146
                'filesystem_name'          => $filesystemName,
147
                'public_name'              => $publicName,
148
                'file_category_id'         => $categoryId,
149
                'mime'                     => $mime,
150
                'description'              => $description,
151
                'uploaded_at'              => $uploadedAt->format(File::DATE_FORMAT),
152
                'file_category_name'       => $categoryName,
153
                'file_category_identifier' => $categoryIdentifier,
154
            ],
155
        ];
156
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
157
158
        $this->readConnectionMock
159
            ->expects($this->once())
160
            ->method('prepare')
161
            ->with($sql0)
162
            ->willReturn($statement0);
163
164
        $actualResult = $this->sut->getPage(0, 10, [], [], []);
165
166
        $this->assertCollection($expectedData, $actualResult);
167
    }
168
169
    public function testGetPageWithOrdersAndConditions()
170
    {
171
        $id                 = '54d0ff01-f6b7-4058-9fcd-40f847cf2aef';
172
        $filesystemName     = 'foo';
173
        $publicName         = 'bar';
174
        $mime               = 'text/yax';
175
        $description        = 'baz';
176
        $categoryId         = 'fc14a949-03cc-4d7a-8a71-7ee31d4d3be2';
177
        $categoryName       = 'qux';
178
        $categoryIdentifier = 'quuux';
179
        $uploadedAt         = new \DateTime();
180
181
        $orders     = ['files.public_name ASC'];
182
        $conditions = ['files.public_name LIKE \'abc%\'', 'files.public_name LIKE \'%bca\''];
183
184
        $sql0         = "SELECT SQL_CALC_FOUND_ROWS files.id, files.filesystem_name, files.public_name, files.mime, files.file_category_id, files.description, files.uploaded_at, file_categories.name AS file_category_name, file_categories.identifier AS file_category_identifier FROM files INNER JOIN file_categories AS file_categories ON file_categories.id = files.file_category_id AND file_categories.deleted_at IS NULL WHERE (files.deleted_at IS NULL) AND (files.public_name LIKE 'abc%') AND (files.public_name LIKE '%bca') GROUP BY files.id ORDER BY files.public_name ASC LIMIT 10 OFFSET 0"; // phpcs:ignore
185
        $values       = [];
186
        $expectedData = [
187
            [
188
                'id'                       => $id,
189
                'filesystem_name'          => $filesystemName,
190
                'public_name'              => $publicName,
191
                'file_category_id'         => $categoryId,
192
                'mime'                     => $mime,
193
                'description'              => $description,
194
                'uploaded_at'              => $uploadedAt->format(File::DATE_FORMAT),
195
                'file_category_name'       => $categoryName,
196
                'file_category_identifier' => $categoryIdentifier,
197
            ],
198
        ];
199
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
200
201
        $this->readConnectionMock
202
            ->expects($this->once())
203
            ->method('prepare')
204
            ->with($sql0)
205
            ->willReturn($statement0);
206
207
        $actualResult = $this->sut->getPage(0, 10, $orders, $conditions, []);
208
209
        $this->assertCollection($expectedData, $actualResult);
210
    }
211
212
    public function testGetPublicByCategoryIdentifiers()
213
    {
214
        $id                 = '54d0ff01-f6b7-4058-9fcd-40f847cf2aef';
215
        $filesystemName     = 'foo';
216
        $publicName         = 'bar';
217
        $mime               = 'text/yax';
218
        $description        = 'baz';
219
        $categoryId         = 'fc14a949-03cc-4d7a-8a71-7ee31d4d3be2';
220
        $categoryName       = 'qux';
221
        $categoryIdentifier = 'quuux';
222
        $uploadedAt         = new \DateTime();
223
224
        $sql0         = 'SELECT files.id, files.filesystem_name, files.public_name, files.mime, files.file_category_id, files.description, files.uploaded_at, file_categories.name AS file_category_name, file_categories.identifier AS file_category_identifier FROM files INNER JOIN file_categories AS file_categories ON file_categories.id = files.file_category_id AND file_categories.deleted_at IS NULL INNER JOIN user_groups_file_categories AS ugfc ON file_categories.id = ugfc.file_category_id AND file_categories.deleted_at IS NULL INNER JOIN user_groups AS user_groups ON user_groups.id = ugfc.user_group_id AND user_groups.deleted_at IS NULL WHERE (files.deleted_at IS NULL) AND (file_categories.identifier IN (?)) GROUP BY files.id'; // phpcs:ignore
225
        $values       = [
226
            [$categoryIdentifier, \PDO::PARAM_STR],
227
        ];
228
        $expectedData = [
229
            [
230
                'id'                       => $id,
231
                'filesystem_name'          => $filesystemName,
232
                'public_name'              => $publicName,
233
                'file_category_id'         => $categoryId,
234
                'mime'                     => $mime,
235
                'description'              => $description,
236
                'uploaded_at'              => $uploadedAt->format(File::DATE_FORMAT),
237
                'file_category_name'       => $categoryName,
238
                'file_category_identifier' => $categoryIdentifier,
239
            ],
240
        ];
241
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
242
243
        $this->readConnectionMock
244
            ->expects($this->once())
245
            ->method('prepare')
246
            ->with($sql0)
247
            ->willReturn($statement0);
248
249
        $actualResult = $this->sut->getPublicByCategoryIdentifiers([$categoryIdentifier]);
250
251
        $this->assertCollection($expectedData, $actualResult);
252
    }
253
254
    public function testGetPublicByCategoryIdentifiersCanReturnEarly()
255
    {
256
        $actualResult = $this->sut->getPublicByCategoryIdentifiers([]);
257
258
        $this->assertSame([], $actualResult);
259
    }
260
261
    public function testGetPublicByFilesystemName()
262
    {
263
        $id                 = '54d0ff01-f6b7-4058-9fcd-40f847cf2aef';
264
        $filesystemName     = 'foo';
265
        $publicName         = 'bar';
266
        $mime               = 'text/yax';
267
        $description        = 'baz';
268
        $categoryId         = 'fc14a949-03cc-4d7a-8a71-7ee31d4d3be2';
269
        $categoryName       = 'qux';
270
        $categoryIdentifier = 'quuux';
271
        $uploadedAt         = new \DateTime();
272
273
        $sql0         = 'SELECT files.id, files.filesystem_name, files.public_name, files.mime, files.file_category_id, files.description, files.uploaded_at, file_categories.name AS file_category_name, file_categories.identifier AS file_category_identifier FROM files INNER JOIN file_categories AS file_categories ON file_categories.id = files.file_category_id AND file_categories.deleted_at IS NULL WHERE (files.deleted_at IS NULL) AND (files.filesystem_name = :filesystem_name) AND (file_categories.is_public = 1) GROUP BY files.id'; // phpcs:ignore
274
        $values       = [
275
            'filesystem_name' => [$filesystemName, \PDO::PARAM_STR],
276
        ];
277
        $expectedData = [
278
            [
279
                'id'                       => $id,
280
                'filesystem_name'          => $filesystemName,
281
                'public_name'              => $publicName,
282
                'file_category_id'         => $categoryId,
283
                'mime'                     => $mime,
284
                'description'              => $description,
285
                'uploaded_at'              => $uploadedAt->format(File::DATE_FORMAT),
286
                'file_category_name'       => $categoryName,
287
                'file_category_identifier' => $categoryIdentifier,
288
            ],
289
        ];
290
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
291
292
        $this->readConnectionMock
293
            ->expects($this->once())
294
            ->method('prepare')
295
            ->with($sql0)
296
            ->willReturn($statement0);
297
298
        $actualResult = $this->sut->getPublicByFilesystemName($filesystemName);
299
300
        $this->assertEntity($expectedData[0], $actualResult);
301
    }
302
303
    public function testGetById()
304
    {
305
        $id                 = '456cdb27-c8e8-4ab5-84c0-2d20d470521f';
306
        $filesystemName     = 'foo';
307
        $publicName         = 'bar';
308
        $mime               = 'text/yax';
309
        $description        = 'baz';
310
        $categoryId         = 'd6ba660f-d131-4dfa-825a-81e7f3f69fcb';
311
        $categoryName       = 'qux';
312
        $categoryIdentifier = 'quux';
313
        $uploadedAt         = new \DateTime();
314
315
        $sql0         = 'SELECT files.id, files.filesystem_name, files.public_name, files.mime, files.file_category_id, files.description, files.uploaded_at, file_categories.name AS file_category_name, file_categories.identifier AS file_category_identifier FROM files INNER JOIN file_categories AS file_categories ON file_categories.id = files.file_category_id AND file_categories.deleted_at IS NULL WHERE (files.deleted_at IS NULL) AND (files.id = :file_id) GROUP BY files.id'; // phpcs:ignore
316
        $values       = ['file_id' => [$id, \PDO::PARAM_STR]];
317
        $expectedData = [
318
            [
319
                'id'                       => $id,
320
                'filesystem_name'          => $filesystemName,
321
                'public_name'              => $publicName,
322
                'mime'                     => $mime,
323
                'file_category_id'         => $categoryId,
324
                'description'              => $description,
325
                'uploaded_at'              => $uploadedAt->format(File::DATE_FORMAT),
326
                'file_category_name'       => $categoryName,
327
                'file_category_identifier' => $categoryIdentifier,
328
            ],
329
        ];
330
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
331
332
        $this->readConnectionMock
333
            ->expects($this->once())
334
            ->method('prepare')
335
            ->with($sql0)
336
            ->willReturn($statement0);
337
338
        $actualResult = $this->sut->getById($id);
339
340
        $this->assertEntity($expectedData[0], $actualResult);
341
    }
342
343
    public function testGetByFilesystemName()
344
    {
345
        $id                 = '456cdb27-c8e8-4ab5-84c0-2d20d470521f';
346
        $filesystemName     = 'foo';
347
        $publicName         = 'bar';
348
        $mime               = 'text/yax';
349
        $description        = 'baz';
350
        $categoryId         = 'd6ba660f-d131-4dfa-825a-81e7f3f69fcb';
351
        $categoryName       = 'qux';
352
        $categoryIdentifier = 'quux';
353
        $uploadedAt         = new \DateTime();
354
355
        $sql0         = 'SELECT files.id, files.filesystem_name, files.public_name, files.mime, files.file_category_id, files.description, files.uploaded_at, file_categories.name AS file_category_name, file_categories.identifier AS file_category_identifier FROM files INNER JOIN file_categories AS file_categories ON file_categories.id = files.file_category_id AND file_categories.deleted_at IS NULL WHERE (files.deleted_at IS NULL) AND (files.filesystem_name = :filesystem_name) GROUP BY files.id'; // phpcs:ignore
356
        $values       = ['filesystem_name' => [$filesystemName, \PDO::PARAM_STR]];
357
        $expectedData = [
358
            [
359
                'id'                       => $id,
360
                'filesystem_name'          => $filesystemName,
361
                'public_name'              => $publicName,
362
                'mime'                     => $mime,
363
                'file_category_id'         => $categoryId,
364
                'description'              => $description,
365
                'uploaded_at'              => $uploadedAt->format(File::DATE_FORMAT),
366
                'file_category_name'       => $categoryName,
367
                'file_category_identifier' => $categoryIdentifier,
368
            ],
369
        ];
370
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
371
372
        $this->readConnectionMock
373
            ->expects($this->once())
374
            ->method('prepare')
375
            ->with($sql0)
376
            ->willReturn($statement0);
377
378
        $actualResult = $this->sut->getByFilesystemName($filesystemName);
379
380
        $this->assertEntity($expectedData[0], $actualResult);
381
    }
382
383
    public function testGetByUserId()
384
    {
385
        $userId = '673459fb-1f34-4339-8436-3fff0774fcf1';
386
387
        $id                 = '5574edba-c3c3-4bed-be07-72921add67b4';
388
        $filesystemName     = 'foo';
389
        $publicName         = 'bar';
390
        $mime               = 'text/yax';
391
        $description        = 'baz';
392
        $categoryId         = '09da12c0-e92b-4a08-a83e-6cb573a8cf79';
393
        $categoryName       = 'qux';
394
        $categoryIdentifier = 'quuux';
395
        $uploadedAt         = new \DateTime();
396
397
        $sql0         = 'SELECT files.id, files.filesystem_name, files.public_name, files.mime, files.file_category_id, files.description, files.uploaded_at, file_categories.name AS file_category_name, file_categories.identifier AS file_category_identifier FROM files INNER JOIN file_categories AS file_categories ON file_categories.id = files.file_category_id AND file_categories.deleted_at IS NULL INNER JOIN user_groups_file_categories AS ugfc ON file_categories.id = ugfc.file_category_id AND file_categories.deleted_at IS NULL INNER JOIN user_groups AS user_groups ON user_groups.id = ugfc.user_group_id AND user_groups.deleted_at IS NULL WHERE (files.deleted_at IS NULL) AND (user_groups.user_id = :user_id) GROUP BY files.id'; // phpcs:ignore
398
        $values       = ['user_id' => [$userId, \PDO::PARAM_STR]];
399
        $expectedData = [
400
            [
401
                'id'                       => $id,
402
                'filesystem_name'          => $filesystemName,
403
                'public_name'              => $publicName,
404
                'mime'                     => $mime,
405
                'file_category_id'         => $categoryId,
406
                'description'              => $description,
407
                'uploaded_at'              => $uploadedAt->format(File::DATE_FORMAT),
408
                'file_category_name'       => $categoryName,
409
                'file_category_identifier' => $categoryIdentifier,
410
            ],
411
        ];
412
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
413
414
        $this->readConnectionMock
415
            ->expects($this->once())
416
            ->method('prepare')
417
            ->with($sql0)
418
            ->willReturn($statement0);
419
420
        $actualResult = $this->sut->getByUserId($userId);
421
422
        $this->assertCollection($expectedData, $actualResult);
423
    }
424
425
    public function testUpdate()
426
    {
427
        $id                 = '542260d0-25be-4088-9253-9ad2bef63ac2';
428
        $filesystemName     = 'foo';
429
        $publicName         = 'bar';
430
        $mime               = 'text/yax';
431
        $description        = 'baz';
432
        $categoryId         = '29a22dcb-c4dd-4d2a-8a50-b282ed0b9b0b';
433
        $categoryIdentifier = 'quux';
434
        $categoryName       = 'qux';
435
        $categoryIsPublic   = false;
436
        $uploadedAt         = new \DateTime();
437
438
        $sql0       = 'UPDATE files AS files SET filesystem_name = ?, public_name = ?, mime = ?, description = ?, uploaded_at = ?, file_category_id = ? WHERE (id = ?) AND (deleted_at IS NULL)'; // phpcs:ignore
439
        $values     = [
440
            [$filesystemName, \PDO::PARAM_STR],
441
            [$publicName, \PDO::PARAM_STR],
442
            [$description, \PDO::PARAM_STR],
443
            [$mime, \PDO::PARAM_STR],
444
            [$uploadedAt->format(File::DATE_FORMAT), \PDO::PARAM_STR],
445
            [$categoryId, \PDO::PARAM_STR],
446
            [$id, \PDO::PARAM_STR],
447
        ];
448
        $statement0 = MockStatementFactory::createWriteStatement($this, $values);
449
450
        $this->writeConnectionMock
451
            ->expects($this->once())
452
            ->method('prepare')
453
            ->with($sql0)
454
            ->willReturn($statement0);
455
456
        $category = new FileCategory($categoryId, $categoryIdentifier, $categoryName, $categoryIsPublic);
457
        $entity   = new File($id, $filesystemName, $publicName, $description, $mime, $category, $uploadedAt);
458
459
        $this->sut->update($entity);
460
    }
461
462
    /**
463
     * @param array $expectedData
464
     * @param File  $entity
465
     */
466
    protected function assertEntity(array $expectedData, $entity)
467
    {
468
        $uploadedAt = $entity->getUploadedAt()->format(File::DATE_FORMAT);
469
470
        $this->assertInstanceOf(File::class, $entity);
471
        $this->assertSame($expectedData['id'], $entity->getId());
472
        $this->assertSame($expectedData['filesystem_name'], $entity->getFilesystemName());
473
        $this->assertSame($expectedData['public_name'], $entity->getPublicName());
474
        $this->assertSame($expectedData['mime'], $entity->getMime());
475
        $this->assertSame($expectedData['file_category_id'], $entity->getCategory()->getId());
476
        $this->assertSame($expectedData['description'], $entity->getDescription());
477
        $this->assertSame($expectedData['uploaded_at'], $uploadedAt);
478
        $this->assertSame($expectedData['file_category_name'], $entity->getCategory()->getName());
479
        $this->assertSame($expectedData['file_category_identifier'], $entity->getCategory()->getIdentifier());
480
    }
481
}
482