@@ -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,428 +27,428 @@ 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 | - array_walk($testData, [$this, 'insertData']); |
|
| 114 | - |
|
| 115 | - $repository = new MysqlActivityRepository(self::$connection); |
|
| 116 | - $data = $repository->getActivities(); |
|
| 117 | - |
|
| 118 | - $this->assertNotFalse($data); |
|
| 119 | - $this->assertInternalType('array', $data); |
|
| 120 | - foreach ($testData as $key => $testRow) { |
|
| 121 | - $this->assertInternalType('array', $testRow); |
|
| 122 | - $this->assertArraySubset($testRow, $data[$key]); |
|
| 123 | - } |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - public function testGetActivitiesFailure() |
|
| 127 | - { |
|
| 128 | - $repository = new MysqlActivityRepository(self::$connection); |
|
| 129 | - $data = $repository->getActivities(); |
|
| 130 | - |
|
| 131 | - $this->assertEmpty($data); |
|
| 132 | - $this->assertInternalType('array', $data); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - public function testGetActivitiesRange() |
|
| 136 | - { |
|
| 137 | - $testData = [ |
|
| 138 | - [ |
|
| 139 | - 'id' => rand(1, 100), |
|
| 140 | - 'message' => 'test one', |
|
| 141 | - ], |
|
| 142 | - [ |
|
| 143 | - 'id' => rand(101, 200), |
|
| 144 | - 'message' => 'test two', |
|
| 145 | - ], |
|
| 146 | - [ |
|
| 147 | - 'id' => rand(201, 300), |
|
| 148 | - 'message' => 'test three', |
|
| 149 | - ], |
|
| 150 | - ]; |
|
| 151 | - |
|
| 152 | - array_walk($testData, [$this, 'insertData']); |
|
| 153 | - |
|
| 154 | - $repository = new MysqlActivityRepository(self::$connection); |
|
| 155 | - $data = $repository->getActivities(2, 1); |
|
| 156 | - |
|
| 157 | - $this->assertNotFalse($data); |
|
| 158 | - $this->assertInternalType('array', $data); |
|
| 159 | - $this->assertCount(2, $data); |
|
| 160 | - |
|
| 161 | - $testData = array_slice($testData, 1, 2); |
|
| 162 | - |
|
| 163 | - foreach ($testData as $key => $testRow) { |
|
| 164 | - $this->assertInternalType('array', $testRow); |
|
| 165 | - $this->assertArraySubset($testRow, $data[$key]); |
|
| 166 | - } |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - public function testGetActivitiesRangeFailure() |
|
| 170 | - { |
|
| 171 | - $testData = [ |
|
| 172 | - [ |
|
| 173 | - 'id' => rand(1, 100), |
|
| 174 | - 'message' => 'test one', |
|
| 175 | - ], |
|
| 176 | - [ |
|
| 177 | - 'id' => rand(101, 200), |
|
| 178 | - 'message' => 'test two', |
|
| 179 | - ], |
|
| 180 | - ]; |
|
| 181 | - |
|
| 182 | - array_walk($testData, [$this, 'insertData']); |
|
| 183 | - |
|
| 184 | - $repository = new MysqlActivityRepository(self::$connection); |
|
| 185 | - $data = $repository->getActivities(1, 3); |
|
| 186 | - |
|
| 187 | - $this->assertEmpty($data); |
|
| 188 | - $this->assertInternalType('array', $data); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - public function testGetActivitiesCount() |
|
| 192 | - { |
|
| 193 | - $testData = [ |
|
| 194 | - [ |
|
| 195 | - 'id' => rand(1, 100), |
|
| 196 | - 'message' => 'test one', |
|
| 197 | - ], |
|
| 198 | - [ |
|
| 199 | - 'id' => rand(101, 200), |
|
| 200 | - 'message' => 'test two', |
|
| 201 | - ], |
|
| 202 | - ]; |
|
| 203 | - |
|
| 204 | - array_walk($testData, [$this, 'insertData']); |
|
| 205 | - |
|
| 206 | - $repository = new MysqlActivityRepository(self::$connection); |
|
| 207 | - $data = $repository->getActivitiesCount(); |
|
| 208 | - |
|
| 209 | - $this->assertNotFalse($data); |
|
| 210 | - $this->assertStringMatchesFormat('%d', $data); |
|
| 211 | - $this->assertEquals(count($testData), $data); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - public function testGetActivitiesCountEmpty() |
|
| 215 | - { |
|
| 216 | - $repository = new MysqlActivityRepository(self::$connection); |
|
| 217 | - $data = $repository->getActivitiesCount(); |
|
| 218 | - |
|
| 219 | - $this->assertNotFalse($data); |
|
| 220 | - $this->assertStringMatchesFormat('%d', $data); |
|
| 221 | - $this->assertEquals('0', $data); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - public function testGetActivitiesByType() |
|
| 225 | - { |
|
| 226 | - $testData = [ |
|
| 227 | - [ |
|
| 228 | - 'id' => rand(1, 100), |
|
| 229 | - 'message' => 'test one', |
|
| 230 | - 'type' => 'type one', |
|
| 231 | - ], |
|
| 232 | - [ |
|
| 233 | - 'id' => rand(101, 200), |
|
| 234 | - 'message' => 'test two', |
|
| 235 | - 'type' => 'type two', |
|
| 236 | - ], |
|
| 237 | - [ |
|
| 238 | - 'id' => rand(201, 300), |
|
| 239 | - 'message' => 'test three', |
|
| 240 | - 'type' => 'type one', |
|
| 241 | - ], |
|
| 242 | - ]; |
|
| 243 | - |
|
| 244 | - array_walk($testData, [$this, 'insertData']); |
|
| 245 | - |
|
| 246 | - $repository = new MysqlActivityRepository(self::$connection); |
|
| 247 | - $data = $repository->getActivitiesByType('type one'); |
|
| 248 | - |
|
| 249 | - $this->assertNotFalse($data); |
|
| 250 | - $this->assertInternalType('array', $data); |
|
| 251 | - |
|
| 252 | - $testData = array_filter($testData, function ($row) { |
|
| 253 | - return ($row['type'] == 'type one'); |
|
| 254 | - }); |
|
| 255 | - $testData = array_values($testData); |
|
| 256 | - |
|
| 257 | - foreach ($testData as $key => $testRow) { |
|
| 258 | - $this->assertInternalType('array', $testRow); |
|
| 259 | - $this->assertArraySubset($testRow, $data[$key]); |
|
| 260 | - } |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - public function testGetActivitiesByTypeFailure() |
|
| 264 | - { |
|
| 265 | - $testData = [ |
|
| 266 | - [ |
|
| 267 | - 'id' => rand(1, 100), |
|
| 268 | - 'message' => 'test one', |
|
| 269 | - 'type' => 'type one', |
|
| 270 | - ], |
|
| 271 | - [ |
|
| 272 | - 'id' => rand(101, 200), |
|
| 273 | - 'message' => 'test two', |
|
| 274 | - 'type' => 'type one', |
|
| 275 | - ], |
|
| 276 | - ]; |
|
| 277 | - |
|
| 278 | - array_walk($testData, [$this, 'insertData']); |
|
| 279 | - |
|
| 280 | - $repository = new MysqlActivityRepository(self::$connection); |
|
| 281 | - $data = $repository->getActivitiesByType('type two'); |
|
| 282 | - |
|
| 283 | - $this->assertEmpty($data); |
|
| 284 | - $this->assertInternalType('array', $data); |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - public function testGetActivitiesByTypeRange() |
|
| 288 | - { |
|
| 289 | - $testData = [ |
|
| 290 | - [ |
|
| 291 | - 'id' => rand(1, 100), |
|
| 292 | - 'message' => 'test one', |
|
| 293 | - 'type' => 'type one', |
|
| 294 | - ], |
|
| 295 | - [ |
|
| 296 | - 'id' => rand(101, 200), |
|
| 297 | - 'message' => 'test two', |
|
| 298 | - 'type' => 'type two', |
|
| 299 | - ], |
|
| 300 | - [ |
|
| 301 | - 'id' => rand(201, 300), |
|
| 302 | - 'message' => 'test three', |
|
| 303 | - 'type' => 'type one', |
|
| 304 | - ], |
|
| 305 | - [ |
|
| 306 | - 'id' => rand(301, 400), |
|
| 307 | - 'message' => 'test four', |
|
| 308 | - 'type' => 'type one', |
|
| 309 | - ], |
|
| 310 | - ]; |
|
| 311 | - |
|
| 312 | - array_walk($testData, [$this, 'insertData']); |
|
| 313 | - |
|
| 314 | - $repository = new MysqlActivityRepository(self::$connection); |
|
| 315 | - $data = $repository->getActivitiesByType('type one', 2, 1); |
|
| 316 | - |
|
| 317 | - $this->assertNotFalse($data); |
|
| 318 | - $this->assertInternalType('array', $data); |
|
| 319 | - $this->assertCount(2, $data); |
|
| 320 | - |
|
| 321 | - $testData = array_filter($testData, function ($row) { |
|
| 322 | - return ($row['type'] == 'type one'); |
|
| 323 | - }); |
|
| 324 | - $testData = array_values($testData); |
|
| 325 | - $testData = array_slice($testData, 1, 2); |
|
| 326 | - |
|
| 327 | - foreach ($testData as $key => $testRow) { |
|
| 328 | - $this->assertInternalType('array', $testRow); |
|
| 329 | - $this->assertArraySubset($testRow, $data[$key]); |
|
| 330 | - } |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - public function testGetActivitiesByTypeRangeFailure() |
|
| 334 | - { |
|
| 335 | - $testData = [ |
|
| 336 | - [ |
|
| 337 | - 'id' => rand(1, 100), |
|
| 338 | - 'message' => 'test one', |
|
| 339 | - 'type' => 'type one', |
|
| 340 | - ], |
|
| 341 | - [ |
|
| 342 | - 'id' => rand(101, 200), |
|
| 343 | - 'message' => 'test two', |
|
| 344 | - 'type' => 'type one', |
|
| 345 | - ], |
|
| 346 | - [ |
|
| 347 | - 'id' => rand(201, 300), |
|
| 348 | - 'message' => 'test three', |
|
| 349 | - 'type' => 'type one', |
|
| 350 | - ], |
|
| 351 | - ]; |
|
| 352 | - |
|
| 353 | - array_walk($testData, [$this, 'insertData']); |
|
| 354 | - |
|
| 355 | - $repository = new MysqlActivityRepository(self::$connection); |
|
| 356 | - $data = $repository->getActivitiesByType('type two', 2, 1); |
|
| 357 | - |
|
| 358 | - $this->assertEmpty($data); |
|
| 359 | - $this->assertInternalType('array', $data); |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - public function testGetActivitiesByTypeCount() |
|
| 363 | - { |
|
| 364 | - $testData = [ |
|
| 365 | - [ |
|
| 366 | - 'id' => rand(1, 100), |
|
| 367 | - 'message' => 'test one', |
|
| 368 | - 'type' => 'type one', |
|
| 369 | - ], |
|
| 370 | - [ |
|
| 371 | - 'id' => rand(101, 200), |
|
| 372 | - 'message' => 'test two', |
|
| 373 | - 'type' => 'type two', |
|
| 374 | - ], |
|
| 375 | - [ |
|
| 376 | - 'id' => rand(201, 300), |
|
| 377 | - 'message' => 'test three', |
|
| 378 | - 'type' => 'type one', |
|
| 379 | - ], |
|
| 380 | - ]; |
|
| 381 | - |
|
| 382 | - array_walk($testData, [$this, 'insertData']); |
|
| 383 | - |
|
| 384 | - $repository = new MysqlActivityRepository(self::$connection); |
|
| 385 | - $data = $repository->getActivitiesByTypeCount('type one'); |
|
| 386 | - |
|
| 387 | - $testData = array_filter($testData, function ($row) { |
|
| 388 | - return ($row['type'] == 'type one'); |
|
| 389 | - }); |
|
| 390 | - |
|
| 391 | - $this->assertNotFalse($data); |
|
| 392 | - $this->assertStringMatchesFormat('%d', $data); |
|
| 393 | - $this->assertEquals(count($testData), $data); |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - public function testGetActivitiesByTypeCountEmpty() |
|
| 397 | - { |
|
| 398 | - $testData = [ |
|
| 399 | - [ |
|
| 400 | - 'id' => rand(1, 100), |
|
| 401 | - 'message' => 'test one', |
|
| 402 | - 'type' => 'type one', |
|
| 403 | - ], |
|
| 404 | - [ |
|
| 405 | - 'id' => rand(101, 200), |
|
| 406 | - 'message' => 'test two', |
|
| 407 | - 'type' => 'type one', |
|
| 408 | - ], |
|
| 409 | - ]; |
|
| 410 | - |
|
| 411 | - array_walk($testData, [$this, 'insertData']); |
|
| 412 | - |
|
| 413 | - $repository = new MysqlActivityRepository(self::$connection); |
|
| 414 | - $data = $repository->getActivitiesByTypeCount('type two'); |
|
| 415 | - |
|
| 416 | - $this->assertNotFalse($data); |
|
| 417 | - $this->assertStringMatchesFormat('%d', $data); |
|
| 418 | - $this->assertEquals('0', $data); |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - protected function tearDown() |
|
| 422 | - { |
|
| 423 | - self::$connection->getDefault()->perform("DELETE FROM `jpemeric_stream`.`activity`"); |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - public static function tearDownAfterClass() |
|
| 427 | - { |
|
| 428 | - self::$connection->getDefault()->disconnect(); |
|
| 429 | - unlink('jpemeric_stream.db'); |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - protected function insertData(array $data) |
|
| 433 | - { |
|
| 434 | - $defaultData = [ |
|
| 435 | - 'id' => null, |
|
| 436 | - 'message' => '', |
|
| 437 | - 'message_long' => '', |
|
| 438 | - 'datetime' => '', |
|
| 439 | - 'metadata' => '', |
|
| 440 | - 'type' => '', |
|
| 441 | - 'type_id' => '', |
|
| 442 | - ]; |
|
| 443 | - |
|
| 444 | - $data = array_merge($defaultData, $data); |
|
| 445 | - |
|
| 446 | - 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 | + array_walk($testData, [$this, 'insertData']); |
|
| 114 | + |
|
| 115 | + $repository = new MysqlActivityRepository(self::$connection); |
|
| 116 | + $data = $repository->getActivities(); |
|
| 117 | + |
|
| 118 | + $this->assertNotFalse($data); |
|
| 119 | + $this->assertInternalType('array', $data); |
|
| 120 | + foreach ($testData as $key => $testRow) { |
|
| 121 | + $this->assertInternalType('array', $testRow); |
|
| 122 | + $this->assertArraySubset($testRow, $data[$key]); |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + public function testGetActivitiesFailure() |
|
| 127 | + { |
|
| 128 | + $repository = new MysqlActivityRepository(self::$connection); |
|
| 129 | + $data = $repository->getActivities(); |
|
| 130 | + |
|
| 131 | + $this->assertEmpty($data); |
|
| 132 | + $this->assertInternalType('array', $data); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + public function testGetActivitiesRange() |
|
| 136 | + { |
|
| 137 | + $testData = [ |
|
| 138 | + [ |
|
| 139 | + 'id' => rand(1, 100), |
|
| 140 | + 'message' => 'test one', |
|
| 141 | + ], |
|
| 142 | + [ |
|
| 143 | + 'id' => rand(101, 200), |
|
| 144 | + 'message' => 'test two', |
|
| 145 | + ], |
|
| 146 | + [ |
|
| 147 | + 'id' => rand(201, 300), |
|
| 148 | + 'message' => 'test three', |
|
| 149 | + ], |
|
| 150 | + ]; |
|
| 151 | + |
|
| 152 | + array_walk($testData, [$this, 'insertData']); |
|
| 153 | + |
|
| 154 | + $repository = new MysqlActivityRepository(self::$connection); |
|
| 155 | + $data = $repository->getActivities(2, 1); |
|
| 156 | + |
|
| 157 | + $this->assertNotFalse($data); |
|
| 158 | + $this->assertInternalType('array', $data); |
|
| 159 | + $this->assertCount(2, $data); |
|
| 160 | + |
|
| 161 | + $testData = array_slice($testData, 1, 2); |
|
| 162 | + |
|
| 163 | + foreach ($testData as $key => $testRow) { |
|
| 164 | + $this->assertInternalType('array', $testRow); |
|
| 165 | + $this->assertArraySubset($testRow, $data[$key]); |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + public function testGetActivitiesRangeFailure() |
|
| 170 | + { |
|
| 171 | + $testData = [ |
|
| 172 | + [ |
|
| 173 | + 'id' => rand(1, 100), |
|
| 174 | + 'message' => 'test one', |
|
| 175 | + ], |
|
| 176 | + [ |
|
| 177 | + 'id' => rand(101, 200), |
|
| 178 | + 'message' => 'test two', |
|
| 179 | + ], |
|
| 180 | + ]; |
|
| 181 | + |
|
| 182 | + array_walk($testData, [$this, 'insertData']); |
|
| 183 | + |
|
| 184 | + $repository = new MysqlActivityRepository(self::$connection); |
|
| 185 | + $data = $repository->getActivities(1, 3); |
|
| 186 | + |
|
| 187 | + $this->assertEmpty($data); |
|
| 188 | + $this->assertInternalType('array', $data); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + public function testGetActivitiesCount() |
|
| 192 | + { |
|
| 193 | + $testData = [ |
|
| 194 | + [ |
|
| 195 | + 'id' => rand(1, 100), |
|
| 196 | + 'message' => 'test one', |
|
| 197 | + ], |
|
| 198 | + [ |
|
| 199 | + 'id' => rand(101, 200), |
|
| 200 | + 'message' => 'test two', |
|
| 201 | + ], |
|
| 202 | + ]; |
|
| 203 | + |
|
| 204 | + array_walk($testData, [$this, 'insertData']); |
|
| 205 | + |
|
| 206 | + $repository = new MysqlActivityRepository(self::$connection); |
|
| 207 | + $data = $repository->getActivitiesCount(); |
|
| 208 | + |
|
| 209 | + $this->assertNotFalse($data); |
|
| 210 | + $this->assertStringMatchesFormat('%d', $data); |
|
| 211 | + $this->assertEquals(count($testData), $data); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + public function testGetActivitiesCountEmpty() |
|
| 215 | + { |
|
| 216 | + $repository = new MysqlActivityRepository(self::$connection); |
|
| 217 | + $data = $repository->getActivitiesCount(); |
|
| 218 | + |
|
| 219 | + $this->assertNotFalse($data); |
|
| 220 | + $this->assertStringMatchesFormat('%d', $data); |
|
| 221 | + $this->assertEquals('0', $data); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + public function testGetActivitiesByType() |
|
| 225 | + { |
|
| 226 | + $testData = [ |
|
| 227 | + [ |
|
| 228 | + 'id' => rand(1, 100), |
|
| 229 | + 'message' => 'test one', |
|
| 230 | + 'type' => 'type one', |
|
| 231 | + ], |
|
| 232 | + [ |
|
| 233 | + 'id' => rand(101, 200), |
|
| 234 | + 'message' => 'test two', |
|
| 235 | + 'type' => 'type two', |
|
| 236 | + ], |
|
| 237 | + [ |
|
| 238 | + 'id' => rand(201, 300), |
|
| 239 | + 'message' => 'test three', |
|
| 240 | + 'type' => 'type one', |
|
| 241 | + ], |
|
| 242 | + ]; |
|
| 243 | + |
|
| 244 | + array_walk($testData, [$this, 'insertData']); |
|
| 245 | + |
|
| 246 | + $repository = new MysqlActivityRepository(self::$connection); |
|
| 247 | + $data = $repository->getActivitiesByType('type one'); |
|
| 248 | + |
|
| 249 | + $this->assertNotFalse($data); |
|
| 250 | + $this->assertInternalType('array', $data); |
|
| 251 | + |
|
| 252 | + $testData = array_filter($testData, function ($row) { |
|
| 253 | + return ($row['type'] == 'type one'); |
|
| 254 | + }); |
|
| 255 | + $testData = array_values($testData); |
|
| 256 | + |
|
| 257 | + foreach ($testData as $key => $testRow) { |
|
| 258 | + $this->assertInternalType('array', $testRow); |
|
| 259 | + $this->assertArraySubset($testRow, $data[$key]); |
|
| 260 | + } |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + public function testGetActivitiesByTypeFailure() |
|
| 264 | + { |
|
| 265 | + $testData = [ |
|
| 266 | + [ |
|
| 267 | + 'id' => rand(1, 100), |
|
| 268 | + 'message' => 'test one', |
|
| 269 | + 'type' => 'type one', |
|
| 270 | + ], |
|
| 271 | + [ |
|
| 272 | + 'id' => rand(101, 200), |
|
| 273 | + 'message' => 'test two', |
|
| 274 | + 'type' => 'type one', |
|
| 275 | + ], |
|
| 276 | + ]; |
|
| 277 | + |
|
| 278 | + array_walk($testData, [$this, 'insertData']); |
|
| 279 | + |
|
| 280 | + $repository = new MysqlActivityRepository(self::$connection); |
|
| 281 | + $data = $repository->getActivitiesByType('type two'); |
|
| 282 | + |
|
| 283 | + $this->assertEmpty($data); |
|
| 284 | + $this->assertInternalType('array', $data); |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + public function testGetActivitiesByTypeRange() |
|
| 288 | + { |
|
| 289 | + $testData = [ |
|
| 290 | + [ |
|
| 291 | + 'id' => rand(1, 100), |
|
| 292 | + 'message' => 'test one', |
|
| 293 | + 'type' => 'type one', |
|
| 294 | + ], |
|
| 295 | + [ |
|
| 296 | + 'id' => rand(101, 200), |
|
| 297 | + 'message' => 'test two', |
|
| 298 | + 'type' => 'type two', |
|
| 299 | + ], |
|
| 300 | + [ |
|
| 301 | + 'id' => rand(201, 300), |
|
| 302 | + 'message' => 'test three', |
|
| 303 | + 'type' => 'type one', |
|
| 304 | + ], |
|
| 305 | + [ |
|
| 306 | + 'id' => rand(301, 400), |
|
| 307 | + 'message' => 'test four', |
|
| 308 | + 'type' => 'type one', |
|
| 309 | + ], |
|
| 310 | + ]; |
|
| 311 | + |
|
| 312 | + array_walk($testData, [$this, 'insertData']); |
|
| 313 | + |
|
| 314 | + $repository = new MysqlActivityRepository(self::$connection); |
|
| 315 | + $data = $repository->getActivitiesByType('type one', 2, 1); |
|
| 316 | + |
|
| 317 | + $this->assertNotFalse($data); |
|
| 318 | + $this->assertInternalType('array', $data); |
|
| 319 | + $this->assertCount(2, $data); |
|
| 320 | + |
|
| 321 | + $testData = array_filter($testData, function ($row) { |
|
| 322 | + return ($row['type'] == 'type one'); |
|
| 323 | + }); |
|
| 324 | + $testData = array_values($testData); |
|
| 325 | + $testData = array_slice($testData, 1, 2); |
|
| 326 | + |
|
| 327 | + foreach ($testData as $key => $testRow) { |
|
| 328 | + $this->assertInternalType('array', $testRow); |
|
| 329 | + $this->assertArraySubset($testRow, $data[$key]); |
|
| 330 | + } |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + public function testGetActivitiesByTypeRangeFailure() |
|
| 334 | + { |
|
| 335 | + $testData = [ |
|
| 336 | + [ |
|
| 337 | + 'id' => rand(1, 100), |
|
| 338 | + 'message' => 'test one', |
|
| 339 | + 'type' => 'type one', |
|
| 340 | + ], |
|
| 341 | + [ |
|
| 342 | + 'id' => rand(101, 200), |
|
| 343 | + 'message' => 'test two', |
|
| 344 | + 'type' => 'type one', |
|
| 345 | + ], |
|
| 346 | + [ |
|
| 347 | + 'id' => rand(201, 300), |
|
| 348 | + 'message' => 'test three', |
|
| 349 | + 'type' => 'type one', |
|
| 350 | + ], |
|
| 351 | + ]; |
|
| 352 | + |
|
| 353 | + array_walk($testData, [$this, 'insertData']); |
|
| 354 | + |
|
| 355 | + $repository = new MysqlActivityRepository(self::$connection); |
|
| 356 | + $data = $repository->getActivitiesByType('type two', 2, 1); |
|
| 357 | + |
|
| 358 | + $this->assertEmpty($data); |
|
| 359 | + $this->assertInternalType('array', $data); |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + public function testGetActivitiesByTypeCount() |
|
| 363 | + { |
|
| 364 | + $testData = [ |
|
| 365 | + [ |
|
| 366 | + 'id' => rand(1, 100), |
|
| 367 | + 'message' => 'test one', |
|
| 368 | + 'type' => 'type one', |
|
| 369 | + ], |
|
| 370 | + [ |
|
| 371 | + 'id' => rand(101, 200), |
|
| 372 | + 'message' => 'test two', |
|
| 373 | + 'type' => 'type two', |
|
| 374 | + ], |
|
| 375 | + [ |
|
| 376 | + 'id' => rand(201, 300), |
|
| 377 | + 'message' => 'test three', |
|
| 378 | + 'type' => 'type one', |
|
| 379 | + ], |
|
| 380 | + ]; |
|
| 381 | + |
|
| 382 | + array_walk($testData, [$this, 'insertData']); |
|
| 383 | + |
|
| 384 | + $repository = new MysqlActivityRepository(self::$connection); |
|
| 385 | + $data = $repository->getActivitiesByTypeCount('type one'); |
|
| 386 | + |
|
| 387 | + $testData = array_filter($testData, function ($row) { |
|
| 388 | + return ($row['type'] == 'type one'); |
|
| 389 | + }); |
|
| 390 | + |
|
| 391 | + $this->assertNotFalse($data); |
|
| 392 | + $this->assertStringMatchesFormat('%d', $data); |
|
| 393 | + $this->assertEquals(count($testData), $data); |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + public function testGetActivitiesByTypeCountEmpty() |
|
| 397 | + { |
|
| 398 | + $testData = [ |
|
| 399 | + [ |
|
| 400 | + 'id' => rand(1, 100), |
|
| 401 | + 'message' => 'test one', |
|
| 402 | + 'type' => 'type one', |
|
| 403 | + ], |
|
| 404 | + [ |
|
| 405 | + 'id' => rand(101, 200), |
|
| 406 | + 'message' => 'test two', |
|
| 407 | + 'type' => 'type one', |
|
| 408 | + ], |
|
| 409 | + ]; |
|
| 410 | + |
|
| 411 | + array_walk($testData, [$this, 'insertData']); |
|
| 412 | + |
|
| 413 | + $repository = new MysqlActivityRepository(self::$connection); |
|
| 414 | + $data = $repository->getActivitiesByTypeCount('type two'); |
|
| 415 | + |
|
| 416 | + $this->assertNotFalse($data); |
|
| 417 | + $this->assertStringMatchesFormat('%d', $data); |
|
| 418 | + $this->assertEquals('0', $data); |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + protected function tearDown() |
|
| 422 | + { |
|
| 423 | + self::$connection->getDefault()->perform("DELETE FROM `jpemeric_stream`.`activity`"); |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + public static function tearDownAfterClass() |
|
| 427 | + { |
|
| 428 | + self::$connection->getDefault()->disconnect(); |
|
| 429 | + unlink('jpemeric_stream.db'); |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + protected function insertData(array $data) |
|
| 433 | + { |
|
| 434 | + $defaultData = [ |
|
| 435 | + 'id' => null, |
|
| 436 | + 'message' => '', |
|
| 437 | + 'message_long' => '', |
|
| 438 | + 'datetime' => '', |
|
| 439 | + 'metadata' => '', |
|
| 440 | + 'type' => '', |
|
| 441 | + 'type_id' => '', |
|
| 442 | + ]; |
|
| 443 | + |
|
| 444 | + $data = array_merge($defaultData, $data); |
|
| 445 | + |
|
| 446 | + return self::$connection->getDefault()->perform(" |
|
| 447 | 447 | INSERT INTO `jpemeric_stream`.`activity` |
| 448 | 448 | (id, message, message_long, datetime, metadata, type, type_id) |
| 449 | 449 | VALUES |
| 450 | 450 | (:id, :message, :message_long, :datetime, :metadata, :type, :type_id)", |
| 451 | - $data |
|
| 452 | - ); |
|
| 453 | - } |
|
| 451 | + $data |
|
| 452 | + ); |
|
| 453 | + } |
|
| 454 | 454 | } |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | )" |
| 30 | 30 | ); |
| 31 | 31 | |
| 32 | - self::$connection = new ConnectionLocator(function () use ($extendedPdo) { |
|
| 32 | + self::$connection = new ConnectionLocator(function() use ($extendedPdo) { |
|
| 33 | 33 | return $extendedPdo; |
| 34 | 34 | }); |
| 35 | 35 | } |
@@ -249,7 +249,7 @@ discard block |
||
| 249 | 249 | $this->assertNotFalse($data); |
| 250 | 250 | $this->assertInternalType('array', $data); |
| 251 | 251 | |
| 252 | - $testData = array_filter($testData, function ($row) { |
|
| 252 | + $testData = array_filter($testData, function($row) { |
|
| 253 | 253 | return ($row['type'] == 'type one'); |
| 254 | 254 | }); |
| 255 | 255 | $testData = array_values($testData); |
@@ -318,7 +318,7 @@ discard block |
||
| 318 | 318 | $this->assertInternalType('array', $data); |
| 319 | 319 | $this->assertCount(2, $data); |
| 320 | 320 | |
| 321 | - $testData = array_filter($testData, function ($row) { |
|
| 321 | + $testData = array_filter($testData, function($row) { |
|
| 322 | 322 | return ($row['type'] == 'type one'); |
| 323 | 323 | }); |
| 324 | 324 | $testData = array_values($testData); |
@@ -384,7 +384,7 @@ discard block |
||
| 384 | 384 | $repository = new MysqlActivityRepository(self::$connection); |
| 385 | 385 | $data = $repository->getActivitiesByTypeCount('type one'); |
| 386 | 386 | |
| 387 | - $testData = array_filter($testData, function ($row) { |
|
| 387 | + $testData = array_filter($testData, function($row) { |
|
| 388 | 388 | return ($row['type'] == 'type one'); |
| 389 | 389 | }); |
| 390 | 390 | |
@@ -9,13 +9,13 @@ discard block |
||
| 9 | 9 | class MysqlChangelogRepositoryTest 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`.`changelog` ( |
| 20 | 20 | `id` integer PRIMARY KEY AUTOINCREMENT, |
| 21 | 21 | `hash` char(40) NOT NULL, |
@@ -27,171 +27,171 @@ 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 testIsInstanceOfChangelogRepository() |
|
| 38 | - { |
|
| 39 | - $repository = new MysqlChangelogRepository(self::$connection); |
|
| 40 | - |
|
| 41 | - $this->assertInstanceOf( |
|
| 42 | - 'Jacobemerick\Web\Domain\Stream\Changelog\MysqlChangelogRepository', |
|
| 43 | - $repository |
|
| 44 | - ); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - public function testImplementsChangelogInterface() |
|
| 48 | - { |
|
| 49 | - $repository = new MysqlChangelogRepository(self::$connection); |
|
| 50 | - |
|
| 51 | - $this->assertInstanceOf( |
|
| 52 | - 'Jacobemerick\Web\Domain\Stream\Changelog\ChangelogRepositoryInterface', |
|
| 53 | - $repository |
|
| 54 | - ); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - public function testConstructSetsConnections() |
|
| 58 | - { |
|
| 59 | - $respository = new MysqlChangelogRepository(self::$connection); |
|
| 60 | - |
|
| 61 | - $this->assertAttributeSame( |
|
| 62 | - self::$connection, |
|
| 63 | - 'connections', |
|
| 64 | - $respository |
|
| 65 | - ); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - public function testGetChanges() |
|
| 69 | - { |
|
| 70 | - $testData = [ |
|
| 71 | - [ |
|
| 72 | - 'id' => rand(1, 100), |
|
| 73 | - 'message' => 'test one', |
|
| 74 | - ], |
|
| 75 | - [ |
|
| 76 | - 'id' => rand(101, 200), |
|
| 77 | - 'message' => 'test two', |
|
| 78 | - ], |
|
| 79 | - ]; |
|
| 80 | - |
|
| 81 | - array_walk($testData, [$this, 'insertData']); |
|
| 82 | - |
|
| 83 | - $repository = new MysqlChangelogRepository(self::$connection); |
|
| 84 | - $data = $repository->getChanges(); |
|
| 85 | - |
|
| 86 | - $this->assertNotFalse($data); |
|
| 87 | - $this->assertInternalType('array', $data); |
|
| 88 | - foreach ($testData as $key => $testRow) { |
|
| 89 | - $this->assertInternalType('array', $testRow); |
|
| 90 | - $this->assertArraySubset($testRow, $data[$key]); |
|
| 91 | - $this->assertArrayHasKey('id', $data[$key]); |
|
| 92 | - $this->assertArrayHasKey('message', $data[$key]); |
|
| 93 | - $this->assertArrayHasKey('message_short', $data[$key]); |
|
| 94 | - $this->assertArrayHasKey('datetime', $data[$key]); |
|
| 95 | - $this->assertArrayHasKey('commit_link', $data[$key]); |
|
| 96 | - } |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - public function testGetChangesFailure() |
|
| 100 | - { |
|
| 101 | - $repository = new MysqlChangelogRepository(self::$connection); |
|
| 102 | - $data = $repository->getChanges(); |
|
| 103 | - |
|
| 104 | - $this->assertEmpty($data); |
|
| 105 | - $this->assertInternalType('array', $data); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - public function testGetChangesRange() |
|
| 109 | - { |
|
| 110 | - $testData = [ |
|
| 111 | - [ |
|
| 112 | - 'id' => rand(1, 100), |
|
| 113 | - 'message' => 'test one', |
|
| 114 | - ], |
|
| 115 | - [ |
|
| 116 | - 'id' => rand(101, 200), |
|
| 117 | - 'message' => 'test two', |
|
| 118 | - ], |
|
| 119 | - [ |
|
| 120 | - 'id' => rand(201, 300), |
|
| 121 | - 'message' => 'test three', |
|
| 122 | - ], |
|
| 123 | - ]; |
|
| 124 | - |
|
| 125 | - array_walk($testData, [$this, 'insertData']); |
|
| 126 | - |
|
| 127 | - $repository = new MysqlChangelogRepository(self::$connection); |
|
| 128 | - $data = $repository->getChanges(2, 1); |
|
| 129 | - |
|
| 130 | - $this->assertNotFalse($data); |
|
| 131 | - $this->assertInternalType('array', $data); |
|
| 132 | - $this->assertCount(2, $data); |
|
| 133 | - |
|
| 134 | - $testData = array_slice($testData, 1, 2); |
|
| 135 | - |
|
| 136 | - foreach ($testData as $key => $testRow) { |
|
| 137 | - $this->assertInternalType('array', $testRow); |
|
| 138 | - $this->assertArraySubset($testRow, $data[$key]); |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - public function testGetChangesRangeFailure() |
|
| 143 | - { |
|
| 144 | - $testData = [ |
|
| 145 | - [ |
|
| 146 | - 'id' => rand(1, 100), |
|
| 147 | - 'message' => 'test one', |
|
| 148 | - ], |
|
| 149 | - [ |
|
| 150 | - 'id' => rand(101, 200), |
|
| 151 | - 'message' => 'test two', |
|
| 152 | - ], |
|
| 153 | - ]; |
|
| 154 | - |
|
| 155 | - array_walk($testData, [$this, 'insertData']); |
|
| 156 | - |
|
| 157 | - $repository = new MysqlChangelogRepository(self::$connection); |
|
| 158 | - $data = $repository->getChanges(1, 3); |
|
| 159 | - |
|
| 160 | - $this->assertEmpty($data); |
|
| 161 | - $this->assertInternalType('array', $data); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - protected function tearDown() |
|
| 165 | - { |
|
| 166 | - self::$connection->getDefault()->perform("DELETE FROM `jpemeric_stream`.`changelog`"); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - public static function tearDownAfterClass() |
|
| 170 | - { |
|
| 171 | - self::$connection->getDefault()->disconnect(); |
|
| 172 | - unlink('jpemeric_stream.db'); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - protected function insertData(array $data) |
|
| 176 | - { |
|
| 177 | - $defaultData = [ |
|
| 178 | - 'id' => null, |
|
| 179 | - 'hash' => '', |
|
| 180 | - 'message' => null, |
|
| 181 | - 'message_short' => null, |
|
| 182 | - 'datetime' => '', |
|
| 183 | - 'author' => '', |
|
| 184 | - 'commit_link' => '', |
|
| 185 | - ]; |
|
| 186 | - |
|
| 187 | - $data = array_merge($defaultData, $data); |
|
| 188 | - |
|
| 189 | - return self::$connection->getDefault()->perform(" |
|
| 30 | + ); |
|
| 31 | + |
|
| 32 | + self::$connection = new ConnectionLocator(function () use ($extendedPdo) { |
|
| 33 | + return $extendedPdo; |
|
| 34 | + }); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + public function testIsInstanceOfChangelogRepository() |
|
| 38 | + { |
|
| 39 | + $repository = new MysqlChangelogRepository(self::$connection); |
|
| 40 | + |
|
| 41 | + $this->assertInstanceOf( |
|
| 42 | + 'Jacobemerick\Web\Domain\Stream\Changelog\MysqlChangelogRepository', |
|
| 43 | + $repository |
|
| 44 | + ); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + public function testImplementsChangelogInterface() |
|
| 48 | + { |
|
| 49 | + $repository = new MysqlChangelogRepository(self::$connection); |
|
| 50 | + |
|
| 51 | + $this->assertInstanceOf( |
|
| 52 | + 'Jacobemerick\Web\Domain\Stream\Changelog\ChangelogRepositoryInterface', |
|
| 53 | + $repository |
|
| 54 | + ); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + public function testConstructSetsConnections() |
|
| 58 | + { |
|
| 59 | + $respository = new MysqlChangelogRepository(self::$connection); |
|
| 60 | + |
|
| 61 | + $this->assertAttributeSame( |
|
| 62 | + self::$connection, |
|
| 63 | + 'connections', |
|
| 64 | + $respository |
|
| 65 | + ); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + public function testGetChanges() |
|
| 69 | + { |
|
| 70 | + $testData = [ |
|
| 71 | + [ |
|
| 72 | + 'id' => rand(1, 100), |
|
| 73 | + 'message' => 'test one', |
|
| 74 | + ], |
|
| 75 | + [ |
|
| 76 | + 'id' => rand(101, 200), |
|
| 77 | + 'message' => 'test two', |
|
| 78 | + ], |
|
| 79 | + ]; |
|
| 80 | + |
|
| 81 | + array_walk($testData, [$this, 'insertData']); |
|
| 82 | + |
|
| 83 | + $repository = new MysqlChangelogRepository(self::$connection); |
|
| 84 | + $data = $repository->getChanges(); |
|
| 85 | + |
|
| 86 | + $this->assertNotFalse($data); |
|
| 87 | + $this->assertInternalType('array', $data); |
|
| 88 | + foreach ($testData as $key => $testRow) { |
|
| 89 | + $this->assertInternalType('array', $testRow); |
|
| 90 | + $this->assertArraySubset($testRow, $data[$key]); |
|
| 91 | + $this->assertArrayHasKey('id', $data[$key]); |
|
| 92 | + $this->assertArrayHasKey('message', $data[$key]); |
|
| 93 | + $this->assertArrayHasKey('message_short', $data[$key]); |
|
| 94 | + $this->assertArrayHasKey('datetime', $data[$key]); |
|
| 95 | + $this->assertArrayHasKey('commit_link', $data[$key]); |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + public function testGetChangesFailure() |
|
| 100 | + { |
|
| 101 | + $repository = new MysqlChangelogRepository(self::$connection); |
|
| 102 | + $data = $repository->getChanges(); |
|
| 103 | + |
|
| 104 | + $this->assertEmpty($data); |
|
| 105 | + $this->assertInternalType('array', $data); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + public function testGetChangesRange() |
|
| 109 | + { |
|
| 110 | + $testData = [ |
|
| 111 | + [ |
|
| 112 | + 'id' => rand(1, 100), |
|
| 113 | + 'message' => 'test one', |
|
| 114 | + ], |
|
| 115 | + [ |
|
| 116 | + 'id' => rand(101, 200), |
|
| 117 | + 'message' => 'test two', |
|
| 118 | + ], |
|
| 119 | + [ |
|
| 120 | + 'id' => rand(201, 300), |
|
| 121 | + 'message' => 'test three', |
|
| 122 | + ], |
|
| 123 | + ]; |
|
| 124 | + |
|
| 125 | + array_walk($testData, [$this, 'insertData']); |
|
| 126 | + |
|
| 127 | + $repository = new MysqlChangelogRepository(self::$connection); |
|
| 128 | + $data = $repository->getChanges(2, 1); |
|
| 129 | + |
|
| 130 | + $this->assertNotFalse($data); |
|
| 131 | + $this->assertInternalType('array', $data); |
|
| 132 | + $this->assertCount(2, $data); |
|
| 133 | + |
|
| 134 | + $testData = array_slice($testData, 1, 2); |
|
| 135 | + |
|
| 136 | + foreach ($testData as $key => $testRow) { |
|
| 137 | + $this->assertInternalType('array', $testRow); |
|
| 138 | + $this->assertArraySubset($testRow, $data[$key]); |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + public function testGetChangesRangeFailure() |
|
| 143 | + { |
|
| 144 | + $testData = [ |
|
| 145 | + [ |
|
| 146 | + 'id' => rand(1, 100), |
|
| 147 | + 'message' => 'test one', |
|
| 148 | + ], |
|
| 149 | + [ |
|
| 150 | + 'id' => rand(101, 200), |
|
| 151 | + 'message' => 'test two', |
|
| 152 | + ], |
|
| 153 | + ]; |
|
| 154 | + |
|
| 155 | + array_walk($testData, [$this, 'insertData']); |
|
| 156 | + |
|
| 157 | + $repository = new MysqlChangelogRepository(self::$connection); |
|
| 158 | + $data = $repository->getChanges(1, 3); |
|
| 159 | + |
|
| 160 | + $this->assertEmpty($data); |
|
| 161 | + $this->assertInternalType('array', $data); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + protected function tearDown() |
|
| 165 | + { |
|
| 166 | + self::$connection->getDefault()->perform("DELETE FROM `jpemeric_stream`.`changelog`"); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + public static function tearDownAfterClass() |
|
| 170 | + { |
|
| 171 | + self::$connection->getDefault()->disconnect(); |
|
| 172 | + unlink('jpemeric_stream.db'); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + protected function insertData(array $data) |
|
| 176 | + { |
|
| 177 | + $defaultData = [ |
|
| 178 | + 'id' => null, |
|
| 179 | + 'hash' => '', |
|
| 180 | + 'message' => null, |
|
| 181 | + 'message_short' => null, |
|
| 182 | + 'datetime' => '', |
|
| 183 | + 'author' => '', |
|
| 184 | + 'commit_link' => '', |
|
| 185 | + ]; |
|
| 186 | + |
|
| 187 | + $data = array_merge($defaultData, $data); |
|
| 188 | + |
|
| 189 | + return self::$connection->getDefault()->perform(" |
|
| 190 | 190 | INSERT INTO `jpemeric_stream`.`changelog` |
| 191 | 191 | (id, hash, message, message_short, datetime, author, commit_link) |
| 192 | 192 | VALUES |
| 193 | 193 | (:id, :hash, :message, :message_short, :datetime, :author, :commit_link)", |
| 194 | - $data |
|
| 195 | - ); |
|
| 196 | - } |
|
| 194 | + $data |
|
| 195 | + ); |
|
| 196 | + } |
|
| 197 | 197 | } |