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\ContentListItem as Entity; |
11
|
|
|
use AbterPhp\Website\Orm\DataMappers\ContentListItemSqlDataMapper as DataMapper; |
12
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
13
|
|
|
|
14
|
|
|
class ContentListItemSqlDataMapperTest extends DataMapperTestCase |
15
|
|
|
{ |
16
|
|
|
/** @var DataMapper - System Under Test */ |
17
|
|
|
protected $sut; |
18
|
|
|
|
19
|
|
|
public function setUp(): void |
20
|
|
|
{ |
21
|
|
|
parent::setUp(); |
22
|
|
|
|
23
|
|
|
$this->sut = new DataMapper($this->readConnectionMock, $this->writeConnectionMock); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testAdd() |
27
|
|
|
{ |
28
|
|
|
$nextId = '9d160dd2-bd83-48f6-a3b0-e15d2f26e76c'; |
29
|
|
|
$listId = 'f95ffd21-eff5-4b10-a423-e222fb7fe56f'; |
30
|
|
|
$label = 'Foo'; |
31
|
|
|
$labelHref = 'foo'; |
32
|
|
|
$content = 'Bar'; |
33
|
|
|
$contentHref = 'bar'; |
34
|
|
|
$imgSrc = 'baz'; |
35
|
|
|
$imgAlt = 'qux'; |
36
|
|
|
$imgHref = 'quix'; |
37
|
|
|
$classes = 'york'; |
38
|
|
|
|
39
|
|
|
$sql0 = 'INSERT INTO list_items (id, list_id, label, label_href, content, content_href, img_src, img_alt, img_href, classes) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; // phpcs:ignore |
40
|
|
|
$values = [ |
41
|
|
|
[$nextId, \PDO::PARAM_STR], |
42
|
|
|
[$listId, \PDO::PARAM_STR], |
43
|
|
|
[$label, \PDO::PARAM_STR], |
44
|
|
|
[$labelHref, \PDO::PARAM_STR], |
45
|
|
|
[$content, \PDO::PARAM_STR], |
46
|
|
|
[$contentHref, \PDO::PARAM_STR], |
47
|
|
|
[$imgSrc, \PDO::PARAM_STR], |
48
|
|
|
[$imgAlt, \PDO::PARAM_STR], |
49
|
|
|
[$imgHref, \PDO::PARAM_STR], |
50
|
|
|
[$classes, \PDO::PARAM_STR], |
51
|
|
|
]; |
52
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values); |
53
|
|
|
|
54
|
|
|
$this->writeConnectionMock |
55
|
|
|
->expects($this->once()) |
56
|
|
|
->method('prepare') |
57
|
|
|
->with($sql0) |
58
|
|
|
->willReturn($statement0); |
59
|
|
|
|
60
|
|
|
$entity = new Entity( |
61
|
|
|
$nextId, |
62
|
|
|
$listId, |
63
|
|
|
$label, |
64
|
|
|
$labelHref, |
65
|
|
|
$content, |
66
|
|
|
$contentHref, |
67
|
|
|
$imgSrc, |
68
|
|
|
$imgAlt, |
69
|
|
|
$imgHref, |
70
|
|
|
$classes |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
$this->sut->add($entity); |
74
|
|
|
|
75
|
|
|
$this->assertSame($nextId, $entity->getId()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testDelete() |
79
|
|
|
{ |
80
|
|
|
$id = '9d160dd2-bd83-48f6-a3b0-e15d2f26e76c'; |
81
|
|
|
$listId = 'f95ffd21-eff5-4b10-a423-e222fb7fe56f'; |
82
|
|
|
$label = 'Foo'; |
83
|
|
|
$labelHref = 'foo'; |
84
|
|
|
$content = 'Bar'; |
85
|
|
|
$contentUrl = 'bar'; |
86
|
|
|
$imgSrc = 'baz'; |
87
|
|
|
$imgHref = 'qux'; |
88
|
|
|
$imgAlt = 'quix'; |
89
|
|
|
$classes = 'york'; |
90
|
|
|
|
91
|
|
|
$sql0 = 'UPDATE list_items AS list_items SET deleted_at = NOW() WHERE (id = ?)'; // phpcs:ignore |
92
|
|
|
$values = [[$id, \PDO::PARAM_STR]]; |
93
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values); |
94
|
|
|
|
95
|
|
|
$this->writeConnectionMock |
96
|
|
|
->expects($this->once()) |
97
|
|
|
->method('prepare') |
98
|
|
|
->with($sql0) |
99
|
|
|
->willReturn($statement0); |
100
|
|
|
|
101
|
|
|
$entity = new Entity( |
102
|
|
|
$id, |
103
|
|
|
$listId, |
104
|
|
|
$content, |
105
|
|
|
$contentUrl, |
106
|
|
|
$label, |
107
|
|
|
$labelHref, |
108
|
|
|
$imgSrc, |
109
|
|
|
$imgAlt, |
110
|
|
|
$imgHref, |
111
|
|
|
$classes |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
$this->sut->delete($entity); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testGetAll() |
118
|
|
|
{ |
119
|
|
|
$id = '9d160dd2-bd83-48f6-a3b0-e15d2f26e76c'; |
120
|
|
|
$listId = 'f95ffd21-eff5-4b10-a423-e222fb7fe56f'; |
121
|
|
|
$label = 'Foo'; |
122
|
|
|
$labelHref = 'foo'; |
123
|
|
|
$content = 'Bar'; |
124
|
|
|
$contentHref = 'bar'; |
125
|
|
|
$imgSrc = 'baz'; |
126
|
|
|
$imgAlt = 'qux'; |
127
|
|
|
$imgHref = 'quix'; |
128
|
|
|
$classes = 'york'; |
129
|
|
|
|
130
|
|
|
$sql0 = 'SELECT list_items.id, list_items.list_id, list_items.label, list_items.label_href, list_items.content, list_items.content_href, list_items.img_src, list_items.img_alt, list_items.img_href, list_items.classes FROM list_items WHERE (list_items.deleted_at IS NULL)'; // phpcs:ignore |
131
|
|
|
$values = []; |
132
|
|
|
$expectedData = [ |
133
|
|
|
[ |
134
|
|
|
'id' => $id, |
135
|
|
|
'list_id' => $listId, |
136
|
|
|
'label' => $label, |
137
|
|
|
'label_href' => $labelHref, |
138
|
|
|
'content' => $content, |
139
|
|
|
'content_href' => $contentHref, |
140
|
|
|
'img_src' => $imgSrc, |
141
|
|
|
'img_alt' => $imgAlt, |
142
|
|
|
'img_href' => $imgHref, |
143
|
|
|
'classes' => $classes, |
144
|
|
|
], |
145
|
|
|
]; |
146
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
147
|
|
|
|
148
|
|
|
$this->readConnectionMock |
149
|
|
|
->expects($this->once()) |
150
|
|
|
->method('prepare') |
151
|
|
|
->with($sql0) |
152
|
|
|
->willReturn($statement0); |
153
|
|
|
|
154
|
|
|
$actualResult = $this->sut->getAll(); |
155
|
|
|
|
156
|
|
|
$this->assertCollection($expectedData, $actualResult); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function testGetPage() |
160
|
|
|
{ |
161
|
|
|
$id = '9d160dd2-bd83-48f6-a3b0-e15d2f26e76c'; |
162
|
|
|
$listId = 'f95ffd21-eff5-4b10-a423-e222fb7fe56f'; |
163
|
|
|
$label = 'Foo'; |
164
|
|
|
$labelHref = 'foo'; |
165
|
|
|
$content = 'Bar'; |
166
|
|
|
$contentHref = 'bar'; |
167
|
|
|
$imgSrc = 'baz'; |
168
|
|
|
$imgAlt = 'qux'; |
169
|
|
|
$imgHref = 'quix'; |
170
|
|
|
$classes = 'york'; |
171
|
|
|
|
172
|
|
|
$sql0 = 'SELECT SQL_CALC_FOUND_ROWS list_items.id, list_items.list_id, list_items.label, list_items.label_href, list_items.content, list_items.content_href, list_items.img_src, list_items.img_alt, list_items.img_href, list_items.classes FROM list_items WHERE (list_items.deleted_at IS NULL) ORDER BY label ASC LIMIT 10 OFFSET 0'; // phpcs:ignore |
173
|
|
|
$values = []; |
174
|
|
|
$expectedData = [ |
175
|
|
|
[ |
176
|
|
|
'id' => $id, |
177
|
|
|
'list_id' => $listId, |
178
|
|
|
'label' => $label, |
179
|
|
|
'label_href' => $labelHref, |
180
|
|
|
'content' => $content, |
181
|
|
|
'content_href' => $contentHref, |
182
|
|
|
'img_src' => $imgSrc, |
183
|
|
|
'img_alt' => $imgAlt, |
184
|
|
|
'img_href' => $imgHref, |
185
|
|
|
'classes' => $classes, |
186
|
|
|
], |
187
|
|
|
]; |
188
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
189
|
|
|
|
190
|
|
|
$this->readConnectionMock |
191
|
|
|
->expects($this->once()) |
192
|
|
|
->method('prepare') |
193
|
|
|
->with($sql0) |
194
|
|
|
->willReturn($statement0); |
195
|
|
|
|
196
|
|
|
$actualResult = $this->sut->getPage(0, 10, [], [], []); |
197
|
|
|
|
198
|
|
|
$this->assertCollection($expectedData, $actualResult); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function testGetById() |
202
|
|
|
{ |
203
|
|
|
$id = '9d160dd2-bd83-48f6-a3b0-e15d2f26e76c'; |
204
|
|
|
$listId = 'f95ffd21-eff5-4b10-a423-e222fb7fe56f'; |
205
|
|
|
$label = 'Foo'; |
|
|
|
|
206
|
|
|
$labelHref = 'foo'; |
|
|
|
|
207
|
|
|
$label = 'Bar'; |
208
|
|
|
$labelHref = 'bar'; |
209
|
|
|
$imgSrc = 'baz'; |
210
|
|
|
$imgAlt = 'qux'; |
211
|
|
|
$imgHref = 'quix'; |
212
|
|
|
$classes = 'york'; |
213
|
|
|
|
214
|
|
|
$sql0 = 'SELECT list_items.id, list_items.list_id, list_items.label, list_items.label_href, list_items.content, list_items.content_href, list_items.img_src, list_items.img_alt, list_items.img_href, list_items.classes FROM list_items WHERE (list_items.deleted_at IS NULL) AND (list_items.id = :list_item_id)'; // phpcs:ignore |
215
|
|
|
$values = ['list_item_id' => [$id, \PDO::PARAM_STR]]; |
216
|
|
|
$expectedData = [ |
217
|
|
|
[ |
218
|
|
|
'id' => $id, |
219
|
|
|
'list_id' => $listId, |
220
|
|
|
'label' => $label, |
221
|
|
|
'label_href' => $labelHref, |
222
|
|
|
'content' => $label, |
223
|
|
|
'content_href' => $labelHref, |
224
|
|
|
'img_src' => $imgSrc, |
225
|
|
|
'img_alt' => $imgAlt, |
226
|
|
|
'img_href' => $imgHref, |
227
|
|
|
'classes' => $classes, |
228
|
|
|
], |
229
|
|
|
]; |
230
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
231
|
|
|
|
232
|
|
|
$this->readConnectionMock |
233
|
|
|
->expects($this->once()) |
234
|
|
|
->method('prepare') |
235
|
|
|
->with($sql0) |
236
|
|
|
->willReturn($statement0); |
237
|
|
|
|
238
|
|
|
$actualResult = $this->sut->getById($id); |
239
|
|
|
|
240
|
|
|
$this->assertEntity($expectedData[0], $actualResult); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
public function testGetByListId() |
244
|
|
|
{ |
245
|
|
|
$id = '9d160dd2-bd83-48f6-a3b0-e15d2f26e76c'; |
246
|
|
|
$listId = 'f95ffd21-eff5-4b10-a423-e222fb7fe56f'; |
247
|
|
|
$label = 'Foo'; |
|
|
|
|
248
|
|
|
$labelHref = 'foo'; |
|
|
|
|
249
|
|
|
$label = 'Bar'; |
250
|
|
|
$labelHref = 'bar'; |
251
|
|
|
$imgSrc = 'baz'; |
252
|
|
|
$imgAlt = 'qux'; |
253
|
|
|
$imgHref = 'quix'; |
254
|
|
|
$classes = 'york'; |
255
|
|
|
|
256
|
|
|
$sql0 = 'SELECT list_items.id, list_items.list_id, list_items.label, list_items.label_href, list_items.content, list_items.content_href, list_items.img_src, list_items.img_alt, list_items.img_href, list_items.classes FROM list_items WHERE (list_items.deleted_at IS NULL) AND (list_items.list_id = :list_id)'; // phpcs:ignore |
257
|
|
|
$values = ['list_id' => [$listId, \PDO::PARAM_STR]]; |
258
|
|
|
$expectedData = [ |
259
|
|
|
[ |
260
|
|
|
'id' => $id, |
261
|
|
|
'list_id' => $listId, |
262
|
|
|
'label' => $label, |
263
|
|
|
'label_href' => $labelHref, |
264
|
|
|
'content' => $label, |
265
|
|
|
'content_href' => $labelHref, |
266
|
|
|
'img_src' => $imgSrc, |
267
|
|
|
'img_alt' => $imgAlt, |
268
|
|
|
'img_href' => $imgHref, |
269
|
|
|
'classes' => $classes, |
270
|
|
|
], |
271
|
|
|
]; |
272
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
273
|
|
|
|
274
|
|
|
$this->readConnectionMock |
275
|
|
|
->expects($this->once()) |
276
|
|
|
->method('prepare') |
277
|
|
|
->with($sql0) |
278
|
|
|
->willReturn($statement0); |
279
|
|
|
|
280
|
|
|
$actualResult = $this->sut->getByListId($listId); |
281
|
|
|
|
282
|
|
|
$this->assertCollection($expectedData, $actualResult); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
public function testGetByListIds() |
286
|
|
|
{ |
287
|
|
|
$id = '9d160dd2-bd83-48f6-a3b0-e15d2f26e76c'; |
288
|
|
|
$listId0 = 'f95ffd21-eff5-4b10-a423-e222fb7fe56f'; |
289
|
|
|
$label = 'Foo'; |
|
|
|
|
290
|
|
|
$labelHref = 'foo'; |
|
|
|
|
291
|
|
|
$label = 'Bar'; |
292
|
|
|
$labelHref = 'bar'; |
293
|
|
|
$imgSrc = 'baz'; |
294
|
|
|
$imgAlt = 'qux'; |
295
|
|
|
$imgHref = 'quix'; |
296
|
|
|
$classes = 'york'; |
297
|
|
|
|
298
|
|
|
$listId1 = '51ec9310-9a6d-4fdc-a26b-3a83dd373a18'; |
299
|
|
|
|
300
|
|
|
$sql0 = 'SELECT list_items.id, list_items.list_id, list_items.label, list_items.label_href, list_items.content, list_items.content_href, list_items.img_src, list_items.img_alt, list_items.img_href, list_items.classes FROM list_items WHERE (list_items.deleted_at IS NULL) AND (list_items.list_id IN (?,?))'; // phpcs:ignore |
301
|
|
|
$values = [[$listId0, \PDO::PARAM_STR], [$listId1, \PDO::PARAM_STR]]; |
302
|
|
|
$expectedData = [ |
303
|
|
|
[ |
304
|
|
|
'id' => $id, |
305
|
|
|
'list_id' => $listId0, |
306
|
|
|
'label' => $label, |
307
|
|
|
'label_href' => $labelHref, |
308
|
|
|
'content' => $label, |
309
|
|
|
'content_href' => $labelHref, |
310
|
|
|
'img_src' => $imgSrc, |
311
|
|
|
'img_alt' => $imgAlt, |
312
|
|
|
'img_href' => $imgHref, |
313
|
|
|
'classes' => $classes, |
314
|
|
|
], |
315
|
|
|
]; |
316
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
317
|
|
|
|
318
|
|
|
$this->readConnectionMock |
319
|
|
|
->expects($this->once()) |
320
|
|
|
->method('prepare') |
321
|
|
|
->with($sql0) |
322
|
|
|
->willReturn($statement0); |
323
|
|
|
|
324
|
|
|
$actualResult = $this->sut->getByListIds([$listId0, $listId1]); |
325
|
|
|
|
326
|
|
|
$this->assertCollection($expectedData, $actualResult); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
public function testUpdate() |
330
|
|
|
{ |
331
|
|
|
$id = '9d160dd2-bd83-48f6-a3b0-e15d2f26e76c'; |
332
|
|
|
$listId = 'f95ffd21-eff5-4b10-a423-e222fb7fe56f'; |
333
|
|
|
$label = 'Foo'; |
|
|
|
|
334
|
|
|
$labelHref = 'foo'; |
|
|
|
|
335
|
|
|
$label = 'Bar'; |
336
|
|
|
$labelHref = 'bar'; |
337
|
|
|
$imgSrc = 'baz'; |
338
|
|
|
$imgAlt = 'qux'; |
339
|
|
|
$imgHref = 'quix'; |
340
|
|
|
$classes = 'york'; |
341
|
|
|
|
342
|
|
|
$sql0 = 'UPDATE list_items AS list_items SET list_id = ?, label = ?, label_href = ?, content = ?, content_href = ?, img_src = ?, img_alt = ?, img_href = ?, classes = ? WHERE (id = ?) AND (list_items.deleted_at IS NULL)'; // phpcs:ignore |
343
|
|
|
$values = [ |
344
|
|
|
[$listId, \PDO::PARAM_STR], |
345
|
|
|
[$label, \PDO::PARAM_STR], |
346
|
|
|
[$labelHref, \PDO::PARAM_STR], |
347
|
|
|
[$label, \PDO::PARAM_STR], |
348
|
|
|
[$labelHref, \PDO::PARAM_STR], |
349
|
|
|
[$imgSrc, \PDO::PARAM_STR], |
350
|
|
|
[$imgAlt, \PDO::PARAM_STR], |
351
|
|
|
[$imgHref, \PDO::PARAM_STR], |
352
|
|
|
[$classes, \PDO::PARAM_STR], |
353
|
|
|
[$id, \PDO::PARAM_STR], |
354
|
|
|
]; |
355
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values); |
356
|
|
|
|
357
|
|
|
$this->writeConnectionMock |
358
|
|
|
->expects($this->once()) |
359
|
|
|
->method('prepare') |
360
|
|
|
->with($sql0) |
361
|
|
|
->willReturn($statement0); |
362
|
|
|
|
363
|
|
|
$entity = new Entity( |
364
|
|
|
$id, |
365
|
|
|
$listId, |
366
|
|
|
$label, |
367
|
|
|
$labelHref, |
368
|
|
|
$label, |
369
|
|
|
$labelHref, |
370
|
|
|
$imgSrc, |
371
|
|
|
$imgAlt, |
372
|
|
|
$imgHref, |
373
|
|
|
$classes |
374
|
|
|
); |
375
|
|
|
|
376
|
|
|
$this->sut->update($entity); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
public function testAddThrowsExceptionIfCalledWithInvalidEntity() |
380
|
|
|
{ |
381
|
|
|
$this->expectException(\InvalidArgumentException::class); |
382
|
|
|
|
383
|
|
|
/** @var IStringerEntity|MockObject $entity */ |
384
|
|
|
$entity = $this->createMock(IStringerEntity::class); |
385
|
|
|
|
386
|
|
|
$this->sut->add($entity); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
public function testDeleteThrowsExceptionIfCalledWithInvalidEntity() |
390
|
|
|
{ |
391
|
|
|
$this->expectException(\InvalidArgumentException::class); |
392
|
|
|
|
393
|
|
|
/** @var IStringerEntity|MockObject $entity */ |
394
|
|
|
$entity = $this->createMock(IStringerEntity::class); |
395
|
|
|
|
396
|
|
|
$this->sut->delete($entity); |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
public function testUpdateThrowsExceptionIfCalledWithInvalidEntity() |
400
|
|
|
{ |
401
|
|
|
$this->expectException(\InvalidArgumentException::class); |
402
|
|
|
|
403
|
|
|
/** @var IStringerEntity|MockObject $entity */ |
404
|
|
|
$entity = $this->createMock(IStringerEntity::class); |
405
|
|
|
|
406
|
|
|
$this->sut->update($entity); |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* @param array $expectedData |
411
|
|
|
* @param Entity $entity |
412
|
|
|
*/ |
413
|
|
|
protected function assertEntity(array $expectedData, $entity) |
414
|
|
|
{ |
415
|
|
|
$this->assertInstanceOf(Entity::class, $entity); |
416
|
|
|
$this->assertSame($expectedData['id'], $entity->getId()); |
417
|
|
|
$this->assertSame($expectedData['list_id'], $entity->getListId()); |
418
|
|
|
$this->assertSame($expectedData['label'], $entity->getLabel()); |
419
|
|
|
$this->assertSame($expectedData['label_href'], $entity->getLabelHref()); |
420
|
|
|
$this->assertSame($expectedData['content'], $entity->getContent()); |
421
|
|
|
$this->assertSame($expectedData['content_href'], $entity->getContentHref()); |
422
|
|
|
$this->assertSame($expectedData['img_src'], $entity->getImgSrc()); |
423
|
|
|
$this->assertSame($expectedData['img_alt'], $entity->getImgAlt()); |
424
|
|
|
$this->assertSame($expectedData['img_href'], $entity->getImgHref()); |
425
|
|
|
$this->assertSame($expectedData['classes'], $entity->getClasses()); |
426
|
|
|
} |
427
|
|
|
} |
428
|
|
|
|