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\ContentList as Entity; |
11
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
12
|
|
|
|
13
|
|
|
class ContentListSqlDataMapperTest extends DataMapperTestCase |
14
|
|
|
{ |
15
|
|
|
/** @var ContentListSqlDataMapper - System Under Test */ |
16
|
|
|
protected $sut; |
17
|
|
|
|
18
|
|
|
public function setUp(): void |
19
|
|
|
{ |
20
|
|
|
parent::setUp(); |
21
|
|
|
|
22
|
|
|
$this->sut = new ContentListSqlDataMapper($this->readConnectionMock, $this->writeConnectionMock); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testAdd() |
26
|
|
|
{ |
27
|
|
|
$nextId = '9d160dd2-bd83-48f6-a3b0-e15d2f26e76c'; |
28
|
|
|
$name = 'Foo'; |
29
|
|
|
$identifier = 'foo'; |
30
|
|
|
$classes = 'foo0 foo1'; |
31
|
|
|
$protected = false; |
32
|
|
|
$withLinks = false; |
33
|
|
|
$withNameLinks = false; |
34
|
|
|
$withImages = false; |
35
|
|
|
$withHtml = false; |
36
|
|
|
$withClasses = false; |
37
|
|
|
|
38
|
|
|
$sql0 = 'INSERT INTO lists (id, name, identifier, classes, protected, with_links, with_label_links, with_html, with_images, with_classes) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; // phpcs:ignore |
39
|
|
|
$values = [ |
40
|
|
|
[$nextId, \PDO::PARAM_STR], |
41
|
|
|
[$name, \PDO::PARAM_STR], |
42
|
|
|
[$identifier, \PDO::PARAM_STR], |
43
|
|
|
[$classes, \PDO::PARAM_STR], |
44
|
|
|
[$protected, \PDO::PARAM_BOOL], |
45
|
|
|
[$withLinks, \PDO::PARAM_BOOL], |
46
|
|
|
[$withNameLinks, \PDO::PARAM_BOOL], |
47
|
|
|
[$withHtml, \PDO::PARAM_BOOL], |
48
|
|
|
[$withImages, \PDO::PARAM_BOOL], |
49
|
|
|
[$withClasses, \PDO::PARAM_BOOL], |
50
|
|
|
]; |
51
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values); |
52
|
|
|
|
53
|
|
|
$this->writeConnectionMock |
54
|
|
|
->expects($this->once()) |
55
|
|
|
->method('prepare') |
56
|
|
|
->with($sql0) |
57
|
|
|
->willReturn($statement0); |
58
|
|
|
|
59
|
|
|
$entity = new Entity( |
60
|
|
|
$nextId, |
61
|
|
|
$name, |
62
|
|
|
$identifier, |
63
|
|
|
$classes, |
64
|
|
|
$protected, |
65
|
|
|
$withLinks, |
66
|
|
|
$withNameLinks, |
67
|
|
|
$withImages, |
68
|
|
|
$withHtml, |
69
|
|
|
$withClasses |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
$this->sut->add($entity); |
73
|
|
|
|
74
|
|
|
$this->assertSame($nextId, $entity->getId()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testDelete() |
78
|
|
|
{ |
79
|
|
|
$id = '9d160dd2-bd83-48f6-a3b0-e15d2f26e76c'; |
80
|
|
|
|
81
|
|
|
$sql0 = 'UPDATE lists AS lists SET deleted_at = NOW() WHERE (id = ?)'; // phpcs:ignore |
82
|
|
|
$values0 = [[$id, \PDO::PARAM_STR]]; |
83
|
|
|
$statement0 = MockStatementFactory::createWriteStatement($this, $values0); |
84
|
|
|
|
85
|
|
|
$sql1 = 'UPDATE list_items AS list_items SET deleted_at = NOW() WHERE (list_id = ?) AND (deleted_at IS NOT NULL)'; // phpcs:ignore |
86
|
|
|
$values1 = [[$id, \PDO::PARAM_STR]]; |
87
|
|
|
$statement1 = MockStatementFactory::createWriteStatement($this, $values1); |
88
|
|
|
|
89
|
|
|
$this->writeConnectionMock |
90
|
|
|
->expects($this->exactly(2)) |
91
|
|
|
->method('prepare') |
92
|
|
|
->withConsecutive([$sql0], [$sql1]) |
93
|
|
|
->willReturnOnConsecutiveCalls($statement0, $statement1); |
94
|
|
|
|
95
|
|
|
$entity = new Entity($id, '', '', '', false, false, false, false, false, false); |
96
|
|
|
|
97
|
|
|
$this->sut->delete($entity); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testGetAll() |
101
|
|
|
{ |
102
|
|
|
$id = '8da63e49-5c76-4520-9280-30c125305239'; |
103
|
|
|
$name = 'Foo'; |
104
|
|
|
$identifier = 'foo'; |
105
|
|
|
$classes = 'foo0 foo1'; |
106
|
|
|
$protected = false; |
107
|
|
|
$withLinks = false; |
108
|
|
|
$withNameLinks = false; |
109
|
|
|
$withImages = false; |
110
|
|
|
$withHtml = false; |
111
|
|
|
$withClasses = false; |
112
|
|
|
|
113
|
|
|
$sql0 = 'SELECT lists.id, lists.name, lists.identifier, lists.classes, lists.protected, lists.with_links, lists.with_label_links, lists.with_html, lists.with_images, lists.with_classes FROM lists WHERE (lists.deleted_at IS NULL)'; // phpcs:ignore |
114
|
|
|
$values = []; |
115
|
|
|
$expectedData = [ |
116
|
|
|
[ |
117
|
|
|
'id' => $id, |
118
|
|
|
'name' => $name, |
119
|
|
|
'identifier' => $identifier, |
120
|
|
|
'classes' => $classes, |
121
|
|
|
'protected' => $protected, |
122
|
|
|
'with_links' => $withLinks, |
123
|
|
|
'with_label_links' => $withNameLinks, |
124
|
|
|
'with_images' => $withImages, |
125
|
|
|
'with_html' => $withHtml, |
126
|
|
|
'with_classes' => $withClasses, |
127
|
|
|
], |
128
|
|
|
]; |
129
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
130
|
|
|
|
131
|
|
|
$this->readConnectionMock |
132
|
|
|
->expects($this->once()) |
133
|
|
|
->method('prepare') |
134
|
|
|
->with($sql0) |
135
|
|
|
->willReturn($statement0); |
136
|
|
|
|
137
|
|
|
$actualResult = $this->sut->getAll(); |
138
|
|
|
|
139
|
|
|
$this->assertCollection($expectedData, $actualResult); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testGetPage() |
143
|
|
|
{ |
144
|
|
|
$id = '8da63e49-5c76-4520-9280-30c125305239'; |
145
|
|
|
$name = 'Foo'; |
146
|
|
|
$identifier = 'foo'; |
147
|
|
|
$classes = 'foo0 foo1'; |
148
|
|
|
$protected = false; |
149
|
|
|
$withLinks = false; |
150
|
|
|
$withNameLinks = false; |
151
|
|
|
$withImages = false; |
152
|
|
|
$withHtml = false; |
153
|
|
|
$withClasses = false; |
154
|
|
|
|
155
|
|
|
$sql0 = 'SELECT SQL_CALC_FOUND_ROWS lists.id, lists.name, lists.identifier, lists.classes, lists.protected, lists.with_links, lists.with_label_links, lists.with_html, lists.with_images, lists.with_classes FROM lists WHERE (lists.deleted_at IS NULL) ORDER BY name ASC LIMIT 10 OFFSET 0'; // phpcs:ignore |
156
|
|
|
$values = []; |
157
|
|
|
$expectedData = [ |
158
|
|
|
[ |
159
|
|
|
'id' => $id, |
160
|
|
|
'name' => $name, |
161
|
|
|
'identifier' => $identifier, |
162
|
|
|
'classes' => $classes, |
163
|
|
|
'protected' => $protected, |
164
|
|
|
'with_links' => $withLinks, |
165
|
|
|
'with_label_links' => $withNameLinks, |
166
|
|
|
'with_images' => $withImages, |
167
|
|
|
'with_html' => $withHtml, |
168
|
|
|
'with_classes' => $withClasses, |
169
|
|
|
], |
170
|
|
|
]; |
171
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
172
|
|
|
|
173
|
|
|
$this->readConnectionMock |
174
|
|
|
->expects($this->once()) |
175
|
|
|
->method('prepare') |
176
|
|
|
->with($sql0) |
177
|
|
|
->willReturn($statement0); |
178
|
|
|
|
179
|
|
|
$actualResult = $this->sut->getPage(0, 10, [], [], []); |
180
|
|
|
|
181
|
|
|
$this->assertCollection($expectedData, $actualResult); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function testGetPageWithOrdersAndConditions() |
185
|
|
|
{ |
186
|
|
|
$id = '8da63e49-5c76-4520-9280-30c125305239'; |
187
|
|
|
$name = 'Foo'; |
188
|
|
|
$identifier = 'foo'; |
189
|
|
|
$classes = 'foo0 foo1'; |
190
|
|
|
$protected = false; |
191
|
|
|
$withLinks = false; |
192
|
|
|
$withNameLinks = false; |
193
|
|
|
$withImages = false; |
194
|
|
|
$withHtml = false; |
195
|
|
|
$withClasses = false; |
196
|
|
|
|
197
|
|
|
$orders = ['lists.identifier ASC']; |
198
|
|
|
$conditions = ['lists.identifier LIKE \'abc%\'', 'lists.identifier LIKE \'%bca\'']; |
199
|
|
|
|
200
|
|
|
$sql0 = 'SELECT SQL_CALC_FOUND_ROWS lists.id, lists.name, lists.identifier, lists.classes, lists.protected, lists.with_links, lists.with_label_links, lists.with_html, lists.with_images, lists.with_classes FROM lists WHERE (lists.deleted_at IS NULL) AND (lists.identifier LIKE \'abc%\') AND (lists.identifier LIKE \'%bca\') ORDER BY lists.identifier ASC LIMIT 10 OFFSET 0'; // phpcs:ignore |
201
|
|
|
$values = []; |
202
|
|
|
$expectedData = [ |
203
|
|
|
[ |
204
|
|
|
'id' => $id, |
205
|
|
|
'name' => $name, |
206
|
|
|
'identifier' => $identifier, |
207
|
|
|
'classes' => $classes, |
208
|
|
|
'protected' => $protected, |
209
|
|
|
'with_links' => $withLinks, |
210
|
|
|
'with_label_links' => $withNameLinks, |
211
|
|
|
'with_html' => $withHtml, |
212
|
|
|
'with_images' => $withImages, |
213
|
|
|
'with_classes' => $withClasses, |
214
|
|
|
], |
215
|
|
|
]; |
216
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
217
|
|
|
|
218
|
|
|
$this->readConnectionMock |
219
|
|
|
->expects($this->once()) |
220
|
|
|
->method('prepare') |
221
|
|
|
->with($sql0) |
222
|
|
|
->willReturn($statement0); |
223
|
|
|
|
224
|
|
|
$actualResult = $this->sut->getPage(0, 10, $orders, $conditions, []); |
225
|
|
|
|
226
|
|
|
$this->assertCollection($expectedData, $actualResult); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public function testGetById() |
230
|
|
|
{ |
231
|
|
|
$id = 'da406cd9-4a65-4384-b1dd-454c4d26c196'; |
232
|
|
|
$name = 'Foo'; |
233
|
|
|
$identifier = 'foo'; |
234
|
|
|
$classes = 'foo0 foo1'; |
235
|
|
|
$protected = false; |
236
|
|
|
$withLinks = false; |
237
|
|
|
$withNameLinks = false; |
238
|
|
|
$withImages = false; |
239
|
|
|
$withHtml = false; |
240
|
|
|
$withClasses = false; |
241
|
|
|
|
242
|
|
|
$sql0 = 'SELECT lists.id, lists.name, lists.identifier, lists.classes, lists.protected, lists.with_links, lists.with_label_links, lists.with_html, lists.with_images, lists.with_classes FROM lists WHERE (lists.deleted_at IS NULL) AND (lists.id = :id)'; // phpcs:ignore |
243
|
|
|
$values = ['id' => [$id, \PDO::PARAM_STR]]; |
244
|
|
|
$expectedData = [ |
245
|
|
|
[ |
246
|
|
|
'id' => $id, |
247
|
|
|
'name' => $name, |
248
|
|
|
'identifier' => $identifier, |
249
|
|
|
'classes' => $classes, |
250
|
|
|
'protected' => $protected, |
251
|
|
|
'with_links' => $withLinks, |
252
|
|
|
'with_label_links' => $withNameLinks, |
253
|
|
|
'with_html' => $withHtml, |
254
|
|
|
'with_images' => $withImages, |
255
|
|
|
'with_classes' => $withClasses, |
256
|
|
|
], |
257
|
|
|
]; |
258
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
259
|
|
|
|
260
|
|
|
$this->readConnectionMock |
261
|
|
|
->expects($this->once()) |
262
|
|
|
->method('prepare') |
263
|
|
|
->with($sql0) |
264
|
|
|
->willReturn($statement0); |
265
|
|
|
|
266
|
|
|
$actualResult = $this->sut->getById($id); |
267
|
|
|
|
268
|
|
|
$this->assertEntity($expectedData[0], $actualResult); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
public function testGetByIdentifier() |
272
|
|
|
{ |
273
|
|
|
$id = '42f019b3-5d49-4ee0-b785-63ef245a1ee0'; |
274
|
|
|
$name = 'Foo'; |
275
|
|
|
$identifier = 'foo'; |
276
|
|
|
$classes = 'foo0 foo1'; |
277
|
|
|
$protected = false; |
278
|
|
|
$withLinks = false; |
279
|
|
|
$withNameLinks = false; |
280
|
|
|
$withImages = false; |
281
|
|
|
$withHtml = false; |
282
|
|
|
$withClasses = false; |
283
|
|
|
|
284
|
|
|
$sql0 = 'SELECT lists.id, lists.name, lists.identifier, lists.classes, lists.protected, lists.with_links, lists.with_label_links, lists.with_html, lists.with_images, lists.with_classes FROM lists WHERE (lists.deleted_at IS NULL) AND (lists.identifier = :identifier)'; // phpcs:ignore |
285
|
|
|
$values = ['identifier' => [$identifier, \PDO::PARAM_STR]]; |
286
|
|
|
$expectedData = [ |
287
|
|
|
[ |
288
|
|
|
'id' => $id, |
289
|
|
|
'name' => $name, |
290
|
|
|
'identifier' => $identifier, |
291
|
|
|
'classes' => $classes, |
292
|
|
|
'protected' => $protected, |
293
|
|
|
'with_links' => $withLinks, |
294
|
|
|
'with_label_links' => $withNameLinks, |
295
|
|
|
'with_html' => $withHtml, |
296
|
|
|
'with_images' => $withImages, |
297
|
|
|
'with_classes' => $withClasses, |
298
|
|
|
], |
299
|
|
|
]; |
300
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $values, $expectedData); |
301
|
|
|
|
302
|
|
|
$this->readConnectionMock |
303
|
|
|
->expects($this->once()) |
304
|
|
|
->method('prepare') |
305
|
|
|
->with($sql0) |
306
|
|
|
->willReturn($statement0); |
307
|
|
|
|
308
|
|
|
$actualResult = $this->sut->getByIdentifier($identifier); |
309
|
|
|
|
310
|
|
|
$this->assertEntity($expectedData[0], $actualResult); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
public function testAddThrowsExceptionIfCalledWithInvalidEntity() |
314
|
|
|
{ |
315
|
|
|
$this->expectException(\InvalidArgumentException::class); |
316
|
|
|
|
317
|
|
|
/** @var IStringerEntity|MockObject $entity */ |
318
|
|
|
$entity = $this->createMock(IStringerEntity::class); |
319
|
|
|
|
320
|
|
|
$this->sut->add($entity); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
public function testDeleteThrowsExceptionIfCalledWithInvalidEntity() |
324
|
|
|
{ |
325
|
|
|
$this->expectException(\InvalidArgumentException::class); |
326
|
|
|
|
327
|
|
|
/** @var IStringerEntity|MockObject $entity */ |
328
|
|
|
$entity = $this->createMock(IStringerEntity::class); |
329
|
|
|
|
330
|
|
|
$this->sut->delete($entity); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
public function testUpdateThrowsExceptionIfCalledWithInvalidEntity() |
334
|
|
|
{ |
335
|
|
|
$this->expectException(\InvalidArgumentException::class); |
336
|
|
|
|
337
|
|
|
/** @var IStringerEntity|MockObject $entity */ |
338
|
|
|
$entity = $this->createMock(IStringerEntity::class); |
339
|
|
|
|
340
|
|
|
$this->sut->update($entity); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* @param string[] $expectedData |
345
|
|
|
* @param Entity $entity |
346
|
|
|
*/ |
347
|
|
|
protected function assertEntity(array $expectedData, $entity) |
348
|
|
|
{ |
349
|
|
|
$this->assertInstanceOf(Entity::class, $entity); |
350
|
|
|
|
351
|
|
|
$this->assertSame($expectedData['id'], $entity->getId()); |
352
|
|
|
$this->assertSame($expectedData['identifier'], $entity->getIdentifier()); |
353
|
|
|
$this->assertSame($expectedData['classes'], $entity->getClasses()); |
354
|
|
|
$this->assertSame($expectedData['protected'], $entity->isProtected()); |
355
|
|
|
$this->assertSame($expectedData['with_links'], $entity->isWithLinks()); |
356
|
|
|
$this->assertSame($expectedData['with_label_links'], $entity->isWithLabelLinks()); |
357
|
|
|
$this->assertSame($expectedData['with_html'], $entity->isWithHtml()); |
358
|
|
|
$this->assertSame($expectedData['with_images'], $entity->isWithImages()); |
359
|
|
|
$this->assertSame($expectedData['with_classes'], $entity->isWithClasses()); |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
|