Test Widget model and dashboard itself
MODIFY
app/Models/Widget.php
app/Models/Widget.php
@@ -5,11 +5,13 @@ namespace App\Models;
use App\Exceptions\WidgetInvalidPropertyValueException;
use App\Exceptions\WidgetMissingPropertyException;
use App\Exceptions\WidgetUnknownTypeException;
+use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Widget extends Model
{
+ use HasFactory;
use SoftDeletes;
protected $fillable = [

ADD
database/factories/WidgetFactory.php
+<?php
+
+namespace Database\Factories;
+
+use App\Models\Widget;
+use Illuminate\Database\Eloquent\Factories\Factory;
+
+class WidgetFactory extends Factory
+{
+ protected $model = Widget::class;
+
+ public function definition(): array
+ {
+ return [
+ 'sorting_index' => $this->faker->numberBetween(0, 999),
+ 'properties' => []
+ ];
+ }
+}

ADD
tests/Feature/DashboardTest.php
+<?php
+
+namespace Tests\Feature;
+
+use App\Models\Space;
+use App\Models\User;
+use App\Models\Widget;
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Foundation\Testing\WithFaker;
+use Tests\TestCase;
+
+class DashboardTest extends TestCase
+{
+ public function testZeroWidgets(): void
+ {
+ $user = User::factory()->create();
+ $space = Space::factory()->create();
+
+ $response = $this
+ ->actingAs($user)
+ ->withSession(['space_id' => $space->id])
+ ->get('/dashboard');
+
+ $response->assertStatus(200);
+ }
+
+ public function testBalanceWidget(): void
+ {
+ $user = User::factory()->create();
+ $space = Space::factory()->create();
+
+ Widget::factory()->create([
+ 'user_id' => $user->id,
+ 'type' => 'balance'
+ ]);
+
+ $response = $this
+ ->actingAs($user)
+ ->withSession(['space_id' => $space->id])
+ ->get('/dashboard');
+
+ $response
+ ->assertStatus(200)
+ ->assertSeeText('Balance');
+ }
+
+ public function testSpentWidget(): void
+ {
+ $user = User::factory()->create();
+ $space = Space::factory()->create();
+
+ Widget::factory()->create([
+ 'user_id' => $user->id,
+ 'type' => 'spent',
+ 'properties' => ['period' => 'this_month']
+ ]);
+
+ $response = $this
+ ->actingAs($user)
+ ->withSession(['space_id' => $space->id])
+ ->get('/dashboard');
+
+ $response
+ ->assertStatus(200)
+ ->assertSeeText('Spent this month');
+ }
+}

ADD
tests/Unit/Models/WidgetTest.php
+<?php
+
+namespace Tests\Unit\Models;
+
+use App\Exceptions\WidgetInvalidPropertyValueException;
+use App\Exceptions\WidgetMissingPropertyException;
+use App\Exceptions\WidgetUnknownTypeException;
+use App\Models\User;
+use App\Models\Widget;
+use Tests\TestCase;
+
+class WidgetTest extends TestCase
+{
+ public function testUnknownType(): void
+ {
+ $user = User::factory()->create();
+
+ $widget = Widget::factory()->create([
+ 'user_id' => $user->id,
+ 'type' => 'not_existing'
+ ]);
+
+ $this->expectException(WidgetUnknownTypeException::class);
+
+ $widget->render();
+ }
+
+ public function testMissingProperty(): void
+ {
+ $user = User::factory()->create();
+
+ $widget = Widget::factory()->create([
+ 'user_id' => $user->id,
+ 'type' => 'spent',
+ 'properties' => []
+ ]);
+
+ $this->expectException(WidgetMissingPropertyException::class);
+
+ $widget->render();
+ }
+
+ public function testInvalidPropertyValue(): void
+ {
+ $user = User::factory()->create();
+
+ $widget = Widget::factory()->create([
+ 'user_id' => $user->id,
+ 'type' => 'spent',
+ 'properties' => ['period' => 'invalid']
+ ]);
+
+ $this->expectException(WidgetInvalidPropertyValueException::class);
+
+ $widget->render();
+ }
+}

Test Widget model and dashboard itself