127 lines
3.2 KiB
PHP
127 lines
3.2 KiB
PHP
<?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());
|
|
}
|
|
}
|