Passed
Push — master ( 6af672...0107a4 )
by Peter
07:37
created

BlockSqlDataMapperTest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 423
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 269
c 0
b 0
f 0
dl 0
loc 423
rs 10
wmc 17

17 Methods

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