Completed
Push — master ( cda546...72ee7f )
by Jacob
03:25
created

MysqlPostRepositoryTest::testGetActivePostsByCategoryRangeFailure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 16

Duplication

Lines 28
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 28
loc 28
rs 8.8571
cc 1
eloc 16
nc 1
nop 0
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
    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`.`series_post` (
39
                `series` integer NOT NULL,
40
                `post` integer NOT NULL,
41
                `order` integer(1) NOT NULL
42
            )"
43
        );
44
        $extendedPdo->exec("
45
            CREATE TABLE IF NOT EXISTS `jpemeric_blog`.`tag` (
46
                `id` integer PRIMARY KEY AUTOINCREMENT,
47
                `tag` varchar(25) NOT NULL
48
            )"
49
        );
50
51
        self::$connection = new ConnectionLocator(function () use ($extendedPdo) {
52
            return $extendedPdo;
53
        });
54
    }
55
56
    public function testIsInstanceOfPostRepository()
57
    {
58
        $repository = new MysqlPostRepository(self::$connection);
59
60
        $this->assertInstanceOf(
61
            'Jacobemerick\Web\Domain\Blog\Post\MysqlPostRepository',
62
            $repository
63
        );
64
    }
65
66
    public function testImplementsPostInterface()
67
    {
68
        $repository = new MysqlPostRepository(self::$connection);
69
70
        $this->assertInstanceOf(
71
            'Jacobemerick\Web\Domain\Blog\Post\PostRepositoryInterface',
72
            $repository
73
        );
74
    }
75
76
    public function testConstructSetsConnections()
77
    {
78
        $respository = new MysqlPostRepository(self::$connection);
79
80
        $this->assertAttributeSame(
81
            self::$connection,
82
            'connections',
83
            $respository
84
        );
85
    }
86
87
    public function testFindPostByPath()
88
    {
89
        $testData = [
90
            'id'       => rand(1, 100),
91
            'title'    => 'test title',
92
            'path'     => 'test-path',
93
            'category' => 'test category',
94
            'date'     => (new DateTime())->format('Y-m-d H:i:s'),
95
            'body'     => 'test body',
96
            'display'  => 1
97
        ];
98
99
        $this->insertPostData($testData);
100
101
        $repository = new MysqlPostRepository(self::$connection);
102
        $data = $repository->findPostByPath($testData['path']);
103
104
        $this->assertNotFalse($data);
105
        $this->assertInternalType('array', $data);
106
        $this->assertArrayHasKey('id', $data);
107
        $this->assertEquals($testData['id'], $data['id']);
108
        $this->assertArrayHasKey('title', $data);
109
        $this->assertEquals($testData['title'], $data['title']);
110
        $this->assertArrayHasKey('path', $data);
111
        $this->assertEquals($testData['path'], $data['path']);
112
        $this->assertArrayHasKey('date', $data);
113
        $this->assertEquals($testData['date'], $data['date']);
114
        $this->assertArrayHasKey('body', $data);
115
        $this->assertEquals($testData['body'], $data['body']);
116
        $this->assertArrayHasKey('category', $data);
117
        $this->assertEquals($testData['category'], $data['category']);
118
    }
119
120
    public function testFindPostByPathInactive()
121
    {
122
        $testData = [
123
            'id'       => rand(1, 100),
124
            'path'     => 'test-path',
125
            'display'  => 0
126
        ];
127
128
        $this->insertPostData($testData);
129
130
        $repository = new MysqlPostRepository(self::$connection);
131
        $data = $repository->findPostByPath($testData['path']);
132
133
        $this->assertFalse($data);
134
    }
135
136
    public function testFindPostByPathFailure()
137
    {
138
        $repository = new MysqlPostRepository(self::$connection);
139
        $data = $repository->findPostByPath('');
140
141
        $this->assertFalse($data);
142
    }
143
144
    public function testGetActivePosts()
145
    {
146
        $testData = [
147
            [
148
                'id'       => rand(1, 100),
149
                'title'    => 'title one',
150
                'path'     => 'path-one',
151
                'category' => 'test category',
152
                'date'     => (new DateTime('-1 day'))->format('Y-m-d H:i:s'),
153
                'body'     => 'body one',
154
                'display'  => 1,
155
            ],
156
            [
157
                'id'       => rand(101, 200),
158
                'title'    => 'title two',
159
                'path'     => 'path-two',
160
                'category' => 'test category',
161
                'date'     => (new DateTime())->format('Y-m-d H:i:s'),
162
                'body'     => 'body one',
163
                'display'  => 1,
164
            ],
165
        ];
166
167
        array_walk($testData, [$this, 'insertPostData']);
168
169
        $repository = new MysqlPostRepository(self::$connection);
170
        $data = $repository->getActivePosts();
171
172
        $this->assertNotFalse($data);
173
        $this->assertInternalType('array', $data);
174
        $this->assertCount(count($testData), $data);
175
176
        usort($testData, function ($rowA, $rowB) {
177
            return ((new DateTime($rowA['date'])) < (new DateTime($rowB['date'])));
178
        });
179
180
        foreach ($testData as $key => $testRow) {
181
            $this->assertArrayHasKey('id', $data[$key]);
182
            $this->assertEquals($testRow['id'], $data[$key]['id']);
183
            $this->assertArrayHasKey('title', $data[$key]);
184
            $this->assertEquals($testRow['title'], $data[$key]['title']);
185
            $this->assertArrayHasKey('path', $data[$key]);
186
            $this->assertEquals($testRow['path'], $data[$key]['path']);
187
            $this->assertArrayHasKey('date', $data[$key]);
188
            $this->assertEquals($testRow['date'], $data[$key]['date']);
189
            $this->assertArrayHasKey('body', $data[$key]);
190
            $this->assertEquals($testRow['body'], $data[$key]['body']);
191
            $this->assertArrayHasKey('category', $data[$key]);
192
            $this->assertEquals($testRow['category'], $data[$key]['category']);
193
        }
194
    }
195
 
196
    public function testGetActivePostsInactive()
197
    {
198
        $testData = [
199
            [
200
                'id'      => rand(1, 100),
201
                'display' => 1,
202
            ],
203
            [
204
                'id'      => rand(101, 200),
205
                'display' => 0,
206
            ],
207
            [
208
                'id'      => rand(201, 300),
209
                'display' => 1,
210
            ],
211
        ];
212
213
        array_walk($testData, [$this, 'insertPostData']);
214
215
        $repository = new MysqlPostRepository(self::$connection);
216
        $data = $repository->getActivePosts();
217
218
        $this->assertNotFalse($data);
219
        $this->assertInternalType('array', $data);
220
221
        $testData = array_filter($testData, function ($row) {
222
            return ($row['display'] == 1);
223
        });
224
225
        $this->assertCount(count($testData), $data);
226
227
        $testIds = array_column($testData, 'ids');
228
        $dataIds = array_column($data, 'ids');
229
230
        $this->assertEmpty(array_merge(
231
            array_diff($testIds, $dataIds),
232
            array_diff($dataIds, $testIds)
233
        ));
234
    }
235
 
236
    public function testGetActivePostsFailure()
237
    {
238
        $repository = new MysqlPostRepository(self::$connection);
239
        $data = $repository->getActivePosts();
240
241
        $this->assertEmpty($data);
242
        $this->assertInternalType('array', $data);
243
    }
244
245
    public function testGetActivePostsRange()
246
    {
247
        $testData = [
248
            [
249
                'id'      => rand(1, 100),
250
                'display' => 1,
251
            ],
252
            [
253
                'id'      => rand(101, 200),
254
                'display' => 1,
255
            ],
256
            [
257
                'id'      => rand(201, 300),
258
                'display' => 1,
259
            ],
260
        ];
261
262
        array_walk($testData, [$this, 'insertPostData']);
263
264
        $repository = new MysqlPostRepository(self::$connection);
265
        $data = $repository->getActivePosts(2, 1);
266
267
        $this->assertNotFalse($data);
268
        $this->assertInternalType('array', $data);
269
        $this->assertCount(2, $data);
270
271
        $testData = array_slice($testData, 2, 1);
272
273
        $testIds = array_column($testData, 'ids');
274
        $dataIds = array_column($data, 'ids');
275
276
        $this->assertEmpty(array_merge(
277
            array_diff($testIds, $dataIds),
278
            array_diff($dataIds, $testIds)
279
        ));
280
    }
281
282
    public function testGetActivePostsRangeFailure()
283
    {
284
        $testData = [
285
            [
286
                'id'      => rand(1, 100),
287
                'display' => 1,
288
            ],
289
            [
290
                'id'      => rand(101, 200),
291
                'display' => 1,
292
            ],
293
            [
294
                'id'      => rand(201, 300),
295
                'display' => 1,
296
            ],
297
        ];
298
299
        array_walk($testData, [$this, 'insertPostData']);
300
301
        $repository = new MysqlPostRepository(self::$connection);
302
        $data = $repository->getActivePosts(1, 3);
303
304
        $this->assertEmpty($data);
305
        $this->assertInternalType('array', $data);
306
    }
307
308
    public function testGetActivePostsCount()
309
    {
310
        $testData = [
311
            [
312
                'id'      => rand(1, 100),
313
                'display' => 1,
314
            ],
315
            [
316
                'id'      => rand(101, 200),
317
                'display' => 1,
318
            ],
319
        ];
320
321
        array_walk($testData, [$this, 'insertPostData']);
322
323
        $repository = new MysqlPostRepository(self::$connection);
324
        $data = $repository->getActivePostsCount();
325
326
        $this->assertNotFalse($data);
327
        $this->assertStringMatchesFormat('%d', $data);
328
        $this->assertEquals(count($testData), $data);
329
    }
330
331
    public function testGetActivePostsCountInactive()
332
    {
333
        $testData = [
334
            [
335
                'id'      => rand(1, 100),
336
                'display' => 1,
337
            ],
338
            [
339
                'id'      => rand(101, 200),
340
                'display' => 1,
341
            ],
342
            [
343
                'id'      => rand(201, 300),
344
                'display' => 0,
345
            ],
346
        ];
347
348
        array_walk($testData, [$this, 'insertPostData']);
349
350
        $repository = new MysqlPostRepository(self::$connection);
351
        $data = $repository->getActivePostsCount();
352
353
        $this->assertNotFalse($data);
354
        $this->assertStringMatchesFormat('%d', $data);
355
356
        $testData = array_filter($testData, function ($row) {
357
            return ($row['display'] == 1);
358
        });
359
360
        $this->assertEquals(count($testData), $data);
361
    }
362
363
    public function testGetActivePostsCountFailure()
364
    {
365
        $repository = new MysqlPostRepository(self::$connection);
366
        $data = $repository->getActivePostsCount();
367
368
        $this->assertNotFalse($data);
369
        $this->assertStringMatchesFormat('%d', $data);
370
        $this->assertEquals('0', $data);
371
    }
372
373
    public function testGetActivePostsByTag()
374
    {
375
        $testPostData = [
376
            [
377
                'id'       => rand(1, 100),
378
                'title'    => 'title one',
379
                'path'     => 'path-one',
380
                'category' => 'test category',
381
                'date'     => (new DateTime('-1 day'))->format('Y-m-d H:i:s'),
382
                'body'     => 'body one',
383
                'display'  => 1,
384
            ],
385
            [
386
                'id'       => rand(101, 200),
387
                'title'    => 'title two',
388
                'path'     => 'path-two',
389
                'category' => 'test category',
390
                'date'     => (new DateTime())->format('Y-m-d H:i:s'),
391
                'body'     => 'body one',
392
                'display'  => 1,
393
            ],
394
        ];
395
396
        $testTagData = [
397
            'id' => rand(1, 100),
398
        ];
399
400
        $testPTLinkData = [];
401
        foreach ($testPostData as $testPostRow) {
402
            array_push($testPTLinkData, [
403
                'post_id' => $testPostRow['id'],
404
                'tag_id' => $testTagData['id'],
405
            ]);
406
        }
407
408
        array_walk($testPostData, [$this, 'insertPostData']);
409
        array_walk($testPTLinkData, [$this, 'insertPTLinkData']);
410
        $this->insertTagData($testTagData);
411
412
        $repository = new MysqlPostRepository(self::$connection);
413
        $data = $repository->getActivePostsByTag($testTagData['id']);
414
415
        $this->assertNotFalse($data);
416
        $this->assertInternalType('array', $data);
417
        $this->assertCount(count($testPostData), $data);
418
419
        usort($testPostData, function ($rowA, $rowB) {
420
            return ((new DateTime($rowA['date'])) < (new DateTime($rowB['date'])));
421
        });
422
423
        foreach ($testPostData as $key => $testPostRow) {
424
            $this->assertArrayHasKey('id', $data[$key]);
425
            $this->assertEquals($testPostRow['id'], $data[$key]['id']);
426
            $this->assertArrayHasKey('title', $data[$key]);
427
            $this->assertEquals($testPostRow['title'], $data[$key]['title']);
428
            $this->assertArrayHasKey('path', $data[$key]);
429
            $this->assertEquals($testPostRow['path'], $data[$key]['path']);
430
            $this->assertArrayHasKey('date', $data[$key]);
431
            $this->assertEquals($testPostRow['date'], $data[$key]['date']);
432
            $this->assertArrayHasKey('body', $data[$key]);
433
            $this->assertEquals($testPostRow['body'], $data[$key]['body']);
434
            $this->assertArrayHasKey('category', $data[$key]);
435
            $this->assertEquals($testPostRow['category'], $data[$key]['category']);
436
        }
437
    }
438
439
    public function testGetActivePostsByTagInactive()
440
    {
441
        $testPostData = [
442
            [
443
                'id'      => rand(1, 100),
444
                'display' => 1,
445
            ],
446
            [
447
                'id'      => rand(101, 200),
448
                'display' => 0,
449
            ],
450
            [
451
                'id'      => rand(201, 300),
452
                'display' => 1,
453
            ],
454
        ];
455
456
        $testTagData = [
457
            'id' => rand(1, 100),
458
        ];
459
460
        $testPTLinkData = [];
461
        foreach ($testPostData as $testPostRow) {
462
            array_push($testPTLinkData, [
463
                'post_id' => $testPostRow['id'],
464
                'tag_id' => $testTagData['id'],
465
            ]);
466
        }
467
468
        array_walk($testPostData, [$this, 'insertPostData']);
469
        array_walk($testPTLinkData, [$this, 'insertPTLinkData']);
470
        $this->insertTagData($testTagData);
471
472
        $repository = new MysqlPostRepository(self::$connection);
473
        $data = $repository->getActivePostsByTag($testTagData['id']);
474
475
        $this->assertNotFalse($data);
476
        $this->assertInternalType('array', $data);
477
478
        $testPostData = array_filter($testPostData, function ($row) {
479
            return ($row['display'] == 1);
480
        });
481
482
        $this->assertCount(count($testPostData), $data);
483
484
        $testIds = array_column($testPostData, 'ids');
485
        $dataIds = array_column($data, 'ids');
486
487
        $this->assertEmpty(array_merge(
488
            array_diff($testIds, $dataIds),
489
            array_diff($dataIds, $testIds)
490
        ));
491
    }
492
 
493
    public function testGetActivePostsByTagFailure()
494
    {
495
        $testTagData = [
496
            'id' => rand(1, 100),
497
        ];
498
499
        $repository = new MysqlPostRepository(self::$connection);
500
        $data = $repository->getActivePostsByTag($testTagData['id']);
501
502
        $this->assertEmpty($data);
503
        $this->assertInternalType('array', $data);
504
    }
505
506
    public function testGetActivePostsByTagRange()
507
    {
508
        $testPostData = [
509
            [
510
                'id'      => rand(1, 100),
511
                'display' => 1,
512
            ],
513
            [
514
                'id'      => rand(101, 200),
515
                'display' => 1,
516
            ],
517
            [
518
                'id'      => rand(201, 300),
519
                'display' => 1,
520
            ],
521
        ];
522
523
        $testTagData = [
524
            'id' => rand(1, 100),
525
        ];
526
527
        $testPTLinkData = [];
528
        foreach ($testPostData as $testPostRow) {
529
            array_push($testPTLinkData, [
530
                'post_id' => $testPostRow['id'],
531
                'tag_id' => $testTagData['id'],
532
            ]);
533
        }
534
535
        array_walk($testPostData, [$this, 'insertPostData']);
536
        array_walk($testPTLinkData, [$this, 'insertPTLinkData']);
537
        $this->insertTagData($testTagData);
538
539
        $repository = new MysqlPostRepository(self::$connection);
540
        $data = $repository->getActivePostsByTag($testTagData['id'], 2, 1);
541
542
        $this->assertNotFalse($data);
543
        $this->assertInternalType('array', $data);
544
545
        $testPostData = array_slice($testPostData, 1, 2);
546
547
        $this->assertCount(count($testPostData), $data);
548
549
        $testIds = array_column($testPostData, 'ids');
550
        $dataIds = array_column($data, 'ids');
551
552
        $this->assertEmpty(array_merge(
553
            array_diff($testIds, $dataIds),
554
            array_diff($dataIds, $testIds)
555
        ));
556
    }
557
558
    public function testGetActivePostsByTagRangeFailure()
559
    {
560
        $testPostData = [
561
            [
562
                'id'      => rand(1, 100),
563
                'display' => 1,
564
            ],
565
            [
566
                'id'      => rand(101, 200),
567
                'display' => 1,
568
            ],
569
            [
570
                'id'      => rand(201, 300),
571
                'display' => 1,
572
            ],
573
        ];
574
575
        $testTagData = [
576
            'id' => rand(1, 100),
577
        ];
578
579
        $testPTLinkData = [];
580
        foreach ($testPostData as $testPostRow) {
581
            array_push($testPTLinkData, [
582
                'post_id' => $testPostRow['id'],
583
                'tag_id' => $testTagData['id'],
584
            ]);
585
        }
586
587
        array_walk($testPostData, [$this, 'insertPostData']);
588
        array_walk($testPTLinkData, [$this, 'insertPTLinkData']);
589
        $this->insertTagData($testTagData);
590
591
        $repository = new MysqlPostRepository(self::$connection);
592
        $data = $repository->getActivePostsByTag($testTagData['id'], 1, 3);
593
594
        $this->assertEmpty($data);
595
        $this->assertInternalType('array', $data);
596
    }
597
598
    public function testGetActivePostsCountByTag()
599
    {
600
        $testPostData = [
601
            [
602
                'id'      => rand(1, 100),
603
                'display' => 1,
604
            ],
605
            [
606
                'id'      => rand(101, 200),
607
                'display' => 1,
608
            ],
609
            [
610
                'id'      => rand(201, 300),
611
                'display' => 1,
612
            ],
613
        ];
614
615
        $testTagData = [
616
            'id' => rand(1, 100),
617
        ];
618
619
        $testPTLinkData = [];
620
        foreach ($testPostData as $testPostRow) {
621
            array_push($testPTLinkData, [
622
                'post_id' => $testPostRow['id'],
623
                'tag_id' => $testTagData['id'],
624
            ]);
625
        }
626
627
        array_walk($testPostData, [$this, 'insertPostData']);
628
        array_walk($testPTLinkData, [$this, 'insertPTLinkData']);
629
        $this->insertTagData($testTagData);
630
631
        $repository = new MysqlPostRepository(self::$connection);
632
        $data = $repository->getActivePostsCountByTag($testTagData['id']);
633
634
        $this->assertNotFalse($data);
635
        $this->assertStringMatchesFormat('%d', $data);
636
        $this->assertEquals(count($testPostData), $data);
637
    }
638
639
    public function testGetActivePostsCountByTagInactive()
640
    {
641
        $testPostData = [
642
            [
643
                'id'      => rand(1, 100),
644
                'display' => 1,
645
            ],
646
            [
647
                'id'      => rand(101, 200),
648
                'display' => 0,
649
            ],
650
            [
651
                'id'      => rand(201, 300),
652
                'display' => 1,
653
            ],
654
        ];
655
656
        $testTagData = [
657
            'id' => rand(1, 100),
658
        ];
659
660
        $testPTLinkData = [];
661
        foreach ($testPostData as $testPostRow) {
662
            array_push($testPTLinkData, [
663
                'post_id' => $testPostRow['id'],
664
                'tag_id' => $testTagData['id'],
665
            ]);
666
        }
667
668
        array_walk($testPostData, [$this, 'insertPostData']);
669
        array_walk($testPTLinkData, [$this, 'insertPTLinkData']);
670
        $this->insertTagData($testTagData);
671
672
        $repository = new MysqlPostRepository(self::$connection);
673
        $data = $repository->getActivePostsCountByTag($testTagData['id']);
674
675
        $this->assertNotFalse($data);
676
        $this->assertStringMatchesFormat('%d', $data);
677
678
        $testPostData = array_filter($testPostData, function ($row) {
679
            return ($row['display'] == 1);
680
        });
681
682
        $this->assertEquals(count($testPostData), $data);
683
    }
684
 
685
    public function testGetActivePostsCountByTagFailure()
686
    {
687
        $testTagData = [
688
            'id' => rand(1, 100),
689
        ];
690
691
        $this->insertTagData($testTagData);
692
693
        $repository = new MysqlPostRepository(self::$connection);
694
        $data = $repository->getActivePostsCountByTag($testTagData['id']);
695
696
        $this->assertNotFalse($data);
697
        $this->assertStringMatchesFormat('%d', $data);
698
        $this->assertEquals('0', $data);
699
    }
700
 
701
    public function testGetActivePostsByCategory()
702
    {
703
        $testData = [
704
            [
705
                'id'       => rand(1, 100),
706
                'title'    => 'title one',
707
                'path'     => 'path-one',
708
                'category' => 'test category',
709
                'date'     => (new DateTime('-1 day'))->format('Y-m-d H:i:s'),
710
                'body'     => 'body one',
711
                'display'  => 1,
712
            ],
713
            [
714
                'id'       => rand(101, 200),
715
                'title'    => 'title two',
716
                'path'     => 'path-two',
717
                'category' => 'test category',
718
                'date'     => (new DateTime())->format('Y-m-d H:i:s'),
719
                'body'     => 'body one',
720
                'display'  => 1,
721
            ],
722
        ];
723
724
        array_walk($testData, [$this, 'insertPostData']);
725
726
        $repository = new MysqlPostRepository(self::$connection);
727
        $data = $repository->getActivePostsByCategory(reset($testData)['category']);
728
729
        $this->assertNotFalse($data);
730
        $this->assertInternalType('array', $data);
731
        $this->assertCount(count($testData), $data);
732
733
        usort($testData, function ($rowA, $rowB) {
734
            return ((new DateTime($rowA['date'])) < (new DateTime($rowB['date'])));
735
        });
736
737
        foreach ($testData as $key => $testDataRow) {
738
            $this->assertArrayHasKey('id', $data[$key]);
739
            $this->assertEquals($testDataRow['id'], $data[$key]['id']);
740
            $this->assertArrayHasKey('title', $data[$key]);
741
            $this->assertEquals($testDataRow['title'], $data[$key]['title']);
742
            $this->assertArrayHasKey('path', $data[$key]);
743
            $this->assertEquals($testDataRow['path'], $data[$key]['path']);
744
            $this->assertArrayHasKey('date', $data[$key]);
745
            $this->assertEquals($testDataRow['date'], $data[$key]['date']);
746
            $this->assertArrayHasKey('body', $data[$key]);
747
            $this->assertEquals($testDataRow['body'], $data[$key]['body']);
748
            $this->assertArrayHasKey('category', $data[$key]);
749
            $this->assertEquals($testDataRow['category'], $data[$key]['category']);
750
        }
751
    }
752
753
    public function testGetActivePostsByCategoryInactive()
754
    {
755
        $testData = [
756
            [
757
                'id'       => rand(1, 100),
758
                'category' => 'test category',
759
                'display'  => 1,
760
            ],
761
            [
762
                'id'       => rand(101, 200),
763
                'category' => 'test category',
764
                'display'  => 1,
765
            ],
766
            [
767
                'id'       => rand(201, 300),
768
                'category' => 'test category',
769
                'display'  => 0,
770
            ],
771
        ];
772
773
        array_walk($testData, [$this, 'insertPostData']);
774
775
        $repository = new MysqlPostRepository(self::$connection);
776
        $data = $repository->getActivePostsByCategory(reset($testData)['category']);
777
778
        $this->assertNotFalse($data);
779
        $this->assertInternalType('array', $data);
780
781
        $testData = array_filter($testData, function ($row) {
782
            return ($row['display'] == 1);
783
        });
784
785
        $this->assertCount(count($testData), $data);
786
787
        $testIds = array_column($testData, 'ids');
788
        $dataIds = array_column($data, 'ids');
789
790
        $this->assertEmpty(array_merge(
791
            array_diff($testIds, $dataIds),
792
            array_diff($dataIds, $testIds)
793
        ));
794
    }
795
 
796
    public function testGetActivePostsByCategoryFailure()
797
    {
798
        $repository = new MysqlPostRepository(self::$connection);
799
        $data = $repository->getActivePostsByCategory('');
800
801
        $this->assertEmpty($data);
802
        $this->assertInternalType('array', $data);
803
    }
804
805
    public function testGetActivePostsByCategoryRange()
806
    {
807
        $testData = [
808
            [
809
                'id'       => rand(1, 100),
810
                'category' => 'test category',
811
                'display'  => 1,
812
            ],
813
            [
814
                'id'       => rand(101, 200),
815
                'category' => 'test category',
816
                'display'  => 1,
817
            ],
818
            [
819
                'id'       => rand(201, 300),
820
                'category' => 'test category',
821
                'display'  => 1,
822
            ],
823
        ];
824
825
        array_walk($testData, [$this, 'insertPostData']);
826
827
        $repository = new MysqlPostRepository(self::$connection);
828
        $data = $repository->getActivePostsByCategory(reset($testData)['category'], 2, 1);
829
830
        $this->assertNotFalse($data);
831
        $this->assertInternalType('array', $data);
832
833
        $testData = array_slice($testData, 1, 2);
834
835
        $this->assertCount(count($testData), $data);
836
837
        $testIds = array_column($testData, 'ids');
838
        $dataIds = array_column($data, 'ids');
839
840
        $this->assertEmpty(array_merge(
841
            array_diff($testIds, $dataIds),
842
            array_diff($dataIds, $testIds)
843
        ));
844
    }
845
846
    public function testGetActivePostsByCategoryRangeFailure()
847
    {
848
        $testData = [
849
            [
850
                'id'       => rand(1, 100),
851
                'category' => 'test category',
852
                'display'  => 1,
853
            ],
854
            [
855
                'id'       => rand(101, 200),
856
                'category' => 'test category',
857
                'display'  => 1,
858
            ],
859
            [
860
                'id'       => rand(201, 300),
861
                'category' => 'test category',
862
                'display'  => 1,
863
            ],
864
        ];
865
866
        array_walk($testData, [$this, 'insertPostData']);
867
868
        $repository = new MysqlPostRepository(self::$connection);
869
        $data = $repository->getActivePostsByCategory(reset($testData)['category'], 1, 3);
870
871
        $this->assertEmpty($data);
872
        $this->assertInternalType('array', $data);
873
    }
874
875
    public function testGetActivePostsCountByCategory()
876
    {
877
        $testData = [
878
            [
879
                'id'       => rand(1, 100),
880
                'category' => 'test category',
881
                'display'  => 1,
882
            ],
883
            [
884
                'id'       => rand(101, 200),
885
                'category' => 'test category',
886
                'display'  => 1,
887
            ],
888
            [
889
                'id'       => rand(201, 300),
890
                'category' => 'test category',
891
                'display'  => 1,
892
            ],
893
        ];
894
895
        array_walk($testData, [$this, 'insertPostData']);
896
897
        $repository = new MysqlPostRepository(self::$connection);
898
        $data = $repository->getActivePostsCountByCategory(reset($testData)['category']);
899
900
        $this->assertNotFalse($data);
901
        $this->assertStringMatchesFormat('%d', $data);
902
        $this->assertEquals(count($testData), $data);
903
    }
904
905
    public function testGetActivePostsCountByCategoryInactive()
906
    {
907
        $testData = [
908
            [
909
                'id'       => rand(1, 100),
910
                'category' => 'test category',
911
                'display'  => 0,
912
            ],
913
            [
914
                'id'       => rand(101, 200),
915
                'category' => 'test category',
916
                'display'  => 1,
917
            ],
918
            [
919
                'id'       => rand(201, 300),
920
                'category' => 'test category',
921
                'display'  => 1,
922
            ],
923
        ];
924
925
        array_walk($testData, [$this, 'insertPostData']);
926
927
        $repository = new MysqlPostRepository(self::$connection);
928
        $data = $repository->getActivePostsCountByCategory(reset($testData)['category']);
929
930
        $this->assertNotFalse($data);
931
        $this->assertStringMatchesFormat('%d', $data);
932
933
        $testData = array_filter($testData, function ($row) {
934
            return ($row['display'] == 1);
935
        });
936
937
        $this->assertEquals(count($testData), $data);
938
    }
939
940
    public function testGetActivePostsCountByCategoryFailure()
941
    {
942
        $repository = new MysqlPostRepository(self::$connection);
943
        $data = $repository->getActivePostsCountByCategory('');
944
945
        $this->assertNotFalse($data);
946
        $this->assertStringMatchesFormat('%d', $data);
947
        $this->assertEquals('0', $data);
948
    }
949
950
    public function testGetActivePostsByRelatedTags()
951
    {
952
        $testPostData = [
953
            [
954
                'id'       => rand(1, 100),
955
                'title'    => 'title one',
956
                'path'     => 'path-one',
957
                'category' => 'test category',
958
                'date'     => (new DateTime('-1 day'))->format('Y-m-d H:i:s'),
959
                'body'     => 'body one',
960
                'display'  => 1,
961
            ],
962
            [
963
                'id'       => rand(101, 200),
964
                'title'    => 'title two',
965
                'path'     => 'path-two',
966
                'category' => 'test category',
967
                'date'     => (new DateTime('-2 days'))->format('Y-m-d H:i:s'),
968
                'body'     => 'body two',
969
                'display'  => 1,
970
            ],
971
            [
972
                'id'       => rand(201, 300),
973
                'title'    => 'title three',
974
                'path'     => 'path-three',
975
                'category' => 'test category',
976
                'date'     => (new DateTime('-3 days'))->format('Y-m-d H:i:s'),
977
                'body'     => 'body three',
978
                'display'  => 1,
979
            ],
980
        ];
981
982
        $testTagData = [
983
            'id' => rand(1, 100),
984
        ];
985
986
        $testPTLinkData = [];
987
        foreach ($testPostData as $testPostRow) {
988
            array_push($testPTLinkData, [
989
                'post_id' => $testPostRow['id'],
990
                'tag_id' => $testTagData['id'],
991
            ]);
992
        }
993
994
        array_walk($testPostData, [$this, 'insertPostData']);
995
        array_walk($testPTLinkData, [$this, 'insertPTLinkData']);
996
        $this->insertTagData($testTagData);
997
998
        $repository = new MysqlPostRepository(self::$connection);
999
        $data = $repository->getActivePostsByRelatedTags(reset($testPostData)['id']);
1000
1001
        $this->assertNotFalse($data);
1002
        $this->assertInternalType('array', $data);
1003
1004
        array_shift($testPostData);
1005
1006
        $this->assertCount(count($testPostData), $data);
1007
        foreach ($testPostData as $key => $testPostRow) {
1008
            $this->assertArrayHasKey('id', $data[$key]);
1009
            $this->assertEquals($testPostRow['id'], $data[$key]['id']);
1010
            $this->assertArrayHasKey('title', $data[$key]);
1011
            $this->assertEquals($testPostRow['title'], $data[$key]['title']);
1012
            $this->assertArrayHasKey('path', $data[$key]);
1013
            $this->assertEquals($testPostRow['path'], $data[$key]['path']);
1014
            $this->assertArrayHasKey('date', $data[$key]);
1015
            $this->assertEquals($testPostRow['date'], $data[$key]['date']);
1016
            $this->assertArrayHasKey('body', $data[$key]);
1017
            $this->assertEquals($testPostRow['body'], $data[$key]['body']);
1018
            $this->assertArrayHasKey('category', $data[$key]);
1019
            $this->assertEquals($testPostRow['category'], $data[$key]['category']);
1020
            $this->assertArrayHasKey('count', $data[$key]);
1021
            $this->assertEquals(count($testTagData), $data[$key]['count']);
1022
        }
1023
    }
1024
1025
    public function testGetActivePostsByRelatedTagsLimit()
1026
    {
1027
        $testPostData = [
1028
            [
1029
                'id'      => rand(1, 100),
1030
                'display' => 1,
1031
            ],
1032
            [
1033
                'id'      => rand(101, 200),
1034
                'display' => 1,
1035
            ],
1036
            [
1037
                'id'      => rand(201, 300),
1038
                'display' => 1,
1039
            ],
1040
        ];
1041
1042
        $testTagData = [
1043
            'id' => rand(1, 100),
1044
        ];
1045
1046
        $testPTLinkData = [];
1047
        foreach ($testPostData as $testPostRow) {
1048
            array_push($testPTLinkData, [
1049
                'post_id' => $testPostRow['id'],
1050
                'tag_id' => $testTagData['id'],
1051
            ]);
1052
        }
1053
1054
        array_walk($testPostData, [$this, 'insertPostData']);
1055
        array_walk($testPTLinkData, [$this, 'insertPTLinkData']);
1056
        $this->insertTagData($testTagData);
1057
1058
        $repository = new MysqlPostRepository(self::$connection);
1059
        $data = $repository->getActivePostsByRelatedTags(reset($testPostData)['id'], 1);
1060
1061
        $this->assertNotFalse($data);
1062
        $this->assertInternalType('array', $data);
1063
1064
        $testPostData = array_slice($testPostData, 1, 1);
1065
1066
        $this->assertCount(count($testPostData), $data);
1067
1068
        $testIds = array_column($testPostData, 'ids');
1069
        $dataIds = array_column($data, 'ids');
1070
1071
        $this->assertEmpty(array_merge(
1072
            array_diff($testIds, $dataIds),
1073
            array_diff($dataIds, $testIds)
1074
        ));
1075
    }
1076
 
1077
    public function testGetActivePostsByRelatedTagsInactive()
1078
    {
1079
        $testPostData = [
1080
            [
1081
                'id'      => rand(1, 100),
1082
                'display' => 1,
1083
            ],
1084
            [
1085
                'id'      => rand(101, 200),
1086
                'display' => 1,
1087
            ],
1088
            [
1089
                'id'      => rand(201, 300),
1090
                'display' => 0,
1091
            ],
1092
            [
1093
                'id'      => rand(301, 400),
1094
                'display' => 1,
1095
            ],
1096
        ];
1097
1098
        $testTagData = [
1099
            'id' => rand(1, 100),
1100
        ];
1101
1102
        $testPTLinkData = [];
1103
        foreach ($testPostData as $testPostRow) {
1104
            array_push($testPTLinkData, [
1105
                'post_id' => $testPostRow['id'],
1106
                'tag_id' => $testTagData['id'],
1107
            ]);
1108
        }
1109
1110
        array_walk($testPostData, [$this, 'insertPostData']);
1111
        array_walk($testPTLinkData, [$this, 'insertPTLinkData']);
1112
        $this->insertTagData($testTagData);
1113
1114
        $repository = new MysqlPostRepository(self::$connection);
1115
        $data = $repository->getActivePostsByRelatedTags(reset($testPostData)['id']);
1116
1117
        $this->assertNotFalse($data);
1118
        $this->assertInternalType('array', $data);
1119
1120
        array_shift($testPostData);
1121
        $testPostData = array_filter($testPostData, function ($row) {
1122
            return ($row['display'] == 1);
1123
        });
1124
1125
        $this->assertCount(count($testPostData), $data);
1126
1127
        $testIds = array_column($testPostData, 'ids');
1128
        $dataIds = array_column($data, 'ids');
1129
1130
        $this->assertEmpty(array_merge(
1131
            array_diff($testIds, $dataIds),
1132
            array_diff($dataIds, $testIds)
1133
        ));
1134
    }
1135
 
1136
    public function testGetActivePostsByRelatedTagsExcludeSeries()
1137
    {
1138
        $testPostData = [
1139
            [
1140
                'id'      => rand(1, 100),
1141
                'display' => 1,
1142
            ],
1143
            [
1144
                'id'      => rand(101, 200),
1145
                'display' => 1,
1146
            ],
1147
            [
1148
                'id'      => rand(201, 300),
1149
                'display' => 1,
1150
            ],
1151
            [
1152
                'id'      => rand(301, 400),
1153
                'display' => 1,
1154
            ],
1155
        ];
1156
1157
        $testTagData = [
1158
            'id' => rand(1, 100),
1159
        ];
1160
1161
        $testPTLinkData = [];
1162
        foreach ($testPostData as $testPostRow) {
1163
            array_push($testPTLinkData, [
1164
                'post_id' => $testPostRow['id'],
1165
                'tag_id' => $testTagData['id'],
1166
            ]);
1167
        }
1168
1169
        $seriesPostKey = rand(1, 3);
1170
        $testSeriesPostData = [
1171
            [
1172
                'series' => 1,
1173
                'post' => reset($testPostData)['id'],
1174
            ],
1175
            [
1176
                'series' => 1,
1177
                'post' => $testPostData[$seriesPostKey]['id'],
1178
            ],
1179
        ];
1180
1181
        array_walk($testPostData, [$this, 'insertPostData']);
1182
        array_walk($testPTLinkData, [$this, 'insertPTLinkData']);
1183
        array_walk($testSeriesPostData, [$this, 'insertSeriesPostData']);
1184
        $this->insertTagData($testTagData);
1185
1186
        $repository = new MysqlPostRepository(self::$connection);
1187
        $data = $repository->getActivePostsByRelatedTags(reset($testPostData)['id']);
1188
1189
        $this->assertNotFalse($data);
1190
        $this->assertInternalType('array', $data);
1191
1192
        array_shift($testPostData);
1193
        $testPostData = array_filter($testPostData, function ($row) use ($testSeriesPostData) {
1194
            return (!in_array($row['id'], array_column($testSeriesPostData, 'post')));
1195
        });
1196
1197
        $this->assertCount(count($testPostData), $data);
1198
1199
        $testIds = array_column($testPostData, 'ids');
1200
        $dataIds = array_column($data, 'ids');
1201
1202
        $this->assertEmpty(array_merge(
1203
            array_diff($testIds, $dataIds),
1204
            array_diff($dataIds, $testIds)
1205
        ));
1206
    }
1207
1208
    public function testGetActivePostsByRelatedTagsFailure()
1209
    {
1210
        $repository = new MysqlPostRepository(self::$connection);
1211
        $data = $repository->getActivePostsByRelatedTags('');
1212
1213
        $this->assertEmpty($data);
1214
        $this->assertInternalType('array', $data);
1215
    }
1216
1217
    protected function insertPostData(array $data)
1218
    {
1219
        $defaultData = [
1220
            'id' => null,
1221
            'title' => '',
1222
            'path' => '',
1223
            'category' => '',
1224
            'date' => '',
1225
            'body' => '',
1226
            'display' => 0,
1227
        ];
1228
1229
        $data = array_merge($defaultData, $data);
1230
1231
        return self::$connection->getDefault()->perform("
1232
            INSERT INTO `jpemeric_blog`.`post`
1233
                (id, title, path, category, date, body, display)
1234
            VALUES
1235
                (:id, :title, :path, :category, :date, :body, :display)",
1236
            $data
1237
        );
1238
    }
1239
1240
    protected function insertPTLinkData(array $data)
1241
    {
1242
        $defaultData = [
1243
            'post' => null,
1244
            'tag' => null,
1245
        ];
1246
1247
        $data = array_merge($defaultData, $data);
1248
1249
        return self::$connection->getDefault()->perform("
1250
            INSERT INTO `jpemeric_blog`.`ptlink`
1251
                (post_id, tag_id)
1252
            VALUES
1253
                (:post_id, :tag_id)",
1254
            $data
1255
        );
1256
    }
1257
1258
    protected function insertSeriesPostData(array $data)
1259
    {
1260
        $defaultData = [
1261
            'series' => '',
1262
            'post' => '',
1263
            'order' => 0,
1264
        ];
1265
1266
        $data = array_merge($defaultData, $data);
1267
1268
        return self::$connection->getDefault()->perform("
1269
            INSERT INTO `jpemeric_blog`.`series_post`
1270
                (series, post, `order`)
1271
            VALUES
1272
                (:series, :post, :order)",
1273
            $data
1274
        );
1275
    }
1276
1277
    protected function insertTagData(array $data)
1278
    {
1279
        $defaultData = [
1280
            'id' => null,
1281
            'tag' => '',
1282
        ];
1283
1284
        $data = array_merge($defaultData, $data);
1285
1286
        return self::$connection->getDefault()->perform("
1287
            INSERT INTO `jpemeric_blog`.`tag`
1288
                (id, tag)
1289
            VALUES
1290
                (:id, :tag)",
1291
            $data
1292
        );
1293
    }
1294
1295
    protected function tearDown()
1296
    {
1297
        self::$connection->getDefault()->perform("DELETE FROM `jpemeric_blog`.`post`");
1298
        self::$connection->getDefault()->perform("DELETE FROM `jpemeric_blog`.`ptlink`");
1299
        self::$connection->getDefault()->perform("DELETE FROM `jpemeric_blog`.`series_post`");
1300
        self::$connection->getDefault()->perform("DELETE FROM `jpemeric_blog`.`tag`");
1301
    }
1302
1303
    public static function tearDownAfterClass()
1304
    {
1305
        self::$connection->getDefault()->disconnect();
1306
        unlink('jpemeric_blog.db');
1307
    }
1308
}
1309