fix(models): correct UserSetting import namespace and add test coverage
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Repositories;
|
||||
|
||||
use App\Features\Consoles\Models\Console;
|
||||
use App\Features\Consoles\Repositories\ConsoleRepository;
|
||||
use App\Features\Games\Models\Game;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ConsoleRepositoryTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private ConsoleRepository $repository;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->repository = new ConsoleRepository(new Console());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_gets_all_consoles_with_games_count(): void
|
||||
{
|
||||
Console::factory()->count(3)->create();
|
||||
|
||||
$consoles = $this->repository->getAll();
|
||||
|
||||
$this->assertCount(3, $consoles);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_gets_console_by_slug(): void
|
||||
{
|
||||
$console = Console::factory()->create(['slug' => 'nes']);
|
||||
|
||||
$found = $this->repository->getBySlug('nes');
|
||||
|
||||
$this->assertNotNull($found);
|
||||
$this->assertEquals($console->id, $found->id);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_null_for_nonexistent_slug(): void
|
||||
{
|
||||
$found = $this->repository->getBySlug('nonexistent');
|
||||
|
||||
$this->assertNull($found);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_gets_consoles_with_games_count(): void
|
||||
{
|
||||
$console = Console::factory()->create();
|
||||
Game::factory()->count(5)->create(['console_id' => $console->id]);
|
||||
|
||||
$consoles = $this->repository->getWithGamesCount();
|
||||
|
||||
$this->assertCount(1, $consoles);
|
||||
$this->assertEquals(5, $consoles->first()->games_count);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Repositories;
|
||||
|
||||
use App\Features\Consoles\Models\Console;
|
||||
use App\Features\Games\Models\Game;
|
||||
use App\Features\Games\Repositories\GameRepository;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Tests\TestCase;
|
||||
|
||||
class GameRepositoryTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private GameRepository $repository;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->repository = new GameRepository(new Game());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_gets_games_by_console(): void
|
||||
{
|
||||
$console = Console::factory()->create();
|
||||
Game::factory()->count(5)->create(['console_id' => $console->id]);
|
||||
Game::factory()->count(3)->create();
|
||||
|
||||
$games = $this->repository->getByConsole($console->id);
|
||||
|
||||
$this->assertCount(5, $games);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_gets_game_by_slug(): void
|
||||
{
|
||||
$game = Game::factory()->create(['slug' => 'super-mario']);
|
||||
|
||||
$found = $this->repository->getBySlug('super-mario');
|
||||
|
||||
$this->assertNotNull($found);
|
||||
$this->assertEquals($game->id, $found->id);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_null_for_nonexistent_slug(): void
|
||||
{
|
||||
$found = $this->repository->getBySlug('nonexistent');
|
||||
|
||||
$this->assertNull($found);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_gets_recent_games(): void
|
||||
{
|
||||
Game::factory()->count(5)->create();
|
||||
|
||||
$recent = $this->repository->getRecent(3);
|
||||
|
||||
$this->assertCount(3, $recent);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_searches_games_by_title(): void
|
||||
{
|
||||
Game::factory()->create(['title' => 'Super Mario Bros']);
|
||||
Game::factory()->create(['title' => 'Super Mario World']);
|
||||
Game::factory()->create(['title' => 'Sonic the Hedgehog']);
|
||||
|
||||
$results = $this->repository->search('Mario');
|
||||
|
||||
$this->assertCount(2, $results);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_gets_all_games_paginated(): void
|
||||
{
|
||||
Game::factory()->count(30)->create();
|
||||
|
||||
$all = $this->repository->getAll();
|
||||
|
||||
$this->assertCount(24, $all);
|
||||
$this->assertEquals(30, $all->total());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_creates_a_game(): void
|
||||
{
|
||||
$game = $this->repository->create([
|
||||
'console_id' => Console::factory()->create()->id,
|
||||
'title' => 'New Game',
|
||||
'slug' => 'new-game',
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('games', ['slug' => 'new-game']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_creates_game_first_or_creates(): void
|
||||
{
|
||||
$console = Console::factory()->create();
|
||||
|
||||
$game1 = $this->repository->firstOrCreate(
|
||||
['console_id' => $console->id, 'slug' => 'test-game'],
|
||||
['title' => 'Test Game']
|
||||
);
|
||||
|
||||
$game2 = $this->repository->firstOrCreate(
|
||||
['console_id' => $console->id, 'slug' => 'test-game'],
|
||||
['title' => 'Different Title']
|
||||
);
|
||||
|
||||
$this->assertEquals($game1->id, $game2->id);
|
||||
$this->assertEquals('Test Game', $game2->title);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_counts_games(): void
|
||||
{
|
||||
Game::factory()->count(5)->create();
|
||||
|
||||
$this->assertEquals(5, $this->repository->count());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Repositories;
|
||||
|
||||
use App\Features\Consoles\Models\Console;
|
||||
use App\Features\Games\Models\Game;
|
||||
use App\Features\Games\Models\RomFile;
|
||||
use App\Features\Games\Repositories\RomFileRepository;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Tests\TestCase;
|
||||
|
||||
class RomFileRepositoryTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private RomFileRepository $repository;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->repository = new RomFileRepository(new RomFile());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_counts_rom_files(): void
|
||||
{
|
||||
$console = Console::factory()->create();
|
||||
$game = Game::factory()->create(['console_id' => $console->id]);
|
||||
RomFile::factory()->count(5)->create(['game_id' => $game->id]);
|
||||
|
||||
$this->assertEquals(5, $this->repository->count());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_finds_by_hash(): void
|
||||
{
|
||||
$console = Console::factory()->create();
|
||||
$game = Game::factory()->create(['console_id' => $console->id]);
|
||||
$romFile = RomFile::factory()->create([
|
||||
'game_id' => $game->id,
|
||||
'file_hash' => 'abc123',
|
||||
]);
|
||||
|
||||
$found = $this->repository->findByHash('abc123');
|
||||
|
||||
$this->assertNotNull($found);
|
||||
$this->assertEquals($romFile->id, $found->id);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_null_for_nonexistent_hash(): void
|
||||
{
|
||||
$found = $this->repository->findByHash('nonexistent');
|
||||
|
||||
$this->assertNull($found);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_creates_a_rom_file(): void
|
||||
{
|
||||
$console = Console::factory()->create();
|
||||
$game = Game::factory()->create(['console_id' => $console->id]);
|
||||
|
||||
$romFile = $this->repository->create([
|
||||
'game_id' => $game->id,
|
||||
'file_path' => 'roms/nes/game.nes',
|
||||
'file_name' => 'game.nes',
|
||||
'file_size' => 102400,
|
||||
'file_hash' => 'abc123',
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('rom_files', ['file_hash' => 'abc123']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_gets_rom_files_by_game(): void
|
||||
{
|
||||
$console = Console::factory()->create();
|
||||
$game = Game::factory()->create(['console_id' => $console->id]);
|
||||
RomFile::factory()->count(3)->create(['game_id' => $game->id]);
|
||||
|
||||
$otherGame = Game::factory()->create(['console_id' => $console->id]);
|
||||
RomFile::factory()->count(2)->create(['game_id' => $otherGame->id]);
|
||||
|
||||
$romFiles = $this->repository->getByGame($game->id);
|
||||
|
||||
$this->assertCount(3, $romFiles);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Repositories;
|
||||
|
||||
use App\Features\Settings\Models\SaveFile;
|
||||
use App\Features\Settings\Models\UserSetting;
|
||||
use App\Features\Settings\Repositories\SettingsRepository;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SettingsRepositoryTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private SettingsRepository $repository;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->repository = new SettingsRepository(
|
||||
new SaveFile(),
|
||||
new UserSetting()
|
||||
);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_gets_user_saves(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
SaveFile::factory()->count(3)->create(['user_id' => $user->id]);
|
||||
SaveFile::factory()->count(2)->create();
|
||||
|
||||
$saves = $this->repository->getUserSaves($user);
|
||||
|
||||
$this->assertCount(3, $saves);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_gets_user_settings(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
UserSetting::factory()->count(2)->create(['user_id' => $user->id]);
|
||||
UserSetting::factory()->count(3)->create();
|
||||
|
||||
$settings = $this->repository->getUserSettings($user);
|
||||
|
||||
$this->assertCount(2, $settings);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_gets_a_setting_by_key(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
UserSetting::factory()->create([
|
||||
'user_id' => $user->id,
|
||||
'setting_key' => 'theme',
|
||||
'setting_value' => 'dark',
|
||||
]);
|
||||
|
||||
$value = $this->repository->getSetting($user, 'theme');
|
||||
|
||||
$this->assertEquals('dark', $value);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_default_when_setting_not_found(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$value = $this->repository->getSetting($user, 'nonexistent', 'default_value');
|
||||
|
||||
$this->assertEquals('default_value', $value);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_null_when_setting_not_found_without_default(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$value = $this->repository->getSetting($user, 'nonexistent');
|
||||
|
||||
$this->assertNull($value);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_creates_a_setting(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->repository->setSetting($user, 'theme', 'dark');
|
||||
|
||||
$this->assertDatabaseHas('user_settings', [
|
||||
'user_id' => $user->id,
|
||||
'setting_key' => 'theme',
|
||||
'setting_value' => 'dark',
|
||||
]);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_updates_an_existing_setting(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
UserSetting::factory()->create([
|
||||
'user_id' => $user->id,
|
||||
'setting_key' => 'theme',
|
||||
'setting_value' => 'light',
|
||||
]);
|
||||
|
||||
$this->repository->setSetting($user, 'theme', 'dark');
|
||||
|
||||
$this->assertDatabaseHas('user_settings', [
|
||||
'user_id' => $user->id,
|
||||
'setting_key' => 'theme',
|
||||
'setting_value' => 'dark',
|
||||
]);
|
||||
$this->assertDatabaseCount('user_settings', 1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user