1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Aura\Sql\ConnectionLocator; |
4
|
|
|
use Aura\Sql\ExtendedPdo; |
5
|
|
|
use Jacobemerick\Web\Domain\Blog\MysqlPostRepository; |
6
|
|
|
|
7
|
|
|
class MysqlPostRepositoryTest extends \PHPUnit_Framework_TestCase |
|
|
|
|
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
protected $connections; |
11
|
|
|
|
12
|
|
|
public function __construct() |
13
|
|
|
{ |
14
|
|
|
$extendedPdo = $this->newExtendedPdo(); |
15
|
|
|
$this->connections = new ConnectionLocator(function () use ($extendedPdo) { |
16
|
|
|
return $extendedPdo; |
17
|
|
|
}); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
protected function newExtendedPdo() |
21
|
|
|
{ |
22
|
|
|
$extendedPdo = new ExtendedPdo('sqlite::memory:'); |
23
|
|
|
$extendedPdo->exec('ATTACH DATABASE `jpemeric_blog.db` AS `jpemeric_blog`'); |
24
|
|
|
$extendedPdo->exec(" |
25
|
|
|
CREATE TABLE IF NOT EXISTS `jpemeric_blog`.`post` ( |
26
|
|
|
`id` integer PRIMARY KEY AUTOINCREMENT, |
27
|
|
|
`title` varchar(60) NOT NULL, |
28
|
|
|
`path` varchar(60) NOT NULL, |
29
|
|
|
`category` varchar(20) NOT NULL, |
30
|
|
|
`date` date NOT NULL, |
31
|
|
|
`body` text NOT NULL, |
32
|
|
|
`display` integer NOT NULL |
33
|
|
|
)" |
34
|
|
|
); |
35
|
|
|
return $extendedPdo; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function newMysqlPostRepository() |
39
|
|
|
{ |
40
|
|
|
return new MysqlPostRepository($this->connections); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testConstructSetsConnections() |
44
|
|
|
{ |
45
|
|
|
$this->assertAttributeEquals( |
46
|
|
|
$this->connections, |
47
|
|
|
'connections', |
48
|
|
|
$this->newMysqlPostRepository() |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testFindPostByPath() |
53
|
|
|
{ |
54
|
|
|
$test_active_post = array( |
55
|
|
|
'title' => 'test findByPath active', |
56
|
|
|
'path' => 'test-findbypath-active', |
57
|
|
|
'category' => 'test-category', |
58
|
|
|
'date' => (new DateTime())->format('Y-m-d H:i:s'), |
59
|
|
|
'body' => 'test content', |
60
|
|
|
'display' => 1 |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$this->connections->getDefault()->perform(" |
64
|
|
|
INSERT INTO jpemeric_blog.post |
65
|
|
|
(title, path, category, date, body, display) |
66
|
|
|
VALUES |
67
|
|
|
(:title, :path, :category, :date, :body, :display)", |
68
|
|
|
$test_active_post); |
69
|
|
|
|
70
|
|
|
$active_post = $this->newMysqlPostRepository()->findPostByPath( |
71
|
|
|
$test_active_post['category'], |
72
|
|
|
$test_active_post['path'] |
73
|
|
|
); |
74
|
|
|
$this->assertSame($test_active_post['title'], $active_post['title']); |
75
|
|
|
|
76
|
|
|
$test_inactive_post = array( |
77
|
|
|
'title' => 'test findByPath inactive', |
78
|
|
|
'path' => 'test-findbypath-inactive', |
79
|
|
|
'category' => 'test-category', |
80
|
|
|
'date' => (new DateTime())->format('Y-m-d H:i:s'), |
81
|
|
|
'body' => 'test content', |
82
|
|
|
'display' => 0 |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
$this->connections->getDefault()->perform(" |
86
|
|
|
INSERT INTO jpemeric_blog.post |
87
|
|
|
(title, path, category, date, body, display) |
88
|
|
|
VALUES |
89
|
|
|
(:title, :path, :category, :date, :body, :display)", |
90
|
|
|
$test_inactive_post); |
91
|
|
|
|
92
|
|
|
$inactive_post = $this->newMysqlPostRepository()->findPostByPath( |
93
|
|
|
$test_inactive_post['category'], |
94
|
|
|
$test_inactive_post['path'] |
95
|
|
|
); |
96
|
|
|
$this->assertFalse($inactive_post); |
97
|
|
|
|
98
|
|
|
$nonexistant_post = $this->newMysqlPostRepository()->findPostByPath( |
99
|
|
|
'test-category', |
100
|
|
|
'test-findbypath-nonexistant' |
101
|
|
|
); |
102
|
|
|
$this->assertFalse($nonexistant_post); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testIsInstanceOfPostRepository() |
106
|
|
|
{ |
107
|
|
|
$this->assertInstanceOf( |
108
|
|
|
'Jacobemerick\Web\Domain\Blog\PostRepository', |
109
|
|
|
$this->newMysqlPostRepository() |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public static function tearDownAfterClass() |
114
|
|
|
{ |
115
|
|
|
// $this->connections->getDefault()->disconnect(); |
|
|
|
|
116
|
|
|
unlink('jpemeric_blog.db'); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.