fix(models): correct UserSetting import namespace and add test coverage
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Models;
|
||||
|
||||
use App\Features\Consoles\Models\Console;
|
||||
use App\Features\Games\Models\Game;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ConsoleTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
#[Test]
|
||||
public function it_has_many_games(): void
|
||||
{
|
||||
$console = Console::factory()->create();
|
||||
Game::factory()->count(3)->create(['console_id' => $console->id]);
|
||||
|
||||
$this->assertCount(3, $console->games);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_uses_slug_as_route_key(): void
|
||||
{
|
||||
$console = new Console();
|
||||
|
||||
$this->assertEquals('slug', $console->getRouteKeyName());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_casts_rom_extensions_to_array(): void
|
||||
{
|
||||
$console = Console::factory()->create(['rom_extensions' => ['nes', 'sms']]);
|
||||
|
||||
$this->assertIsArray($console->rom_extensions);
|
||||
$this->assertEquals(['nes', 'sms'], $console->rom_extensions);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_has_fillable_attributes(): void
|
||||
{
|
||||
$console = new Console();
|
||||
|
||||
$this->assertEquals([
|
||||
'name',
|
||||
'slug',
|
||||
'icon_path',
|
||||
'rom_extensions',
|
||||
'igdb_platform_id',
|
||||
'tgdb_platform_id',
|
||||
], $console->getFillable());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Models;
|
||||
|
||||
use App\Features\Consoles\Models\Console;
|
||||
use App\Features\Games\Models\Game;
|
||||
use App\Features\Games\Models\RomFile;
|
||||
use App\Features\Settings\Models\SaveFile;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Tests\TestCase;
|
||||
|
||||
class GameTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
#[Test]
|
||||
public function it_belongs_to_a_console(): void
|
||||
{
|
||||
$game = Game::factory()->create();
|
||||
|
||||
$this->assertInstanceOf(Console::class, $game->console);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_has_many_rom_files(): void
|
||||
{
|
||||
$game = Game::factory()->create();
|
||||
RomFile::factory()->count(3)->create(['game_id' => $game->id]);
|
||||
|
||||
$this->assertCount(3, $game->romFiles);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_has_many_save_files(): void
|
||||
{
|
||||
$game = Game::factory()->create();
|
||||
SaveFile::factory()->count(2)->create(['game_id' => $game->id]);
|
||||
|
||||
$this->assertCount(2, $game->saveFiles);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_uses_slug_as_route_key(): void
|
||||
{
|
||||
$game = new Game();
|
||||
|
||||
$this->assertEquals('slug', $game->getRouteKeyName());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_auto_generates_slug_from_title_on_creating(): void
|
||||
{
|
||||
$game = Game::factory()->create([
|
||||
'title' => 'Super Mario Bros',
|
||||
'slug' => null,
|
||||
]);
|
||||
|
||||
$this->assertEquals('super-mario-bros', $game->slug);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_keeps_existing_slug(): void
|
||||
{
|
||||
$game = Game::factory()->create([
|
||||
'title' => 'Super Mario Bros',
|
||||
'slug' => 'custom-slug',
|
||||
]);
|
||||
|
||||
$this->assertEquals('custom-slug', $game->slug);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_casts_rating_to_decimal(): void
|
||||
{
|
||||
$game = Game::factory()->create(['rating' => 8.5]);
|
||||
|
||||
$this->assertEquals('8.5', (string) $game->rating);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_has_fillable_attributes(): void
|
||||
{
|
||||
$game = new Game();
|
||||
|
||||
$this->assertEquals([
|
||||
'console_id',
|
||||
'title',
|
||||
'slug',
|
||||
'igdb_id',
|
||||
'tgdb_id',
|
||||
'description',
|
||||
'release_year',
|
||||
'developer',
|
||||
'publisher',
|
||||
'genre',
|
||||
'rating',
|
||||
'cover_path',
|
||||
'thumbnail_path',
|
||||
], $game->getFillable());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Models;
|
||||
|
||||
use App\Features\Games\Models\Game;
|
||||
use App\Features\Games\Models\RomFile;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Tests\TestCase;
|
||||
|
||||
class RomFileTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
#[Test]
|
||||
public function it_belongs_to_a_game(): void
|
||||
{
|
||||
$romFile = RomFile::factory()->create();
|
||||
|
||||
$this->assertInstanceOf(Game::class, $romFile->game);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_has_fillable_attributes(): void
|
||||
{
|
||||
$romFile = new RomFile();
|
||||
|
||||
$this->assertEquals([
|
||||
'game_id',
|
||||
'file_path',
|
||||
'file_name',
|
||||
'file_size',
|
||||
'file_hash',
|
||||
'region',
|
||||
'language',
|
||||
], $romFile->getFillable());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Models;
|
||||
|
||||
use App\Features\Games\Models\Game;
|
||||
use App\Features\Settings\Models\SaveFile;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SaveFileTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
#[Test]
|
||||
public function it_belongs_to_a_user(): void
|
||||
{
|
||||
$saveFile = SaveFile::factory()->create();
|
||||
|
||||
$this->assertInstanceOf(User::class, $saveFile->user);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_belongs_to_a_game(): void
|
||||
{
|
||||
$saveFile = SaveFile::factory()->create();
|
||||
|
||||
$this->assertInstanceOf(Game::class, $saveFile->game);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_casts_last_synced_at_to_datetime(): void
|
||||
{
|
||||
$saveFile = SaveFile::factory()->create([
|
||||
'last_synced_at' => '2024-01-15 10:30:00',
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(\Illuminate\Support\Carbon::class, $saveFile->last_synced_at);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_has_fillable_attributes(): void
|
||||
{
|
||||
$saveFile = new SaveFile();
|
||||
|
||||
$this->assertEquals([
|
||||
'user_id',
|
||||
'game_id',
|
||||
'file_path',
|
||||
'file_name',
|
||||
'file_size',
|
||||
'last_synced_at',
|
||||
], $saveFile->getFillable());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Models;
|
||||
|
||||
use App\Features\Settings\Models\UserSetting;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Tests\TestCase;
|
||||
|
||||
class UserSettingTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
#[Test]
|
||||
public function it_belongs_to_a_user(): void
|
||||
{
|
||||
$setting = UserSetting::factory()->create();
|
||||
|
||||
$this->assertInstanceOf(User::class, $setting->user);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_has_fillable_attributes(): void
|
||||
{
|
||||
$setting = new UserSetting();
|
||||
|
||||
$this->assertEquals([
|
||||
'user_id',
|
||||
'setting_key',
|
||||
'setting_value',
|
||||
], $setting->getFillable());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Models;
|
||||
|
||||
use App\Features\Settings\Models\SaveFile;
|
||||
use App\Features\Settings\Models\UserSetting;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Tests\TestCase;
|
||||
|
||||
class UserTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
#[Test]
|
||||
public function it_has_many_save_files(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
SaveFile::factory()->count(3)->create(['user_id' => $user->id]);
|
||||
|
||||
$this->assertCount(3, $user->saveFiles);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_has_many_settings(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
UserSetting::factory()->count(2)->create(['user_id' => $user->id]);
|
||||
|
||||
$this->assertCount(2, $user->settings);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_gets_setting_by_key(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
UserSetting::factory()->create([
|
||||
'user_id' => $user->id,
|
||||
'setting_key' => 'theme',
|
||||
'setting_value' => 'dark',
|
||||
]);
|
||||
|
||||
$this->assertEquals('dark', $user->getSetting('theme'));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_default_when_setting_not_found(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->assertEquals('default', $user->getSetting('nonexistent', 'default'));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_null_when_setting_not_found_without_default(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->assertNull($user->getSetting('nonexistent'));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_sets_setting(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$user->setSetting('theme', 'dark');
|
||||
|
||||
$this->assertDatabaseHas('user_settings', [
|
||||
'user_id' => $user->id,
|
||||
'setting_key' => 'theme',
|
||||
'setting_value' => 'dark',
|
||||
]);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_updates_existing_setting(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
UserSetting::factory()->create([
|
||||
'user_id' => $user->id,
|
||||
'setting_key' => 'theme',
|
||||
'setting_value' => 'light',
|
||||
]);
|
||||
|
||||
$user->setSetting('theme', 'dark');
|
||||
|
||||
$this->assertDatabaseHas('user_settings', [
|
||||
'user_id' => $user->id,
|
||||
'setting_key' => 'theme',
|
||||
'setting_value' => 'dark',
|
||||
]);
|
||||
$this->assertDatabaseCount('user_settings', 1);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_casts_email_verified_at_to_datetime(): void
|
||||
{
|
||||
$user = User::factory()->create(['email_verified_at' => '2024-01-15 10:30:00']);
|
||||
|
||||
$this->assertInstanceOf(\Illuminate\Support\Carbon::class, $user->email_verified_at);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_hashes_password(): void
|
||||
{
|
||||
$user = User::factory()->create(['password' => 'test-password']);
|
||||
|
||||
$this->assertNotEquals('test-password', $user->password);
|
||||
$this->assertTrue(\Hash::check('test-password', $user->password));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_has_fillable_attributes(): void
|
||||
{
|
||||
$user = new User();
|
||||
|
||||
$this->assertEquals(['name', 'email', 'password'], $user->getFillable());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_has_hidden_attributes(): void
|
||||
{
|
||||
$user = new User();
|
||||
|
||||
$this->assertEquals(['password', 'remember_token'], $user->getHidden());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user