|
5 | 5 | use DirectoryTree\Metrics\Metric; |
6 | 6 | use DirectoryTree\Metrics\MetricData; |
7 | 7 | use DirectoryTree\Metrics\Tests\User; |
| 8 | +use Illuminate\Database\Schema\Blueprint; |
| 9 | +use Illuminate\Support\Facades\Schema; |
8 | 10 |
|
9 | 11 | it('can record a single metric', function () { |
10 | 12 | $metric = new MetricData('page_views'); |
|
224 | 226 |
|
225 | 227 | expect($updated->updated_at->isAfter($originalUpdatedAt))->toBeTrue(); |
226 | 228 | }); |
| 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