1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jacobemerick\Web\Domain\Stream\Activity; |
4
|
|
|
|
5
|
|
|
use Aura\Sql\ConnectionLocator; |
6
|
|
|
use Aura\Sql\ExtendedPdo; |
7
|
|
|
use PHPUnit_Framework_TestCase; |
8
|
|
|
|
9
|
|
|
class MysqlActivityRepositoryTest 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_stream.db` AS `jpemeric_stream`"); |
18
|
|
|
$extendedPdo->exec(" |
19
|
|
|
CREATE TABLE IF NOT EXISTS `jpemeric_stream`.`activity` ( |
20
|
|
|
`id` integer PRIMARY KEY AUTOINCREMENT, |
21
|
|
|
`message` text NOT NULL, |
22
|
|
|
`message_long` text NOT NULL, |
23
|
|
|
`datetime` datetime NOT NULL, |
24
|
|
|
`metadata` text NOT NULL, |
25
|
|
|
`type` varchar(10) NOT NULL, |
26
|
|
|
`type_id` integer NOT NULL, |
27
|
|
|
`created_at` datetime, |
28
|
|
|
`updated_at` datetime |
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
|
INSERT INTO `jpemeric_stream`.`activity` |
448
|
|
|
(id, message, message_long, datetime, metadata, type, type_id) |
449
|
|
|
VALUES |
450
|
|
|
(:id, :message, :message_long, :datetime, :metadata, :type, :type_id)", |
451
|
|
|
$data |
452
|
|
|
); |
453
|
|
|
} |
454
|
|
|
} |
455
|
|
|
|
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.