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