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