FileCategorySqlDataMapperTest::assertUserGroups()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 12
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Files\Orm\DataMappers;
6
7
use AbterPhp\Admin\Domain\Entities\UserGroup;
8
use AbterPhp\Admin\TestCase\Orm\DataMapperTestCase;
9
use AbterPhp\Admin\TestDouble\Orm\MockIdGeneratorFactory;
10
use AbterPhp\Files\Domain\Entities\FileCategory;
11
use AbterPhp\Framework\TestDouble\Database\MockStatementFactory;
12
13
class FileCategorySqlDataMapperTest extends DataMapperTestCase
14
{
15
    /** @var FileCategorySqlDataMapper */
16
    protected $sut;
17
18
    public function setUp(): void
19
    {
20
        parent::setUp();
21
22
        $this->sut = new FileCategorySqlDataMapper($this->readConnectionMock, $this->writeConnectionMock);
23
    }
24
25
    public function testAddWithoutUserGroup()
26
    {
27
        $nextId     = '90962131-c7bc-40c3-8cdf-1a1e3b0936e4';
28
        $identifier = 'bar';
29
        $name       = 'foo';
30
        $isPublic   = true;
31
32
        $sql0       = 'INSERT INTO file_categories (id, identifier, name, is_public) VALUES (?, ?, ?, ?)'; // phpcs:ignore
33
        $values     = [
34
            [$nextId, \PDO::PARAM_STR],
35
            [$identifier, \PDO::PARAM_STR],
36
            [$name, \PDO::PARAM_STR],
37
            [$isPublic, \PDO::PARAM_BOOL],
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 FileCategory($nextId, $identifier, $name, $isPublic);
48
49
        $this->sut->add($entity);
50
51
        $this->assertSame($nextId, $entity->getId());
52
    }
53
54
    public function testAddWithUserGroup()
55
    {
56
        $nextId     = '2c86ba76-aadc-4463-a5f4-3d36c8f9e400';
57
        $identifier = 'bar';
58
        $name       = 'foo';
59
        $isPublic   = true;
60
        $ugfc0      = 'aba84d8d-fa47-4960-ae5e-3108b2329df6';
61
        $ugfc1      = '7d9755dc-a2b3-4149-b4c0-6b21b99dd524';
62
        $userGroups = [
63
            new UserGroup('90f489a6-7fd2-4cee-8e15-6e0a11dd5686', '', ''),
64
            new UserGroup('87c7f025-ad53-400c-8bf9-703fe02f88a0', '', ''),
65
        ];
66
67
        $this->sut->setIdGenerator(MockIdGeneratorFactory::create($this, $ugfc0, $ugfc1));
68
69
        $sql0       = 'INSERT INTO file_categories (id, identifier, name, is_public) VALUES (?, ?, ?, ?)'; // phpcs:ignore
70
        $values0    = [
71
            [$nextId, \PDO::PARAM_STR],
72
            [$identifier, \PDO::PARAM_STR],
73
            [$name, \PDO::PARAM_STR],
74
            [$isPublic, \PDO::PARAM_BOOL],
75
        ];
76
        $statement0 = MockStatementFactory::createWriteStatement($this, $values0);
77
78
        $entity = new FileCategory($nextId, $identifier, $name, $isPublic, $userGroups);
79
80
        $sql1       = 'INSERT INTO user_groups_file_categories (id, user_group_id, file_category_id) VALUES (?, ?, ?)'; // phpcs:ignore
81
        $values1    = [
82
            [$ugfc0, \PDO::PARAM_STR],
83
            [$userGroups[0]->getId(), \PDO::PARAM_STR],
84
            [$nextId, \PDO::PARAM_STR],
85
        ];
86
        $statement1 = MockStatementFactory::createWriteStatement($this, $values1);
87
88
        $sql2       = 'INSERT INTO user_groups_file_categories (id, user_group_id, file_category_id) VALUES (?, ?, ?)'; // phpcs:ignore
89
        $values2    = [
90
            [$ugfc1, \PDO::PARAM_STR],
91
            [$userGroups[1]->getId(), \PDO::PARAM_STR],
92
            [$nextId, \PDO::PARAM_STR],
93
        ];
94
        $statement2 = MockStatementFactory::createWriteStatement($this, $values2);
95
96
        $this->writeConnectionMock
97
            ->expects($this->exactly(3))
98
            ->method('prepare')
99
            ->withConsecutive([$sql0], [$sql1], [$sql2])
100
            ->willReturnOnConsecutiveCalls($statement0, $statement1, $statement2);
101
102
        $this->sut->add($entity);
103
104
        $this->assertSame($nextId, $entity->getId());
105
    }
106
107
    public function testDelete()
108
    {
109
        $id         = '42484052-b758-41f8-b55b-d61467596a3f';
110
        $identifier = 'bar';
111
        $name       = 'foo';
112
        $isPublic   = true;
113
114
        $sql0       = 'UPDATE file_categories AS file_categories SET deleted_at = NOW() WHERE (id = ?)'; // phpcs:ignore
115
        $values0    = [[$id, \PDO::PARAM_STR]];
116
        $statement0 = MockStatementFactory::createWriteStatement($this, $values0);
117
118
        $entity = new FileCategory($id, $identifier, $name, $isPublic);
119
120
        $sql1       = 'DELETE FROM user_groups_file_categories WHERE (file_category_id = ?)'; // phpcs:ignore
121
        $values1    = [[$id, \PDO::PARAM_STR]];
122
        $statement1 = MockStatementFactory::createWriteStatement($this, $values1);
123
124
        $this->writeConnectionMock
125
            ->expects($this->exactly(2))
126
            ->method('prepare')
127
            ->withConsecutive([$sql0], [$sql1])
128
            ->willReturnOnConsecutiveCalls($statement0, $statement1);
129
130
        $this->sut->delete($entity);
131
    }
132
133
    public function testGetAll()
134
    {
135
        $id         = '72420a3e-0fb9-4017-9ad8-57d6f2e1d016';
136
        $identifier = 'bar';
137
        $name       = 'foo';
138
        $isPublic   = true;
139
140
        $sql0         = 'SELECT fc.id, fc.identifier, fc.name, fc.is_public, GROUP_CONCAT(ugfc.user_group_id) AS user_group_ids FROM file_categories AS fc LEFT JOIN user_groups_file_categories AS ugfc ON ugfc.file_category_id = fc.id WHERE (fc.deleted_at IS NULL) GROUP BY fc.id'; // phpcs:ignore
141
        $values       = [];
142
        $expectedData = [['id' => $id, 'identifier' => $identifier, 'name' => $name, 'is_public' => $isPublic]];
143
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
144
145
        $this->readConnectionMock
146
            ->expects($this->once())
147
            ->method('prepare')
148
            ->with($sql0)
149
            ->willReturn($statement0);
150
151
        $actualResult = $this->sut->getAll();
152
153
        $this->assertCollection($expectedData, $actualResult);
154
    }
155
156
    public function testGetPage()
157
    {
158
        $id         = '72420a3e-0fb9-4017-9ad8-57d6f2e1d016';
159
        $identifier = 'bar';
160
        $name       = 'foo';
161
        $isPublic   = true;
162
163
        $sql0         = 'SELECT SQL_CALC_FOUND_ROWS fc.id, fc.identifier, fc.name, fc.is_public, GROUP_CONCAT(ugfc.user_group_id) AS user_group_ids FROM file_categories AS fc LEFT JOIN user_groups_file_categories AS ugfc ON ugfc.file_category_id = fc.id WHERE (fc.deleted_at IS NULL) GROUP BY fc.id ORDER BY fc.name ASC LIMIT 10 OFFSET 0'; // phpcs:ignore
164
        $values       = [];
165
        $expectedData = [['id' => $id, 'identifier' => $identifier, 'name' => $name, 'is_public' => $isPublic]];
166
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
167
168
        $this->readConnectionMock
169
            ->expects($this->once())
170
            ->method('prepare')
171
            ->with($sql0)
172
            ->willReturn($statement0);
173
174
        $actualResult = $this->sut->getPage(0, 10, [], [], []);
175
176
        $this->assertCollection($expectedData, $actualResult);
177
    }
178
179
    public function testGetPageWithOrdersAndConditions()
180
    {
181
        $id         = '72420a3e-0fb9-4017-9ad8-57d6f2e1d016';
182
        $identifier = 'bar';
183
        $name       = 'foo';
184
        $isPublic   = true;
185
186
        $orders     = ['fc.name ASC'];
187
        $conditions = ['fc.name LIKE \'abc%\'', 'fc.name LIKE \'%bca\''];
188
189
        $sql0         = "SELECT SQL_CALC_FOUND_ROWS fc.id, fc.identifier, fc.name, fc.is_public, GROUP_CONCAT(ugfc.user_group_id) AS user_group_ids FROM file_categories AS fc LEFT JOIN user_groups_file_categories AS ugfc ON ugfc.file_category_id = fc.id WHERE (fc.deleted_at IS NULL) AND (fc.name LIKE 'abc%') AND (fc.name LIKE '%bca') GROUP BY fc.id ORDER BY fc.name ASC LIMIT 10 OFFSET 0"; // phpcs:ignore
190
        $values       = [];
191
        $expectedData = [['id' => $id, 'identifier' => $identifier, 'name' => $name, 'is_public' => $isPublic]];
192
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
193
194
        $this->readConnectionMock
195
            ->expects($this->once())
196
            ->method('prepare')
197
            ->with($sql0)
198
            ->willReturn($statement0);
199
200
        $actualResult = $this->sut->getPage(0, 10, $orders, $conditions, []);
201
202
        $this->assertCollection($expectedData, $actualResult);
203
    }
204
205
    public function testGetById()
206
    {
207
        $id         = 'c44a45e8-67fb-4e96-85ff-88fb30d1c0e9';
208
        $identifier = 'bar';
209
        $name       = 'foo';
210
        $isPublic   = true;
211
212
        $sql0         = 'SELECT fc.id, fc.identifier, fc.name, fc.is_public, GROUP_CONCAT(ugfc.user_group_id) AS user_group_ids FROM file_categories AS fc LEFT JOIN user_groups_file_categories AS ugfc ON ugfc.file_category_id = fc.id WHERE (fc.deleted_at IS NULL) AND (fc.id = :file_category_id) GROUP BY fc.id'; // phpcs:ignore
213
        $values       = ['file_category_id' => [$id, \PDO::PARAM_STR]];
214
        $expectedData = [['id' => $id, 'identifier' => $identifier, 'name' => $name, 'is_public' => $isPublic]];
215
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
216
217
        $this->readConnectionMock
218
            ->expects($this->once())
219
            ->method('prepare')
220
            ->with($sql0)
221
            ->willReturn($statement0);
222
223
        $actualResult = $this->sut->getById($id);
224
225
        $this->assertEntity($expectedData[0], $actualResult);
226
    }
227
228
    public function testGetByIdWithUserGroups()
229
    {
230
        $id         = 'c44a45e8-67fb-4e96-85ff-88fb30d1c0e9';
231
        $identifier = 'bar';
232
        $name       = 'foo';
233
        $isPublic   = true;
234
        $ugId0      = '11c7ff36-3a00-447e-bcfe-594eee978ff7';
235
        $ugId1      = 'bc577876-3fa4-4bd8-833d-e52b9ff7b94d';
236
237
        $sql0         = 'SELECT fc.id, fc.identifier, fc.name, fc.is_public, GROUP_CONCAT(ugfc.user_group_id) AS user_group_ids FROM file_categories AS fc LEFT JOIN user_groups_file_categories AS ugfc ON ugfc.file_category_id = fc.id WHERE (fc.deleted_at IS NULL) AND (fc.id = :file_category_id) GROUP BY fc.id'; // phpcs:ignore
238
        $values       = ['file_category_id' => [$id, \PDO::PARAM_STR]];
239
        $expectedData = [
240
            [
241
                'id'             => $id,
242
                'identifier'     => $identifier,
243
                'name'           => $name,
244
                'is_public'      => $isPublic,
245
                'user_group_ids' => "$ugId0,$ugId1",
246
            ],
247
        ];
248
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
249
250
        $this->readConnectionMock
251
            ->expects($this->once())
252
            ->method('prepare')
253
            ->with($sql0)
254
            ->willReturn($statement0);
255
256
        $actualResult = $this->sut->getById($id);
257
258
        $this->assertEntity($expectedData[0], $actualResult);
259
    }
260
261
    public function testGetByUserGroupId()
262
    {
263
        $userGroupId = '11c7ff36-3a00-447e-bcfe-594eee978ff7';
264
265
        $id         = '3525f6d8-52ad-4bf2-ad68-15314ccff70d';
266
        $identifier = 'bar';
267
        $name       = 'foo';
268
        $isPublic   = true;
269
        $ugId0      = '11c7ff36-3a00-447e-bcfe-594eee978ff7';
270
        $ugId1      = 'bc577876-3fa4-4bd8-833d-e52b9ff7b94d';
271
272
        $sql0         = 'SELECT fc.id, fc.identifier, fc.name, fc.is_public, GROUP_CONCAT(ugfc.user_group_id) AS user_group_ids FROM file_categories AS fc INNER JOIN user_groups_file_categories AS ugfc2 ON fc.id = ugfc2.file_category_id LEFT JOIN user_groups_file_categories AS ugfc ON ugfc.file_category_id = fc.id WHERE (fc.deleted_at IS NULL) AND (ugfc2.user_group_id = :user_group_id) GROUP BY fc.id'; // phpcs:ignore
273
        $values       = ['ugfc2.user_group_id' => [$userGroupId, \PDO::PARAM_STR]];
274
        $expectedData = [
275
            [
276
                'id'             => $id,
277
                'identifier'     => $identifier,
278
                'name'           => $name,
279
                'is_public'      => $isPublic,
280
                'user_group_ids' => "$ugId0,$ugId1",
281
            ],
282
        ];
283
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
284
285
        $this->readConnectionMock
286
            ->expects($this->once())
287
            ->method('prepare')
288
            ->with($sql0)
289
            ->willReturn($statement0);
290
291
        $actualResult = $this->sut->getByUserGroupId($userGroupId);
292
293
        $this->assertCollection($expectedData, $actualResult);
294
    }
295
296
    public function testUpdateWithoutUserGroup()
297
    {
298
        $id         = 'de8f969e-381e-4655-89db-46c8a7793bb3';
299
        $identifier = 'bar';
300
        $name       = 'foo';
301
        $isPublic   = true;
302
303
        $sql0       = 'UPDATE file_categories AS file_categories SET identifier = ?, name = ?, is_public = ? WHERE (id = ?) AND (deleted_at IS NULL)'; // phpcs:ignore
304
        $values0    = [
305
            [$identifier, \PDO::PARAM_STR],
306
            [$name, \PDO::PARAM_STR],
307
            [$isPublic, \PDO::PARAM_BOOL],
308
            [$id, \PDO::PARAM_STR],
309
        ];
310
        $statement0 = MockStatementFactory::createWriteStatement($this, $values0);
311
312
        $entity = new FileCategory($id, $identifier, $name, $isPublic);
313
314
        $sql1       = 'DELETE FROM user_groups_file_categories WHERE (file_category_id = ?)'; // phpcs:ignore
315
        $values1    = [
316
            [$id, \PDO::PARAM_STR],
317
        ];
318
        $statement1 = MockStatementFactory::createWriteStatement($this, $values1);
319
320
        $this->writeConnectionMock
321
            ->expects($this->exactly(2))
322
            ->method('prepare')
323
            ->withConsecutive([$sql0], [$sql1])
324
            ->willReturnOnConsecutiveCalls($statement0, $statement1);
325
326
        $this->sut->update($entity);
327
    }
328
329
    public function testUpdateWithUserGroup()
330
    {
331
        $id         = 'a441487b-0bee-4137-8f76-c2a2b8d8c058';
332
        $identifier = 'bar';
333
        $name       = 'foo';
334
        $isPublic   = true;
335
        $ugfc0      = '6ac51550-d682-44b3-906e-0a8dac6f555f';
336
        $ugfc1      = '5791b3e6-18ce-4132-9ec1-d31a26a22c3d';
337
        $userGroups = [
338
            new UserGroup('4206761a-00f9-4285-8721-da7d2a1677bf', '', ''),
339
            new UserGroup('15e94e76-dc94-47fa-87f4-db97995d195e', '', ''),
340
        ];
341
342
        $this->sut->setIdGenerator(MockIdGeneratorFactory::create($this, $ugfc0, $ugfc1));
343
344
        $sql0       = 'UPDATE file_categories AS file_categories SET identifier = ?, name = ?, is_public = ? WHERE (id = ?) AND (deleted_at IS NULL)'; // phpcs:ignore
345
        $values0    = [
346
            [$identifier, \PDO::PARAM_STR],
347
            [$name, \PDO::PARAM_STR],
348
            [$isPublic, \PDO::PARAM_BOOL],
349
            [$id, \PDO::PARAM_STR],
350
        ];
351
        $statement0 = MockStatementFactory::createWriteStatement($this, $values0);
352
353
        $entity = new FileCategory($id, $identifier, $name, $isPublic, $userGroups);
354
355
        $sql1       = 'DELETE FROM user_groups_file_categories WHERE (file_category_id = ?)'; // phpcs:ignore
356
        $values1    = [
357
            [$id, \PDO::PARAM_STR],
358
        ];
359
        $statement1 = MockStatementFactory::createWriteStatement($this, $values1);
360
361
        $sql2       = 'INSERT INTO user_groups_file_categories (id, user_group_id, file_category_id) VALUES (?, ?, ?)'; // phpcs:ignore
362
        $values2    = [
363
            [$ugfc0, \PDO::PARAM_STR],
364
            [$userGroups[0]->getId(), \PDO::PARAM_STR],
365
            [$id, \PDO::PARAM_STR],
366
        ];
367
        $statement2 = MockStatementFactory::createWriteStatement($this, $values2);
368
369
        $sql3       = 'INSERT INTO user_groups_file_categories (id, user_group_id, file_category_id) VALUES (?, ?, ?)'; // phpcs:ignore
370
        $values3    = [
371
            [$ugfc1, \PDO::PARAM_STR],
372
            [$userGroups[1]->getId(), \PDO::PARAM_STR],
373
            [$id, \PDO::PARAM_STR],
374
        ];
375
        $statement3 = MockStatementFactory::createWriteStatement($this, $values3);
376
377
        $this->writeConnectionMock
378
            ->expects($this->exactly(4))
379
            ->method('prepare')
380
            ->withConsecutive([$sql0], [$sql1], [$sql2], [$sql3])
381
            ->willReturnOnConsecutiveCalls($statement0, $statement1, $statement2, $statement3);
382
383
        $this->sut->update($entity);
384
    }
385
386
    /**
387
     * @param array        $expectedData
388
     * @param FileCategory $entity
389
     */
390
    protected function assertEntity(array $expectedData, $entity)
391
    {
392
        $this->assertInstanceOf(FileCategory::class, $entity);
393
        $this->assertEquals($expectedData['id'], $entity->getId());
394
        $this->assertSame($expectedData['name'], $entity->getName());
395
        $this->assertSame($expectedData['is_public'], $entity->isPublic());
396
397
        $this->assertUserGroups($expectedData, $entity);
398
    }
399
400
    /**
401
     * @param array        $expectedData
402
     * @param FileCategory $entity
403
     */
404
    protected function assertUserGroups(array $expectedData, $entity)
405
    {
406
        if (empty($expectedData['user_group_ids'])) {
407
            return;
408
        }
409
410
        $ugIds = [];
411
        foreach ($entity->getUserGroups() as $userGroup) {
412
            $ugIds[] = $userGroup->getId();
413
        }
414
415
        $this->assertSame($expectedData['user_group_ids'], implode(',', $ugIds));
416
    }
417
}
418