1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jacobemerick\Web\Domain\Blog\Introduction; |
4
|
|
|
|
5
|
|
|
use Aura\Sql\ConnectionLocator; |
6
|
|
|
use Aura\Sql\ExtendedPdo; |
7
|
|
|
use PHPUnit_Framework_TestCase; |
8
|
|
|
|
9
|
|
|
class MysqlIntroductionRepositoryTest extends PHPUnit_Framework_TestCase |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
protected static $connection; |
13
|
|
|
|
14
|
|
View Code Duplication |
public static function setUpBeforeClass() |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
$extendedPdo = new ExtendedPdo('sqlite::memory:'); |
17
|
|
|
$extendedPdo->exec("ATTACH DATABASE `jpemeric_blog.db` AS `jpemeric_blog`"); |
18
|
|
|
|
19
|
|
|
$extendedPdo->exec(" |
20
|
|
|
CREATE TABLE IF NOT EXISTS `jpemeric_blog`.`introduction` ( |
21
|
|
|
`id` integer PRIMARY KEY AUTOINCREMENT, |
22
|
|
|
`type` varchar(10) NOT NULL, |
23
|
|
|
`value` varchar(25) NOT NULL, |
24
|
|
|
`title` varchar(100) NOT NULL, |
25
|
|
|
`content` text NOT NULL, |
26
|
|
|
`image` image |
27
|
|
|
)" |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
self::$connection = new ConnectionLocator(function () use ($extendedPdo) { |
31
|
|
|
return $extendedPdo; |
32
|
|
|
}); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testIsInstanceOfIntroductionRepository() |
36
|
|
|
{ |
37
|
|
|
$repository = new MysqlIntroductionRepository(self::$connection); |
38
|
|
|
|
39
|
|
|
$this->assertInstanceOf( |
40
|
|
|
'Jacobemerick\Web\Domain\Blog\Introduction\MysqlIntroductionRepository', |
41
|
|
|
$repository |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testImplementsIntroductionInterface() |
46
|
|
|
{ |
47
|
|
|
$repository = new MysqlIntroductionRepository(self::$connection); |
48
|
|
|
|
49
|
|
|
$this->assertInstanceOf( |
50
|
|
|
'Jacobemerick\Web\Domain\Blog\Introduction\IntroductionRepositoryInterface', |
51
|
|
|
$repository |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testConstructSetsConnections() |
56
|
|
|
{ |
57
|
|
|
$respository = new MysqlIntroductionRepository(self::$connection); |
58
|
|
|
|
59
|
|
|
$this->assertAttributeSame( |
60
|
|
|
self::$connection, |
61
|
|
|
'connections', |
62
|
|
|
$respository |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testFindByType() |
67
|
|
|
{ |
68
|
|
|
$testData = [ |
69
|
|
|
'id' => rand(1, 100), |
70
|
|
|
'type' => 'test', |
71
|
|
|
'title' => 'test title', |
72
|
|
|
'content' => 'test content', |
73
|
|
|
'image' => rand(1, 100), |
74
|
|
|
]; |
75
|
|
|
|
76
|
|
|
$this->insertData($testData); |
77
|
|
|
|
78
|
|
|
$repository = new MysqlIntroductionRepository(self::$connection); |
79
|
|
|
$data = $repository->findByType('test'); |
80
|
|
|
|
81
|
|
|
$this->assertNotFalse($data); |
82
|
|
|
$this->assertInternalType('array', $data); |
83
|
|
|
$this->assertArrayHasKey('title', $data); |
84
|
|
|
$this->assertEquals($testData['title'], $data['title']); |
85
|
|
|
$this->assertArrayHasKey('content', $data); |
86
|
|
|
$this->assertEquals($testData['content'], $data['content']); |
87
|
|
|
$this->assertArrayHasKey('image', $data); |
88
|
|
|
$this->assertEquals($testData['image'], $data['image']); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testFindByTypeFilterValue() |
92
|
|
|
{ |
93
|
|
|
$testData = [ |
94
|
|
|
[ |
95
|
|
|
'id' => rand(1, 100), |
96
|
|
|
'type' => 'test', |
97
|
|
|
'title' => 'title a', |
98
|
|
|
'value' => 'value a', |
99
|
|
|
], |
100
|
|
|
[ |
101
|
|
|
'id' => rand(101, 200), |
102
|
|
|
'type' => 'test', |
103
|
|
|
'title' => 'title b', |
104
|
|
|
'value' => 'value b', |
105
|
|
|
], |
106
|
|
|
]; |
107
|
|
|
|
108
|
|
|
array_walk($testData, [$this, 'insertData']); |
109
|
|
|
|
110
|
|
|
$repository = new MysqlIntroductionRepository(self::$connection); |
111
|
|
|
$data = $repository->findByType('test', reset($testData)['value']); |
112
|
|
|
|
113
|
|
|
$this->assertNotFalse($data); |
114
|
|
|
$this->assertInternalType('array', $data); |
115
|
|
|
$this->assertEquals(reset($testData)['title'], $data['title']); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testFindByTypeFailure() |
119
|
|
|
{ |
120
|
|
|
$repository = new MysqlIntroductionRepository(self::$connection); |
121
|
|
|
$data = $repository->findByType('empty type'); |
122
|
|
|
|
123
|
|
|
$this->assertFalse($data); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
protected function tearDown() |
127
|
|
|
{ |
128
|
|
|
self::$connection->getDefault()->perform("DELETE FROM `jpemeric_blog`.`introduction`"); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public static function tearDownAfterClass() |
132
|
|
|
{ |
133
|
|
|
self::$connection->getDefault()->disconnect(); |
|
|
|
|
134
|
|
|
unlink('jpemeric_blog.db'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
View Code Duplication |
protected function insertData(array $data) |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$defaultData = [ |
140
|
|
|
'id' => null, |
141
|
|
|
'type' => '', |
142
|
|
|
'value' => '', |
143
|
|
|
'title' => '', |
144
|
|
|
'content' => '', |
145
|
|
|
'image' => null, |
146
|
|
|
]; |
147
|
|
|
|
148
|
|
|
$data = array_merge($defaultData, $data); |
149
|
|
|
|
150
|
|
|
return self::$connection->getDefault()->perform(" |
151
|
|
|
INSERT INTO `jpemeric_blog`.`introduction` |
152
|
|
|
(id, type, value, title, content, image) |
153
|
|
|
VALUES |
154
|
|
|
(:id, :type, :value, :title, :content, :image)", |
155
|
|
|
$data |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
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.