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

PageLayoutSqlDataMapperTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 366
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 237
dl 0
loc 366
rs 10
c 0
b 0
f 0
wmc 15

14 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetAll() 0 38 1
A setUp() 0 5 1
A assertEntityAssets() 0 11 2
A testGetPage() 0 38 1
A testDeleteThrowsExceptionIfCalledWithInvalidEntity() 0 8 1
A testGetById() 0 38 1
A testGetByIdentifier() 0 38 1
A testUpdateThrowsExceptionIfCalledWithInvalidEntity() 0 8 1
A testAdd() 0 29 1
A testDelete() 0 20 1
A testGetPageWithOrdersAndConditions() 0 41 1
A testAddThrowsExceptionIfCalledWithInvalidEntity() 0 8 1
A assertEntity() 0 9 1
A testUpdate() 0 36 1
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\PageLayout;
11
use PHPUnit\Framework\MockObject\MockObject;
12
13
class PageLayoutSqlDataMapperTest extends DataMapperTestCase
14
{
15
    /** @var PageLayoutSqlDataMapper - System Under Test */
16
    protected $sut;
17
18
    public function setUp(): void
19
    {
20
        parent::setUp();
21
22
        $this->sut = new PageLayoutSqlDataMapper($this->readConnectionMock, $this->writeConnectionMock);
23
    }
24
25
    public function testAdd()
26
    {
27
        $nextId     = 'c840500d-bd00-410a-912e-e923b8e965e3';
28
        $name       = 'Foo';
29
        $identifier = 'foo';
30
        $classes    = 'tsa';
31
        $body       = 'bar';
32
33
        $sql0       = 'INSERT INTO page_layouts (id, name, identifier, classes, body) VALUES (?, ?, ?, ?, ?)'; // phpcs:ignore
34
        $values     = [
35
            [$nextId, \PDO::PARAM_STR],
36
            [$name, \PDO::PARAM_STR],
37
            [$identifier, \PDO::PARAM_STR],
38
            [$classes, \PDO::PARAM_STR],
39
            [$body, \PDO::PARAM_STR],
40
        ];
41
        $statement0 = MockStatementFactory::createWriteStatement($this, $values);
42
43
        $this->writeConnectionMock
44
            ->expects($this->once())
45
            ->method('prepare')
46
            ->with($sql0)
47
            ->willReturn($statement0);
48
49
        $entity = new PageLayout($nextId, $name, $identifier, $classes, $body, null);
50
51
        $this->sut->add($entity);
52
53
        $this->assertSame($nextId, $entity->getId());
54
    }
55
56
    public function testDelete()
57
    {
58
        $id         = '1dab2760-9aaa-4f36-a303-42b12e65d165';
59
        $name       = 'Foo';
60
        $identifier = 'foo';
61
        $body       = 'bar';
62
63
        $sql0       = 'UPDATE page_layouts AS page_layouts SET deleted_at = NOW() WHERE (id = ?)'; // phpcs:ignore
64
        $values     = [[$id, \PDO::PARAM_STR]];
65
        $statement0 = MockStatementFactory::createWriteStatement($this, $values);
66
67
        $this->writeConnectionMock
68
            ->expects($this->once())
69
            ->method('prepare')
70
            ->with($sql0)
71
            ->willReturn($statement0);
72
73
        $entity = new PageLayout($id, $name, $identifier, '', $body, null);
74
75
        $this->sut->delete($entity);
76
    }
77
78
    public function testGetAll()
79
    {
80
        $id         = 'df6b4637-634e-4544-a167-2bddf3eab498';
81
        $name       = 'Foo';
82
        $identifier = 'foo';
83
        $classes    = 'tsa';
84
        $body       = 'bar';
85
        $header     = 'baz';
86
        $footer     = 'yak';
87
        $cssFiles   = 'zar';
88
        $jsFiles    = 'boi';
89
90
        $sql0         = 'SELECT page_layouts.id, page_layouts.name, page_layouts.identifier, page_layouts.classes, page_layouts.body, page_layouts.header, page_layouts.footer, page_layouts.css_files, page_layouts.js_files FROM page_layouts WHERE (page_layouts.deleted_at IS NULL)'; // phpcs:ignore
91
        $values       = [];
92
        $expectedData = [
93
            [
94
                'id'         => $id,
95
                'name'       => $name,
96
                'identifier' => $identifier,
97
                'classes'    => $classes,
98
                'body'       => $body,
99
                'header'     => $header,
100
                'footer'     => $footer,
101
                'css_files'  => $cssFiles,
102
                'js_files'   => $jsFiles,
103
            ],
104
        ];
105
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
106
107
        $this->readConnectionMock
108
            ->expects($this->once())
109
            ->method('prepare')
110
            ->with($sql0)
111
            ->willReturn($statement0);
112
113
        $actualResult = $this->sut->getAll();
114
115
        $this->assertCollection($expectedData, $actualResult);
116
    }
117
118
    public function testGetPage()
119
    {
120
        $id         = 'df6b4637-634e-4544-a167-2bddf3eab498';
121
        $name       = 'Foo';
122
        $identifier = 'foo';
123
        $classes    = 'tsa';
124
        $body       = 'bar';
125
        $header     = 'baz';
126
        $footer     = 'yak';
127
        $cssFiles   = 'zar';
128
        $jsFiles    = 'boi';
129
130
        $sql0         = 'SELECT SQL_CALC_FOUND_ROWS page_layouts.id, page_layouts.name, page_layouts.identifier, page_layouts.classes, page_layouts.body, page_layouts.header, page_layouts.footer, page_layouts.css_files, page_layouts.js_files FROM page_layouts WHERE (page_layouts.deleted_at IS NULL) ORDER BY page_layouts.name ASC LIMIT 10 OFFSET 0'; // phpcs:ignore
131
        $values       = [];
132
        $expectedData = [
133
            [
134
                'id'         => $id,
135
                'name'       => $name,
136
                'identifier' => $identifier,
137
                'classes'    => $classes,
138
                'body'       => $body,
139
                'header'     => $header,
140
                'footer'     => $footer,
141
                'css_files'  => $cssFiles,
142
                'js_files'   => $jsFiles,
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->getPage(0, 10, [], [], []);
154
155
        $this->assertCollection($expectedData, $actualResult);
156
    }
157
158
    public function testGetPageWithOrdersAndConditions()
159
    {
160
        $id         = 'df6b4637-634e-4544-a167-2bddf3eab498';
161
        $name       = 'Foo';
162
        $identifier = 'foo';
163
        $classes    = 'tsa';
164
        $body       = 'bar';
165
        $header     = 'baz';
166
        $footer     = 'yak';
167
        $cssFiles   = 'zar';
168
        $jsFiles    = 'boi';
169
170
        $orders     = ['page_layouts.identifier ASC'];
171
        $conditions = ['page_layouts.identifier LIKE \'abc%\'', 'page_layouts.identifier LIKE \'%bca\''];
172
173
        $sql0         = 'SELECT SQL_CALC_FOUND_ROWS page_layouts.id, page_layouts.name, page_layouts.identifier, page_layouts.classes, page_layouts.body, page_layouts.header, page_layouts.footer, page_layouts.css_files, page_layouts.js_files FROM page_layouts WHERE (page_layouts.deleted_at IS NULL) AND (page_layouts.identifier LIKE \'abc%\') AND (page_layouts.identifier LIKE \'%bca\') ORDER BY page_layouts.identifier ASC LIMIT 10 OFFSET 0'; // phpcs:ignore
174
        $values       = [];
175
        $expectedData = [
176
            [
177
                'id'         => $id,
178
                'name'       => $name,
179
                'identifier' => $identifier,
180
                'classes'    => $classes,
181
                'body'       => $body,
182
                'header'     => $header,
183
                'footer'     => $footer,
184
                'css_files'  => $cssFiles,
185
                'js_files'   => $jsFiles,
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, $orders, $conditions, []);
197
198
        $this->assertCollection($expectedData, $actualResult);
199
    }
200
201
    public function testGetById()
202
    {
203
        $id         = 'fc2bdb23-bdd1-49aa-8613-b5e0ce76450d';
204
        $name       = 'Foo';
205
        $identifier = 'foo';
206
        $classes    = 'tsa';
207
        $body       = 'bar';
208
        $header     = 'baz';
209
        $footer     = 'yak';
210
        $cssFiles   = 'zar';
211
        $jsFiles    = 'boi';
212
213
        $sql0         = 'SELECT page_layouts.id, page_layouts.name, page_layouts.identifier, page_layouts.classes, page_layouts.body, page_layouts.header, page_layouts.footer, page_layouts.css_files, page_layouts.js_files FROM page_layouts WHERE (page_layouts.deleted_at IS NULL) AND (page_layouts.id = :layout_id)'; // phpcs:ignore
214
        $values       = ['layout_id' => [$id, \PDO::PARAM_STR]];
215
        $expectedData = [
216
            [
217
                'id'         => $id,
218
                'name'       => $name,
219
                'identifier' => $identifier,
220
                'classes'    => $classes,
221
                'body'       => $body,
222
                'header'     => $header,
223
                'footer'     => $footer,
224
                'css_files'  => $cssFiles,
225
                'js_files'   => $jsFiles,
226
            ],
227
        ];
228
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
229
230
        $this->readConnectionMock
231
            ->expects($this->once())
232
            ->method('prepare')
233
            ->with($sql0)
234
            ->willReturn($statement0);
235
236
        $actualResult = $this->sut->getById($id);
237
238
        $this->assertEntity($expectedData[0], $actualResult);
239
    }
240
241
    public function testGetByIdentifier()
242
    {
243
        $id         = '4a7847c6-d202-4fc7-972b-bd4d634efda1';
244
        $name       = 'Foo';
245
        $identifier = 'foo';
246
        $classes    = 'tsa';
247
        $body       = 'bar';
248
        $header     = 'baz';
249
        $footer     = 'yak';
250
        $cssFiles   = 'zar';
251
        $jsFiles    = 'boi';
252
253
        $sql0         = 'SELECT page_layouts.id, page_layouts.name, page_layouts.identifier, page_layouts.classes, page_layouts.body, page_layouts.header, page_layouts.footer, page_layouts.css_files, page_layouts.js_files FROM page_layouts WHERE (page_layouts.deleted_at IS NULL) AND (identifier = :identifier)'; // phpcs:ignore
254
        $values       = ['identifier' => $identifier];
255
        $expectedData = [
256
            [
257
                'id'         => $id,
258
                'name'       => $name,
259
                'identifier' => $identifier,
260
                'classes'    => $classes,
261
                'body'       => $body,
262
                'header'     => $header,
263
                'footer'     => $footer,
264
                'css_files'  => $cssFiles,
265
                'js_files'   => $jsFiles,
266
            ],
267
        ];
268
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
269
270
        $this->readConnectionMock
271
            ->expects($this->once())
272
            ->method('prepare')
273
            ->with($sql0)
274
            ->willReturn($statement0);
275
276
        $actualResult = $this->sut->getByIdentifier($identifier);
277
278
        $this->assertEntity($expectedData[0], $actualResult);
279
    }
280
281
    public function testUpdate()
282
    {
283
        $id         = 'e62dd68b-c72c-464e-8e03-6ee86f26f592';
284
        $name       = 'Foo';
285
        $identifier = 'foo';
286
        $classes    = 'tsa';
287
        $body       = 'bar';
288
        $header     = 'baz';
289
        $footer     = 'yak';
290
        $cssFiles   = 'zar';
291
        $jsFiles    = 'boi';
292
293
        $sql0       = 'UPDATE page_layouts AS page_layouts SET name = ?, identifier = ?, classes = ?, body = ?, header = ?, footer = ?, css_files = ?, js_files = ? WHERE (id = ?) AND (deleted_at IS NULL)'; // phpcs:ignore
294
        $values     = [
295
            [$name, \PDO::PARAM_STR],
296
            [$identifier, \PDO::PARAM_STR],
297
            [$classes, \PDO::PARAM_STR],
298
            [$body, \PDO::PARAM_STR],
299
            [$header, \PDO::PARAM_STR],
300
            [$footer, \PDO::PARAM_STR],
301
            [$cssFiles, \PDO::PARAM_STR],
302
            [$jsFiles, \PDO::PARAM_STR],
303
            [$id, \PDO::PARAM_STR],
304
        ];
305
        $statement0 = MockStatementFactory::createWriteStatement($this, $values);
306
307
        $this->writeConnectionMock
308
            ->expects($this->once())
309
            ->method('prepare')
310
            ->with($sql0)
311
            ->willReturn($statement0);
312
313
        $assets = new PageLayout\Assets($identifier, $header, $footer, (array)$cssFiles, (array)$jsFiles);
314
        $entity = new PageLayout($id, $name, $identifier, $classes, $body, $assets);
315
316
        $this->sut->update($entity);
317
    }
318
319
    public function testAddThrowsExceptionIfCalledWithInvalidEntity()
320
    {
321
        $this->expectException(\InvalidArgumentException::class);
322
323
        /** @var IStringerEntity|MockObject $entity */
324
        $entity = $this->createMock(IStringerEntity::class);
325
326
        $this->sut->add($entity);
327
    }
328
329
    public function testDeleteThrowsExceptionIfCalledWithInvalidEntity()
330
    {
331
        $this->expectException(\InvalidArgumentException::class);
332
333
        /** @var IStringerEntity|MockObject $entity */
334
        $entity = $this->createMock(IStringerEntity::class);
335
336
        $this->sut->delete($entity);
337
    }
338
339
    public function testUpdateThrowsExceptionIfCalledWithInvalidEntity()
340
    {
341
        $this->expectException(\InvalidArgumentException::class);
342
343
        /** @var IStringerEntity|MockObject $entity */
344
        $entity = $this->createMock(IStringerEntity::class);
345
346
        $this->sut->update($entity);
347
    }
348
349
    /**
350
     * @param array      $expectedData
351
     * @param PageLayout $entity
352
     */
353
    protected function assertEntity(array $expectedData, $entity)
354
    {
355
        $this->assertInstanceOf(PageLayout::class, $entity);
356
        $this->assertSame($expectedData['id'], $entity->getId());
357
        $this->assertSame($expectedData['name'], $entity->getName());
358
        $this->assertSame($expectedData['identifier'], $entity->getIdentifier());
359
        $this->assertSame($expectedData['body'], $entity->getBody());
360
361
        $this->assertEntityAssets($expectedData, $entity);
362
    }
363
364
    /**
365
     * @param array      $expectedData
366
     * @param PageLayout $entity
367
     */
368
    protected function assertEntityAssets(array $expectedData, $entity)
369
    {
370
        $assets = $entity->getAssets();
371
        if (!$assets) {
372
            return;
373
        }
374
375
        $this->assertSame($expectedData['header'], $assets->getHeader());
376
        $this->assertSame($expectedData['footer'], $assets->getFooter());
377
        $this->assertSame((array)$expectedData['css_files'], $assets->getCssFiles());
378
        $this->assertSame((array)$expectedData['js_files'], $assets->getJsFiles());
379
    }
380
}
381