|
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\TestCase\Orm\DataMapperTestCase; |
|
9
|
|
|
use AbterPhp\Framework\Domain\Entities\IStringerEntity; |
|
10
|
|
|
use AbterPhp\Framework\TestDouble\Database\MockStatementFactory; |
|
11
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
12
|
|
|
|
|
13
|
|
|
class AdminResourceSqlDataMapperTest extends DataMapperTestCase |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var AdminResourceSqlDataMapper */ |
|
16
|
|
|
protected $sut; |
|
17
|
|
|
|
|
18
|
|
|
public function setUp(): void |
|
19
|
|
|
{ |
|
20
|
|
|
parent::setUp(); |
|
21
|
|
|
|
|
22
|
|
|
$this->sut = new AdminResourceSqlDataMapper($this->readConnectionMock, $this->writeConnectionMock); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function testAdd() |
|
26
|
|
|
{ |
|
27
|
|
|
$nextId = '9b6ae58b-1aff-4344-a2ae-cda43a40674e'; |
|
28
|
|
|
$identifier = 'foo'; |
|
29
|
|
|
|
|
30
|
|
|
$sql0 = 'INSERT INTO admin_resources (id, identifier) VALUES (?, ?)'; // phpcs:ignore |
|
31
|
|
|
$values = [[$nextId, \PDO::PARAM_STR], [$identifier, \PDO::PARAM_STR]]; |
|
32
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values); |
|
33
|
|
|
|
|
34
|
|
|
$this->writeConnectionMock |
|
35
|
|
|
->expects($this->once()) |
|
36
|
|
|
->method('prepare') |
|
37
|
|
|
->with($sql0) |
|
38
|
|
|
->willReturn($statement0); |
|
39
|
|
|
|
|
40
|
|
|
$entity = new AdminResource($nextId, $identifier); |
|
41
|
|
|
|
|
42
|
|
|
$this->sut->add($entity); |
|
43
|
|
|
|
|
44
|
|
|
$this->assertSame($nextId, $entity->getId()); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function testDelete() |
|
48
|
|
|
{ |
|
49
|
|
|
$id = '8fe2f659-dbe5-4995-9e07-f49fb018cfe7'; |
|
50
|
|
|
$identifier = 'foo'; |
|
51
|
|
|
|
|
52
|
|
|
$sql0 = 'UPDATE admin_resources AS admin_resources SET deleted_at = NOW() WHERE (id = ?)'; // phpcs:ignore |
|
53
|
|
|
$values = [[$id, \PDO::PARAM_STR]]; |
|
54
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values); |
|
55
|
|
|
|
|
56
|
|
|
$this->writeConnectionMock |
|
57
|
|
|
->expects($this->once()) |
|
58
|
|
|
->method('prepare') |
|
59
|
|
|
->with($sql0) |
|
60
|
|
|
->willReturn($statement0); |
|
61
|
|
|
|
|
62
|
|
|
$entity = new AdminResource($id, $identifier); |
|
63
|
|
|
|
|
64
|
|
|
$this->sut->delete($entity); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testGetAll() |
|
68
|
|
|
{ |
|
69
|
|
|
$id0 = '24bd4165-1229-4a6e-a679-76bf90743ee1'; |
|
70
|
|
|
$identifier0 = 'foo'; |
|
71
|
|
|
$id1 = '51eac0fc-2b26-4231-9559-469e59fae694'; |
|
72
|
|
|
$identifier1 = 'bar'; |
|
73
|
|
|
|
|
74
|
|
|
$sql0 = 'SELECT ar.id, ar.identifier FROM admin_resources AS ar WHERE (ar.deleted_at IS NULL)'; // phpcs:ignore |
|
75
|
|
|
$values = []; |
|
76
|
|
|
$expectedData = [ |
|
77
|
|
|
['id' => $id0, 'identifier' => $identifier0], |
|
78
|
|
|
['id' => $id1, 'identifier' => $identifier1], |
|
79
|
|
|
]; |
|
80
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
|
81
|
|
|
|
|
82
|
|
|
$this->readConnectionMock |
|
83
|
|
|
->expects($this->once()) |
|
84
|
|
|
->method('prepare') |
|
85
|
|
|
->with($sql0) |
|
86
|
|
|
->willReturn($statement0); |
|
87
|
|
|
|
|
88
|
|
|
$actualResult = $this->sut->getAll(); |
|
89
|
|
|
|
|
90
|
|
|
$this->assertCollection($expectedData, $actualResult); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function testGetById() |
|
94
|
|
|
{ |
|
95
|
|
|
$id = '4b72daf8-81a9-400f-b865-28306d1c1646'; |
|
96
|
|
|
$identifier = 'foo'; |
|
97
|
|
|
|
|
98
|
|
|
$sql0 = 'SELECT ar.id, ar.identifier FROM admin_resources AS ar WHERE (ar.deleted_at IS NULL) AND (ar.id = :admin_resource_id)'; // phpcs:ignore |
|
99
|
|
|
$values = ['admin_resource_id' => [$id, \PDO::PARAM_STR]]; |
|
100
|
|
|
$expectedData = [['id' => $id, 'identifier' => $identifier]]; |
|
101
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
|
102
|
|
|
|
|
103
|
|
|
$this->readConnectionMock |
|
104
|
|
|
->expects($this->once()) |
|
105
|
|
|
->method('prepare') |
|
106
|
|
|
->with($sql0) |
|
107
|
|
|
->willReturn($statement0); |
|
108
|
|
|
|
|
109
|
|
|
$actualResult = $this->sut->getById($id); |
|
110
|
|
|
|
|
111
|
|
|
$this->assertEntity($expectedData[0], $actualResult); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function testGetByIdentifier() |
|
115
|
|
|
{ |
|
116
|
|
|
$id = '998ac138-85be-4b8f-ac7a-3fb8c249a7bf'; |
|
117
|
|
|
$identifier = 'foo'; |
|
118
|
|
|
|
|
119
|
|
|
$sql0 = 'SELECT ar.id, ar.identifier FROM admin_resources AS ar WHERE (ar.deleted_at IS NULL) AND (ar.identifier = :identifier)'; // phpcs:ignore |
|
120
|
|
|
$values = ['identifier' => [$identifier, \PDO::PARAM_STR]]; |
|
121
|
|
|
$expectedData = [['id' => $id, 'identifier' => $identifier]]; |
|
122
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
|
123
|
|
|
|
|
124
|
|
|
$this->readConnectionMock |
|
125
|
|
|
->expects($this->once()) |
|
126
|
|
|
->method('prepare') |
|
127
|
|
|
->with($sql0) |
|
128
|
|
|
->willReturn($statement0); |
|
129
|
|
|
|
|
130
|
|
|
$actualResult = $this->sut->getByIdentifier($identifier); |
|
131
|
|
|
|
|
132
|
|
|
$this->assertEntity($expectedData[0], $actualResult); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function testGetByUserId() |
|
136
|
|
|
{ |
|
137
|
|
|
$userId = '81704325-5d93-4987-8425-8a80062db406'; |
|
138
|
|
|
$id0 = '24bd4165-1229-4a6e-a679-76bf90743ee1'; |
|
139
|
|
|
$identifier0 = 'foo'; |
|
140
|
|
|
$id1 = '51eac0fc-2b26-4231-9559-469e59fae694'; |
|
141
|
|
|
$identifier1 = 'bar'; |
|
142
|
|
|
|
|
143
|
|
|
$sql0 = 'SELECT ar.id, ar.identifier FROM admin_resources AS ar INNER JOIN user_groups_admin_resources AS ugar ON ugar.admin_resource_id = ar.id INNER JOIN user_groups AS ug ON ug.id = ugar.user_group_id INNER JOIN users_user_groups AS uug ON uug.user_group_id = ug.id WHERE (ar.deleted_at IS NULL) AND (uug.user_id = :user_id) GROUP BY ar.id'; // phpcs:ignore |
|
144
|
|
|
$values = ['user_id' => [$userId, \PDO::PARAM_STR]]; |
|
145
|
|
|
$expectedData = [ |
|
146
|
|
|
['id' => $id0, 'identifier' => $identifier0], |
|
147
|
|
|
['id' => $id1, 'identifier' => $identifier1], |
|
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->getByUserId($userId); |
|
158
|
|
|
|
|
159
|
|
|
$this->assertCollection($expectedData, $actualResult); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
public function testUpdate() |
|
163
|
|
|
{ |
|
164
|
|
|
$id = '91693481-276e-495b-82a1-33209c47ca09'; |
|
165
|
|
|
$identifier = 'foo'; |
|
166
|
|
|
|
|
167
|
|
|
$sql0 = 'UPDATE admin_resources AS admin_resources SET identifier = ? WHERE (id = ?) AND (deleted_at IS NULL)'; // phpcs:ignore |
|
168
|
|
|
$values = [[$identifier, \PDO::PARAM_STR], [$id, \PDO::PARAM_STR]]; |
|
169
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values); |
|
170
|
|
|
|
|
171
|
|
|
$this->writeConnectionMock |
|
172
|
|
|
->expects($this->once()) |
|
173
|
|
|
->method('prepare') |
|
174
|
|
|
->with($sql0) |
|
175
|
|
|
->willReturn($statement0); |
|
176
|
|
|
|
|
177
|
|
|
$entity = new AdminResource($id, $identifier); |
|
178
|
|
|
|
|
179
|
|
|
$this->sut->update($entity); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function testAddThrowsExceptionIfCalledWithInvalidEntity() |
|
183
|
|
|
{ |
|
184
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
185
|
|
|
|
|
186
|
|
|
/** @var IStringerEntity|MockObject $entity */ |
|
187
|
|
|
$entity = $this->createMock(IStringerEntity::class); |
|
188
|
|
|
|
|
189
|
|
|
$this->sut->add($entity); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
public function testDeleteThrowsExceptionIfCalledWithInvalidEntity() |
|
193
|
|
|
{ |
|
194
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
195
|
|
|
|
|
196
|
|
|
/** @var IStringerEntity|MockObject $entity */ |
|
197
|
|
|
$entity = $this->createMock(IStringerEntity::class); |
|
198
|
|
|
|
|
199
|
|
|
$this->sut->delete($entity); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
public function testUpdateThrowsExceptionIfCalledWithInvalidEntity() |
|
203
|
|
|
{ |
|
204
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
205
|
|
|
|
|
206
|
|
|
/** @var IStringerEntity|MockObject $entity */ |
|
207
|
|
|
$entity = $this->createMock(IStringerEntity::class); |
|
208
|
|
|
|
|
209
|
|
|
$this->sut->update($entity); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @param array $expectedData |
|
214
|
|
|
* @param AdminResource $entity |
|
215
|
|
|
*/ |
|
216
|
|
|
protected function assertEntity(array $expectedData, $entity) |
|
217
|
|
|
{ |
|
218
|
|
|
$this->assertInstanceOf(AdminResource::class, $entity); |
|
219
|
|
|
$this->assertEquals($expectedData['id'], $entity->getId()); |
|
220
|
|
|
$this->assertSame($expectedData['identifier'], $entity->getIdentifier()); |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
|