-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathRecordAdditionalMetricTest.php
More file actions
66 lines (51 loc) · 2.1 KB
/
RecordAdditionalMetricTest.php
File metadata and controls
66 lines (51 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
use DirectoryTree\Metrics\Jobs\RecordMetric;
use DirectoryTree\Metrics\Metric;
use DirectoryTree\Metrics\MetricData;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
beforeEach(function () {
Metric::query()->delete();
if (! Schema::hasColumn('metrics', 'payload')) {
Schema::table('metrics', function (Blueprint $table) {
$table->json('payload');
});
}
});
afterEach(function () {
if (Schema::hasColumn('metrics', 'payload')) {
Schema::table('metrics', function (Blueprint $table) {
$table->dropColumn('payload');
});
}
});
it('creates metrics with json payload attributes', function () {
$data = new MetricData('page_views_with_json', additional: [
'payload' => [
'a' => 1,
'b' => 'test',
],
]);
(new RecordMetric($data))->handle();
(new RecordMetric($data))->handle();
// Count may be 1 (SQLite dedupes by JSON string) or 2 (MySQL compares JSON differently).
expect(Metric::where('name', 'page_views_with_json')->count())->toBeGreaterThanOrEqual(1);
$metric = Metric::where('name', 'page_views_with_json')->first();
// Cast the payload attribute manually since we added the column dynamically
$metric->mergeCasts(['payload' => 'array']);
expect($metric->payload)->toBe(['a' => 1, 'b' => 'test']);
// Total value across all matching records should equal 2 regardless of DB.
expect(Metric::where('name', 'page_views_with_json')->sum('value'))->toEqual(2);
});
it('differentiates metrics by json payload content', function () {
$data1 = new MetricData('page_views_by_source', additional: [
'payload' => ['source' => 'google'],
]);
$data2 = new MetricData('page_views_by_source', additional: [
'payload' => ['source' => 'facebook'],
]);
(new RecordMetric($data1))->handle();
(new RecordMetric($data2))->handle();
// Count may be 2 (SQLite) or more (MySQL) depending on JSON comparison behavior.
expect(Metric::where('name', 'page_views_by_source')->count())->toBeGreaterThanOrEqual(2);
});