fix(models): correct UserSetting import namespace and add test coverage
This commit is contained in:
@@ -0,0 +1,319 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services;
|
||||
|
||||
use App\Features\Scraper\Services\TGDBCoverScraper;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Tests\TestCase;
|
||||
|
||||
class TGDBCoverScraperTest extends TestCase
|
||||
{
|
||||
#[Test]
|
||||
public function it_returns_empty_when_no_config(): void
|
||||
{
|
||||
config(['services.thegamesdb.api_key' => '']);
|
||||
|
||||
$scraper = new TGDBCoverScraper();
|
||||
|
||||
$results = $scraper->search('mario');
|
||||
|
||||
$this->assertTrue($results->isEmpty());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_null_cover_when_no_config(): void
|
||||
{
|
||||
config(['services.thegamesdb.api_key' => '']);
|
||||
|
||||
$scraper = new TGDBCoverScraper();
|
||||
|
||||
$this->assertNull($scraper->getCoverUrl(123));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_null_screenshot_when_no_config(): void
|
||||
{
|
||||
config(['services.thegamesdb.api_key' => '']);
|
||||
|
||||
$scraper = new TGDBCoverScraper();
|
||||
|
||||
$this->assertNull($scraper->getScreenshotUrl(123));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_empty_images_when_no_config(): void
|
||||
{
|
||||
config(['services.thegamesdb.api_key' => '']);
|
||||
|
||||
$scraper = new TGDBCoverScraper();
|
||||
|
||||
$images = $scraper->getImages(123);
|
||||
|
||||
$this->assertEmpty($images);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_searches_games(): void
|
||||
{
|
||||
config(['services.thegamesdb.api_key' => 'test-key']);
|
||||
|
||||
Http::fake([
|
||||
'*/Games/ByGameName*' => Http::response([
|
||||
'data' => [
|
||||
'games' => [
|
||||
[
|
||||
'id' => 1,
|
||||
'game_title' => 'Super Mario Bros',
|
||||
'cover' => 'https://example.com/cover.jpg',
|
||||
'release_date' => '1985-09-13',
|
||||
],
|
||||
],
|
||||
],
|
||||
]),
|
||||
]);
|
||||
|
||||
$scraper = new TGDBCoverScraper();
|
||||
$results = $scraper->search('mario');
|
||||
|
||||
$this->assertCount(1, $results);
|
||||
$this->assertEquals('Super Mario Bros', $results->first()['title']);
|
||||
$this->assertEquals('thegamesdb', $results->first()['source']);
|
||||
$this->assertEquals('1985', $results->first()['year']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_searches_games_with_console_id(): void
|
||||
{
|
||||
config(['services.thegamesdb.api_key' => 'test-key']);
|
||||
|
||||
Http::fake([
|
||||
'*/Games/ByGameName*' => Http::response([
|
||||
'data' => ['games' => []],
|
||||
]),
|
||||
]);
|
||||
|
||||
$scraper = new TGDBCoverScraper();
|
||||
$results = $scraper->search('mario', 21);
|
||||
|
||||
$this->assertTrue($results->isEmpty());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_searches_games_without_release_date(): void
|
||||
{
|
||||
config(['services.thegamesdb.api_key' => 'test-key']);
|
||||
|
||||
Http::fake([
|
||||
'*/Games/ByGameName*' => Http::response([
|
||||
'data' => [
|
||||
'games' => [
|
||||
[
|
||||
'id' => 1,
|
||||
'game_title' => 'Test Game',
|
||||
'cover' => null,
|
||||
],
|
||||
],
|
||||
],
|
||||
]),
|
||||
]);
|
||||
|
||||
$scraper = new TGDBCoverScraper();
|
||||
$results = $scraper->search('test');
|
||||
|
||||
$this->assertCount(1, $results);
|
||||
$this->assertNull($results->first()['year']);
|
||||
$this->assertNull($results->first()['cover_url']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_empty_on_api_failure(): void
|
||||
{
|
||||
config(['services.thegamesdb.api_key' => 'test-key']);
|
||||
|
||||
Http::fake([
|
||||
'*/Games/ByGameName*' => Http::response([], 500),
|
||||
]);
|
||||
|
||||
$scraper = new TGDBCoverScraper();
|
||||
$results = $scraper->search('mario');
|
||||
|
||||
$this->assertTrue($results->isEmpty());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_empty_on_exception(): void
|
||||
{
|
||||
config(['services.thegamesdb.api_key' => 'test-key']);
|
||||
|
||||
Http::fake([
|
||||
'*/Games/ByGameName*' => fn () => throw new \Exception('Network error'),
|
||||
]);
|
||||
|
||||
$scraper = new TGDBCoverScraper();
|
||||
$results = $scraper->search('mario');
|
||||
|
||||
$this->assertTrue($results->isEmpty());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_gets_cover_url(): void
|
||||
{
|
||||
config(['services.thegamesdb.api_key' => 'test-key']);
|
||||
|
||||
Http::fake([
|
||||
'*/Games/Images*' => Http::response([
|
||||
'data' => [
|
||||
'images' => [
|
||||
['filename' => 'cover.jpg'],
|
||||
],
|
||||
],
|
||||
]),
|
||||
]);
|
||||
|
||||
$scraper = new TGDBCoverScraper();
|
||||
$url = $scraper->getCoverUrl(123);
|
||||
|
||||
$this->assertNotNull($url);
|
||||
$this->assertStringContainsString('cover.jpg', $url);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_null_cover_when_empty(): void
|
||||
{
|
||||
config(['services.thegamesdb.api_key' => 'test-key']);
|
||||
|
||||
Http::fake([
|
||||
'*/Games/Images*' => Http::response([
|
||||
'data' => ['images' => []],
|
||||
]),
|
||||
]);
|
||||
|
||||
$scraper = new TGDBCoverScraper();
|
||||
$url = $scraper->getCoverUrl(123);
|
||||
|
||||
$this->assertNull($url);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_null_cover_on_api_failure(): void
|
||||
{
|
||||
config(['services.thegamesdb.api_key' => 'test-key']);
|
||||
|
||||
Http::fake([
|
||||
'*/Games/Images*' => Http::response([], 500),
|
||||
]);
|
||||
|
||||
$scraper = new TGDBCoverScraper();
|
||||
$url = $scraper->getCoverUrl(123);
|
||||
|
||||
$this->assertNull($url);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_null_cover_on_exception(): void
|
||||
{
|
||||
config(['services.thegamesdb.api_key' => 'test-key']);
|
||||
|
||||
Http::fake([
|
||||
'*/Games/Images*' => fn () => throw new \Exception('Network error'),
|
||||
]);
|
||||
|
||||
$scraper = new TGDBCoverScraper();
|
||||
$url = $scraper->getCoverUrl(123);
|
||||
|
||||
$this->assertNull($url);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_gets_screenshot_url(): void
|
||||
{
|
||||
config(['services.thegamesdb.api_key' => 'test-key']);
|
||||
|
||||
Http::fake([
|
||||
'*/Games/Images*' => Http::response([
|
||||
'data' => [
|
||||
'images' => [
|
||||
['filename' => 'screenshot.jpg'],
|
||||
],
|
||||
],
|
||||
]),
|
||||
]);
|
||||
|
||||
$scraper = new TGDBCoverScraper();
|
||||
$url = $scraper->getScreenshotUrl(123);
|
||||
|
||||
$this->assertNotNull($url);
|
||||
$this->assertStringContainsString('screenshot.jpg', $url);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_null_screenshot_when_empty(): void
|
||||
{
|
||||
config(['services.thegamesdb.api_key' => 'test-key']);
|
||||
|
||||
Http::fake([
|
||||
'*/Games/Images*' => Http::response([
|
||||
'data' => ['images' => []],
|
||||
]),
|
||||
]);
|
||||
|
||||
$scraper = new TGDBCoverScraper();
|
||||
$url = $scraper->getScreenshotUrl(123);
|
||||
|
||||
$this->assertNull($url);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_null_screenshot_on_api_failure(): void
|
||||
{
|
||||
config(['services.thegamesdb.api_key' => 'test-key']);
|
||||
|
||||
Http::fake([
|
||||
'*/Games/Images*' => Http::response([], 500),
|
||||
]);
|
||||
|
||||
$scraper = new TGDBCoverScraper();
|
||||
$url = $scraper->getScreenshotUrl(123);
|
||||
|
||||
$this->assertNull($url);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_null_screenshot_on_exception(): void
|
||||
{
|
||||
config(['services.thegamesdb.api_key' => 'test-key']);
|
||||
|
||||
Http::fake([
|
||||
'*/Games/Images*' => fn () => throw new \Exception('Network error'),
|
||||
]);
|
||||
|
||||
$scraper = new TGDBCoverScraper();
|
||||
$url = $scraper->getScreenshotUrl(123);
|
||||
|
||||
$this->assertNull($url);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_gets_images(): void
|
||||
{
|
||||
config(['services.thegamesdb.api_key' => 'test-key']);
|
||||
|
||||
Http::fake([
|
||||
'*/Games/Images*' => Http::response([
|
||||
'data' => [
|
||||
'images' => [
|
||||
['filename' => 'boxart.jpg'],
|
||||
],
|
||||
],
|
||||
]),
|
||||
]);
|
||||
|
||||
$scraper = new TGDBCoverScraper();
|
||||
$images = $scraper->getImages(123);
|
||||
|
||||
$this->assertCount(2, $images);
|
||||
$this->assertArrayHasKey('cover', $images);
|
||||
$this->assertArrayHasKey('screenshot', $images);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user