1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Website\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\Framework\Domain\Entities\IStringerEntity; |
11
|
|
|
use AbterPhp\Framework\TestDouble\Database\MockStatementFactory; |
12
|
|
|
use AbterPhp\Website\Domain\Entities\PageCategory; |
13
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
14
|
|
|
|
15
|
|
|
class PageCategorySqlDataMapperTest extends DataMapperTestCase |
16
|
|
|
{ |
17
|
|
|
/** @var PageCategorySqlDataMapper - System Under Test */ |
18
|
|
|
protected $sut; |
19
|
|
|
|
20
|
|
|
public function setUp(): void |
21
|
|
|
{ |
22
|
|
|
parent::setUp(); |
23
|
|
|
|
24
|
|
|
$this->sut = new PageCategorySqlDataMapper($this->readConnectionMock, $this->writeConnectionMock); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testAdd() |
28
|
|
|
{ |
29
|
|
|
$nextId = 'c840500d-bd00-410a-912e-e923b8e965e3'; |
30
|
|
|
$identifier = 'foo'; |
31
|
|
|
$name = 'bar'; |
32
|
|
|
|
33
|
|
|
$sql0 = 'INSERT INTO page_categories (id, name, identifier) VALUES (?, ?, ?)'; // phpcs:ignore |
34
|
|
|
$values = [[$nextId, \PDO::PARAM_STR], [$name, \PDO::PARAM_STR], [$identifier, \PDO::PARAM_STR]]; |
35
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values); |
36
|
|
|
|
37
|
|
|
$this->writeConnectionMock |
38
|
|
|
->expects($this->once()) |
39
|
|
|
->method('prepare') |
40
|
|
|
->with($sql0) |
41
|
|
|
->willReturn($statement0); |
42
|
|
|
|
43
|
|
|
$entity = new PageCategory($nextId, $name, $identifier); |
44
|
|
|
|
45
|
|
|
$this->sut->add($entity); |
46
|
|
|
|
47
|
|
|
$this->assertSame($nextId, $entity->getId()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testDelete() |
51
|
|
|
{ |
52
|
|
|
$id = '1dab2760-9aaa-4f36-a303-42b12e65d165'; |
53
|
|
|
$identifier = 'foo'; |
54
|
|
|
$name = 'bar'; |
55
|
|
|
|
56
|
|
|
$sql0 = 'UPDATE page_categories AS page_categories SET deleted_at = NOW() WHERE (id = ?)'; // phpcs:ignore |
57
|
|
|
$values0 = [[$id, \PDO::PARAM_STR]]; |
58
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values0); |
59
|
|
|
|
60
|
|
|
$sql1 = 'DELETE FROM user_groups_page_categories WHERE (page_category_id = ?)'; // phpcs:ignore |
61
|
|
|
$values1 = [[$id, \PDO::PARAM_STR]]; |
62
|
|
|
$statement1 = MockStatementFactory::createWriteStatement($this, $values1); |
63
|
|
|
|
64
|
|
|
$this->writeConnectionMock |
65
|
|
|
->expects($this->exactly(2)) |
66
|
|
|
->method('prepare') |
67
|
|
|
->withConsecutive([$sql0], [$sql1]) |
68
|
|
|
->willReturnOnConsecutiveCalls($statement0, $statement1); |
69
|
|
|
|
70
|
|
|
$entity = new PageCategory($id, $name, $identifier); |
71
|
|
|
|
72
|
|
|
$this->sut->delete($entity); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testGetAll() |
76
|
|
|
{ |
77
|
|
|
$id = 'df6b4637-634e-4544-a167-2bddf3eab498'; |
78
|
|
|
$identifier = 'foo'; |
79
|
|
|
$name = 'bar'; |
80
|
|
|
|
81
|
|
|
$sql0 = 'SELECT pc.id, pc.name, pc.identifier, GROUP_CONCAT(ugpc.user_group_id) AS user_group_ids FROM page_categories AS pc LEFT JOIN user_groups_page_categories AS ugpc ON ugpc.page_category_id = pc.id WHERE (pc.deleted_at IS NULL) GROUP BY pc.id'; // phpcs:ignore |
82
|
|
|
$values = []; |
83
|
|
|
$expectedData = [ |
84
|
|
|
[ |
85
|
|
|
'id' => $id, |
86
|
|
|
'name' => $name, |
87
|
|
|
'identifier' => $identifier, |
88
|
|
|
], |
89
|
|
|
]; |
90
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
91
|
|
|
|
92
|
|
|
$this->readConnectionMock |
93
|
|
|
->expects($this->once()) |
94
|
|
|
->method('prepare') |
95
|
|
|
->with($sql0) |
96
|
|
|
->willReturn($statement0); |
97
|
|
|
|
98
|
|
|
$actualResult = $this->sut->getAll(); |
99
|
|
|
|
100
|
|
|
$this->assertCollection($expectedData, $actualResult); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testGetPage() |
104
|
|
|
{ |
105
|
|
|
$id = 'df6b4637-634e-4544-a167-2bddf3eab498'; |
106
|
|
|
$identifier = 'foo'; |
107
|
|
|
$name = 'bar'; |
108
|
|
|
|
109
|
|
|
$sql0 = 'SELECT SQL_CALC_FOUND_ROWS pc.id, pc.name, pc.identifier, GROUP_CONCAT(ugpc.user_group_id) AS user_group_ids FROM page_categories AS pc LEFT JOIN user_groups_page_categories AS ugpc ON ugpc.page_category_id = pc.id WHERE (pc.deleted_at IS NULL) GROUP BY pc.id ORDER BY pc.name ASC LIMIT 10 OFFSET 0'; // phpcs:ignore |
110
|
|
|
$values = []; |
111
|
|
|
$expectedData = [ |
112
|
|
|
[ |
113
|
|
|
'id' => $id, |
114
|
|
|
'name' => $name, |
115
|
|
|
'identifier' => $identifier, |
116
|
|
|
], |
117
|
|
|
]; |
118
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
119
|
|
|
|
120
|
|
|
$this->readConnectionMock |
121
|
|
|
->expects($this->once()) |
122
|
|
|
->method('prepare') |
123
|
|
|
->with($sql0) |
124
|
|
|
->willReturn($statement0); |
125
|
|
|
|
126
|
|
|
$actualResult = $this->sut->getPage(0, 10, [], [], []); |
127
|
|
|
|
128
|
|
|
$this->assertCollection($expectedData, $actualResult); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testGetPageWithOrdersAndConditions() |
132
|
|
|
{ |
133
|
|
|
$id = 'df6b4637-634e-4544-a167-2bddf3eab498'; |
134
|
|
|
$identifier = 'foo'; |
135
|
|
|
$name = 'bar'; |
136
|
|
|
|
137
|
|
|
$orders = ['pc.identifier ASC']; |
138
|
|
|
$conditions = ['pc.identifier LIKE \'abc%\'', 'pc.identifier LIKE \'%bca\'']; |
139
|
|
|
|
140
|
|
|
$sql0 = 'SELECT SQL_CALC_FOUND_ROWS pc.id, pc.name, pc.identifier, GROUP_CONCAT(ugpc.user_group_id) AS user_group_ids FROM page_categories AS pc LEFT JOIN user_groups_page_categories AS ugpc ON ugpc.page_category_id = pc.id WHERE (pc.deleted_at IS NULL) AND (pc.identifier LIKE \'abc%\') AND (pc.identifier LIKE \'%bca\') GROUP BY pc.id ORDER BY pc.identifier ASC LIMIT 10 OFFSET 0'; // phpcs:ignore |
141
|
|
|
$values = []; |
142
|
|
|
$expectedData = [ |
143
|
|
|
[ |
144
|
|
|
'id' => $id, |
145
|
|
|
'name' => $name, |
146
|
|
|
'identifier' => $identifier, |
147
|
|
|
], |
148
|
|
|
]; |
149
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
150
|
|
|
|
151
|
|
|
$this->readConnectionMock |
152
|
|
|
->expects($this->once()) |
153
|
|
|
->method('prepare') |
154
|
|
|
->with($sql0) |
155
|
|
|
->willReturn($statement0); |
156
|
|
|
|
157
|
|
|
$actualResult = $this->sut->getPage(0, 10, $orders, $conditions, []); |
158
|
|
|
|
159
|
|
|
$this->assertCollection($expectedData, $actualResult); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function testGetById() |
163
|
|
|
{ |
164
|
|
|
$id = 'fc2bdb23-bdd1-49aa-8613-b5e0ce76450d'; |
165
|
|
|
$identifier = 'foo'; |
166
|
|
|
$name = 'bar'; |
167
|
|
|
|
168
|
|
|
$sql0 = 'SELECT pc.id, pc.name, pc.identifier, GROUP_CONCAT(ugpc.user_group_id) AS user_group_ids FROM page_categories AS pc LEFT JOIN user_groups_page_categories AS ugpc ON ugpc.page_category_id = pc.id WHERE (pc.deleted_at IS NULL) AND (pc.id = :category_id) GROUP BY pc.id'; // phpcs:ignore |
169
|
|
|
$values = ['category_id' => [$id, \PDO::PARAM_STR]]; |
170
|
|
|
$expectedData = [ |
171
|
|
|
[ |
172
|
|
|
'id' => $id, |
173
|
|
|
'name' => $name, |
174
|
|
|
'identifier' => $identifier, |
175
|
|
|
], |
176
|
|
|
]; |
177
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
178
|
|
|
|
179
|
|
|
$this->readConnectionMock |
180
|
|
|
->expects($this->once()) |
181
|
|
|
->method('prepare') |
182
|
|
|
->with($sql0) |
183
|
|
|
->willReturn($statement0); |
184
|
|
|
|
185
|
|
|
$actualResult = $this->sut->getById($id); |
186
|
|
|
|
187
|
|
|
$this->assertEntity($expectedData[0], $actualResult); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function testGetByIdWithUserGroups() |
191
|
|
|
{ |
192
|
|
|
$id = 'fc2bdb23-bdd1-49aa-8613-b5e0ce76450d'; |
193
|
|
|
$identifier = 'foo'; |
194
|
|
|
$name = 'bar'; |
195
|
|
|
|
196
|
|
|
$ugId0 = '92dcb09a-eb3b-49b8-96c2-1a37818c780c'; |
197
|
|
|
$ugId1 = '2f962fe9-7e5b-4e06-a02f-bcd68152a83c'; |
198
|
|
|
|
199
|
|
|
$sql0 = 'SELECT pc.id, pc.name, pc.identifier, GROUP_CONCAT(ugpc.user_group_id) AS user_group_ids FROM page_categories AS pc LEFT JOIN user_groups_page_categories AS ugpc ON ugpc.page_category_id = pc.id WHERE (pc.deleted_at IS NULL) AND (pc.id = :category_id) GROUP BY pc.id'; // phpcs:ignore |
200
|
|
|
$values = ['category_id' => [$id, \PDO::PARAM_STR]]; |
201
|
|
|
$expectedData = [ |
202
|
|
|
[ |
203
|
|
|
'id' => $id, |
204
|
|
|
'name' => $name, |
205
|
|
|
'identifier' => $identifier, |
206
|
|
|
'user_group_ids' => "$ugId0,$ugId1", |
207
|
|
|
], |
208
|
|
|
]; |
209
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
210
|
|
|
|
211
|
|
|
$this->readConnectionMock |
212
|
|
|
->expects($this->once()) |
213
|
|
|
->method('prepare') |
214
|
|
|
->with($sql0) |
215
|
|
|
->willReturn($statement0); |
216
|
|
|
|
217
|
|
|
$actualResult = $this->sut->getById($id); |
218
|
|
|
|
219
|
|
|
$this->assertEntity($expectedData[0], $actualResult); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function testGetByIdentifier() |
223
|
|
|
{ |
224
|
|
|
$id = '4a7847c6-d202-4fc7-972b-bd4d634efda1'; |
225
|
|
|
$identifier = 'foo'; |
226
|
|
|
$name = 'bar'; |
227
|
|
|
|
228
|
|
|
$sql0 = 'SELECT pc.id, pc.name, pc.identifier, GROUP_CONCAT(ugpc.user_group_id) AS user_group_ids FROM page_categories AS pc LEFT JOIN user_groups_page_categories AS ugpc ON ugpc.page_category_id = pc.id WHERE (pc.deleted_at IS NULL) AND (identifier = :identifier) GROUP BY pc.id'; // phpcs:ignore |
229
|
|
|
$values = ['identifier' => $identifier]; |
230
|
|
|
$expectedData = [ |
231
|
|
|
[ |
232
|
|
|
'id' => $id, |
233
|
|
|
'name' => $name, |
234
|
|
|
'identifier' => $identifier, |
235
|
|
|
], |
236
|
|
|
]; |
237
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
238
|
|
|
|
239
|
|
|
$this->readConnectionMock |
240
|
|
|
->expects($this->once()) |
241
|
|
|
->method('prepare') |
242
|
|
|
->with($sql0) |
243
|
|
|
->willReturn($statement0); |
244
|
|
|
|
245
|
|
|
$actualResult = $this->sut->getByIdentifier($identifier); |
246
|
|
|
|
247
|
|
|
$this->assertEntity($expectedData[0], $actualResult); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function testUpdateWithoutUserGroup() |
251
|
|
|
{ |
252
|
|
|
$id = 'de8f969e-381e-4655-89db-46c8a7793bb3'; |
253
|
|
|
$identifier = 'bar'; |
254
|
|
|
$name = 'foo'; |
255
|
|
|
|
256
|
|
|
$sql0 = 'UPDATE page_categories AS page_categories SET name = ?, identifier = ? WHERE (id = ?) AND (deleted_at IS NULL)'; // phpcs:ignore |
257
|
|
|
$values0 = [ |
258
|
|
|
[$name, \PDO::PARAM_STR], |
259
|
|
|
[$identifier, \PDO::PARAM_STR], |
260
|
|
|
[$id, \PDO::PARAM_STR], |
261
|
|
|
]; |
262
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values0); |
263
|
|
|
|
264
|
|
|
$sql1 = 'DELETE FROM user_groups_page_categories WHERE (page_category_id = ?)'; // phpcs:ignore |
265
|
|
|
$values1 = [ |
266
|
|
|
[$id, \PDO::PARAM_STR], |
267
|
|
|
]; |
268
|
|
|
$statement1 = MockStatementFactory::createWriteStatement($this, $values1); |
269
|
|
|
|
270
|
|
|
$this->writeConnectionMock |
271
|
|
|
->expects($this->exactly(2)) |
272
|
|
|
->method('prepare') |
273
|
|
|
->withConsecutive([$sql0], [$sql1]) |
274
|
|
|
->willReturnOnConsecutiveCalls($statement0, $statement1); |
275
|
|
|
|
276
|
|
|
$entity = new PageCategory($id, $name, $identifier); |
277
|
|
|
|
278
|
|
|
$this->sut->update($entity); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
public function testUpdateWithUserGroup() |
282
|
|
|
{ |
283
|
|
|
$pcId = 'a441487b-0bee-4137-8f76-c2a2b8d8c058'; |
284
|
|
|
$identifier = 'bar'; |
285
|
|
|
$name = 'foo'; |
286
|
|
|
$ugpc0 = '6ac51550-d682-44b3-906e-0a8dac6f555f'; |
287
|
|
|
$ugpc1 = '5791b3e6-18ce-4132-9ec1-d31a26a22c3d'; |
288
|
|
|
$userGroups = [ |
289
|
|
|
new UserGroup('4206761a-00f9-4285-8721-da7d2a1677bf', '', ''), |
290
|
|
|
new UserGroup('15e94e76-dc94-47fa-87f4-db97995d195e', '', ''), |
291
|
|
|
]; |
292
|
|
|
|
293
|
|
|
$this->sut->setIdGenerator(MockIdGeneratorFactory::create($this, $ugpc0, $ugpc1)); |
294
|
|
|
|
295
|
|
|
$sql0 = 'UPDATE page_categories AS page_categories SET name = ?, identifier = ? WHERE (id = ?) AND (deleted_at IS NULL)'; // phpcs:ignore |
296
|
|
|
$values0 = [ |
297
|
|
|
[$name, \PDO::PARAM_STR], |
298
|
|
|
[$identifier, \PDO::PARAM_STR], |
299
|
|
|
[$pcId, \PDO::PARAM_STR], |
300
|
|
|
]; |
301
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values0); |
302
|
|
|
|
303
|
|
|
$sql1 = 'DELETE FROM user_groups_page_categories WHERE (page_category_id = ?)'; // phpcs:ignore |
304
|
|
|
$values1 = [ |
305
|
|
|
[$pcId, \PDO::PARAM_STR], |
306
|
|
|
]; |
307
|
|
|
$statement1 = MockStatementFactory::createWriteStatement($this, $values1); |
308
|
|
|
|
309
|
|
|
$sql2 = 'INSERT INTO user_groups_page_categories (id, user_group_id, page_category_id) VALUES (?, ?, ?)'; // phpcs:ignore |
310
|
|
|
$values2 = [ |
311
|
|
|
[$ugpc0, \PDO::PARAM_STR], |
312
|
|
|
[$userGroups[0]->getId(), \PDO::PARAM_STR], |
313
|
|
|
[$pcId, \PDO::PARAM_STR], |
314
|
|
|
]; |
315
|
|
|
$statement2 = MockStatementFactory::createWriteStatement($this, $values2); |
316
|
|
|
|
317
|
|
|
$sql3 = 'INSERT INTO user_groups_page_categories (id, user_group_id, page_category_id) VALUES (?, ?, ?)'; // phpcs:ignore |
318
|
|
|
$values3 = [ |
319
|
|
|
[$ugpc1, \PDO::PARAM_STR], |
320
|
|
|
[$userGroups[1]->getId(), \PDO::PARAM_STR], |
321
|
|
|
[$pcId, \PDO::PARAM_STR], |
322
|
|
|
]; |
323
|
|
|
$statement3 = MockStatementFactory::createWriteStatement($this, $values3); |
324
|
|
|
|
325
|
|
|
$this->writeConnectionMock |
326
|
|
|
->expects($this->exactly(4)) |
327
|
|
|
->method('prepare') |
328
|
|
|
->withConsecutive([$sql0], [$sql1], [$sql2], [$sql3]) |
329
|
|
|
->willReturnOnConsecutiveCalls($statement0, $statement1, $statement2, $statement3); |
330
|
|
|
|
331
|
|
|
$entity = new PageCategory($pcId, $name, $identifier, $userGroups); |
332
|
|
|
|
333
|
|
|
$this->sut->update($entity); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
public function testAddThrowsExceptionIfCalledWithInvalidEntity() |
337
|
|
|
{ |
338
|
|
|
$this->expectException(\InvalidArgumentException::class); |
339
|
|
|
|
340
|
|
|
/** @var IStringerEntity|MockObject $entity */ |
341
|
|
|
$entity = $this->createMock(IStringerEntity::class); |
342
|
|
|
|
343
|
|
|
$this->sut->add($entity); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
public function testDeleteThrowsExceptionIfCalledWithInvalidEntity() |
347
|
|
|
{ |
348
|
|
|
$this->expectException(\InvalidArgumentException::class); |
349
|
|
|
|
350
|
|
|
/** @var IStringerEntity|MockObject $entity */ |
351
|
|
|
$entity = $this->createMock(IStringerEntity::class); |
352
|
|
|
|
353
|
|
|
$this->sut->delete($entity); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
public function testUpdateThrowsExceptionIfCalledWithInvalidEntity() |
357
|
|
|
{ |
358
|
|
|
$this->expectException(\InvalidArgumentException::class); |
359
|
|
|
|
360
|
|
|
/** @var IStringerEntity|MockObject $entity */ |
361
|
|
|
$entity = $this->createMock(IStringerEntity::class); |
362
|
|
|
|
363
|
|
|
$this->sut->update($entity); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* @param array $expectedData |
368
|
|
|
* @param PageCategory $entity |
369
|
|
|
*/ |
370
|
|
|
protected function assertEntity(array $expectedData, $entity) |
371
|
|
|
{ |
372
|
|
|
$this->assertInstanceOf(PageCategory::class, $entity); |
373
|
|
|
$this->assertSame($expectedData['id'], $entity->getId()); |
374
|
|
|
$this->assertSame($expectedData['identifier'], $entity->getIdentifier()); |
375
|
|
|
$this->assertSame($expectedData['name'], $entity->getName()); |
376
|
|
|
|
377
|
|
|
$this->assertUserGroups($expectedData, $entity); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* @param array $expectedData |
382
|
|
|
* @param PageCategory $entity |
383
|
|
|
*/ |
384
|
|
|
protected function assertUserGroups(array $expectedData, $entity) |
385
|
|
|
{ |
386
|
|
|
if (empty($expectedData['user_group_ids'])) { |
387
|
|
|
return; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
$ugIds = []; |
391
|
|
|
foreach ($entity->getUserGroups() as $ug) { |
392
|
|
|
$ugIds[] = $ug->getId(); |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
$this->assertSame($expectedData['user_group_ids'], implode(',', $ugIds)); |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
|