87 lines
2.4 KiB
PHP
87 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Jobs;
|
|
|
|
use App\Features\Consoles\Models\Console;
|
|
use App\Features\Games\Models\Game;
|
|
use App\Features\Scraper\Services\ScraperService;
|
|
use App\Jobs\ScrapeCoverJob;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
use Tests\TestCase;
|
|
|
|
class ScrapeCoverJobTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private ScraperService|MockObject $scraper;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->scraper = $this->createMock(ScraperService::class);
|
|
}
|
|
|
|
public function test_job_updates_cover_when_match_found(): void
|
|
{
|
|
Storage::fake('local');
|
|
$console = Console::factory()->create(['slug' => 'nes']);
|
|
$game = Game::factory()->create(['console_id' => $console->id]);
|
|
$game->load('console');
|
|
|
|
$match = new \stdClass();
|
|
$match->coverUrl = 'https://example.com/cover.jpg';
|
|
|
|
$this->scraper->expects($this->once())
|
|
->method('search')
|
|
->willReturn(collect([$match]));
|
|
|
|
$this->scraper->expects($this->once())
|
|
->method('downloadCover')
|
|
->willReturn('covers/nes/super-mario-cover.jpg');
|
|
|
|
$job = new ScrapeCoverJob($game);
|
|
$job->handle($this->scraper);
|
|
|
|
$game->refresh();
|
|
$this->assertEquals('covers/nes/super-mario-cover.jpg', $game->cover_path);
|
|
}
|
|
|
|
public function test_job_does_nothing_when_no_results(): void
|
|
{
|
|
$game = Game::factory()->create();
|
|
|
|
$this->scraper->expects($this->once())
|
|
->method('search')
|
|
->willReturn(collect());
|
|
|
|
$this->scraper->expects($this->never())
|
|
->method('downloadCover');
|
|
|
|
$job = new ScrapeCoverJob($game);
|
|
$job->handle($this->scraper);
|
|
|
|
$game->refresh();
|
|
$this->assertNull($game->cover_path);
|
|
}
|
|
|
|
public function test_job_does_nothing_when_no_cover_url(): void
|
|
{
|
|
$game = Game::factory()->create();
|
|
|
|
$match = new \stdClass();
|
|
$match->coverUrl = null;
|
|
|
|
$this->scraper->expects($this->once())
|
|
->method('search')
|
|
->willReturn(collect([$match]));
|
|
|
|
$this->scraper->expects($this->never())
|
|
->method('downloadCover');
|
|
|
|
$job = new ScrapeCoverJob($game);
|
|
$job->handle($this->scraper);
|
|
}
|
|
}
|