Passed
Push — master ( 54ae18...548540 )
by Peter
02:51
created

ContentListItemSqlDataMapperTest::testUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

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