1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Website\Orm\DataMappers; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Admin\TestCase\Orm\DataMapperTestCase; |
8
|
|
|
use AbterPhp\Framework\Domain\Entities\IStringerEntity; |
9
|
|
|
use AbterPhp\Framework\TestDouble\Database\MockStatementFactory; |
10
|
|
|
use AbterPhp\Website\Domain\Entities\BlockLayout; |
11
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
12
|
|
|
|
13
|
|
|
class BlockLayoutSqlDataMapperTest extends DataMapperTestCase |
14
|
|
|
{ |
15
|
|
|
/** @var BlockLayoutSqlDataMapper - System Under Test */ |
16
|
|
|
protected $sut; |
17
|
|
|
|
18
|
|
|
public function setUp(): void |
19
|
|
|
{ |
20
|
|
|
parent::setUp(); |
21
|
|
|
|
22
|
|
|
$this->sut = new BlockLayoutSqlDataMapper($this->readConnectionMock, $this->writeConnectionMock); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testAdd() |
26
|
|
|
{ |
27
|
|
|
$nextId = 'c2883287-ae5d-42d1-ab0c-7d3da2846452'; |
28
|
|
|
$name = 'Foo'; |
29
|
|
|
$identifier = 'foo'; |
30
|
|
|
$body = 'bar'; |
31
|
|
|
|
32
|
|
|
$sql0 = 'INSERT INTO block_layouts (id, name, identifier, body) VALUES (?, ?, ?, ?)'; // phpcs:ignore |
33
|
|
|
$values = [ |
34
|
|
|
[$nextId, \PDO::PARAM_STR], |
35
|
|
|
[$name, \PDO::PARAM_STR], |
36
|
|
|
[$identifier, \PDO::PARAM_STR], |
37
|
|
|
[$body, \PDO::PARAM_STR], |
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 BlockLayout($nextId, $name, $identifier, $body); |
48
|
|
|
|
49
|
|
|
$this->sut->add($entity); |
50
|
|
|
|
51
|
|
|
$this->assertSame($nextId, $entity->getId()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testDelete() |
55
|
|
|
{ |
56
|
|
|
$id = 'd23a94ed-b75c-43a9-9987-a783183dadd5'; |
57
|
|
|
$name = 'Foo'; |
58
|
|
|
$identifier = 'foo'; |
59
|
|
|
$body = 'bar'; |
60
|
|
|
|
61
|
|
|
$sql0 = 'UPDATE block_layouts AS block_layouts SET deleted_at = NOW() WHERE (id = ?)'; // phpcs:ignore |
62
|
|
|
$values = [[$id, \PDO::PARAM_STR]]; |
63
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values); |
64
|
|
|
|
65
|
|
|
$this->writeConnectionMock |
66
|
|
|
->expects($this->once()) |
67
|
|
|
->method('prepare') |
68
|
|
|
->with($sql0) |
69
|
|
|
->willReturn($statement0); |
70
|
|
|
|
71
|
|
|
$entity = new BlockLayout($id, $name, $identifier, $body); |
72
|
|
|
|
73
|
|
|
$this->sut->delete($entity); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testGetAll() |
77
|
|
|
{ |
78
|
|
|
$id = '40a59d7d-7550-4b16-a90b-89adbfec8979'; |
79
|
|
|
$name = 'Foo'; |
80
|
|
|
$identifier = 'foo'; |
81
|
|
|
$body = 'bar'; |
82
|
|
|
|
83
|
|
|
$sql0 = 'SELECT block_layouts.id, block_layouts.name, block_layouts.identifier, block_layouts.body FROM block_layouts WHERE (block_layouts.deleted_at IS NULL)'; // phpcs:ignore |
84
|
|
|
$values = []; |
85
|
|
|
$expectedData = [['id' => $id, 'name' => $name, 'identifier' => $identifier, 'body' => $body]]; |
86
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
87
|
|
|
|
88
|
|
|
$this->readConnectionMock |
89
|
|
|
->expects($this->once()) |
90
|
|
|
->method('prepare') |
91
|
|
|
->with($sql0) |
92
|
|
|
->willReturn($statement0); |
93
|
|
|
|
94
|
|
|
$actualResult = $this->sut->getAll(); |
95
|
|
|
|
96
|
|
|
$this->assertCollection($expectedData, $actualResult); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testGetPage() |
100
|
|
|
{ |
101
|
|
|
$id = 'bde8a749-b409-43c6-a061-c6a7d2dce6a0'; |
102
|
|
|
$name = 'Foo'; |
103
|
|
|
$identifier = 'foo'; |
104
|
|
|
$body = 'bar'; |
105
|
|
|
|
106
|
|
|
$sql0 = 'SELECT SQL_CALC_FOUND_ROWS block_layouts.id, block_layouts.name, block_layouts.identifier, block_layouts.body FROM block_layouts WHERE (block_layouts.deleted_at IS NULL) ORDER BY name ASC LIMIT 10 OFFSET 0'; // phpcs:ignore |
107
|
|
|
$values = []; |
108
|
|
|
$expectedData = [['id' => $id, 'name' => $name, 'identifier' => $identifier, 'body' => $body]]; |
109
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
110
|
|
|
|
111
|
|
|
$this->readConnectionMock |
112
|
|
|
->expects($this->once()) |
113
|
|
|
->method('prepare') |
114
|
|
|
->with($sql0) |
115
|
|
|
->willReturn($statement0); |
116
|
|
|
|
117
|
|
|
$actualResult = $this->sut->getPage(0, 10, [], [], []); |
118
|
|
|
|
119
|
|
|
$this->assertCollection($expectedData, $actualResult); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function testGetPageWithOrdersAndConditions() |
123
|
|
|
{ |
124
|
|
|
$id = 'bde8a749-b409-43c6-a061-c6a7d2dce6a0'; |
125
|
|
|
$name = 'Foo'; |
126
|
|
|
$identifier = 'foo'; |
127
|
|
|
$body = 'bar'; |
128
|
|
|
|
129
|
|
|
$orders = ['block_layouts.identifier ASC']; |
130
|
|
|
$conditions = ['block_layouts.identifier LIKE \'abc%\'', 'block_layouts.identifier LIKE \'%bca\'']; |
131
|
|
|
|
132
|
|
|
$sql0 = 'SELECT SQL_CALC_FOUND_ROWS block_layouts.id, block_layouts.name, block_layouts.identifier, block_layouts.body FROM block_layouts WHERE (block_layouts.deleted_at IS NULL) AND (block_layouts.identifier LIKE \'abc%\') AND (block_layouts.identifier LIKE \'%bca\') ORDER BY block_layouts.identifier ASC LIMIT 10 OFFSET 0'; // phpcs:ignore |
133
|
|
|
$values = []; |
134
|
|
|
$expectedData = [['id' => $id, 'name' => $name, 'identifier' => $identifier, 'body' => $body]]; |
135
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
136
|
|
|
|
137
|
|
|
$this->readConnectionMock |
138
|
|
|
->expects($this->once()) |
139
|
|
|
->method('prepare') |
140
|
|
|
->with($sql0) |
141
|
|
|
->willReturn($statement0); |
142
|
|
|
|
143
|
|
|
$actualResult = $this->sut->getPage(0, 10, $orders, $conditions, []); |
144
|
|
|
|
145
|
|
|
$this->assertCollection($expectedData, $actualResult); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function testGetById() |
149
|
|
|
{ |
150
|
|
|
$id = 'adbeb333-3110-42ec-a2ed-74a33db518ff'; |
151
|
|
|
$name = 'Foo'; |
152
|
|
|
$identifier = 'foo'; |
153
|
|
|
$body = 'bar'; |
154
|
|
|
|
155
|
|
|
$sql0 = 'SELECT block_layouts.id, block_layouts.name, block_layouts.identifier, block_layouts.body FROM block_layouts WHERE (block_layouts.deleted_at IS NULL) AND (block_layouts.id = :layout_id)'; // phpcs:ignore |
156
|
|
|
$values = ['layout_id' => [$id, \PDO::PARAM_STR]]; |
157
|
|
|
$expectedData = [['id' => $id, 'name' => $name, 'identifier' => $identifier, 'body' => $body]]; |
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->getById($id); |
167
|
|
|
|
168
|
|
|
$this->assertEntity($expectedData[0], $actualResult); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testGetByIdentifier() |
172
|
|
|
{ |
173
|
|
|
$id = 'b0538bd0-5762-417c-8208-4e6b04b72f86'; |
174
|
|
|
$name = 'Foo'; |
175
|
|
|
$identifier = 'foo'; |
176
|
|
|
$body = 'bar'; |
177
|
|
|
|
178
|
|
|
$sql0 = 'SELECT block_layouts.id, block_layouts.name, block_layouts.identifier, block_layouts.body FROM block_layouts WHERE (block_layouts.deleted_at IS NULL) AND (identifier = :identifier)'; // phpcs:ignore |
179
|
|
|
$values = ['identifier' => $identifier]; |
180
|
|
|
$expectedData = [['id' => $id, 'name' => $name, 'identifier' => $identifier, 'body' => $body]]; |
181
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
182
|
|
|
|
183
|
|
|
$this->readConnectionMock |
184
|
|
|
->expects($this->once()) |
185
|
|
|
->method('prepare') |
186
|
|
|
->with($sql0) |
187
|
|
|
->willReturn($statement0); |
188
|
|
|
|
189
|
|
|
$actualResult = $this->sut->getByIdentifier($identifier); |
190
|
|
|
|
191
|
|
|
$this->assertEntity($expectedData[0], $actualResult); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function testUpdate() |
195
|
|
|
{ |
196
|
|
|
$id = '10ada92f-9ed8-4b7b-897a-9e10c640caec'; |
197
|
|
|
$name = 'Foo'; |
198
|
|
|
$identifier = 'foo'; |
199
|
|
|
$body = 'bar'; |
200
|
|
|
|
201
|
|
|
$sql0 = 'UPDATE block_layouts AS block_layouts SET name = ?, identifier = ?, body = ? WHERE (id = ?) AND (deleted_at IS NULL)'; // phpcs:ignore |
202
|
|
|
$values = [ |
203
|
|
|
[$name, \PDO::PARAM_STR], |
204
|
|
|
[$identifier, \PDO::PARAM_STR], |
205
|
|
|
[$body, \PDO::PARAM_STR], |
206
|
|
|
[$id, \PDO::PARAM_STR], |
207
|
|
|
]; |
208
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values); |
209
|
|
|
|
210
|
|
|
$this->writeConnectionMock |
211
|
|
|
->expects($this->once()) |
212
|
|
|
->method('prepare') |
213
|
|
|
->with($sql0) |
214
|
|
|
->willReturn($statement0); |
215
|
|
|
|
216
|
|
|
$entity = new BlockLayout($id, $name, $identifier, $body); |
217
|
|
|
|
218
|
|
|
$this->sut->update($entity); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function testAddThrowsExceptionIfCalledWithInvalidEntity() |
222
|
|
|
{ |
223
|
|
|
$this->expectException(\InvalidArgumentException::class); |
224
|
|
|
|
225
|
|
|
/** @var IStringerEntity|MockObject $entity */ |
226
|
|
|
$entity = $this->createMock(IStringerEntity::class); |
227
|
|
|
|
228
|
|
|
$this->sut->add($entity); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
public function testDeleteThrowsExceptionIfCalledWithInvalidEntity() |
232
|
|
|
{ |
233
|
|
|
$this->expectException(\InvalidArgumentException::class); |
234
|
|
|
|
235
|
|
|
/** @var IStringerEntity|MockObject $entity */ |
236
|
|
|
$entity = $this->createMock(IStringerEntity::class); |
237
|
|
|
|
238
|
|
|
$this->sut->delete($entity); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
public function testUpdateThrowsExceptionIfCalledWithInvalidEntity() |
242
|
|
|
{ |
243
|
|
|
$this->expectException(\InvalidArgumentException::class); |
244
|
|
|
|
245
|
|
|
/** @var IStringerEntity|MockObject $entity */ |
246
|
|
|
$entity = $this->createMock(IStringerEntity::class); |
247
|
|
|
|
248
|
|
|
$this->sut->update($entity); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param array $expectedData |
253
|
|
|
* @param BlockLayout $entity |
254
|
|
|
*/ |
255
|
|
|
protected function assertEntity(array $expectedData, $entity) |
256
|
|
|
{ |
257
|
|
|
$this->assertInstanceOf(BlockLayout::class, $entity); |
258
|
|
|
$this->assertSame($expectedData['id'], $entity->getId()); |
259
|
|
|
$this->assertSame($expectedData['name'], $entity->getName()); |
260
|
|
|
$this->assertSame($expectedData['identifier'], $entity->getIdentifier()); |
261
|
|
|
$this->assertSame($expectedData['body'], $entity->getBody()); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|