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

PageLayoutSqlDataMapperTest::testAdd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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