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