@@ -9,13 +9,13 @@ discard block |
||
9 | 9 | class MysqlActivityRepositoryTest extends PHPUnit_Framework_TestCase |
10 | 10 | { |
11 | 11 | |
12 | - protected static $connection; |
|
12 | + protected static $connection; |
|
13 | 13 | |
14 | - public static function setUpBeforeClass() |
|
15 | - { |
|
16 | - $extendedPdo = new ExtendedPdo('sqlite::memory:'); |
|
17 | - $extendedPdo->exec("ATTACH DATABASE `jpemeric_stream.db` AS `jpemeric_stream`"); |
|
18 | - $extendedPdo->exec(" |
|
14 | + public static function setUpBeforeClass() |
|
15 | + { |
|
16 | + $extendedPdo = new ExtendedPdo('sqlite::memory:'); |
|
17 | + $extendedPdo->exec("ATTACH DATABASE `jpemeric_stream.db` AS `jpemeric_stream`"); |
|
18 | + $extendedPdo->exec(" |
|
19 | 19 | CREATE TABLE IF NOT EXISTS `jpemeric_stream`.`activity` ( |
20 | 20 | `id` integer PRIMARY KEY AUTOINCREMENT, |
21 | 21 | `message` text NOT NULL, |
@@ -27,193 +27,193 @@ discard block |
||
27 | 27 | `created_at` datetime, |
28 | 28 | `updated_at` datetime |
29 | 29 | )" |
30 | - ); |
|
31 | - |
|
32 | - self::$connection = new ConnectionLocator(function () use ($extendedPdo) { |
|
33 | - return $extendedPdo; |
|
34 | - }); |
|
35 | - } |
|
36 | - |
|
37 | - public function testIsInstanceOfActivityRepository() |
|
38 | - { |
|
39 | - $repository = new MysqlActivityRepository(self::$connection); |
|
40 | - |
|
41 | - $this->assertInstanceOf( |
|
42 | - 'Jacobemerick\Web\Domain\Stream\Activity\MysqlActivityRepository', |
|
43 | - $repository |
|
44 | - ); |
|
45 | - } |
|
46 | - |
|
47 | - public function testImplementsActivityInterface() |
|
48 | - { |
|
49 | - $repository = new MysqlActivityRepository(self::$connection); |
|
50 | - |
|
51 | - $this->assertInstanceOf( |
|
52 | - 'Jacobemerick\Web\Domain\Stream\Activity\ActivityRepositoryInterface', |
|
53 | - $repository |
|
54 | - ); |
|
55 | - } |
|
56 | - |
|
57 | - public function testConstructSetsConnections() |
|
58 | - { |
|
59 | - $respository = new MysqlActivityRepository(self::$connection); |
|
60 | - |
|
61 | - $this->assertAttributeSame( |
|
62 | - self::$connection, |
|
63 | - 'connections', |
|
64 | - $respository |
|
65 | - ); |
|
66 | - } |
|
67 | - |
|
68 | - public function testGetActivityById() |
|
69 | - { |
|
70 | - $testData = [ |
|
71 | - 'id' => rand(1, 100), |
|
72 | - 'message' => 'test data', |
|
73 | - ]; |
|
74 | - |
|
75 | - $this->insertData($testData); |
|
76 | - |
|
77 | - $repository = new MysqlActivityRepository(self::$connection); |
|
78 | - $data = $repository->getActivityById($testData['id']); |
|
79 | - |
|
80 | - $this->assertNotFalse($data); |
|
81 | - $this->assertInternalType('array', $data); |
|
82 | - $this->assertArraySubset($testData, $data); |
|
83 | - } |
|
84 | - |
|
85 | - public function testGetActivityByIdFailure() |
|
86 | - { |
|
87 | - $testData = [ |
|
88 | - 'id' => rand(1, 100), |
|
89 | - 'message' => 'test data', |
|
90 | - ]; |
|
91 | - |
|
92 | - $this->insertData($testData); |
|
93 | - |
|
94 | - $repository = new MysqlActivityRepository(self::$connection); |
|
95 | - $data = $repository->getActivityById($testData['id'] + 1); |
|
96 | - |
|
97 | - $this->assertFalse($data); |
|
98 | - } |
|
99 | - |
|
100 | - public function testGetActivities() |
|
101 | - { |
|
102 | - $testData = [ |
|
103 | - [ |
|
104 | - 'id' => rand(1, 100), |
|
105 | - 'message' => 'test one', |
|
106 | - ], |
|
107 | - [ |
|
108 | - 'id' => rand(101, 200), |
|
109 | - 'message' => 'test two', |
|
110 | - ], |
|
111 | - ]; |
|
112 | - |
|
113 | - $this->insertData($testData[0]); |
|
114 | - $this->insertData($testData[1]); |
|
115 | - |
|
116 | - $repository = new MysqlActivityRepository(self::$connection); |
|
117 | - $data = $repository->getActivities(); |
|
118 | - |
|
119 | - $this->assertNotFalse($data); |
|
120 | - $this->assertInternalType('array', $data); |
|
121 | - foreach ($testData as $key => $testRow) { |
|
122 | - $this->assertInternalType('array', $testRow); |
|
123 | - $this->assertArraySubset($testRow, $data[$key]); |
|
124 | - } |
|
125 | - } |
|
126 | - |
|
127 | - public function testGetActivitiesFailure() |
|
128 | - { |
|
129 | - $repository = new MysqlActivityRepository(self::$connection); |
|
130 | - $data = $repository->getActivities(); |
|
131 | - |
|
132 | - $this->assertEmpty($data); |
|
133 | - $this->assertInternalType('array', $data); |
|
134 | - } |
|
135 | - |
|
136 | - public function testGetActivitiesRange() {} |
|
137 | - |
|
138 | - public function testGetActivitiesRangeFailure() {} |
|
139 | - |
|
140 | - public function testGetActivitiesCount() |
|
141 | - { |
|
142 | - $testData = [ |
|
143 | - [ |
|
144 | - 'id' => rand(1, 100), |
|
145 | - 'message' => 'test one', |
|
146 | - ], |
|
147 | - [ |
|
148 | - 'id' => rand(101, 200), |
|
149 | - 'message' => 'test two', |
|
150 | - ], |
|
151 | - ]; |
|
152 | - |
|
153 | - $this->insertData($testData[0]); |
|
154 | - $this->insertData($testData[1]); |
|
155 | - |
|
156 | - $repository = new MysqlActivityRepository(self::$connection); |
|
157 | - $data = $repository->getActivitiesCount(); |
|
158 | - |
|
159 | - $this->assertNotFalse($data); |
|
160 | - $this->assertStringMatchesFormat('%d', $data); |
|
161 | - $this->assertEquals(count($testData), $data); |
|
162 | - } |
|
163 | - |
|
164 | - public function testGetActivitiesCountEmpty() |
|
165 | - { |
|
166 | - $repository = new MysqlActivityRepository(self::$connection); |
|
167 | - $data = $repository->getActivitiesCount(); |
|
168 | - |
|
169 | - $this->assertNotFalse($data); |
|
170 | - $this->assertStringMatchesFormat('%d', $data); |
|
171 | - $this->assertEquals('0', $data); |
|
172 | - } |
|
173 | - |
|
174 | - public function testGetActivitiesByType() {} |
|
175 | - |
|
176 | - public function testGetActivitiesByTypeFailure() {} |
|
177 | - |
|
178 | - public function testGetActivitiesByTypeRange() {} |
|
179 | - |
|
180 | - public function testGetActivitiesByTypeRangeFailure() {} |
|
181 | - |
|
182 | - public function testGetActivitiesByTypeCount() {} |
|
183 | - |
|
184 | - public function testGetActivitiesByTypeCountEmpty() {} |
|
185 | - |
|
186 | - protected function tearDown() |
|
187 | - { |
|
188 | - self::$connection->getDefault()->perform("DELETE FROM `jpemeric_stream`.`activity`"); |
|
189 | - } |
|
190 | - |
|
191 | - public static function tearDownAfterClass() |
|
192 | - { |
|
193 | - self::$connection->getDefault()->disconnect(); |
|
194 | - unlink('jpemeric_stream.db'); |
|
195 | - } |
|
196 | - |
|
197 | - protected function insertData(array $data) |
|
198 | - { |
|
199 | - $defaultData = [ |
|
200 | - 'id' => null, |
|
201 | - 'message' => '', |
|
202 | - 'message_long' => '', |
|
203 | - 'datetime' => '', |
|
204 | - 'metadata' => '', |
|
205 | - 'type' => '', |
|
206 | - 'type_id' => '', |
|
207 | - ]; |
|
208 | - |
|
209 | - $data = array_merge($defaultData, $data); |
|
210 | - |
|
211 | - return self::$connection->getDefault()->perform(" |
|
30 | + ); |
|
31 | + |
|
32 | + self::$connection = new ConnectionLocator(function () use ($extendedPdo) { |
|
33 | + return $extendedPdo; |
|
34 | + }); |
|
35 | + } |
|
36 | + |
|
37 | + public function testIsInstanceOfActivityRepository() |
|
38 | + { |
|
39 | + $repository = new MysqlActivityRepository(self::$connection); |
|
40 | + |
|
41 | + $this->assertInstanceOf( |
|
42 | + 'Jacobemerick\Web\Domain\Stream\Activity\MysqlActivityRepository', |
|
43 | + $repository |
|
44 | + ); |
|
45 | + } |
|
46 | + |
|
47 | + public function testImplementsActivityInterface() |
|
48 | + { |
|
49 | + $repository = new MysqlActivityRepository(self::$connection); |
|
50 | + |
|
51 | + $this->assertInstanceOf( |
|
52 | + 'Jacobemerick\Web\Domain\Stream\Activity\ActivityRepositoryInterface', |
|
53 | + $repository |
|
54 | + ); |
|
55 | + } |
|
56 | + |
|
57 | + public function testConstructSetsConnections() |
|
58 | + { |
|
59 | + $respository = new MysqlActivityRepository(self::$connection); |
|
60 | + |
|
61 | + $this->assertAttributeSame( |
|
62 | + self::$connection, |
|
63 | + 'connections', |
|
64 | + $respository |
|
65 | + ); |
|
66 | + } |
|
67 | + |
|
68 | + public function testGetActivityById() |
|
69 | + { |
|
70 | + $testData = [ |
|
71 | + 'id' => rand(1, 100), |
|
72 | + 'message' => 'test data', |
|
73 | + ]; |
|
74 | + |
|
75 | + $this->insertData($testData); |
|
76 | + |
|
77 | + $repository = new MysqlActivityRepository(self::$connection); |
|
78 | + $data = $repository->getActivityById($testData['id']); |
|
79 | + |
|
80 | + $this->assertNotFalse($data); |
|
81 | + $this->assertInternalType('array', $data); |
|
82 | + $this->assertArraySubset($testData, $data); |
|
83 | + } |
|
84 | + |
|
85 | + public function testGetActivityByIdFailure() |
|
86 | + { |
|
87 | + $testData = [ |
|
88 | + 'id' => rand(1, 100), |
|
89 | + 'message' => 'test data', |
|
90 | + ]; |
|
91 | + |
|
92 | + $this->insertData($testData); |
|
93 | + |
|
94 | + $repository = new MysqlActivityRepository(self::$connection); |
|
95 | + $data = $repository->getActivityById($testData['id'] + 1); |
|
96 | + |
|
97 | + $this->assertFalse($data); |
|
98 | + } |
|
99 | + |
|
100 | + public function testGetActivities() |
|
101 | + { |
|
102 | + $testData = [ |
|
103 | + [ |
|
104 | + 'id' => rand(1, 100), |
|
105 | + 'message' => 'test one', |
|
106 | + ], |
|
107 | + [ |
|
108 | + 'id' => rand(101, 200), |
|
109 | + 'message' => 'test two', |
|
110 | + ], |
|
111 | + ]; |
|
112 | + |
|
113 | + $this->insertData($testData[0]); |
|
114 | + $this->insertData($testData[1]); |
|
115 | + |
|
116 | + $repository = new MysqlActivityRepository(self::$connection); |
|
117 | + $data = $repository->getActivities(); |
|
118 | + |
|
119 | + $this->assertNotFalse($data); |
|
120 | + $this->assertInternalType('array', $data); |
|
121 | + foreach ($testData as $key => $testRow) { |
|
122 | + $this->assertInternalType('array', $testRow); |
|
123 | + $this->assertArraySubset($testRow, $data[$key]); |
|
124 | + } |
|
125 | + } |
|
126 | + |
|
127 | + public function testGetActivitiesFailure() |
|
128 | + { |
|
129 | + $repository = new MysqlActivityRepository(self::$connection); |
|
130 | + $data = $repository->getActivities(); |
|
131 | + |
|
132 | + $this->assertEmpty($data); |
|
133 | + $this->assertInternalType('array', $data); |
|
134 | + } |
|
135 | + |
|
136 | + public function testGetActivitiesRange() {} |
|
137 | + |
|
138 | + public function testGetActivitiesRangeFailure() {} |
|
139 | + |
|
140 | + public function testGetActivitiesCount() |
|
141 | + { |
|
142 | + $testData = [ |
|
143 | + [ |
|
144 | + 'id' => rand(1, 100), |
|
145 | + 'message' => 'test one', |
|
146 | + ], |
|
147 | + [ |
|
148 | + 'id' => rand(101, 200), |
|
149 | + 'message' => 'test two', |
|
150 | + ], |
|
151 | + ]; |
|
152 | + |
|
153 | + $this->insertData($testData[0]); |
|
154 | + $this->insertData($testData[1]); |
|
155 | + |
|
156 | + $repository = new MysqlActivityRepository(self::$connection); |
|
157 | + $data = $repository->getActivitiesCount(); |
|
158 | + |
|
159 | + $this->assertNotFalse($data); |
|
160 | + $this->assertStringMatchesFormat('%d', $data); |
|
161 | + $this->assertEquals(count($testData), $data); |
|
162 | + } |
|
163 | + |
|
164 | + public function testGetActivitiesCountEmpty() |
|
165 | + { |
|
166 | + $repository = new MysqlActivityRepository(self::$connection); |
|
167 | + $data = $repository->getActivitiesCount(); |
|
168 | + |
|
169 | + $this->assertNotFalse($data); |
|
170 | + $this->assertStringMatchesFormat('%d', $data); |
|
171 | + $this->assertEquals('0', $data); |
|
172 | + } |
|
173 | + |
|
174 | + public function testGetActivitiesByType() {} |
|
175 | + |
|
176 | + public function testGetActivitiesByTypeFailure() {} |
|
177 | + |
|
178 | + public function testGetActivitiesByTypeRange() {} |
|
179 | + |
|
180 | + public function testGetActivitiesByTypeRangeFailure() {} |
|
181 | + |
|
182 | + public function testGetActivitiesByTypeCount() {} |
|
183 | + |
|
184 | + public function testGetActivitiesByTypeCountEmpty() {} |
|
185 | + |
|
186 | + protected function tearDown() |
|
187 | + { |
|
188 | + self::$connection->getDefault()->perform("DELETE FROM `jpemeric_stream`.`activity`"); |
|
189 | + } |
|
190 | + |
|
191 | + public static function tearDownAfterClass() |
|
192 | + { |
|
193 | + self::$connection->getDefault()->disconnect(); |
|
194 | + unlink('jpemeric_stream.db'); |
|
195 | + } |
|
196 | + |
|
197 | + protected function insertData(array $data) |
|
198 | + { |
|
199 | + $defaultData = [ |
|
200 | + 'id' => null, |
|
201 | + 'message' => '', |
|
202 | + 'message_long' => '', |
|
203 | + 'datetime' => '', |
|
204 | + 'metadata' => '', |
|
205 | + 'type' => '', |
|
206 | + 'type_id' => '', |
|
207 | + ]; |
|
208 | + |
|
209 | + $data = array_merge($defaultData, $data); |
|
210 | + |
|
211 | + return self::$connection->getDefault()->perform(" |
|
212 | 212 | INSERT INTO `jpemeric_stream`.`activity` |
213 | 213 | (id, message, message_long, datetime, metadata, type, type_id) |
214 | 214 | VALUES |
215 | 215 | (:id, :message, :message_long, :datetime, :metadata, :type, :type_id)", |
216 | - $data |
|
217 | - ); |
|
218 | - } |
|
216 | + $data |
|
217 | + ); |
|
218 | + } |
|
219 | 219 | } |