Skip to content

Commit e488535

Browse files
committed
Add tests
1 parent b82c2f8 commit e488535

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

tests/Jobs/RecordMetricTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use DirectoryTree\Metrics\Metric;
66
use DirectoryTree\Metrics\MetricData;
77
use DirectoryTree\Metrics\Tests\User;
8+
use Illuminate\Database\Schema\Blueprint;
9+
use Illuminate\Support\Facades\Schema;
810

911
it('can record a single metric', function () {
1012
$metric = new MetricData('page_views');
@@ -224,3 +226,50 @@
224226

225227
expect($updated->updated_at->isAfter($originalUpdatedAt))->toBeTrue();
226228
});
229+
230+
it('creates metrics with additional attributes', function () {
231+
Schema::table('metrics', function (Blueprint $table) {
232+
$table->string('source')->nullable();
233+
$table->string('country')->nullable();
234+
});
235+
236+
$data = new MetricData('page_views', additional: [
237+
'source' => 'google',
238+
'country' => 'US',
239+
]);
240+
241+
(new RecordMetric($data))->handle();
242+
(new RecordMetric($data))->handle();
243+
244+
$recorded = Metric::first();
245+
246+
expect($recorded->source)->toBe('google');
247+
expect($recorded->country)->toBe('US');
248+
expect($recorded->value)->toBe(2);
249+
});
250+
251+
it('cannot override core attributes with additional attributes', function () {
252+
$data = new MetricData('page_views', additional: [
253+
'name' => 'api_calls',
254+
'category' => 'marketing',
255+
'year' => 2025,
256+
'month' => 1,
257+
'day' => 1,
258+
'measurable_type' => 'App\Models\User',
259+
'measurable_id' => 1,
260+
'value' => 100,
261+
]);
262+
263+
(new RecordMetric($data))->handle();
264+
265+
$recorded = Metric::first();
266+
267+
expect($recorded->name)->toBe('page_views')
268+
->and($recorded->category)->toBeNull()
269+
->and($recorded->year)->toBe(today()->year)
270+
->and($recorded->month)->toBe(today()->month)
271+
->and($recorded->day)->toBe(today()->day)
272+
->and($recorded->measurable_type)->toBeNull()
273+
->and($recorded->measurable_id)->toBeNull()
274+
->and($recorded->value)->toBe(1);
275+
});

0 commit comments

Comments
 (0)