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 $connections; |
14
|
|
|
|
15
|
|
|
public function __construct() |
16
|
|
|
{ |
17
|
|
|
$extendedPdo = $this->newExtendedPdo(); |
18
|
|
|
$this->connections = new ConnectionLocator(function () use ($extendedPdo) { |
19
|
|
|
return $extendedPdo; |
20
|
|
|
}); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
protected function newExtendedPdo() |
24
|
|
|
{ |
25
|
|
|
$extendedPdo = new ExtendedPdo('sqlite::memory:'); |
26
|
|
|
$extendedPdo->exec('ATTACH DATABASE `jpemeric_blog.db` AS `jpemeric_blog`'); |
27
|
|
|
$extendedPdo->exec(" |
28
|
|
|
CREATE TABLE IF NOT EXISTS `jpemeric_blog`.`post` ( |
29
|
|
|
`id` integer PRIMARY KEY AUTOINCREMENT, |
30
|
|
|
`title` varchar(60) NOT NULL, |
31
|
|
|
`path` varchar(60) NOT NULL, |
32
|
|
|
`category` varchar(20) NOT NULL, |
33
|
|
|
`date` date NOT NULL, |
34
|
|
|
`body` text NOT NULL, |
35
|
|
|
`display` integer NOT NULL |
36
|
|
|
)" |
37
|
|
|
); |
38
|
|
|
return $extendedPdo; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function newMysqlPostRepository() |
42
|
|
|
{ |
43
|
|
|
return new MysqlPostRepository($this->connections); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testConstructSetsConnections() |
47
|
|
|
{ |
48
|
|
|
$this->assertAttributeEquals( |
49
|
|
|
$this->connections, |
50
|
|
|
'connections', |
51
|
|
|
$this->newMysqlPostRepository() |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testFindPostByPath() |
56
|
|
|
{ |
57
|
|
|
$test_active_post = array( |
58
|
|
|
'title' => 'test findByPath active', |
59
|
|
|
'path' => 'test-findbypath-active', |
60
|
|
|
'category' => 'test-category', |
61
|
|
|
'date' => (new DateTime())->format('Y-m-d H:i:s'), |
62
|
|
|
'body' => 'test content', |
63
|
|
|
'display' => 1 |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
$this->connections->getDefault()->perform(" |
67
|
|
|
INSERT INTO jpemeric_blog.post |
68
|
|
|
(title, path, category, date, body, display) |
69
|
|
|
VALUES |
70
|
|
|
(:title, :path, :category, :date, :body, :display)", |
71
|
|
|
$test_active_post); |
72
|
|
|
|
73
|
|
|
$active_post = $this->newMysqlPostRepository()->findPostByPath( |
74
|
|
|
$test_active_post['category'], |
75
|
|
|
$test_active_post['path'] |
76
|
|
|
); |
77
|
|
|
$this->assertSame($test_active_post['title'], $active_post['title']); |
78
|
|
|
|
79
|
|
|
$test_inactive_post = array( |
80
|
|
|
'title' => 'test findByPath inactive', |
81
|
|
|
'path' => 'test-findbypath-inactive', |
82
|
|
|
'category' => 'test-category', |
83
|
|
|
'date' => (new DateTime())->format('Y-m-d H:i:s'), |
84
|
|
|
'body' => 'test content', |
85
|
|
|
'display' => 0 |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$this->connections->getDefault()->perform(" |
89
|
|
|
INSERT INTO jpemeric_blog.post |
90
|
|
|
(title, path, category, date, body, display) |
91
|
|
|
VALUES |
92
|
|
|
(:title, :path, :category, :date, :body, :display)", |
93
|
|
|
$test_inactive_post); |
94
|
|
|
|
95
|
|
|
$inactive_post = $this->newMysqlPostRepository()->findPostByPath( |
96
|
|
|
$test_inactive_post['category'], |
97
|
|
|
$test_inactive_post['path'] |
98
|
|
|
); |
99
|
|
|
$this->assertFalse($inactive_post); |
100
|
|
|
|
101
|
|
|
$nonexistant_post = $this->newMysqlPostRepository()->findPostByPath( |
102
|
|
|
'test-category', |
103
|
|
|
'test-findbypath-nonexistant' |
104
|
|
|
); |
105
|
|
|
$this->assertFalse($nonexistant_post); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testIsInstanceOfPostRepository() |
109
|
|
|
{ |
110
|
|
|
$this->assertInstanceOf( |
111
|
|
|
'Jacobemerick\Web\Domain\Blog\Post\PostRepositoryInterface', |
112
|
|
|
$this->newMysqlPostRepository() |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public static function tearDownAfterClass() |
117
|
|
|
{ |
118
|
|
|
// $this->connections->getDefault()->disconnect(); |
|
|
|
|
119
|
|
|
unlink('jpemeric_blog.db'); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.