1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jacobemerick\Web\Domain\Blog\Post; |
4
|
|
|
|
5
|
|
|
use Aura\Sql\ConnectionLocator; |
6
|
|
|
use Aura\Sql\ExtendedPdo; |
7
|
|
|
use DateTime; |
8
|
|
|
use PHPUnit_Framework_TestCase; |
9
|
|
|
|
10
|
|
|
class MysqlPostRepositoryTest extends PHPUnit_Framework_TestCase |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
protected static $connection; |
14
|
|
|
|
15
|
|
View Code Duplication |
public static function setUpBeforeClass() |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
$extendedPdo = new ExtendedPdo('sqlite::memory:'); |
18
|
|
|
$extendedPdo->exec('ATTACH DATABASE `jpemeric_blog.db` AS `jpemeric_blog`'); |
19
|
|
|
|
20
|
|
|
$extendedPdo->exec(" |
21
|
|
|
CREATE TABLE IF NOT EXISTS `jpemeric_blog`.`post` ( |
22
|
|
|
`id` integer PRIMARY KEY AUTOINCREMENT, |
23
|
|
|
`title` varchar(60) NOT NULL, |
24
|
|
|
`path` varchar(60) NOT NULL, |
25
|
|
|
`category` varchar(15) NOT NULL, |
26
|
|
|
`date` datetime, |
27
|
|
|
`body` text, |
28
|
|
|
`display` integer(1) NOT NULL |
29
|
|
|
)" |
30
|
|
|
); |
31
|
|
|
$extendedPdo->exec(" |
32
|
|
|
CREATE TABLE IF NOT EXISTS `jpemeric_blog`.`ptlink` ( |
33
|
|
|
`post_id` integer NOT NULL, |
34
|
|
|
`tag_id` integer NOT NULL |
35
|
|
|
)" |
36
|
|
|
); |
37
|
|
|
$extendedPdo->exec(" |
38
|
|
|
CREATE TABLE IF NOT EXISTS `jpemeric_blog`.`tag` ( |
39
|
|
|
`id` integer PRIMARY KEY AUTOINCREMENT, |
40
|
|
|
`tag` varchar(25) NOT NULL |
41
|
|
|
)" |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
self::$connection = new ConnectionLocator(function () use ($extendedPdo) { |
45
|
|
|
return $extendedPdo; |
46
|
|
|
}); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testIsInstanceOfPostRepository() |
50
|
|
|
{ |
51
|
|
|
$repository = new MysqlPostRepository(self::$connection); |
52
|
|
|
|
53
|
|
|
$this->assertInstanceOf( |
54
|
|
|
'Jacobemerick\Web\Domain\Blog\Post\MysqlPostRepository', |
55
|
|
|
$repository |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testImplementsPostInterface() |
60
|
|
|
{ |
61
|
|
|
$repository = new MysqlPostRepository(self::$connection); |
62
|
|
|
|
63
|
|
|
$this->assertInstanceOf( |
64
|
|
|
'Jacobemerick\Web\Domain\Blog\Post\PostRepositoryInterface', |
65
|
|
|
$repository |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testConstructSetsConnections() |
70
|
|
|
{ |
71
|
|
|
$respository = new MysqlPostRepository(self::$connection); |
72
|
|
|
|
73
|
|
|
$this->assertAttributeSame( |
74
|
|
|
self::$connection, |
75
|
|
|
'connections', |
76
|
|
|
$respository |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testFindPostByPath() |
81
|
|
|
{ |
82
|
|
|
$testData = [ |
83
|
|
|
'id' => rand(1, 100), |
84
|
|
|
'title' => 'test title', |
85
|
|
|
'path' => 'test-path', |
86
|
|
|
'category' => 'test category', |
87
|
|
|
'date' => (new DateTime())->format('Y-m-d H:i:s'), |
88
|
|
|
'body' => 'test body', |
89
|
|
|
'display' => 1 |
90
|
|
|
]; |
91
|
|
|
|
92
|
|
|
$this->insertPostData($testData); |
93
|
|
|
|
94
|
|
|
$repository = new MysqlPostRepository(self::$connection); |
95
|
|
|
$data = $repository->findPostByPath($testData['category'], $testData['path']); |
96
|
|
|
|
97
|
|
|
$this->assertNotFalse($data); |
98
|
|
|
$this->assertInternalType('array', $data); |
99
|
|
|
$this->assertArrayHasKey('id', $data); |
100
|
|
|
$this->assertEquals($testData['id'], $data['id']); |
101
|
|
|
$this->assertArrayHasKey('title', $data); |
102
|
|
|
$this->assertEquals($testData['title'], $data['title']); |
103
|
|
|
$this->assertArrayHasKey('path', $data); |
104
|
|
|
$this->assertEquals($testData['path'], $data['path']); |
105
|
|
|
$this->assertArrayHasKey('date', $data); |
106
|
|
|
$this->assertEquals($testData['date'], $data['date']); |
107
|
|
|
$this->assertArrayHasKey('body', $data); |
108
|
|
|
$this->assertEquals($testData['body'], $data['body']); |
109
|
|
|
$this->assertArrayHasKey('category', $data); |
110
|
|
|
$this->assertEquals($testData['category'], $data['category']); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testFindPostByPathInactive() |
114
|
|
|
{ |
115
|
|
|
$testData = [ |
116
|
|
|
'id' => rand(1, 100), |
117
|
|
|
'path' => 'test-path', |
118
|
|
|
'category' => 'test category', |
119
|
|
|
'display' => 0 |
120
|
|
|
]; |
121
|
|
|
|
122
|
|
|
$this->insertPostData($testData); |
123
|
|
|
|
124
|
|
|
$repository = new MysqlPostRepository(self::$connection); |
125
|
|
|
$data = $repository->findPostByPath($testData['category'], $testData['path']); |
126
|
|
|
|
127
|
|
|
$this->assertFalse($data); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function testFindPostByPathFailure() |
131
|
|
|
{ |
132
|
|
|
$repository = new MysqlPostRepository(self::$connection); |
133
|
|
|
$data = $repository->findPostByPath('', ''); |
134
|
|
|
|
135
|
|
|
$this->assertFalse($data); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function testGetActivePosts() |
139
|
|
|
{ |
140
|
|
|
$testData = [ |
141
|
|
|
[ |
142
|
|
|
'id' => rand(1, 100), |
143
|
|
|
'title' => 'title one', |
144
|
|
|
'path' => 'path-one', |
145
|
|
|
'category' => 'test category', |
146
|
|
|
'date' => (new DateTime('-1 day'))->format('Y-m-d H:i:s'), |
147
|
|
|
'body' => 'body one', |
148
|
|
|
'display' => 1, |
149
|
|
|
], |
150
|
|
|
[ |
151
|
|
|
'id' => rand(101, 200), |
152
|
|
|
'title' => 'title two', |
153
|
|
|
'path' => 'path-two', |
154
|
|
|
'category' => 'test category', |
155
|
|
|
'date' => (new DateTime())->format('Y-m-d H:i:s'), |
156
|
|
|
'body' => 'body one', |
157
|
|
|
'display' => 1, |
158
|
|
|
], |
159
|
|
|
]; |
160
|
|
|
|
161
|
|
|
array_walk($testData, [$this, 'insertPostData']); |
162
|
|
|
|
163
|
|
|
$repository = new MysqlPostRepository(self::$connection); |
164
|
|
|
$data = $repository->getActivePosts(); |
165
|
|
|
|
166
|
|
|
$this->assertNotFalse($data); |
167
|
|
|
$this->assertInternalType('array', $data); |
168
|
|
|
$this->assertCount(count($testData), $data); |
169
|
|
|
|
170
|
|
|
usort($testData, function ($rowA, $rowB) { |
171
|
|
|
return ((new DateTime($rowA['date'])) < (new DateTime($rowB['date']))); |
172
|
|
|
}); |
173
|
|
|
|
174
|
|
View Code Duplication |
foreach ($testData as $key => $testRow) { |
|
|
|
|
175
|
|
|
$this->assertArrayHasKey('id', $data[$key]); |
176
|
|
|
$this->assertEquals($testRow['id'], $data[$key]['id']); |
177
|
|
|
$this->assertArrayHasKey('title', $data[$key]); |
178
|
|
|
$this->assertEquals($testRow['title'], $data[$key]['title']); |
179
|
|
|
$this->assertArrayHasKey('path', $data[$key]); |
180
|
|
|
$this->assertEquals($testRow['path'], $data[$key]['path']); |
181
|
|
|
$this->assertArrayHasKey('date', $data[$key]); |
182
|
|
|
$this->assertEquals($testRow['date'], $data[$key]['date']); |
183
|
|
|
$this->assertArrayHasKey('body', $data[$key]); |
184
|
|
|
$this->assertEquals($testRow['body'], $data[$key]['body']); |
185
|
|
|
$this->assertArrayHasKey('category', $data[$key]); |
186
|
|
|
$this->assertEquals($testRow['category'], $data[$key]['category']); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function testGetActivePostsInactive() |
191
|
|
|
{ |
192
|
|
|
$testData = [ |
193
|
|
|
[ |
194
|
|
|
'id' => rand(1, 100), |
195
|
|
|
'display' => 1, |
196
|
|
|
], |
197
|
|
|
[ |
198
|
|
|
'id' => rand(101, 200), |
199
|
|
|
'display' => 0, |
200
|
|
|
], |
201
|
|
|
[ |
202
|
|
|
'id' => rand(201, 300), |
203
|
|
|
'display' => 1, |
204
|
|
|
], |
205
|
|
|
]; |
206
|
|
|
|
207
|
|
|
array_walk($testData, [$this, 'insertPostData']); |
208
|
|
|
|
209
|
|
|
$repository = new MysqlPostRepository(self::$connection); |
210
|
|
|
$data = $repository->getActivePosts(); |
211
|
|
|
|
212
|
|
|
$this->assertNotFalse($data); |
213
|
|
|
$this->assertInternalType('array', $data); |
214
|
|
|
|
215
|
|
|
$testData = array_filter($testData, function ($row) { |
216
|
|
|
return ($row['display'] == 1); |
217
|
|
|
}); |
218
|
|
|
|
219
|
|
|
$this->assertCount(count($testData), $data); |
220
|
|
|
|
221
|
|
|
$testIds = array_column($testData, 'ids'); |
222
|
|
|
$dataIds = array_column($data, 'ids'); |
223
|
|
|
|
224
|
|
|
$this->assertEmpty(array_merge( |
225
|
|
|
array_diff($testIds, $dataIds), |
226
|
|
|
array_diff($dataIds, $testIds) |
227
|
|
|
)); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function testGetActivePostsFailure() |
231
|
|
|
{ |
232
|
|
|
$repository = new MysqlPostRepository(self::$connection); |
233
|
|
|
$data = $repository->getActivePosts(); |
234
|
|
|
|
235
|
|
|
$this->assertEmpty($data); |
236
|
|
|
$this->assertInternalType('array', $data); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function testGetActivePostsRange() |
240
|
|
|
{ |
241
|
|
|
$testData = [ |
242
|
|
|
[ |
243
|
|
|
'id' => rand(1, 100), |
244
|
|
|
'display' => 1, |
245
|
|
|
], |
246
|
|
|
[ |
247
|
|
|
'id' => rand(101, 200), |
248
|
|
|
'display' => 1, |
249
|
|
|
], |
250
|
|
|
[ |
251
|
|
|
'id' => rand(201, 300), |
252
|
|
|
'display' => 1, |
253
|
|
|
], |
254
|
|
|
]; |
255
|
|
|
|
256
|
|
|
array_walk($testData, [$this, 'insertPostData']); |
257
|
|
|
|
258
|
|
|
$repository = new MysqlPostRepository(self::$connection); |
259
|
|
|
$data = $repository->getActivePosts(2, 1); |
260
|
|
|
|
261
|
|
|
$this->assertNotFalse($data); |
262
|
|
|
$this->assertInternalType('array', $data); |
263
|
|
|
$this->assertCount(2, $data); |
264
|
|
|
|
265
|
|
|
$testData = array_slice($testData, 2, 1); |
266
|
|
|
|
267
|
|
|
$testIds = array_column($testData, 'ids'); |
268
|
|
|
$dataIds = array_column($data, 'ids'); |
269
|
|
|
|
270
|
|
|
$this->assertEmpty(array_merge( |
271
|
|
|
array_diff($testIds, $dataIds), |
272
|
|
|
array_diff($dataIds, $testIds) |
273
|
|
|
)); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
View Code Duplication |
public function testGetActivePostsRangeFailure() |
|
|
|
|
277
|
|
|
{ |
278
|
|
|
$testData = [ |
279
|
|
|
[ |
280
|
|
|
'id' => rand(1, 100), |
281
|
|
|
'display' => 1, |
282
|
|
|
], |
283
|
|
|
[ |
284
|
|
|
'id' => rand(101, 200), |
285
|
|
|
'display' => 1, |
286
|
|
|
], |
287
|
|
|
[ |
288
|
|
|
'id' => rand(201, 300), |
289
|
|
|
'display' => 1, |
290
|
|
|
], |
291
|
|
|
]; |
292
|
|
|
|
293
|
|
|
array_walk($testData, [$this, 'insertPostData']); |
294
|
|
|
|
295
|
|
|
$repository = new MysqlPostRepository(self::$connection); |
296
|
|
|
$data = $repository->getActivePosts(1, 3); |
297
|
|
|
|
298
|
|
|
$this->assertEmpty($data); |
299
|
|
|
$this->assertInternalType('array', $data); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
View Code Duplication |
public function testGetActivePostsCount() |
|
|
|
|
303
|
|
|
{ |
304
|
|
|
$testData = [ |
305
|
|
|
[ |
306
|
|
|
'id' => rand(1, 100), |
307
|
|
|
'display' => 1, |
308
|
|
|
], |
309
|
|
|
[ |
310
|
|
|
'id' => rand(101, 200), |
311
|
|
|
'display' => 1, |
312
|
|
|
], |
313
|
|
|
]; |
314
|
|
|
|
315
|
|
|
array_walk($testData, [$this, 'insertPostData']); |
316
|
|
|
|
317
|
|
|
$repository = new MysqlPostRepository(self::$connection); |
318
|
|
|
$data = $repository->getActivePostsCount(); |
319
|
|
|
|
320
|
|
|
$this->assertNotFalse($data); |
321
|
|
|
$this->assertStringMatchesFormat('%d', $data); |
322
|
|
|
$this->assertEquals(count($testData), $data); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
public function testGetActivePostsCountInactive() |
326
|
|
|
{ |
327
|
|
|
$testData = [ |
328
|
|
|
[ |
329
|
|
|
'id' => rand(1, 100), |
330
|
|
|
'display' => 1, |
331
|
|
|
], |
332
|
|
|
[ |
333
|
|
|
'id' => rand(101, 200), |
334
|
|
|
'display' => 1, |
335
|
|
|
], |
336
|
|
|
[ |
337
|
|
|
'id' => rand(201, 300), |
338
|
|
|
'display' => 0, |
339
|
|
|
], |
340
|
|
|
]; |
341
|
|
|
|
342
|
|
|
array_walk($testData, [$this, 'insertPostData']); |
343
|
|
|
|
344
|
|
|
$repository = new MysqlPostRepository(self::$connection); |
345
|
|
|
$data = $repository->getActivePostsCount(); |
346
|
|
|
|
347
|
|
|
$this->assertNotFalse($data); |
348
|
|
|
$this->assertStringMatchesFormat('%d', $data); |
349
|
|
|
|
350
|
|
|
$testData = array_filter($testData, function ($row) { |
351
|
|
|
return ($row['display'] == 1); |
352
|
|
|
}); |
353
|
|
|
|
354
|
|
|
$this->assertEquals(count($testData), $data); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
View Code Duplication |
public function testGetActivePostsCountFailure() |
|
|
|
|
358
|
|
|
{ |
359
|
|
|
$repository = new MysqlPostRepository(self::$connection); |
360
|
|
|
$data = $repository->getActivePostsCount(); |
361
|
|
|
|
362
|
|
|
$this->assertNotFalse($data); |
363
|
|
|
$this->assertStringMatchesFormat('%d', $data); |
364
|
|
|
$this->assertEquals('0', $data); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
public function testGetActivePostsByTag() |
368
|
|
|
{ |
369
|
|
|
$testPostData = [ |
370
|
|
|
[ |
371
|
|
|
'id' => rand(1, 100), |
372
|
|
|
'title' => 'title one', |
373
|
|
|
'path' => 'path-one', |
374
|
|
|
'category' => 'test category', |
375
|
|
|
'date' => (new DateTime('-1 day'))->format('Y-m-d H:i:s'), |
376
|
|
|
'body' => 'body one', |
377
|
|
|
'display' => 1, |
378
|
|
|
], |
379
|
|
|
[ |
380
|
|
|
'id' => rand(101, 200), |
381
|
|
|
'title' => 'title two', |
382
|
|
|
'path' => 'path-two', |
383
|
|
|
'category' => 'test category', |
384
|
|
|
'date' => (new DateTime())->format('Y-m-d H:i:s'), |
385
|
|
|
'body' => 'body one', |
386
|
|
|
'display' => 1, |
387
|
|
|
], |
388
|
|
|
]; |
389
|
|
|
|
390
|
|
|
$testTagData = [ |
391
|
|
|
'id' => rand(1, 100), |
392
|
|
|
]; |
393
|
|
|
|
394
|
|
|
$testPTLinkData = []; |
395
|
|
|
foreach ($testPostData as $testPostRow) { |
396
|
|
|
array_push($testPTLinkData, [ |
397
|
|
|
'post_id' => $testPostRow['id'], |
398
|
|
|
'tag_id' => $testTagData['id'], |
399
|
|
|
]); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
array_walk($testPostData, [$this, 'insertPostData']); |
403
|
|
|
array_walk($testPTLinkData, [$this, 'insertPTLinkData']); |
404
|
|
|
$this->insertTagData($testTagData); |
405
|
|
|
|
406
|
|
|
$repository = new MysqlPostRepository(self::$connection); |
407
|
|
|
$data = $repository->getActivePostsByTag($testTagData['id']); |
408
|
|
|
|
409
|
|
|
$this->assertNotFalse($data); |
410
|
|
|
$this->assertInternalType('array', $data); |
411
|
|
|
$this->assertCount(count($testPostData), $data); |
412
|
|
View Code Duplication |
foreach ($testPostData as $key => $testPostRow) { |
|
|
|
|
413
|
|
|
$this->assertArrayHasKey('id', $data[$key]); |
414
|
|
|
$this->assertEquals($testPostRow['id'], $data[$key]['id']); |
415
|
|
|
$this->assertArrayHasKey('title', $data[$key]); |
416
|
|
|
$this->assertEquals($testPostRow['title'], $data[$key]['title']); |
417
|
|
|
$this->assertArrayHasKey('path', $data[$key]); |
418
|
|
|
$this->assertEquals($testPostRow['path'], $data[$key]['path']); |
419
|
|
|
$this->assertArrayHasKey('date', $data[$key]); |
420
|
|
|
$this->assertEquals($testPostRow['date'], $data[$key]['date']); |
421
|
|
|
$this->assertArrayHasKey('body', $data[$key]); |
422
|
|
|
$this->assertEquals($testPostRow['body'], $data[$key]['body']); |
423
|
|
|
$this->assertArrayHasKey('category', $data[$key]); |
424
|
|
|
$this->assertEquals($testPostRow['category'], $data[$key]['category']); |
425
|
|
|
} |
426
|
|
|
} |
427
|
|
|
|
428
|
|
View Code Duplication |
public function testGetActivePostsByTagInactive() |
|
|
|
|
429
|
|
|
{ |
430
|
|
|
$testPostData = [ |
431
|
|
|
[ |
432
|
|
|
'id' => rand(1, 100), |
433
|
|
|
'display' => 1, |
434
|
|
|
], |
435
|
|
|
[ |
436
|
|
|
'id' => rand(101, 200), |
437
|
|
|
'display' => 0, |
438
|
|
|
], |
439
|
|
|
[ |
440
|
|
|
'id' => rand(201, 300), |
441
|
|
|
'display' => 1, |
442
|
|
|
], |
443
|
|
|
]; |
444
|
|
|
|
445
|
|
|
$testTagData = [ |
446
|
|
|
'id' => rand(1, 100), |
447
|
|
|
]; |
448
|
|
|
|
449
|
|
|
$testPTLinkData = []; |
450
|
|
|
foreach ($testPostData as $testPostRow) { |
451
|
|
|
array_push($testPTLinkData, [ |
452
|
|
|
'post_id' => $testPostRow['id'], |
453
|
|
|
'tag_id' => $testTagData['id'], |
454
|
|
|
]); |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
array_walk($testPostData, [$this, 'insertPostData']); |
458
|
|
|
array_walk($testPTLinkData, [$this, 'insertPTLinkData']); |
459
|
|
|
$this->insertTagData($testTagData); |
460
|
|
|
|
461
|
|
|
$repository = new MysqlPostRepository(self::$connection); |
462
|
|
|
$data = $repository->getActivePostsByTag($testTagData['id']); |
463
|
|
|
|
464
|
|
|
$this->assertNotFalse($data); |
465
|
|
|
$this->assertInternalType('array', $data); |
466
|
|
|
|
467
|
|
|
$testPostData = array_filter($testPostData, function ($row) { |
468
|
|
|
return ($row['display'] == 1); |
469
|
|
|
}); |
470
|
|
|
|
471
|
|
|
$this->assertCount(count($testPostData), $data); |
472
|
|
|
|
473
|
|
|
$testIds = array_column($testPostData, 'ids'); |
474
|
|
|
$dataIds = array_column($data, 'ids'); |
475
|
|
|
|
476
|
|
|
$this->assertEmpty(array_merge( |
477
|
|
|
array_diff($testIds, $dataIds), |
478
|
|
|
array_diff($dataIds, $testIds) |
479
|
|
|
)); |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
public function testGetActivePostsByTagFailure() |
483
|
|
|
{ |
484
|
|
|
$testTagData = [ |
485
|
|
|
'id' => rand(1, 100), |
486
|
|
|
]; |
487
|
|
|
|
488
|
|
|
$repository = new MysqlPostRepository(self::$connection); |
489
|
|
|
$data = $repository->getActivePostsByTag($testTagData['id']); |
490
|
|
|
|
491
|
|
|
$this->assertEmpty($data); |
492
|
|
|
$this->assertInternalType('array', $data); |
493
|
|
|
} |
494
|
|
|
|
495
|
|
View Code Duplication |
public function testGetActivePostsByTagRange() |
|
|
|
|
496
|
|
|
{ |
497
|
|
|
$testPostData = [ |
498
|
|
|
[ |
499
|
|
|
'id' => rand(1, 100), |
500
|
|
|
'display' => 1, |
501
|
|
|
], |
502
|
|
|
[ |
503
|
|
|
'id' => rand(101, 200), |
504
|
|
|
'display' => 1, |
505
|
|
|
], |
506
|
|
|
[ |
507
|
|
|
'id' => rand(201, 300), |
508
|
|
|
'display' => 1, |
509
|
|
|
], |
510
|
|
|
]; |
511
|
|
|
|
512
|
|
|
$testTagData = [ |
513
|
|
|
'id' => rand(1, 100), |
514
|
|
|
]; |
515
|
|
|
|
516
|
|
|
$testPTLinkData = []; |
517
|
|
|
foreach ($testPostData as $testPostRow) { |
518
|
|
|
array_push($testPTLinkData, [ |
519
|
|
|
'post_id' => $testPostRow['id'], |
520
|
|
|
'tag_id' => $testTagData['id'], |
521
|
|
|
]); |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
array_walk($testPostData, [$this, 'insertPostData']); |
525
|
|
|
array_walk($testPTLinkData, [$this, 'insertPTLinkData']); |
526
|
|
|
$this->insertTagData($testTagData); |
527
|
|
|
|
528
|
|
|
$repository = new MysqlPostRepository(self::$connection); |
529
|
|
|
$data = $repository->getActivePostsByTag($testTagData['id'], 2, 1); |
530
|
|
|
|
531
|
|
|
$this->assertNotFalse($data); |
532
|
|
|
$this->assertInternalType('array', $data); |
533
|
|
|
|
534
|
|
|
$testPostData = array_slice($testPostData, 1, 2); |
535
|
|
|
|
536
|
|
|
$this->assertCount(count($testPostData), $data); |
537
|
|
|
|
538
|
|
|
$testIds = array_column($testPostData, 'ids'); |
539
|
|
|
$dataIds = array_column($data, 'ids'); |
540
|
|
|
|
541
|
|
|
$this->assertEmpty(array_merge( |
542
|
|
|
array_diff($testIds, $dataIds), |
543
|
|
|
array_diff($dataIds, $testIds) |
544
|
|
|
)); |
545
|
|
|
} |
546
|
|
|
|
547
|
|
|
public function testGetActivePostsByTagRangeFailure() |
548
|
|
|
{ |
549
|
|
|
$testPostData = [ |
550
|
|
|
[ |
551
|
|
|
'id' => rand(1, 100), |
552
|
|
|
'display' => 1, |
553
|
|
|
], |
554
|
|
|
[ |
555
|
|
|
'id' => rand(101, 200), |
556
|
|
|
'display' => 1, |
557
|
|
|
], |
558
|
|
|
[ |
559
|
|
|
'id' => rand(201, 300), |
560
|
|
|
'display' => 1, |
561
|
|
|
], |
562
|
|
|
]; |
563
|
|
|
|
564
|
|
|
$testTagData = [ |
565
|
|
|
'id' => rand(1, 100), |
566
|
|
|
]; |
567
|
|
|
|
568
|
|
|
$testPTLinkData = []; |
569
|
|
|
foreach ($testPostData as $testPostRow) { |
570
|
|
|
array_push($testPTLinkData, [ |
571
|
|
|
'post_id' => $testPostRow['id'], |
572
|
|
|
'tag_id' => $testTagData['id'], |
573
|
|
|
]); |
574
|
|
|
} |
575
|
|
|
|
576
|
|
|
array_walk($testPostData, [$this, 'insertPostData']); |
577
|
|
|
array_walk($testPTLinkData, [$this, 'insertPTLinkData']); |
578
|
|
|
$this->insertTagData($testTagData); |
579
|
|
|
|
580
|
|
|
$repository = new MysqlPostRepository(self::$connection); |
581
|
|
|
$data = $repository->getActivePostsByTag($testTagData['id'], 1, 3); |
582
|
|
|
|
583
|
|
|
$this->assertEmpty($data); |
584
|
|
|
$this->assertInternalType('array', $data); |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
public function testGetActivePostsCountByTag() |
588
|
|
|
{ |
589
|
|
|
$testPostData = [ |
590
|
|
|
[ |
591
|
|
|
'id' => rand(1, 100), |
592
|
|
|
'display' => 1, |
593
|
|
|
], |
594
|
|
|
[ |
595
|
|
|
'id' => rand(101, 200), |
596
|
|
|
'display' => 1, |
597
|
|
|
], |
598
|
|
|
[ |
599
|
|
|
'id' => rand(201, 300), |
600
|
|
|
'display' => 1, |
601
|
|
|
], |
602
|
|
|
]; |
603
|
|
|
|
604
|
|
|
$testTagData = [ |
605
|
|
|
'id' => rand(1, 100), |
606
|
|
|
]; |
607
|
|
|
|
608
|
|
|
$testPTLinkData = []; |
609
|
|
|
foreach ($testPostData as $testPostRow) { |
610
|
|
|
array_push($testPTLinkData, [ |
611
|
|
|
'post_id' => $testPostRow['id'], |
612
|
|
|
'tag_id' => $testTagData['id'], |
613
|
|
|
]); |
614
|
|
|
} |
615
|
|
|
|
616
|
|
|
array_walk($testPostData, [$this, 'insertPostData']); |
617
|
|
|
array_walk($testPTLinkData, [$this, 'insertPTLinkData']); |
618
|
|
|
$this->insertTagData($testTagData); |
619
|
|
|
|
620
|
|
|
$repository = new MysqlPostRepository(self::$connection); |
621
|
|
|
$data = $repository->getActivePostsCountByTag($testTagData['id']); |
622
|
|
|
|
623
|
|
|
$this->assertNotFalse($data); |
624
|
|
|
$this->assertStringMatchesFormat('%d', $data); |
625
|
|
|
$this->assertEquals(count($testPostData), $data); |
626
|
|
|
} |
627
|
|
|
|
628
|
|
|
public function testGetActivePostsCountByTagInactive() |
629
|
|
|
{ |
630
|
|
|
$testPostData = [ |
631
|
|
|
[ |
632
|
|
|
'id' => rand(1, 100), |
633
|
|
|
'display' => 1, |
634
|
|
|
], |
635
|
|
|
[ |
636
|
|
|
'id' => rand(101, 200), |
637
|
|
|
'display' => 0, |
638
|
|
|
], |
639
|
|
|
[ |
640
|
|
|
'id' => rand(201, 300), |
641
|
|
|
'display' => 1, |
642
|
|
|
], |
643
|
|
|
]; |
644
|
|
|
|
645
|
|
|
$testTagData = [ |
646
|
|
|
'id' => rand(1, 100), |
647
|
|
|
]; |
648
|
|
|
|
649
|
|
|
$testPTLinkData = []; |
650
|
|
|
foreach ($testPostData as $testPostRow) { |
651
|
|
|
array_push($testPTLinkData, [ |
652
|
|
|
'post_id' => $testPostRow['id'], |
653
|
|
|
'tag_id' => $testTagData['id'], |
654
|
|
|
]); |
655
|
|
|
} |
656
|
|
|
|
657
|
|
|
array_walk($testPostData, [$this, 'insertPostData']); |
658
|
|
|
array_walk($testPTLinkData, [$this, 'insertPTLinkData']); |
659
|
|
|
$this->insertTagData($testTagData); |
660
|
|
|
|
661
|
|
|
$repository = new MysqlPostRepository(self::$connection); |
662
|
|
|
$data = $repository->getActivePostsCountByTag($testTagData['id']); |
663
|
|
|
|
664
|
|
|
$this->assertNotFalse($data); |
665
|
|
|
$this->assertStringMatchesFormat('%d', $data); |
666
|
|
|
|
667
|
|
|
$testPostData = array_filter($testPostData, function ($row) { |
668
|
|
|
return ($row['display'] == 1); |
669
|
|
|
}); |
670
|
|
|
|
671
|
|
|
$this->assertEquals(count($testPostData), $data); |
672
|
|
|
} |
673
|
|
|
|
674
|
|
|
public function testGetActivePostsCountByTagFailure() |
675
|
|
|
{ |
676
|
|
|
$testTagData = [ |
677
|
|
|
'id' => rand(1, 100), |
678
|
|
|
]; |
679
|
|
|
|
680
|
|
|
$this->insertTagData($testTagData); |
681
|
|
|
|
682
|
|
|
$repository = new MysqlPostRepository(self::$connection); |
683
|
|
|
$data = $repository->getActivePostsCountByTag($testTagData['id']); |
684
|
|
|
|
685
|
|
|
$this->assertNotFalse($data); |
686
|
|
|
$this->assertStringMatchesFormat('%d', $data); |
687
|
|
|
$this->assertEquals('0', $data); |
688
|
|
|
} |
689
|
|
|
|
690
|
|
|
public function testGetActivePostsByCategory() {} |
691
|
|
|
|
692
|
|
|
public function testGetActivePostsByCategoryInactive() {} |
693
|
|
|
|
694
|
|
|
public function testGetActivePostsByCategoryFailure() {} |
695
|
|
|
|
696
|
|
|
public function testGetActivePostsByCategoryRange() {} |
697
|
|
|
|
698
|
|
|
public function testGetActivePostsByCategoryRangeFailure() {} |
699
|
|
|
|
700
|
|
|
public function testGetActivePostsCountByCategory() {} |
701
|
|
|
|
702
|
|
|
public function testGetActivePostsCountByCategoryInactive() {} |
703
|
|
|
|
704
|
|
|
public function testGetActivePostsCountByCategoryFailure() {} |
705
|
|
|
|
706
|
|
|
public function testGetActivePostsByRelatedTags() {} |
707
|
|
|
|
708
|
|
|
public function testGetActivePostsByRelatedTagsLimit() {} |
709
|
|
|
|
710
|
|
|
public function testGetActivePostsByRelatedTagsInactive() {} |
711
|
|
|
|
712
|
|
|
public function testGetActivePostsByRelatedTagsExcludeSeries() {} |
713
|
|
|
|
714
|
|
|
public function testGetActivePostsByRelatedTagsFailure() {} |
715
|
|
|
|
716
|
|
View Code Duplication |
protected function insertPostData(array $data) |
|
|
|
|
717
|
|
|
{ |
718
|
|
|
$defaultData = [ |
719
|
|
|
'id' => null, |
720
|
|
|
'title' => '', |
721
|
|
|
'path' => '', |
722
|
|
|
'category' => '', |
723
|
|
|
'date' => '', |
724
|
|
|
'body' => '', |
725
|
|
|
'display' => 0, |
726
|
|
|
]; |
727
|
|
|
|
728
|
|
|
$data = array_merge($defaultData, $data); |
729
|
|
|
|
730
|
|
|
return self::$connection->getDefault()->perform(" |
731
|
|
|
INSERT INTO `jpemeric_blog`.`post` |
732
|
|
|
(id, title, path, category, date, body, display) |
733
|
|
|
VALUES |
734
|
|
|
(:id, :title, :path, :category, :date, :body, :display)", |
735
|
|
|
$data |
736
|
|
|
); |
737
|
|
|
} |
738
|
|
|
|
739
|
|
View Code Duplication |
protected function insertPTLinkData(array $data) |
|
|
|
|
740
|
|
|
{ |
741
|
|
|
$defaultData = [ |
742
|
|
|
'post' => null, |
743
|
|
|
'tag' => null, |
744
|
|
|
]; |
745
|
|
|
|
746
|
|
|
$data = array_merge($defaultData, $data); |
747
|
|
|
|
748
|
|
|
return self::$connection->getDefault()->perform(" |
749
|
|
|
INSERT INTO `jpemeric_blog`.`ptlink` |
750
|
|
|
(post_id, tag_id) |
751
|
|
|
VALUES |
752
|
|
|
(:post_id, :tag_id)", |
753
|
|
|
$data |
754
|
|
|
); |
755
|
|
|
} |
756
|
|
|
|
757
|
|
View Code Duplication |
protected function insertTagData(array $data) |
|
|
|
|
758
|
|
|
{ |
759
|
|
|
$defaultData = [ |
760
|
|
|
'id' => null, |
761
|
|
|
'tag' => '', |
762
|
|
|
]; |
763
|
|
|
|
764
|
|
|
$data = array_merge($defaultData, $data); |
765
|
|
|
|
766
|
|
|
return self::$connection->getDefault()->perform(" |
767
|
|
|
INSERT INTO `jpemeric_blog`.`tag` |
768
|
|
|
(id, tag) |
769
|
|
|
VALUES |
770
|
|
|
(:id, :tag)", |
771
|
|
|
$data |
772
|
|
|
); |
773
|
|
|
} |
774
|
|
|
|
775
|
|
|
protected function tearDown() |
776
|
|
|
{ |
777
|
|
|
self::$connection->getDefault()->perform("DELETE FROM `jpemeric_blog`.`post`"); |
778
|
|
|
self::$connection->getDefault()->perform("DELETE FROM `jpemeric_blog`.`ptlink`"); |
779
|
|
|
self::$connection->getDefault()->perform("DELETE FROM `jpemeric_blog`.`tag`"); |
780
|
|
|
} |
781
|
|
|
|
782
|
|
|
public static function tearDownAfterClass() |
783
|
|
|
{ |
784
|
|
|
self::$connection->getDefault()->disconnect(); |
|
|
|
|
785
|
|
|
unlink('jpemeric_blog.db'); |
786
|
|
|
} |
787
|
|
|
} |
788
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.