|
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 testFindByUri() |
|
44
|
|
|
{ |
|
45
|
|
|
$test_post = array( |
|
46
|
|
|
'title' => 'test title', |
|
47
|
|
|
'path' => 'test-uri', |
|
48
|
|
|
'category' => 'test', |
|
49
|
|
|
'date' => date('Y-m-d H:i:s'), |
|
50
|
|
|
'body' => 'test content', |
|
51
|
|
|
'display' => 1 |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
$this->connections->getDefault()->perform(" |
|
55
|
|
|
INSERT INTO jpemeric_blog.post |
|
56
|
|
|
(title, path, category, date, body, display) |
|
57
|
|
|
VALUES |
|
58
|
|
|
(:title, :path, :category, :date, :body, :display)", |
|
59
|
|
|
$test_post); |
|
60
|
|
|
|
|
61
|
|
|
$post = $this->newMysqlPostRepository()->findByUri($test_post['path']); |
|
62
|
|
|
$this->assertSame($test_post['path'], $post['path']); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function testIsInstanceOfPostRepository() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->assertInstanceOf( |
|
68
|
|
|
'Jacobemerick\Web\Domain\Blog\PostRepository', |
|
69
|
|
|
$this->newMysqlPostRepository() |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public static function tearDownAfterClass() |
|
74
|
|
|
{ |
|
75
|
|
|
// $this->connections->getDefault()->disconnect(); |
|
|
|
|
|
|
76
|
|
|
unlink('jpemeric_blog.db'); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
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.