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