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(20) NOT NULL, |
26
|
|
|
`date` date NOT NULL, |
27
|
|
|
`body` text NOT NULL, |
28
|
|
|
`display` integer NOT NULL |
29
|
|
|
)" |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
self::$connection = new ConnectionLocator(function () use ($extendedPdo) { |
33
|
|
|
return $extendedPdo; |
34
|
|
|
}); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function newMysqlPostRepository() |
38
|
|
|
{ |
39
|
|
|
return new MysqlPostRepository(self::$connection); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testConstructSetsConnections() |
43
|
|
|
{ |
44
|
|
|
$this->assertAttributeEquals( |
45
|
|
|
self::$connection, |
46
|
|
|
'connections', |
47
|
|
|
$this->newMysqlPostRepository() |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testFindPostByPath() |
52
|
|
|
{ |
53
|
|
|
$test_active_post = array( |
54
|
|
|
'title' => 'test findByPath active', |
55
|
|
|
'path' => 'test-findbypath-active', |
56
|
|
|
'category' => 'test-category', |
57
|
|
|
'date' => (new DateTime())->format('Y-m-d H:i:s'), |
58
|
|
|
'body' => 'test content', |
59
|
|
|
'display' => 1 |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
self::$connection->getDefault()->perform(" |
63
|
|
|
INSERT INTO jpemeric_blog.post |
64
|
|
|
(title, path, category, date, body, display) |
65
|
|
|
VALUES |
66
|
|
|
(:title, :path, :category, :date, :body, :display)", |
67
|
|
|
$test_active_post); |
68
|
|
|
|
69
|
|
|
$active_post = $this->newMysqlPostRepository()->findPostByPath( |
70
|
|
|
$test_active_post['category'], |
71
|
|
|
$test_active_post['path'] |
72
|
|
|
); |
73
|
|
|
$this->assertSame($test_active_post['title'], $active_post['title']); |
74
|
|
|
|
75
|
|
|
$test_inactive_post = array( |
76
|
|
|
'title' => 'test findByPath inactive', |
77
|
|
|
'path' => 'test-findbypath-inactive', |
78
|
|
|
'category' => 'test-category', |
79
|
|
|
'date' => (new DateTime())->format('Y-m-d H:i:s'), |
80
|
|
|
'body' => 'test content', |
81
|
|
|
'display' => 0 |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
self::$connection->getDefault()->perform(" |
85
|
|
|
INSERT INTO jpemeric_blog.post |
86
|
|
|
(title, path, category, date, body, display) |
87
|
|
|
VALUES |
88
|
|
|
(:title, :path, :category, :date, :body, :display)", |
89
|
|
|
$test_inactive_post); |
90
|
|
|
|
91
|
|
|
$inactive_post = $this->newMysqlPostRepository()->findPostByPath( |
92
|
|
|
$test_inactive_post['category'], |
93
|
|
|
$test_inactive_post['path'] |
94
|
|
|
); |
95
|
|
|
$this->assertFalse($inactive_post); |
96
|
|
|
|
97
|
|
|
$nonexistant_post = $this->newMysqlPostRepository()->findPostByPath( |
98
|
|
|
'test-category', |
99
|
|
|
'test-findbypath-nonexistant' |
100
|
|
|
); |
101
|
|
|
$this->assertFalse($nonexistant_post); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testIsInstanceOfPostRepository() |
105
|
|
|
{ |
106
|
|
|
$this->assertInstanceOf( |
107
|
|
|
'Jacobemerick\Web\Domain\Blog\Post\PostRepositoryInterface', |
108
|
|
|
$this->newMysqlPostRepository() |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public static function tearDownAfterClass() |
113
|
|
|
{ |
114
|
|
|
self::$connection->getDefault()->disconnect(); |
|
|
|
|
115
|
|
|
unlink('jpemeric_blog.db'); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
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.