1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Admin\Orm\DataMappers; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Admin\Domain\Entities\AdminResource; |
8
|
|
|
use AbterPhp\Admin\Domain\Entities\UserGroup; |
9
|
|
|
use AbterPhp\Admin\TestCase\Orm\DataMapperTestCase; |
10
|
|
|
use AbterPhp\Admin\TestDouble\Orm\MockIdGeneratorFactory; |
11
|
|
|
use AbterPhp\Framework\Domain\Entities\IStringerEntity; |
12
|
|
|
use AbterPhp\Framework\TestDouble\Database\MockStatementFactory; |
13
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
14
|
|
|
|
15
|
|
|
class UserGroupSqlDataMapperTest extends DataMapperTestCase |
16
|
|
|
{ |
17
|
|
|
/** @var UserGroupSqlDataMapper */ |
18
|
|
|
protected $sut; |
19
|
|
|
|
20
|
|
|
public function setUp(): void |
21
|
|
|
{ |
22
|
|
|
parent::setUp(); |
23
|
|
|
|
24
|
|
|
$this->sut = new UserGroupSqlDataMapper($this->readConnectionMock, $this->writeConnectionMock); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testAddWithoutRelated() |
28
|
|
|
{ |
29
|
|
|
$nextId = 'dab4e209-8f21-4421-955a-f83fc1527238'; |
30
|
|
|
$identifier = 'foo'; |
31
|
|
|
$name = 'bar'; |
32
|
|
|
|
33
|
|
|
$sql0 = 'INSERT INTO user_groups (id, identifier, name) VALUES (?, ?, ?)'; // phpcs:ignore |
34
|
|
|
$values = [[$nextId, \PDO::PARAM_STR], [$identifier, \PDO::PARAM_STR], [$name, \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 UserGroup($nextId, $identifier, $name); |
44
|
|
|
$this->sut->add($entity); |
45
|
|
|
|
46
|
|
|
$this->assertSame($nextId, $entity->getId()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testAddWithRelated() |
50
|
|
|
{ |
51
|
|
|
$nextId = '4675ecf5-593d-4568-9ff9-434ca25db5a7'; |
52
|
|
|
$identifier = 'foo'; |
53
|
|
|
$name = 'bar'; |
54
|
|
|
$ugarId0 = '7f08b114-3a04-415a-8365-9e67d4a50cea'; |
55
|
|
|
$ugarId1 = '6bd44298-7319-4428-b2b2-29c3d4652f39'; |
56
|
|
|
$adminResources = [ |
57
|
|
|
new AdminResource('a2e64f70-1914-402a-8d49-6d15abb62462', ''), |
58
|
|
|
new AdminResource('58d491ac-2742-401a-85f2-e05470dd1879', ''), |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
$this->sut->setIdGenerator(MockIdGeneratorFactory::create($this, $ugarId0, $ugarId1)); |
62
|
|
|
|
63
|
|
|
$sql0 = 'INSERT INTO user_groups (id, identifier, name) VALUES (?, ?, ?)'; // phpcs:ignore |
64
|
|
|
$values0 = [[$nextId, \PDO::PARAM_STR], [$identifier, \PDO::PARAM_STR], [$name, \PDO::PARAM_STR]]; |
65
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values0); |
66
|
|
|
|
67
|
|
|
$sql1 = 'INSERT INTO user_groups_admin_resources (id, user_group_id, admin_resource_id) VALUES (?, ?, ?)'; // phpcs:ignore |
68
|
|
|
$values1 = [ |
69
|
|
|
[$ugarId0, \PDO::PARAM_STR], |
70
|
|
|
[$nextId, \PDO::PARAM_STR], |
71
|
|
|
[$adminResources[0]->getId(), \PDO::PARAM_STR], |
72
|
|
|
]; |
73
|
|
|
$statement1 = MockStatementFactory::createWriteStatement($this, $values1); |
74
|
|
|
|
75
|
|
|
$sql2 = 'INSERT INTO user_groups_admin_resources (id, user_group_id, admin_resource_id) VALUES (?, ?, ?)'; // phpcs:ignore |
76
|
|
|
$values2 = [ |
77
|
|
|
[$ugarId1, \PDO::PARAM_STR], |
78
|
|
|
[$nextId, \PDO::PARAM_STR], |
79
|
|
|
[$adminResources[1]->getId(), \PDO::PARAM_STR], |
80
|
|
|
]; |
81
|
|
|
$statement2 = MockStatementFactory::createWriteStatement($this, $values2); |
82
|
|
|
|
83
|
|
|
$this->writeConnectionMock |
84
|
|
|
->expects($this->exactly(3)) |
85
|
|
|
->method('prepare') |
86
|
|
|
->withConsecutive([$sql0], [$sql1], [$sql2]) |
87
|
|
|
->willReturnOnConsecutiveCalls($statement0, $statement1, $statement2); |
88
|
|
|
|
89
|
|
|
$entity = new UserGroup($nextId, $identifier, $name, $adminResources); |
90
|
|
|
$this->sut->add($entity); |
91
|
|
|
|
92
|
|
|
$this->assertSame($nextId, $entity->getId()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testDelete() |
96
|
|
|
{ |
97
|
|
|
$id = '7f1b4ea5-6adf-49d8-98a7-9dc820ee8c97'; |
98
|
|
|
$identifier = 'foo'; |
99
|
|
|
$name = 'bar'; |
100
|
|
|
|
101
|
|
|
$sql0 = 'DELETE FROM user_groups_admin_resources WHERE (user_group_id = ?)'; // phpcs:ignore |
102
|
|
|
$values0 = [[$id, \PDO::PARAM_STR]]; |
103
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values0); |
104
|
|
|
|
105
|
|
|
$sql1 = 'UPDATE user_groups AS user_groups SET deleted_at = NOW() WHERE (id = ?)'; // phpcs:ignore |
106
|
|
|
$values1 = [[$id, \PDO::PARAM_STR]]; |
107
|
|
|
$statement1 = MockStatementFactory::createWriteStatement($this, $values1); |
108
|
|
|
|
109
|
|
|
$this->writeConnectionMock |
110
|
|
|
->expects($this->exactly(2)) |
111
|
|
|
->method('prepare') |
112
|
|
|
->withConsecutive([$sql0], [$sql1]) |
113
|
|
|
->willReturnOnConsecutiveCalls($statement0, $statement1); |
114
|
|
|
|
115
|
|
|
$entity = new UserGroup($id, $identifier, $name); |
116
|
|
|
$this->sut->delete($entity); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testGetAll() |
120
|
|
|
{ |
121
|
|
|
$id = 'bde8a749-b409-43c6-a061-c6a7d2dce6a0'; |
122
|
|
|
$identifier = 'foo'; |
123
|
|
|
$name = 'bar'; |
124
|
|
|
|
125
|
|
|
$sql0 = 'SELECT ug.id, ug.identifier, ug.name, GROUP_CONCAT(ugar.admin_resource_id) AS admin_resource_ids FROM user_groups AS ug LEFT JOIN user_groups_admin_resources AS ugar ON ugar.user_group_id = ug.id WHERE (ug.deleted_at IS NULL) GROUP BY ug.id'; // phpcs:ignore |
126
|
|
|
$values = []; |
127
|
|
|
$expectedData = [ |
128
|
|
|
[ |
129
|
|
|
'id' => $id, |
130
|
|
|
'identifier' => $identifier, |
131
|
|
|
'name' => $name, |
132
|
|
|
'admin_resource_ids' => 'b7853a53-6187-429a-828e-638cdd9e1381,704ab848-4ac6-460d-b31c-aeafc9faa0ab', |
133
|
|
|
// phpcs:ignore |
134
|
|
|
], |
135
|
|
|
]; |
136
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
137
|
|
|
|
138
|
|
|
$this->readConnectionMock |
139
|
|
|
->expects($this->once()) |
140
|
|
|
->method('prepare') |
141
|
|
|
->with($sql0) |
142
|
|
|
->willReturn($statement0); |
143
|
|
|
|
144
|
|
|
$actualResult = $this->sut->getAll(); |
145
|
|
|
|
146
|
|
|
$this->assertCollection($expectedData, $actualResult); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function testGetPage() |
150
|
|
|
{ |
151
|
|
|
$id = 'bde8a749-b409-43c6-a061-c6a7d2dce6a0'; |
152
|
|
|
$identifier = 'foo'; |
153
|
|
|
$name = 'bar'; |
154
|
|
|
|
155
|
|
|
$sql0 = 'SELECT SQL_CALC_FOUND_ROWS ug.id, ug.identifier, ug.name, GROUP_CONCAT(ugar.admin_resource_id) AS admin_resource_ids FROM user_groups AS ug LEFT JOIN user_groups_admin_resources AS ugar ON ugar.user_group_id = ug.id WHERE (ug.deleted_at IS NULL) GROUP BY ug.id ORDER BY ug.created_at ASC LIMIT 10 OFFSET 0'; // phpcs:ignore |
156
|
|
|
$values = []; |
157
|
|
|
$expectedData = [['id' => $id, 'identifier' => $identifier, 'name' => $name]]; |
158
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
159
|
|
|
|
160
|
|
|
$this->readConnectionMock |
161
|
|
|
->expects($this->once()) |
162
|
|
|
->method('prepare') |
163
|
|
|
->with($sql0) |
164
|
|
|
->willReturn($statement0); |
165
|
|
|
|
166
|
|
|
$actualResult = $this->sut->getPage(0, 10, [], [], []); |
167
|
|
|
|
168
|
|
|
$this->assertCollection($expectedData, $actualResult); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testGetPageWithOrdersAndConditions() |
172
|
|
|
{ |
173
|
|
|
$id = 'bde8a749-b409-43c6-a061-c6a7d2dce6a0'; |
174
|
|
|
$identifier = 'foo'; |
175
|
|
|
$name = 'bar'; |
176
|
|
|
|
177
|
|
|
$orders = ['ug.name ASC']; |
178
|
|
|
$conditions = ['ug.name = \'abc\'', 'abc.name = \'bca\'']; |
179
|
|
|
|
180
|
|
|
$sql0 = 'SELECT SQL_CALC_FOUND_ROWS ug.id, ug.identifier, ug.name, GROUP_CONCAT(ugar.admin_resource_id) AS admin_resource_ids FROM user_groups AS ug LEFT JOIN user_groups_admin_resources AS ugar ON ugar.user_group_id = ug.id WHERE (ug.deleted_at IS NULL) AND (ug.name = \'abc\') AND (abc.name = \'bca\') GROUP BY ug.id ORDER BY ug.name ASC LIMIT 10 OFFSET 0'; // phpcs:ignore |
181
|
|
|
$values = []; |
182
|
|
|
$expectedData = [['id' => $id, 'identifier' => $identifier, 'name' => $name]]; |
183
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
184
|
|
|
|
185
|
|
|
$this->readConnectionMock |
186
|
|
|
->expects($this->once()) |
187
|
|
|
->method('prepare') |
188
|
|
|
->with($sql0) |
189
|
|
|
->willReturn($statement0); |
190
|
|
|
|
191
|
|
|
$actualResult = $this->sut->getPage(0, 10, $orders, $conditions, []); |
192
|
|
|
|
193
|
|
|
$this->assertCollection($expectedData, $actualResult); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function testGetById() |
197
|
|
|
{ |
198
|
|
|
$id = '35ccea14-6a34-4fcf-a303-9bb8c827ff16'; |
199
|
|
|
$identifier = 'foo'; |
200
|
|
|
$name = 'bar'; |
201
|
|
|
|
202
|
|
|
$sql0 = 'SELECT ug.id, ug.identifier, ug.name, GROUP_CONCAT(ugar.admin_resource_id) AS admin_resource_ids FROM user_groups AS ug LEFT JOIN user_groups_admin_resources AS ugar ON ugar.user_group_id = ug.id WHERE (ug.deleted_at IS NULL) AND (ug.id = :user_group_id) GROUP BY ug.id'; // phpcs:ignore |
203
|
|
|
$values = ['user_group_id' => [$id, \PDO::PARAM_STR]]; |
204
|
|
|
$expectedData = [['id' => $id, 'identifier' => $identifier, 'name' => $name]]; |
205
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
206
|
|
|
|
207
|
|
|
$this->readConnectionMock |
208
|
|
|
->expects($this->once()) |
209
|
|
|
->method('prepare') |
210
|
|
|
->with($sql0) |
211
|
|
|
->willReturn($statement0); |
212
|
|
|
|
213
|
|
|
$actualResult = $this->sut->getById($id); |
214
|
|
|
|
215
|
|
|
$this->assertEntity($expectedData[0], $actualResult); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function testGetByIdentifier() |
219
|
|
|
{ |
220
|
|
|
$id = 'cf2bca2a-7ef5-4e01-a95c-5b4d92186e35'; |
221
|
|
|
$identifier = 'foo'; |
222
|
|
|
$name = 'bar'; |
223
|
|
|
|
224
|
|
|
$sql0 = 'SELECT ug.id, ug.identifier, ug.name, GROUP_CONCAT(ugar.admin_resource_id) AS admin_resource_ids FROM user_groups AS ug LEFT JOIN user_groups_admin_resources AS ugar ON ugar.user_group_id = ug.id WHERE (ug.deleted_at IS NULL) AND (ug.identifier = :identifier) GROUP BY ug.id'; // phpcs:ignore |
225
|
|
|
$values = ['identifier' => [$identifier, \PDO::PARAM_STR]]; |
226
|
|
|
$expectedData = [['id' => $id, 'identifier' => $identifier, 'name' => $name]]; |
227
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
228
|
|
|
|
229
|
|
|
$this->readConnectionMock |
230
|
|
|
->expects($this->once()) |
231
|
|
|
->method('prepare') |
232
|
|
|
->with($sql0) |
233
|
|
|
->willReturn($statement0); |
234
|
|
|
|
235
|
|
|
$actualResult = $this->sut->getByIdentifier($identifier); |
236
|
|
|
|
237
|
|
|
$this->assertEntity($expectedData[0], $actualResult); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
public function testUpdateWithoutRelated() |
241
|
|
|
{ |
242
|
|
|
$id = '368e8be3-58b2-4b60-8a43-5b98242e6716'; |
243
|
|
|
$identifier = 'foo'; |
244
|
|
|
$name = 'bar'; |
245
|
|
|
|
246
|
|
|
$sql0 = 'UPDATE user_groups AS user_groups SET identifier = ?, name = ? WHERE (id = ?) AND (deleted_at IS NULL)'; // phpcs:ignore |
247
|
|
|
$values0 = [[$identifier, \PDO::PARAM_STR], [$name, \PDO::PARAM_STR], [$id, \PDO::PARAM_STR]]; |
248
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values0); |
249
|
|
|
|
250
|
|
|
$sql1 = 'DELETE FROM user_groups_admin_resources WHERE (user_group_id = ?)'; // phpcs:ignore |
251
|
|
|
$values1 = [[$id, \PDO::PARAM_STR]]; |
252
|
|
|
$statement1 = MockStatementFactory::createWriteStatement($this, $values1); |
253
|
|
|
|
254
|
|
|
$this->writeConnectionMock |
255
|
|
|
->expects($this->exactly(2)) |
256
|
|
|
->method('prepare') |
257
|
|
|
->withConsecutive([$sql0], [$sql1]) |
258
|
|
|
->willReturnOnConsecutiveCalls($statement0, $statement1); |
259
|
|
|
|
260
|
|
|
$entity = new UserGroup($id, $identifier, $name); |
261
|
|
|
$this->sut->update($entity); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function testUpdateWithRelated() |
265
|
|
|
{ |
266
|
|
|
$id = 'e2c961a8-d844-4dbd-96eb-7fb603dcd6d7'; |
267
|
|
|
$identifier = 'foo'; |
268
|
|
|
$name = 'bar'; |
269
|
|
|
$ugarId0 = '7f08b114-3a04-415a-8365-9e67d4a50cea'; |
270
|
|
|
$ugarId1 = '6bd44298-7319-4428-b2b2-29c3d4652f39'; |
271
|
|
|
$adminResources = [ |
272
|
|
|
new AdminResource('aacc2773-9549-438e-8b43-b27236ca5c64', ''), |
273
|
|
|
new AdminResource('e87c7ab9-b86e-4de6-a4fe-8ad3486cd952', ''), |
274
|
|
|
]; |
275
|
|
|
|
276
|
|
|
$this->sut->setIdGenerator(MockIdGeneratorFactory::create($this, $ugarId0, $ugarId1)); |
277
|
|
|
|
278
|
|
|
$sql0 = 'UPDATE user_groups AS user_groups SET identifier = ?, name = ? WHERE (id = ?) AND (deleted_at IS NULL)'; // phpcs:ignore |
279
|
|
|
$values0 = [[$identifier, \PDO::PARAM_STR], [$name, \PDO::PARAM_STR], [$id, \PDO::PARAM_STR]]; |
280
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values0); |
281
|
|
|
|
282
|
|
|
$sql1 = 'DELETE FROM user_groups_admin_resources WHERE (user_group_id = ?)'; // phpcs:ignore |
283
|
|
|
$values1 = [[$id, \PDO::PARAM_STR]]; |
284
|
|
|
$statement1 = MockStatementFactory::createWriteStatement($this, $values1); |
285
|
|
|
|
286
|
|
|
$sql2 = 'INSERT INTO user_groups_admin_resources (id, user_group_id, admin_resource_id) VALUES (?, ?, ?)'; // phpcs:ignore |
287
|
|
|
$values2 = [ |
288
|
|
|
[$ugarId0, \PDO::PARAM_STR], |
289
|
|
|
[$id, \PDO::PARAM_STR], |
290
|
|
|
[$adminResources[0]->getId(), \PDO::PARAM_STR], |
291
|
|
|
]; |
292
|
|
|
$statement2 = MockStatementFactory::createWriteStatement($this, $values2); |
293
|
|
|
|
294
|
|
|
$sql3 = 'INSERT INTO user_groups_admin_resources (id, user_group_id, admin_resource_id) VALUES (?, ?, ?)'; // phpcs:ignore |
295
|
|
|
$values3 = [ |
296
|
|
|
[$ugarId1, \PDO::PARAM_STR], |
297
|
|
|
[$id, \PDO::PARAM_STR], |
298
|
|
|
[$adminResources[1]->getId(), \PDO::PARAM_STR], |
299
|
|
|
]; |
300
|
|
|
$statement3 = MockStatementFactory::createWriteStatement($this, $values3); |
301
|
|
|
|
302
|
|
|
$this->writeConnectionMock |
303
|
|
|
->expects($this->exactly(4)) |
304
|
|
|
->method('prepare') |
305
|
|
|
->withConsecutive([$sql0], [$sql1], [$sql2], [$sql3]) |
306
|
|
|
->willReturnOnConsecutiveCalls($statement0, $statement1, $statement2, $statement3); |
307
|
|
|
|
308
|
|
|
$entity = new UserGroup($id, $identifier, $name, $adminResources); |
309
|
|
|
$this->sut->update($entity); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
public function testAddThrowsExceptionIfCalledWithInvalidEntity() |
313
|
|
|
{ |
314
|
|
|
$this->expectException(\InvalidArgumentException::class); |
315
|
|
|
|
316
|
|
|
/** @var IStringerEntity|MockObject $entity */ |
317
|
|
|
$entity = $this->createMock(IStringerEntity::class); |
318
|
|
|
|
319
|
|
|
$this->sut->add($entity); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
public function testDeleteThrowsExceptionIfCalledWithInvalidEntity() |
323
|
|
|
{ |
324
|
|
|
$this->expectException(\InvalidArgumentException::class); |
325
|
|
|
|
326
|
|
|
/** @var IStringerEntity|MockObject $entity */ |
327
|
|
|
$entity = $this->createMock(IStringerEntity::class); |
328
|
|
|
|
329
|
|
|
$this->sut->delete($entity); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
public function testUpdateThrowsExceptionIfCalledWithInvalidEntity() |
333
|
|
|
{ |
334
|
|
|
$this->expectException(\InvalidArgumentException::class); |
335
|
|
|
|
336
|
|
|
/** @var IStringerEntity|MockObject $entity */ |
337
|
|
|
$entity = $this->createMock(IStringerEntity::class); |
338
|
|
|
|
339
|
|
|
$this->sut->update($entity); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* @param array $expectedData |
344
|
|
|
* @param UserGroup $entity |
345
|
|
|
*/ |
346
|
|
|
protected function assertEntity(array $expectedData, $entity) |
347
|
|
|
{ |
348
|
|
|
$this->assertInstanceOf(UserGroup::class, $entity); |
349
|
|
|
$this->assertEquals($expectedData['id'], $entity->getId()); |
350
|
|
|
$this->assertSame($expectedData['identifier'], $entity->getIdentifier()); |
351
|
|
|
$this->assertSame($expectedData['name'], $entity->getName()); |
352
|
|
|
|
353
|
|
|
if (!array_key_exists('admin_resource_ids', $expectedData)) { |
354
|
|
|
return; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
$ids = []; |
358
|
|
|
foreach ($entity->getAdminResources() as $resource) { |
359
|
|
|
$ids[] = $resource->getId(); |
360
|
|
|
} |
361
|
|
|
$this->assertSame($expectedData['admin_resource_ids'], implode(',', $ids)); |
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
|