PageSqlDataMapperTest   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 726
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 506
dl 0
loc 726
rs 10
c 0
b 0
f 0
wmc 27

23 Methods

Rating   Name   Duplication   Size   Complexity  
A assertEntity() 0 12 2
A testDelete() 0 16 1
A testUpdateWithoutAssets() 0 37 1
A testUpdateWithCategoryId() 0 42 1
A testAddThrowsExceptionIfCalledWithInvalidEntity() 0 8 1
A createEntity() 0 12 3
A testGetAll() 0 29 1
A testUpdateSimple() 0 41 1
A setUp() 0 5 1
A testAddSimple() 0 44 1
A testGetPage() 0 32 1
A testUpdateThrowsExceptionIfCalledWithInvalidEntity() 0 8 1
A testUpdateWithLayoutId() 0 42 1
A testGetPageWithOrdersAndConditions() 0 35 1
A assertEntityAssets() 0 11 2
A testDeleteThrowsExceptionIfCalledWithInvalidEntity() 0 8 1
A testAddWithLayoutId() 0 45 1
A testGetById() 0 46 1
A testGetWithLayout() 0 55 1
A testGetByCategoryIdentifiersCanReturnEarly() 0 5 1
A testGetByCategoryIdentifiers() 0 36 1
A testAddWithCategoryId() 0 45 1
A testGetByIdentifier() 0 46 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\Page;
11
use AbterPhp\Website\Domain\Entities\PageCategory;
12
use AbterPhp\Website\Domain\Entities\PageLayout;
13
use PHPUnit\Framework\MockObject\MockObject;
14
15
class PageSqlDataMapperTest extends DataMapperTestCase
16
{
17
    /** @var PageSqlDataMapper - System Under Test */
18
    protected $sut;
19
20
    public function setUp(): void
21
    {
22
        parent::setUp();
23
24
        $this->sut = new PageSqlDataMapper($this->readConnectionMock, $this->writeConnectionMock);
25
    }
26
27
    /**
28
     * @param string          $id
29
     * @param string|null     $categoryId
30
     * @param string          $layout
31
     * @param PageLayout|null $layoutEntity
32
     * @param bool            $withAssets
33
     *
34
     * @return Page
35
     */
36
    protected function createEntity(
37
        string $id = '',
38
        ?string $categoryId = null,
39
        string $layout = 'qux',
40
        ?string $layoutId = null,
41
        bool $withAssets = true
42
    ): Page {
43
        $meta     = new Page\Meta('m1', 'm2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8');
44
        $assets   = $withAssets ? new Page\Assets('foo', 'baz', 'yak', ['zar'], ['boi'], null) : null;
45
        $category = $categoryId ? new PageCategory($categoryId, '', '') : null;
46
47
        return new Page($id, 'foo', 'bar', 'baz', 'quix', 'qvi', false, $category, $layout, $layoutId, $meta, $assets);
48
    }
49
50
    public function testAddSimple()
51
    {
52
        $nextId = 'fee8891b-2c31-49db-9a44-3e6179865c1f';
53
54
        $entity = $this->createEntity($nextId);
55
        $meta   = $entity->getMeta();
56
        $assets = $entity->getAssets();
57
58
        $sql0       = 'INSERT INTO pages (id, identifier, title, classes, lede, body, is_draft, category_id, layout, layout_id, meta_description, meta_robots, meta_author, meta_copyright, meta_keywords, meta_og_title, meta_og_image, meta_og_description, header, footer, css_files, js_files) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; // phpcs:ignore
59
        $values     = [
60
            [$entity->getId(), \PDO::PARAM_STR],
61
            [$entity->getIdentifier(), \PDO::PARAM_STR],
62
            [$entity->getTitle(), \PDO::PARAM_STR],
63
            [$entity->getClasses(), \PDO::PARAM_STR],
64
            [$entity->getLede(), \PDO::PARAM_STR],
65
            [$entity->getBody(), \PDO::PARAM_STR],
66
            [$entity->isDraft(), \PDO::PARAM_BOOL],
67
            [null, \PDO::PARAM_NULL],
68
            [$entity->getLayout(), \PDO::PARAM_STR],
69
            [$entity->getLayoutId(), \PDO::PARAM_NULL],
70
            [$meta->getDescription(), \PDO::PARAM_STR],
71
            [$meta->getRobots(), \PDO::PARAM_STR],
72
            [$meta->getAuthor(), \PDO::PARAM_STR],
73
            [$meta->getCopyright(), \PDO::PARAM_STR],
74
            [$meta->getKeywords(), \PDO::PARAM_STR],
75
            [$meta->getOGTitle(), \PDO::PARAM_STR],
76
            [$meta->getOGImage(), \PDO::PARAM_STR],
77
            [$meta->getOGDescription(), \PDO::PARAM_STR],
78
            [$assets->getHeader(), \PDO::PARAM_STR],
79
            [$assets->getFooter(), \PDO::PARAM_STR],
80
            [implode("\r\n", $assets->getCssFiles()), \PDO::PARAM_STR],
81
            [implode("\r\n", $assets->getJsFiles()), \PDO::PARAM_STR],
82
        ];
83
        $statement0 = MockStatementFactory::createWriteStatement($this, $values);
84
85
        $this->writeConnectionMock
86
            ->expects($this->once())
87
            ->method('prepare')
88
            ->with($sql0)
89
            ->willReturn($statement0);
90
91
        $this->sut->add($entity);
92
93
        $this->assertSame($nextId, (string)$entity->getId());
94
    }
95
96
    public function testAddWithLayoutId()
97
    {
98
        $nextId   = '9340c9ec-f1cd-4a85-bc71-15c13c31a22e';
99
        $layoutId = 'f1be9cd6-e7cb-40c1-b584-f265259bd8de';
100
101
        $entity = $this->createEntity($nextId, null, '', $layoutId);
102
        $meta   = $entity->getMeta();
103
        $assets = $entity->getAssets();
104
105
        $sql0       = 'INSERT INTO pages (id, identifier, title, classes, lede, body, is_draft, category_id, layout, layout_id, meta_description, meta_robots, meta_author, meta_copyright, meta_keywords, meta_og_title, meta_og_image, meta_og_description, header, footer, css_files, js_files) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; // phpcs:ignore
106
        $values     = [
107
            [$entity->getId(), \PDO::PARAM_STR],
108
            [$entity->getIdentifier(), \PDO::PARAM_STR],
109
            [$entity->getTitle(), \PDO::PARAM_STR],
110
            [$entity->getClasses(), \PDO::PARAM_STR],
111
            [$entity->getLede(), \PDO::PARAM_STR],
112
            [$entity->getBody(), \PDO::PARAM_STR],
113
            [$entity->isDraft(), \PDO::PARAM_BOOL],
114
            [null, \PDO::PARAM_NULL],
115
            [$entity->getLayout(), \PDO::PARAM_STR],
116
            [$entity->getLayoutId(), \PDO::PARAM_STR],
117
            [$meta->getDescription(), \PDO::PARAM_STR],
118
            [$meta->getRobots(), \PDO::PARAM_STR],
119
            [$meta->getAuthor(), \PDO::PARAM_STR],
120
            [$meta->getCopyright(), \PDO::PARAM_STR],
121
            [$meta->getKeywords(), \PDO::PARAM_STR],
122
            [$meta->getOGTitle(), \PDO::PARAM_STR],
123
            [$meta->getOGImage(), \PDO::PARAM_STR],
124
            [$meta->getOGDescription(), \PDO::PARAM_STR],
125
            [$assets->getHeader(), \PDO::PARAM_STR],
126
            [$assets->getFooter(), \PDO::PARAM_STR],
127
            [implode("\r\n", $assets->getCssFiles()), \PDO::PARAM_STR],
128
            [implode("\r\n", $assets->getJsFiles()), \PDO::PARAM_STR],
129
        ];
130
        $statement0 = MockStatementFactory::createWriteStatement($this, $values);
131
132
        $this->writeConnectionMock
133
            ->expects($this->once())
134
            ->method('prepare')
135
            ->with($sql0)
136
            ->willReturn($statement0);
137
138
        $this->sut->add($entity);
139
140
        $this->assertSame($nextId, $entity->getId());
141
    }
142
143
    public function testAddWithCategoryId()
144
    {
145
        $nextId     = '9340c9ec-f1cd-4a85-bc71-15c13c31a22e';
146
        $categoryId = 'f1be9cd6-e7cb-40c1-b584-f265259bd8de';
147
148
        $entity = $this->createEntity($nextId, $categoryId);
149
        $meta   = $entity->getMeta();
150
        $assets = $entity->getAssets();
151
152
        $sql0       = 'INSERT INTO pages (id, identifier, title, classes, lede, body, is_draft, category_id, layout, layout_id, meta_description, meta_robots, meta_author, meta_copyright, meta_keywords, meta_og_title, meta_og_image, meta_og_description, header, footer, css_files, js_files) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; // phpcs:ignore
153
        $values     = [
154
            [$entity->getId(), \PDO::PARAM_STR],
155
            [$entity->getIdentifier(), \PDO::PARAM_STR],
156
            [$entity->getTitle(), \PDO::PARAM_STR],
157
            [$entity->getClasses(), \PDO::PARAM_STR],
158
            [$entity->getLede(), \PDO::PARAM_STR],
159
            [$entity->getBody(), \PDO::PARAM_STR],
160
            [$entity->isDraft(), \PDO::PARAM_BOOL],
161
            [$entity->getCategory()->getId(), \PDO::PARAM_STR],
162
            [$entity->getLayout(), \PDO::PARAM_STR],
163
            [$entity->getLayoutId(), \PDO::PARAM_NULL],
164
            [$meta->getDescription(), \PDO::PARAM_STR],
165
            [$meta->getRobots(), \PDO::PARAM_STR],
166
            [$meta->getAuthor(), \PDO::PARAM_STR],
167
            [$meta->getCopyright(), \PDO::PARAM_STR],
168
            [$meta->getKeywords(), \PDO::PARAM_STR],
169
            [$meta->getOGTitle(), \PDO::PARAM_STR],
170
            [$meta->getOGImage(), \PDO::PARAM_STR],
171
            [$meta->getOGDescription(), \PDO::PARAM_STR],
172
            [$assets->getHeader(), \PDO::PARAM_STR],
173
            [$assets->getFooter(), \PDO::PARAM_STR],
174
            [implode("\r\n", $assets->getCssFiles()), \PDO::PARAM_STR],
175
            [implode("\r\n", $assets->getJsFiles()), \PDO::PARAM_STR],
176
        ];
177
        $statement0 = MockStatementFactory::createWriteStatement($this, $values);
178
179
        $this->writeConnectionMock
180
            ->expects($this->once())
181
            ->method('prepare')
182
            ->with($sql0)
183
            ->willReturn($statement0);
184
185
        $this->sut->add($entity);
186
187
        $this->assertSame($nextId, $entity->getId());
188
    }
189
190
    public function testDelete()
191
    {
192
        $id     = '2fdfb4aa-b199-40d6-86bd-06eed25bff43';
193
        $entity = $this->createEntity($id);
194
195
        $sql0       = 'UPDATE pages AS pages SET deleted_at = NOW() WHERE (id = ?)'; // phpcs:ignore
196
        $values     = [[$entity->getId(), \PDO::PARAM_STR]];
197
        $statement0 = MockStatementFactory::createWriteStatement($this, $values);
198
199
        $this->writeConnectionMock
200
            ->expects($this->once())
201
            ->method('prepare')
202
            ->with($sql0)
203
            ->willReturn($statement0);
204
205
        $this->sut->delete($entity);
206
    }
207
208
    public function testGetAll()
209
    {
210
        $id       = '63111dd1-4ea8-4152-83fa-59463d1d92fb';
211
        $entity   = $this->createEntity($id);
212
        $layoutId = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $layoutId is dead and can be removed.
Loading history...
213
214
        $sql0         = 'SELECT pages.id, pages.identifier, pages.title, pages.is_draft, pages.category_id, pages.layout_id FROM pages WHERE (pages.deleted_at IS NULL)'; // phpcs:ignore
215
        $values       = [];
216
        $expectedData = [
217
            [
218
                'id'          => $entity->getId(),
219
                'identifier'  => $entity->getIdentifier(),
220
                'title'       => $entity->getTitle(),
221
                'is_draft'    => $entity->isDraft(),
222
                'category_id' => null,
223
                'layout_id'   => $entity->getLayoutId(),
224
            ],
225
        ];
226
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
227
228
        $this->readConnectionMock
229
            ->expects($this->once())
230
            ->method('prepare')
231
            ->with($sql0)
232
            ->willReturn($statement0);
233
234
        $actualResult = $this->sut->getAll();
235
236
        $this->assertCollection($expectedData, $actualResult);
237
    }
238
239
    public function testGetPage()
240
    {
241
        $id         = 'df6b4637-634e-4544-a167-2bddf3eab498';
242
        $identifier = 'foo';
243
        $title      = 'bar';
244
        $isDraft    = false;
245
        $categoryId = '6c79a886-9c74-441b-b205-dc1d274e7e55';
246
        $layoutId   = '0ec12802-0eb4-4a90-b0ba-454d4d42a367';
247
248
        $sql0         = 'SELECT SQL_CALC_FOUND_ROWS pages.id, pages.identifier, pages.title, pages.is_draft, categories.name AS category_name, pages.layout_id, IF(layouts.name <> \'\', layouts.name, pages.layout) AS layout FROM pages LEFT JOIN page_categories AS categories ON categories.id = pages.category_id LEFT JOIN page_layouts AS layouts ON layouts.id = pages.layout_id WHERE (pages.deleted_at IS NULL) ORDER BY pages.title ASC LIMIT 10 OFFSET 0'; // phpcs:ignore
249
        $values       = [];
250
        $expectedData = [
251
            [
252
                'id'          => $id,
253
                'identifier'  => $identifier,
254
                'title'       => $title,
255
                'is_draft'    => $isDraft,
256
                'category_id' => $categoryId,
257
                'layout_id'   => $layoutId,
258
            ],
259
        ];
260
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
261
262
        $this->readConnectionMock
263
            ->expects($this->once())
264
            ->method('prepare')
265
            ->with($sql0)
266
            ->willReturn($statement0);
267
268
        $actualResult = $this->sut->getPage(0, 10, [], [], []);
269
270
        $this->assertCollection($expectedData, $actualResult);
271
    }
272
273
    public function testGetPageWithOrdersAndConditions()
274
    {
275
        $id         = 'df6b4637-634e-4544-a167-2bddf3eab498';
276
        $identifier = 'foo';
277
        $title      = 'bar';
278
        $isDraft    = false;
279
        $categoryId = '6c79a886-9c74-441b-b205-dc1d274e7e55';
280
        $layoutId   = '0ec12802-0eb4-4a90-b0ba-454d4d42a367';
281
282
        $orders     = ['pages.identifier ASC'];
283
        $conditions = ['pages.identifier LIKE \'abc%\'', 'pages.identifier LIKE \'%bca\''];
284
285
        $sql0         = 'SELECT SQL_CALC_FOUND_ROWS pages.id, pages.identifier, pages.title, pages.is_draft, categories.name AS category_name, pages.layout_id, IF(layouts.name <> \'\', layouts.name, pages.layout) AS layout FROM pages LEFT JOIN page_categories AS categories ON categories.id = pages.category_id LEFT JOIN page_layouts AS layouts ON layouts.id = pages.layout_id WHERE (pages.deleted_at IS NULL) AND (pages.identifier LIKE \'abc%\') AND (pages.identifier LIKE \'%bca\') ORDER BY pages.identifier ASC LIMIT 10 OFFSET 0'; // phpcs:ignore
286
        $values       = [];
287
        $expectedData = [
288
            [
289
                'id'          => $id,
290
                'identifier'  => $identifier,
291
                'title'       => $title,
292
                'is_draft'    => $isDraft,
293
                'category_id' => $categoryId,
294
                'layout_id'   => $layoutId,
295
            ],
296
        ];
297
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
298
299
        $this->readConnectionMock
300
            ->expects($this->once())
301
            ->method('prepare')
302
            ->with($sql0)
303
            ->willReturn($statement0);
304
305
        $actualResult = $this->sut->getPage(0, 10, $orders, $conditions, []);
306
307
        $this->assertCollection($expectedData, $actualResult);
308
    }
309
310
    public function testGetByCategoryIdentifiers()
311
    {
312
        $id         = 'df6b4637-634e-4544-a167-2bddf3eab498';
313
        $identifier = 'foo';
314
        $title      = 'bar';
315
        $isDraft    = false;
316
        $categoryId = '6c79a886-9c74-441b-b205-dc1d274e7e55';
317
        $layoutId   = '0ec12802-0eb4-4a90-b0ba-454d4d42a367';
318
319
        $identifiers = [$identifier];
320
321
        $sql0         = 'SELECT pages.id, pages.identifier, pages.title, pages.lede, pages.is_draft, page_categories.id AS category_id, page_categories.identifier AS category_identifier, page_categories.name AS category_name FROM pages INNER JOIN page_categories AS page_categories ON page_categories.id = pages.category_id WHERE (pages.deleted_at IS NULL) AND (page_categories.identifier IN (?)) AND (pages.is_draft = 0)'; // phpcs:ignore
322
        $values       = [
323
            [$identifier, \PDO::PARAM_STR],
324
        ];
325
        $expectedData = [
326
            [
327
                'id'          => $id,
328
                'identifier'  => $identifier,
329
                'title'       => $title,
330
                'is_draft'    => $isDraft,
331
                'category_id' => $categoryId,
332
                'layout_id'   => $layoutId,
333
            ],
334
        ];
335
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
336
337
        $this->readConnectionMock
338
            ->expects($this->once())
339
            ->method('prepare')
340
            ->with($sql0)
341
            ->willReturn($statement0);
342
343
        $actualResult = $this->sut->getByCategoryIdentifiers($identifiers);
344
345
        $this->assertCollection($expectedData, $actualResult);
346
    }
347
348
    public function testGetByCategoryIdentifiersCanReturnEarly()
349
    {
350
        $actualResult = $this->sut->getByCategoryIdentifiers([]);
351
352
        $this->assertSame([], $actualResult);
353
    }
354
355
    public function testGetById()
356
    {
357
        $id     = '24ce60d4-95a6-441b-9c95-fe578ef1e23c';
358
        $entity = $this->createEntity($id);
359
        $meta   = $entity->getMeta();
360
        $assets = $entity->getAssets();
361
362
        $sql0         = 'SELECT pages.id, pages.identifier, pages.title, pages.classes, pages.lede, pages.body, pages.is_draft, pages.category_id, pages.layout_id, pages.layout, pages.meta_description, pages.meta_robots, pages.meta_author, pages.meta_copyright, pages.meta_keywords, pages.meta_og_title, pages.meta_og_image, pages.meta_og_description, pages.header, pages.footer, pages.css_files, pages.js_files FROM pages WHERE (pages.deleted_at IS NULL) AND (pages.id = :page_id)'; // phpcs:ignore
363
        $values       = ['page_id' => [$id, \PDO::PARAM_STR]];
364
        $expectedData = [
365
            [
366
                'id'                  => $entity->getId(),
367
                'identifier'          => $entity->getIdentifier(),
368
                'title'               => $entity->getTitle(),
369
                'classes'             => $entity->getClasses(),
370
                'lede'                => $entity->getLede(),
371
                'body'                => $entity->getBody(),
372
                'is_draft'            => $entity->isDraft(),
373
                'category_id'         => null,
374
                'layout'              => $entity->getLayout(),
375
                'layout_id'           => $entity->getLayoutId(),
376
                'meta_description'    => $meta->getDescription(),
377
                'meta_robots'         => $meta->getRobots(),
378
                'meta_author'         => $meta->getAuthor(),
379
                'meta_copyright'      => $meta->getCopyright(),
380
                'meta_keywords'       => $meta->getKeywords(),
381
                'meta_og_title'       => $meta->getOGTitle(),
382
                'meta_og_image'       => $meta->getOGImage(),
383
                'meta_og_description' => $meta->getOGDescription(),
384
                'header'              => $assets->getHeader(),
385
                'footer'              => $assets->getFooter(),
386
                'css_files'           => implode("\r\n", $assets->getCssFiles()),
387
                'js_files'            => implode("\r\n", $assets->getJsFiles()),
388
            ],
389
        ];
390
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
391
392
        $this->readConnectionMock
393
            ->expects($this->once())
394
            ->method('prepare')
395
            ->with($sql0)
396
            ->willReturn($statement0);
397
398
        $actualResult = $this->sut->getById($id);
399
400
        $this->assertEntity($expectedData[0], $actualResult);
401
    }
402
403
    public function testGetByIdentifier()
404
    {
405
        $id     = '08cf6a6b-5d86-405b-b573-fa4a6f4c6122';
406
        $entity = $this->createEntity($id);
407
        $meta   = $entity->getMeta();
408
        $assets = $entity->getAssets();
409
410
        $sql0         = 'SELECT pages.id, pages.identifier, pages.title, pages.classes, pages.lede, pages.body, pages.is_draft, pages.category_id, pages.layout_id, pages.layout, pages.meta_description, pages.meta_robots, pages.meta_author, pages.meta_copyright, pages.meta_keywords, pages.meta_og_title, pages.meta_og_image, pages.meta_og_description, pages.header, pages.footer, pages.css_files, pages.js_files FROM pages WHERE (pages.deleted_at IS NULL) AND (pages.identifier = :identifier)'; // phpcs:ignore
411
        $values       = ['identifier' => [$entity->getIdentifier(), \PDO::PARAM_STR]];
412
        $expectedData = [
413
            [
414
                'id'                  => $entity->getId(),
415
                'identifier'          => $entity->getIdentifier(),
416
                'title'               => $entity->getTitle(),
417
                'classes'             => $entity->getClasses(),
418
                'lede'                => $entity->getLede(),
419
                'body'                => $entity->getBody(),
420
                'is_draft'            => (string)$entity->isDraft(),
421
                'category_id'         => null,
422
                'layout'              => $entity->getLayout(),
423
                'layout_id'           => $entity->getLayoutId(),
424
                'meta_description'    => $meta->getDescription(),
425
                'meta_robots'         => $meta->getRobots(),
426
                'meta_author'         => $meta->getAuthor(),
427
                'meta_copyright'      => $meta->getCopyright(),
428
                'meta_keywords'       => $meta->getKeywords(),
429
                'meta_og_title'       => $meta->getOGTitle(),
430
                'meta_og_image'       => $meta->getOGImage(),
431
                'meta_og_description' => $meta->getOGDescription(),
432
                'header'              => $assets->getHeader(),
433
                'footer'              => $assets->getFooter(),
434
                'css_files'           => implode("\r\n", $assets->getCssFiles()),
435
                'js_files'            => implode("\r\n", $assets->getJsFiles()),
436
            ],
437
        ];
438
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
439
440
        $this->readConnectionMock
441
            ->expects($this->once())
442
            ->method('prepare')
443
            ->with($sql0)
444
            ->willReturn($statement0);
445
446
        $actualResult = $this->sut->getByIdentifier($entity->getIdentifier());
447
448
        $this->assertEntity($expectedData[0], $actualResult);
449
    }
450
451
    public function testGetWithLayout()
452
    {
453
        $id       = '08cf6a6b-5d86-405b-b573-fa4a6f4c6122';
454
        $entity   = $this->createEntity($id);
455
        $meta     = $entity->getMeta();
456
        $assets   = $entity->getAssets();
457
        $layoutId = '3f98d8ae-06b3-4fd3-8539-284b377f741b';
458
459
        $sql0         = 'SELECT pages.id, pages.identifier, pages.title, CONCAT(layouts.classes, \' \', pages.classes) AS classes, pages.lede, pages.body, pages.is_draft, pages.category_id, pages.layout_id, COALESCE(layouts.body, pages.layout) AS layout, pages.meta_description, pages.meta_robots, pages.meta_author, pages.meta_copyright, pages.meta_keywords, pages.meta_og_title, pages.meta_og_image, pages.meta_og_description, pages.header AS header, pages.footer AS footer, pages.css_files AS css_files, pages.js_files AS js_files, layouts.identifier AS layout_identifier, layouts.header AS layout_header, layouts.footer AS layout_footer, layouts.css_files AS layout_css_files, layouts.js_files AS layout_js_files FROM pages LEFT JOIN page_layouts AS layouts ON layouts.id = pages.layout_id AND layouts.deleted_at IS NULL WHERE (pages.deleted_at IS NULL) AND ((pages.identifier = :identifier OR pages.id = :identifier))';  // phpcs:ignore
460
        $values       = ['identifier' => [$entity->getIdentifier(), \PDO::PARAM_STR]];
461
        $expectedData = [
462
            [
463
                'id'                  => $entity->getId(),
464
                'identifier'          => $entity->getIdentifier(),
465
                'title'               => $entity->getTitle(),
466
                'classes'             => $entity->getClasses(),
467
                'lede'                => $entity->getLede(),
468
                'body'                => $entity->getBody(),
469
                'is_draft'            => (string)$entity->isDraft(),
470
                'category_id'         => null,
471
                'layout'              => $entity->getLayout(),
472
                'layout_id'           => $entity->getLayoutId(),
473
                'meta_description'    => $meta->getDescription(),
474
                'meta_robots'         => $meta->getRobots(),
475
                'meta_author'         => $meta->getAuthor(),
476
                'meta_copyright'      => $meta->getCopyright(),
477
                'meta_keywords'       => $meta->getKeywords(),
478
                'meta_og_title'       => $meta->getOGTitle(),
479
                'meta_og_image'       => $meta->getOGImage(),
480
                'meta_og_description' => $meta->getOGDescription(),
481
                'header'              => '',
482
                'footer'              => '',
483
                'css_files'           => '',
484
                'js_files'            => '',
485
                'layout_identifier'   => $layoutId,
486
                'layout_header'       => $assets->getHeader(),
487
                'layout_footer'       => $assets->getFooter(),
488
                'layout_css_files'    => implode("\r\n", $assets->getCssFiles()),
489
                'layout_js_files'     => implode("\r\n", $assets->getJsFiles()),
490
            ],
491
        ];
492
        $statement0   = MockStatementFactory::createReadStatement($this, $values, $expectedData);
493
494
        $this->readConnectionMock
495
            ->expects($this->once())
496
            ->method('prepare')
497
            ->with($sql0)
498
            ->willReturn($statement0);
499
500
        $actualResult = $this->sut->getWithLayout($entity->getIdentifier());
501
502
        $this->assertNotEmpty($actualResult->getAssets());
503
        $this->assertNotEmpty($actualResult->getAssets()->getLayoutAssets());
504
        $this->assertEquals($assets->getCssFiles(), $actualResult->getAssets()->getLayoutAssets()->getCssFiles());
505
        $this->assertEquals($assets->getJsFiles(), $actualResult->getAssets()->getLayoutAssets()->getJsFiles());
506
    }
507
508
    public function testUpdateSimple()
509
    {
510
        $id     = 'ea075f20-95de-4ce4-9dfb-13bae781031d';
511
        $entity = $this->createEntity($id);
512
        $meta   = $entity->getMeta();
513
        $assets = $entity->getAssets();
514
515
        $sql0       = 'UPDATE pages AS pages SET identifier = ?, title = ?, classes = ?, lede = ?, body = ?, is_draft = ?, category_id = ?, layout = ?, layout_id = ?, meta_description = ?, meta_robots = ?, meta_author = ?, meta_copyright = ?, meta_keywords = ?, meta_og_title = ?, meta_og_image = ?, meta_og_description = ?, header = ?, footer = ?, css_files = ?, js_files = ? WHERE (id = ?) AND (deleted_at IS NULL)'; // phpcs:ignore
516
        $values     = [
517
            [$entity->getIdentifier(), \PDO::PARAM_STR],
518
            [$entity->getTitle(), \PDO::PARAM_STR],
519
            [$entity->getClasses(), \PDO::PARAM_STR],
520
            [$entity->getLede(), \PDO::PARAM_STR],
521
            [$entity->getBody(), \PDO::PARAM_STR],
522
            [$entity->isDraft(), \PDO::PARAM_BOOL],
523
            [null, \PDO::PARAM_NULL],
524
            [$entity->getLayout(), \PDO::PARAM_STR],
525
            [$entity->getLayoutId(), \PDO::PARAM_NULL],
526
            [$meta->getDescription(), \PDO::PARAM_STR],
527
            [$meta->getRobots(), \PDO::PARAM_STR],
528
            [$meta->getAuthor(), \PDO::PARAM_STR],
529
            [$meta->getCopyright(), \PDO::PARAM_STR],
530
            [$meta->getKeywords(), \PDO::PARAM_STR],
531
            [$meta->getOGTitle(), \PDO::PARAM_STR],
532
            [$meta->getOGImage(), \PDO::PARAM_STR],
533
            [$meta->getOGDescription(), \PDO::PARAM_STR],
534
            [$assets->getHeader(), \PDO::PARAM_STR],
535
            [$assets->getFooter(), \PDO::PARAM_STR],
536
            [implode("\r\n", $assets->getCssFiles()), \PDO::PARAM_STR],
537
            [implode("\r\n", $assets->getJsFiles()), \PDO::PARAM_STR],
538
            [$entity->getId(), \PDO::PARAM_STR],
539
        ];
540
        $statement0 = MockStatementFactory::createWriteStatement($this, $values);
541
542
        $this->writeConnectionMock
543
            ->expects($this->once())
544
            ->method('prepare')
545
            ->with($sql0)
546
            ->willReturn($statement0);
547
548
        $this->sut->update($entity);
549
    }
550
551
    public function testUpdateWithLayoutId()
552
    {
553
        $id       = 'd90e6027-75ef-4121-8fda-7f38f7cd39e6';
554
        $layoutId = 'ffe0bd1c-8b9a-4cb4-8255-86444e37223a';
555
        $entity   = $this->createEntity($id, null, '', $layoutId);
556
        $meta     = $entity->getMeta();
557
        $assets   = $entity->getAssets();
558
559
        $sql0       = 'UPDATE pages AS pages SET identifier = ?, title = ?, classes = ?, lede = ?, body = ?, is_draft = ?, category_id = ?, layout = ?, layout_id = ?, meta_description = ?, meta_robots = ?, meta_author = ?, meta_copyright = ?, meta_keywords = ?, meta_og_title = ?, meta_og_image = ?, meta_og_description = ?, header = ?, footer = ?, css_files = ?, js_files = ? WHERE (id = ?) AND (deleted_at IS NULL)'; // phpcs:ignore
560
        $values     = [
561
            [$entity->getIdentifier(), \PDO::PARAM_STR],
562
            [$entity->getTitle(), \PDO::PARAM_STR],
563
            [$entity->getClasses(), \PDO::PARAM_STR],
564
            [$entity->getLede(), \PDO::PARAM_STR],
565
            [$entity->getBody(), \PDO::PARAM_STR],
566
            [$entity->isDraft(), \PDO::PARAM_BOOL],
567
            [null, \PDO::PARAM_NULL],
568
            [$entity->getLayout(), \PDO::PARAM_STR],
569
            [$entity->getLayoutId(), \PDO::PARAM_STR],
570
            [$meta->getDescription(), \PDO::PARAM_STR],
571
            [$meta->getRobots(), \PDO::PARAM_STR],
572
            [$meta->getAuthor(), \PDO::PARAM_STR],
573
            [$meta->getCopyright(), \PDO::PARAM_STR],
574
            [$meta->getKeywords(), \PDO::PARAM_STR],
575
            [$meta->getOGTitle(), \PDO::PARAM_STR],
576
            [$meta->getOGImage(), \PDO::PARAM_STR],
577
            [$meta->getOGDescription(), \PDO::PARAM_STR],
578
            [$assets->getHeader(), \PDO::PARAM_STR],
579
            [$assets->getFooter(), \PDO::PARAM_STR],
580
            [implode("\r\n", $assets->getCssFiles()), \PDO::PARAM_STR],
581
            [implode("\r\n", $assets->getJsFiles()), \PDO::PARAM_STR],
582
            [$entity->getId(), \PDO::PARAM_STR],
583
        ];
584
        $statement0 = MockStatementFactory::createWriteStatement($this, $values);
585
586
        $this->writeConnectionMock
587
            ->expects($this->once())
588
            ->method('prepare')
589
            ->with($sql0)
590
            ->willReturn($statement0);
591
592
        $this->sut->update($entity);
593
    }
594
595
    public function testUpdateWithoutAssets()
596
    {
597
        $id       = 'd90e6027-75ef-4121-8fda-7f38f7cd39e6';
598
        $layoutId = 'ffe0bd1c-8b9a-4cb4-8255-86444e37223a';
599
        $entity   = $this->createEntity($id, null, '', $layoutId, false);
600
        $meta     = $entity->getMeta();
601
602
        $sql0       = 'UPDATE pages AS pages SET identifier = ?, title = ?, classes = ?, lede = ?, body = ?, is_draft = ?, category_id = ?, layout = ?, layout_id = ?, meta_description = ?, meta_robots = ?, meta_author = ?, meta_copyright = ?, meta_keywords = ?, meta_og_title = ?, meta_og_image = ?, meta_og_description = ? WHERE (id = ?) AND (deleted_at IS NULL)'; // phpcs:ignore
603
        $values     = [
604
            [$entity->getIdentifier(), \PDO::PARAM_STR],
605
            [$entity->getTitle(), \PDO::PARAM_STR],
606
            [$entity->getClasses(), \PDO::PARAM_STR],
607
            [$entity->getLede(), \PDO::PARAM_STR],
608
            [$entity->getBody(), \PDO::PARAM_STR],
609
            [$entity->isDraft(), \PDO::PARAM_BOOL],
610
            [null, \PDO::PARAM_NULL],
611
            [$entity->getLayout(), \PDO::PARAM_STR],
612
            [$entity->getLayoutId(), \PDO::PARAM_STR],
613
            [$meta->getDescription(), \PDO::PARAM_STR],
614
            [$meta->getRobots(), \PDO::PARAM_STR],
615
            [$meta->getAuthor(), \PDO::PARAM_STR],
616
            [$meta->getCopyright(), \PDO::PARAM_STR],
617
            [$meta->getKeywords(), \PDO::PARAM_STR],
618
            [$meta->getOGTitle(), \PDO::PARAM_STR],
619
            [$meta->getOGImage(), \PDO::PARAM_STR],
620
            [$meta->getOGDescription(), \PDO::PARAM_STR],
621
            [$entity->getId(), \PDO::PARAM_STR],
622
        ];
623
        $statement0 = MockStatementFactory::createWriteStatement($this, $values);
624
625
        $this->writeConnectionMock
626
            ->expects($this->once())
627
            ->method('prepare')
628
            ->with($sql0)
629
            ->willReturn($statement0);
630
631
        $this->sut->update($entity);
632
    }
633
634
    public function testUpdateWithCategoryId()
635
    {
636
        $id         = 'd90e6027-75ef-4121-8fda-7f38f7cd39e6';
637
        $categoryId = 'ffe0bd1c-8b9a-4cb4-8255-86444e37223a';
638
        $entity     = $this->createEntity($id, $categoryId);
639
        $meta       = $entity->getMeta();
640
        $assets     = $entity->getAssets();
641
642
        $sql0       = 'UPDATE pages AS pages SET identifier = ?, title = ?, classes = ?, lede = ?, body = ?, is_draft = ?, category_id = ?, layout = ?, layout_id = ?, meta_description = ?, meta_robots = ?, meta_author = ?, meta_copyright = ?, meta_keywords = ?, meta_og_title = ?, meta_og_image = ?, meta_og_description = ?, header = ?, footer = ?, css_files = ?, js_files = ? WHERE (id = ?) AND (deleted_at IS NULL)'; // phpcs:ignore
643
        $values     = [
644
            [$entity->getIdentifier(), \PDO::PARAM_STR],
645
            [$entity->getTitle(), \PDO::PARAM_STR],
646
            [$entity->getClasses(), \PDO::PARAM_STR],
647
            [$entity->getLede(), \PDO::PARAM_STR],
648
            [$entity->getBody(), \PDO::PARAM_STR],
649
            [$entity->isDraft(), \PDO::PARAM_BOOL],
650
            [$entity->getCategory()->getId(), \PDO::PARAM_STR],
651
            [$entity->getLayout(), \PDO::PARAM_STR],
652
            [$entity->getLayoutId(), \PDO::PARAM_NULL],
653
            [$meta->getDescription(), \PDO::PARAM_STR],
654
            [$meta->getRobots(), \PDO::PARAM_STR],
655
            [$meta->getAuthor(), \PDO::PARAM_STR],
656
            [$meta->getCopyright(), \PDO::PARAM_STR],
657
            [$meta->getKeywords(), \PDO::PARAM_STR],
658
            [$meta->getOGTitle(), \PDO::PARAM_STR],
659
            [$meta->getOGImage(), \PDO::PARAM_STR],
660
            [$meta->getOGDescription(), \PDO::PARAM_STR],
661
            [$assets->getHeader(), \PDO::PARAM_STR],
662
            [$assets->getFooter(), \PDO::PARAM_STR],
663
            [implode("\r\n", $assets->getCssFiles()), \PDO::PARAM_STR],
664
            [implode("\r\n", $assets->getJsFiles()), \PDO::PARAM_STR],
665
            [$entity->getId(), \PDO::PARAM_STR],
666
        ];
667
        $statement0 = MockStatementFactory::createWriteStatement($this, $values);
668
669
        $this->writeConnectionMock
670
            ->expects($this->once())
671
            ->method('prepare')
672
            ->with($sql0)
673
            ->willReturn($statement0);
674
675
        $this->sut->update($entity);
676
    }
677
678
    public function testAddThrowsExceptionIfCalledWithInvalidEntity()
679
    {
680
        $this->expectException(\InvalidArgumentException::class);
681
682
        /** @var IStringerEntity|MockObject $entity */
683
        $entity = $this->createMock(IStringerEntity::class);
684
685
        $this->sut->add($entity);
686
    }
687
688
    public function testDeleteThrowsExceptionIfCalledWithInvalidEntity()
689
    {
690
        $this->expectException(\InvalidArgumentException::class);
691
692
        /** @var IStringerEntity|MockObject $entity */
693
        $entity = $this->createMock(IStringerEntity::class);
694
695
        $this->sut->delete($entity);
696
    }
697
698
    public function testUpdateThrowsExceptionIfCalledWithInvalidEntity()
699
    {
700
        $this->expectException(\InvalidArgumentException::class);
701
702
        /** @var IStringerEntity|MockObject $entity */
703
        $entity = $this->createMock(IStringerEntity::class);
704
705
        $this->sut->update($entity);
706
    }
707
708
    /**
709
     * @param array $expectedData
710
     * @param Page  $entity
711
     */
712
    protected function assertEntity(array $expectedData, $entity)
713
    {
714
        $categoryId = $entity->getCategory() ? $entity->getCategory()->getId() : null;
715
716
        $this->assertInstanceOf(Page::class, $entity);
717
        $this->assertSame($expectedData['id'], $entity->getId());
718
        $this->assertSame($expectedData['identifier'], $entity->getIdentifier());
719
        $this->assertSame($expectedData['title'], $entity->getTitle());
720
        $this->assertSame($expectedData['category_id'], $categoryId);
721
        $this->assertSame($expectedData['layout_id'], $entity->getLayoutId());
722
723
        $this->assertEntityAssets($expectedData, $entity);
724
    }
725
726
    /**
727
     * @param array $expectedData
728
     * @param Page  $entity
729
     */
730
    protected function assertEntityAssets(array $expectedData, $entity)
731
    {
732
        $assets = $entity->getAssets();
733
        if (!$assets) {
734
            return;
735
        }
736
737
        $this->assertSame($expectedData['header'], $assets->getHeader());
738
        $this->assertSame($expectedData['footer'], $assets->getFooter());
739
        $this->assertSame((array)$expectedData['css_files'], $assets->getCssFiles());
740
        $this->assertSame((array)$expectedData['js_files'], $assets->getJsFiles());
741
    }
742
}
743