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\Block as Entity; |
11
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
12
|
|
|
|
13
|
|
|
class BlockSqlDataMapperTest extends DataMapperTestCase |
14
|
|
|
{ |
15
|
|
|
/** @var BlockSqlDataMapper - System Under Test */ |
16
|
|
|
protected $sut; |
17
|
|
|
|
18
|
|
|
public function setUp(): void |
19
|
|
|
{ |
20
|
|
|
parent::setUp(); |
21
|
|
|
|
22
|
|
|
$this->sut = new BlockSqlDataMapper($this->readConnectionMock, $this->writeConnectionMock); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testAddWithoutLayoutId() |
26
|
|
|
{ |
27
|
|
|
$nextId = '9d160dd2-bd83-48f6-a3b0-e15d2f26e76c'; |
28
|
|
|
$identifier = 'foo'; |
29
|
|
|
$title = 'bar'; |
30
|
|
|
$body = 'baz'; |
31
|
|
|
$layout = 'qux'; |
32
|
|
|
$layoutId = null; |
33
|
|
|
|
34
|
|
|
$sql0 = 'INSERT INTO blocks (id, identifier, title, body, layout, layout_id) VALUES (?, ?, ?, ?, ?, ?)'; // phpcs:ignore |
35
|
|
|
$values = [ |
36
|
|
|
[$nextId, \PDO::PARAM_STR], |
37
|
|
|
[$identifier, \PDO::PARAM_STR], |
38
|
|
|
[$title, \PDO::PARAM_STR], |
39
|
|
|
[$body, \PDO::PARAM_STR], |
40
|
|
|
[$layout, \PDO::PARAM_STR], |
41
|
|
|
[$layoutId, \PDO::PARAM_NULL], |
42
|
|
|
]; |
43
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values); |
44
|
|
|
|
45
|
|
|
$this->writeConnectionMock |
46
|
|
|
->expects($this->once()) |
47
|
|
|
->method('prepare') |
48
|
|
|
->with($sql0) |
49
|
|
|
->willReturn($statement0); |
50
|
|
|
|
51
|
|
|
$entity = new Entity($nextId, $identifier, $title, $body, $layout, $layoutId); |
52
|
|
|
|
53
|
|
|
$this->sut->add($entity); |
54
|
|
|
|
55
|
|
|
$this->assertSame($nextId, $entity->getId()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testAddWithLayoutId() |
59
|
|
|
{ |
60
|
|
|
$nextId = '5b6c9874-bb65-4a6f-a5f1-78445434db84'; |
61
|
|
|
$identifier = 'foo'; |
62
|
|
|
$title = 'bar'; |
63
|
|
|
$body = 'baz'; |
64
|
|
|
$layout = ''; |
65
|
|
|
$layoutId = 'ae749110-e1f9-4f76-9391-5fe3b28a0b0d'; |
66
|
|
|
|
67
|
|
|
$sql0 = 'INSERT INTO blocks (id, identifier, title, body, layout, layout_id) VALUES (?, ?, ?, ?, ?, ?)'; // phpcs:ignore |
68
|
|
|
$values = [ |
69
|
|
|
[$nextId, \PDO::PARAM_STR], |
70
|
|
|
[$identifier, \PDO::PARAM_STR], |
71
|
|
|
[$title, \PDO::PARAM_STR], |
72
|
|
|
[$body, \PDO::PARAM_STR], |
73
|
|
|
[$layout, \PDO::PARAM_STR], |
74
|
|
|
[$layoutId, \PDO::PARAM_STR], |
75
|
|
|
]; |
76
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values); |
77
|
|
|
|
78
|
|
|
$this->writeConnectionMock |
79
|
|
|
->expects($this->once()) |
80
|
|
|
->method('prepare') |
81
|
|
|
->with($sql0) |
82
|
|
|
->willReturn($statement0); |
83
|
|
|
|
84
|
|
|
$entity = new Entity($nextId, $identifier, $title, $body, $layout, $layoutId); |
85
|
|
|
|
86
|
|
|
$this->sut->add($entity); |
87
|
|
|
|
88
|
|
|
$this->assertSame($nextId, $entity->getId()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testDelete() |
92
|
|
|
{ |
93
|
|
|
$id = 'f95ffd21-eff5-4b10-a423-e222fb7fe56f'; |
94
|
|
|
$identifier = 'foo'; |
95
|
|
|
$title = 'bar'; |
96
|
|
|
$body = 'baz'; |
97
|
|
|
$layout = 'qux'; |
98
|
|
|
$layoutId = null; |
99
|
|
|
|
100
|
|
|
$sql0 = 'UPDATE blocks AS blocks SET deleted_at = NOW() WHERE (id = ?)'; // phpcs:ignore |
101
|
|
|
$values = [[$id, \PDO::PARAM_STR]]; |
102
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values); |
103
|
|
|
|
104
|
|
|
$this->writeConnectionMock |
105
|
|
|
->expects($this->once()) |
106
|
|
|
->method('prepare') |
107
|
|
|
->with($sql0) |
108
|
|
|
->willReturn($statement0); |
109
|
|
|
|
110
|
|
|
$entity = new Entity($id, $identifier, $title, $body, $layout, $layoutId); |
111
|
|
|
|
112
|
|
|
$this->sut->delete($entity); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testGetAll() |
116
|
|
|
{ |
117
|
|
|
$id = '8da63e49-5c76-4520-9280-30c125305239'; |
118
|
|
|
$identifier = 'foo'; |
119
|
|
|
$title = 'bar'; |
120
|
|
|
$body = 'baz'; |
121
|
|
|
$layout = 'qux'; |
122
|
|
|
$layoutId = null; |
123
|
|
|
|
124
|
|
|
$sql0 = 'SELECT blocks.id, blocks.identifier, blocks.title, blocks.body, blocks.layout_id, blocks.layout FROM blocks WHERE (blocks.deleted_at IS NULL)'; // phpcs:ignore |
125
|
|
|
$values = []; |
126
|
|
|
$expectedData = [ |
127
|
|
|
[ |
128
|
|
|
'id' => $id, |
129
|
|
|
'identifier' => $identifier, |
130
|
|
|
'title' => $title, |
131
|
|
|
'body' => $body, |
132
|
|
|
'layout' => $layout, |
133
|
|
|
'layout_id' => $layoutId, |
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 = '8da63e49-5c76-4520-9280-30c125305239'; |
152
|
|
|
$identifier = 'foo'; |
153
|
|
|
$title = 'bar'; |
154
|
|
|
$body = 'baz'; |
155
|
|
|
$layout = 'qux'; |
156
|
|
|
$layoutId = null; |
157
|
|
|
|
158
|
|
|
$sql0 = 'SELECT SQL_CALC_FOUND_ROWS blocks.id, blocks.identifier, blocks.title, blocks.body, blocks.layout_id, blocks.layout FROM blocks WHERE (blocks.deleted_at IS NULL) ORDER BY title ASC LIMIT 10 OFFSET 0'; // phpcs:ignore |
159
|
|
|
$values = []; |
160
|
|
|
$expectedData = [ |
161
|
|
|
[ |
162
|
|
|
'id' => $id, |
163
|
|
|
'identifier' => $identifier, |
164
|
|
|
'title' => $title, |
165
|
|
|
'body' => $body, |
166
|
|
|
'layout' => $layout, |
167
|
|
|
'layout_id' => $layoutId, |
168
|
|
|
], |
169
|
|
|
]; |
170
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
171
|
|
|
|
172
|
|
|
$this->readConnectionMock |
173
|
|
|
->expects($this->once()) |
174
|
|
|
->method('prepare') |
175
|
|
|
->with($sql0) |
176
|
|
|
->willReturn($statement0); |
177
|
|
|
|
178
|
|
|
$actualResult = $this->sut->getPage(0, 10, [], [], []); |
179
|
|
|
|
180
|
|
|
$this->assertCollection($expectedData, $actualResult); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function testGetPageWithOrdersAndConditions() |
184
|
|
|
{ |
185
|
|
|
$id = '8da63e49-5c76-4520-9280-30c125305239'; |
186
|
|
|
$identifier = 'foo'; |
187
|
|
|
$title = 'bar'; |
188
|
|
|
$body = 'baz'; |
189
|
|
|
$layout = 'qux'; |
190
|
|
|
$layoutId = null; |
191
|
|
|
|
192
|
|
|
$orders = ['blocks.identifier ASC']; |
193
|
|
|
$conditions = ['blocks.identifier LIKE \'abc%\'', 'blocks.identifier LIKE \'%bca\'']; |
194
|
|
|
|
195
|
|
|
$sql0 = 'SELECT SQL_CALC_FOUND_ROWS blocks.id, blocks.identifier, blocks.title, blocks.body, blocks.layout_id, blocks.layout FROM blocks WHERE (blocks.deleted_at IS NULL) AND (blocks.identifier LIKE \'abc%\') AND (blocks.identifier LIKE \'%bca\') ORDER BY blocks.identifier ASC LIMIT 10 OFFSET 0'; // phpcs:ignore |
196
|
|
|
$values = []; |
197
|
|
|
$expectedData = [ |
198
|
|
|
[ |
199
|
|
|
'id' => $id, |
200
|
|
|
'identifier' => $identifier, |
201
|
|
|
'title' => $title, |
202
|
|
|
'body' => $body, |
203
|
|
|
'layout' => $layout, |
204
|
|
|
'layout_id' => $layoutId, |
205
|
|
|
], |
206
|
|
|
]; |
207
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
208
|
|
|
|
209
|
|
|
$this->readConnectionMock |
210
|
|
|
->expects($this->once()) |
211
|
|
|
->method('prepare') |
212
|
|
|
->with($sql0) |
213
|
|
|
->willReturn($statement0); |
214
|
|
|
|
215
|
|
|
$actualResult = $this->sut->getPage(0, 10, $orders, $conditions, []); |
216
|
|
|
|
217
|
|
|
$this->assertCollection($expectedData, $actualResult); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function testGetById() |
221
|
|
|
{ |
222
|
|
|
$id = 'da406cd9-4a65-4384-b1dd-454c4d26c196'; |
223
|
|
|
$identifier = 'foo'; |
224
|
|
|
$title = 'bar'; |
225
|
|
|
$body = 'baz'; |
226
|
|
|
$layout = 'qux'; |
227
|
|
|
$layoutId = null; |
228
|
|
|
|
229
|
|
|
$sql0 = 'SELECT blocks.id, blocks.identifier, blocks.title, blocks.body, blocks.layout_id, blocks.layout FROM blocks WHERE (blocks.deleted_at IS NULL) AND (blocks.id = :block_id)'; // phpcs:ignore |
230
|
|
|
$values = ['block_id' => [$id, \PDO::PARAM_STR]]; |
231
|
|
|
$expectedData = [ |
232
|
|
|
[ |
233
|
|
|
'id' => $id, |
234
|
|
|
'identifier' => $identifier, |
235
|
|
|
'title' => $title, |
236
|
|
|
'body' => $body, |
237
|
|
|
'layout' => $layout, |
238
|
|
|
'layout_id' => $layoutId, |
239
|
|
|
], |
240
|
|
|
]; |
241
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
242
|
|
|
|
243
|
|
|
$this->readConnectionMock |
244
|
|
|
->expects($this->once()) |
245
|
|
|
->method('prepare') |
246
|
|
|
->with($sql0) |
247
|
|
|
->willReturn($statement0); |
248
|
|
|
|
249
|
|
|
$actualResult = $this->sut->getById($id); |
250
|
|
|
|
251
|
|
|
$this->assertEntity($expectedData[0], $actualResult); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function testGetByIdentifier() |
255
|
|
|
{ |
256
|
|
|
$id = '42f019b3-5d49-4ee0-b785-63ef245a1ee0'; |
257
|
|
|
$identifier = 'foo'; |
258
|
|
|
$title = 'bar'; |
259
|
|
|
$body = 'baz'; |
260
|
|
|
$layout = 'qux'; |
261
|
|
|
$layoutId = null; |
262
|
|
|
|
263
|
|
|
$sql0 = 'SELECT blocks.id, blocks.identifier, blocks.title, blocks.body, blocks.layout_id, blocks.layout FROM blocks WHERE (blocks.deleted_at IS NULL) AND (blocks.identifier = :identifier)'; // phpcs:ignore |
264
|
|
|
$values = ['identifier' => [$identifier, \PDO::PARAM_STR]]; |
265
|
|
|
$expectedData = [ |
266
|
|
|
[ |
267
|
|
|
'id' => $id, |
268
|
|
|
'identifier' => $identifier, |
269
|
|
|
'title' => $title, |
270
|
|
|
'body' => $body, |
271
|
|
|
'layout' => $layout, |
272
|
|
|
'layout_id' => $layoutId, |
273
|
|
|
], |
274
|
|
|
]; |
275
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
276
|
|
|
|
277
|
|
|
$this->readConnectionMock |
278
|
|
|
->expects($this->once()) |
279
|
|
|
->method('prepare') |
280
|
|
|
->with($sql0) |
281
|
|
|
->willReturn($statement0); |
282
|
|
|
|
283
|
|
|
$actualResult = $this->sut->getByIdentifier($identifier); |
284
|
|
|
|
285
|
|
|
$this->assertEntity($expectedData[0], $actualResult); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
public function testGetWithLayoutByIdentifiers() |
289
|
|
|
{ |
290
|
|
|
$id = '76815a32-359e-4898-996a-ef9695f875bb'; |
291
|
|
|
$identifier = 'foo'; |
292
|
|
|
$title = 'bar'; |
293
|
|
|
$body = 'baz'; |
294
|
|
|
$layout = 'qux'; |
295
|
|
|
$layoutId = null; |
296
|
|
|
|
297
|
|
|
$sql0 = 'SELECT blocks.id, blocks.identifier, blocks.title, blocks.body, blocks.layout_id, COALESCE(layouts.body, blocks.layout) AS layout FROM blocks LEFT JOIN block_layouts AS layouts ON layouts.id = blocks.layout_id AND layouts.deleted_at IS NULL WHERE (blocks.deleted_at IS NULL) AND (blocks.identifier IN (?))'; // phpcs:ignore |
298
|
|
|
$values = [[$identifier, \PDO::PARAM_STR]]; |
299
|
|
|
$expectedData = [ |
300
|
|
|
[ |
301
|
|
|
'id' => $id, |
302
|
|
|
'identifier' => $identifier, |
303
|
|
|
'title' => $title, |
304
|
|
|
'body' => $body, |
305
|
|
|
'layout' => $layout, |
306
|
|
|
'layout_id' => $layoutId, |
307
|
|
|
], |
308
|
|
|
]; |
309
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
310
|
|
|
|
311
|
|
|
$this->readConnectionMock |
312
|
|
|
->expects($this->once()) |
313
|
|
|
->method('prepare') |
314
|
|
|
->with($sql0) |
315
|
|
|
->willReturn($statement0); |
316
|
|
|
|
317
|
|
|
$actualResult = $this->sut->getWithLayoutByIdentifiers([$identifier]); |
318
|
|
|
|
319
|
|
|
$this->assertCollection($expectedData, $actualResult); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
public function testGetWithLayoutByIdentifiersCanReturnEarly() |
323
|
|
|
{ |
324
|
|
|
$expectedData = []; |
325
|
|
|
|
326
|
|
|
$actualResult = $this->sut->getWithLayoutByIdentifiers([]); |
327
|
|
|
|
328
|
|
|
$this->assertCollection($expectedData, $actualResult); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
public function testUpdateWithoutLayoutId() |
332
|
|
|
{ |
333
|
|
|
$id = 'f7cdde13-7f39-493e-9c7a-ddeab4adb8eb'; |
334
|
|
|
$identifier = 'foo'; |
335
|
|
|
$title = 'bar'; |
336
|
|
|
$body = 'baz'; |
337
|
|
|
$layout = 'qux'; |
338
|
|
|
$layoutId = null; |
339
|
|
|
|
340
|
|
|
$sql0 = 'UPDATE blocks AS blocks SET identifier = ?, title = ?, body = ?, layout = ?, layout_id = ? WHERE (id = ?) AND (deleted_at IS NULL)'; // phpcs:ignore |
341
|
|
|
$values = [ |
342
|
|
|
[$identifier, \PDO::PARAM_STR], |
343
|
|
|
[$title, \PDO::PARAM_STR], |
344
|
|
|
[$body, \PDO::PARAM_STR], |
345
|
|
|
[$layout, \PDO::PARAM_STR], |
346
|
|
|
[$layoutId, \PDO::PARAM_NULL], |
347
|
|
|
[$id, \PDO::PARAM_STR], |
348
|
|
|
]; |
349
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values); |
350
|
|
|
|
351
|
|
|
$this->writeConnectionMock |
352
|
|
|
->expects($this->once()) |
353
|
|
|
->method('prepare') |
354
|
|
|
->with($sql0) |
355
|
|
|
->willReturn($statement0); |
356
|
|
|
|
357
|
|
|
$entity = new Entity($id, $identifier, $title, $body, $layout, $layoutId); |
358
|
|
|
|
359
|
|
|
$this->sut->update($entity); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
public function testUpdateWithLayoutId() |
363
|
|
|
{ |
364
|
|
|
$id = 'e0075c90-80ff-40dd-aafe-a8d866a42bcd'; |
365
|
|
|
$identifier = 'foo'; |
366
|
|
|
$title = 'bar'; |
367
|
|
|
$body = 'baz'; |
368
|
|
|
$layout = ''; |
369
|
|
|
$layoutId = 'd7a7bcad-71bc-40a1-8a0d-dc2b28a54811'; |
370
|
|
|
|
371
|
|
|
$sql0 = 'UPDATE blocks AS blocks SET identifier = ?, title = ?, body = ?, layout = ?, layout_id = ? WHERE (id = ?) AND (deleted_at IS NULL)'; // phpcs:ignore |
372
|
|
|
$values = [ |
373
|
|
|
[$identifier, \PDO::PARAM_STR], |
374
|
|
|
[$title, \PDO::PARAM_STR], |
375
|
|
|
[$body, \PDO::PARAM_STR], |
376
|
|
|
[$layout, \PDO::PARAM_STR], |
377
|
|
|
[$layoutId, \PDO::PARAM_STR], |
378
|
|
|
[$id, \PDO::PARAM_STR], |
379
|
|
|
]; |
380
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values); |
381
|
|
|
|
382
|
|
|
$this->writeConnectionMock |
383
|
|
|
->expects($this->once()) |
384
|
|
|
->method('prepare') |
385
|
|
|
->with($sql0) |
386
|
|
|
->willReturn($statement0); |
387
|
|
|
|
388
|
|
|
$entity = new Entity($id, $identifier, $title, $body, $layout, $layoutId); |
389
|
|
|
|
390
|
|
|
$this->sut->update($entity); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
public function testAddThrowsExceptionIfCalledWithInvalidEntity() |
394
|
|
|
{ |
395
|
|
|
$this->expectException(\InvalidArgumentException::class); |
396
|
|
|
|
397
|
|
|
/** @var IStringerEntity|MockObject $entity */ |
398
|
|
|
$entity = $this->createMock(IStringerEntity::class); |
399
|
|
|
|
400
|
|
|
$this->sut->add($entity); |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
public function testDeleteThrowsExceptionIfCalledWithInvalidEntity() |
404
|
|
|
{ |
405
|
|
|
$this->expectException(\InvalidArgumentException::class); |
406
|
|
|
|
407
|
|
|
/** @var IStringerEntity|MockObject $entity */ |
408
|
|
|
$entity = $this->createMock(IStringerEntity::class); |
409
|
|
|
|
410
|
|
|
$this->sut->delete($entity); |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
public function testUpdateThrowsExceptionIfCalledWithInvalidEntity() |
414
|
|
|
{ |
415
|
|
|
$this->expectException(\InvalidArgumentException::class); |
416
|
|
|
|
417
|
|
|
/** @var IStringerEntity|MockObject $entity */ |
418
|
|
|
$entity = $this->createMock(IStringerEntity::class); |
419
|
|
|
|
420
|
|
|
$this->sut->update($entity); |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* @param array $expectedData |
425
|
|
|
* @param Entity $entity |
426
|
|
|
*/ |
427
|
|
|
protected function assertEntity(array $expectedData, $entity) |
428
|
|
|
{ |
429
|
|
|
$this->assertInstanceOf(Entity::class, $entity); |
430
|
|
|
$this->assertSame($expectedData['id'], $entity->getId()); |
431
|
|
|
$this->assertSame($expectedData['identifier'], $entity->getIdentifier()); |
432
|
|
|
$this->assertSame($expectedData['title'], $entity->getTitle()); |
433
|
|
|
$this->assertSame($expectedData['body'], $entity->getBody()); |
434
|
|
|
$this->assertSame($expectedData['layout'], $entity->getLayout()); |
435
|
|
|
$this->assertSame($expectedData['layout_id'], $entity->getLayoutId()); |
436
|
|
|
} |
437
|
|
|
} |
438
|
|
|
|