diff --git a/app/Models/User.php b/app/Models/User.php index 7e7da3b..949cafa 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -66,4 +66,9 @@ class User extends Authenticatable // implements MustVerifyEmail { return $this->hasMany(Device::class); } + + public function plugins(): HasMany + { + return $this->hasMany(Plugin::class); + } } diff --git a/tests/Pest.php b/tests/Pest.php index bf47dd2..627fd57 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -13,7 +13,7 @@ pest()->extend(Tests\TestCase::class) ->use(Illuminate\Foundation\Testing\RefreshDatabase::class) - ->in('Feature'); + ->in('Feature', 'Unit'); registerSpatiePestHelpers(); diff --git a/tests/Unit/Models/PlaylistItemTest.php b/tests/Unit/Models/PlaylistItemTest.php new file mode 100644 index 0000000..482549a --- /dev/null +++ b/tests/Unit/Models/PlaylistItemTest.php @@ -0,0 +1,24 @@ +create(); + $playlistItem = PlaylistItem::factory()->create(['playlist_id' => $playlist->id]); + + expect($playlistItem->playlist) + ->toBeInstanceOf(Playlist::class) + ->id->toBe($playlist->id); +}); + +test('playlist item belongs to plugin', function () { + $plugin = Plugin::factory()->create(); + $playlistItem = PlaylistItem::factory()->create(['plugin_id' => $plugin->id]); + + expect($playlistItem->plugin) + ->toBeInstanceOf(Plugin::class) + ->id->toBe($plugin->id); +}); diff --git a/tests/Unit/Models/PlaylistTest.php b/tests/Unit/Models/PlaylistTest.php new file mode 100644 index 0000000..55d31c7 --- /dev/null +++ b/tests/Unit/Models/PlaylistTest.php @@ -0,0 +1,46 @@ +create([ + 'name' => 'Test Playlist', + 'is_active' => true, + 'weekdays' => [1, 2, 3], + 'active_from' => '09:00', + 'active_until' => '17:00', + ]); + + expect($playlist) + ->name->toBe('Test Playlist') + ->is_active->toBeTrue() + ->weekdays->toBe([1, 2, 3]) + ->active_from->format('H:i')->toBe('09:00') + ->active_until->format('H:i')->toBe('17:00'); +}); + +test('playlist belongs to device', function () { + $device = Device::factory()->create(); + $playlist = Playlist::factory()->create(['device_id' => $device->id]); + + expect($playlist->device) + ->toBeInstanceOf(Device::class) + ->id->toBe($device->id); +}); + +test('playlist has many items', function () { + $playlist = Playlist::factory()->create(); + $items = PlaylistItem::factory()->count(3)->create(['playlist_id' => $playlist->id]); + + expect($playlist->items) + ->toHaveCount(3) + ->each->toBeInstanceOf(PlaylistItem::class); +}); + +test('getNextPlaylistItem returns null when playlist is inactive', function () { + $playlist = Playlist::factory()->create(['is_active' => false]); + + expect($playlist->getNextPlaylistItem())->toBeNull(); +}); diff --git a/tests/Unit/Models/PluginTest.php b/tests/Unit/Models/PluginTest.php new file mode 100644 index 0000000..f5cd2b3 --- /dev/null +++ b/tests/Unit/Models/PluginTest.php @@ -0,0 +1,42 @@ +create([ + 'name' => 'Test Plugin', + 'data_payload' => ['key' => 'value'], + ]); + + expect($plugin) + ->name->toBe('Test Plugin') + ->data_payload->toBe(['key' => 'value']) + ->uuid->toBeString() + ->uuid->toMatch('/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/'); +}); + +test('plugin automatically generates uuid on creation', function () { + $plugin = Plugin::factory()->create(); + + expect($plugin->uuid) + ->toBeString() + ->toMatch('/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/'); +}); + +test('plugin can have custom uuid', function () { + $uuid = \Illuminate\Support\Str::uuid(); + $plugin = Plugin::factory()->create(['uuid' => $uuid]); + + expect($plugin->uuid)->toBe($uuid); +}); + +test('plugin data_payload is cast to array', function () { + $data = ['key' => 'value']; + $plugin = Plugin::factory()->create(['data_payload' => $data]); + + expect($plugin->data_payload) + ->toBeArray() + ->toBe($data); +});