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

PageSqlDataMapperTest::testGetWithLayout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 55
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 46
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 55
rs 9.1781

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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