fix(models): correct UserSetting import namespace and add test coverage
This commit is contained in:
@@ -0,0 +1,256 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services;
|
||||
|
||||
use App\Features\Scraper\Services\IGDBCoverScraper;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Tests\TestCase;
|
||||
|
||||
class IGDBCoverScraperTest extends TestCase
|
||||
{
|
||||
#[Test]
|
||||
public function it_returns_empty_when_no_config(): void
|
||||
{
|
||||
config(['services.igdb.client_id' => '', 'services.igdb.client_secret' => '']);
|
||||
|
||||
$scraper = new IGDBCoverScraper();
|
||||
|
||||
$results = $scraper->search('mario');
|
||||
|
||||
$this->assertTrue($results->isEmpty());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_null_cover_when_no_config(): void
|
||||
{
|
||||
config(['services.igdb.client_id' => '', 'services.igdb.client_secret' => '']);
|
||||
|
||||
$scraper = new IGDBCoverScraper();
|
||||
|
||||
$this->assertNull($scraper->getCoverUrl(123));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_null_screenshot_when_no_config(): void
|
||||
{
|
||||
config(['services.igdb.client_id' => '', 'services.igdb.client_secret' => '']);
|
||||
|
||||
$scraper = new IGDBCoverScraper();
|
||||
|
||||
$this->assertNull($scraper->getScreenshotUrl(123));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_empty_images_when_no_config(): void
|
||||
{
|
||||
config(['services.igdb.client_id' => '', 'services.igdb.client_secret' => '']);
|
||||
|
||||
$scraper = new IGDBCoverScraper();
|
||||
|
||||
$images = $scraper->getImages(123);
|
||||
|
||||
$this->assertEmpty($images);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_searches_games_with_valid_config(): void
|
||||
{
|
||||
config([
|
||||
'services.igdb.client_id' => 'test-id',
|
||||
'services.igdb.client_secret' => 'test-secret',
|
||||
]);
|
||||
|
||||
Http::fake([
|
||||
'https://id.twitch.tv/oauth2/token' => Http::response(['access_token' => 'fake-token']),
|
||||
'https://api.igdb.com/v4/games' => Http::response([
|
||||
[
|
||||
'id' => 1,
|
||||
'name' => 'Super Mario Bros',
|
||||
'cover' => ['url' => 'https://images.igdb.com/igdb/image/upload/thumb/test.jpg'],
|
||||
'release_dates' => [['year' => 1985]],
|
||||
],
|
||||
]),
|
||||
]);
|
||||
|
||||
$scraper = new IGDBCoverScraper();
|
||||
$results = $scraper->search('mario');
|
||||
|
||||
$this->assertCount(1, $results);
|
||||
$this->assertEquals('Super Mario Bros', $results->first()['title']);
|
||||
$this->assertEquals('igdb', $results->first()['source']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_searches_games_with_console_id(): void
|
||||
{
|
||||
config([
|
||||
'services.igdb.client_id' => 'test-id',
|
||||
'services.igdb.client_secret' => 'test-secret',
|
||||
]);
|
||||
|
||||
Http::fake([
|
||||
'https://id.twitch.tv/oauth2/token' => Http::response(['access_token' => 'fake-token']),
|
||||
'https://api.igdb.com/v4/games' => Http::response([
|
||||
[
|
||||
'id' => 1,
|
||||
'name' => 'Super Mario Bros',
|
||||
'cover' => null,
|
||||
'release_dates' => [],
|
||||
],
|
||||
]),
|
||||
]);
|
||||
|
||||
$scraper = new IGDBCoverScraper();
|
||||
$results = $scraper->search('mario', 21);
|
||||
|
||||
$this->assertCount(1, $results);
|
||||
$this->assertNull($results->first()['cover_url']);
|
||||
$this->assertNull($results->first()['year']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_empty_on_api_failure(): void
|
||||
{
|
||||
config([
|
||||
'services.igdb.client_id' => 'test-id',
|
||||
'services.igdb.client_secret' => 'test-secret',
|
||||
]);
|
||||
|
||||
Http::fake([
|
||||
'https://id.twitch.tv/oauth2/token' => Http::response(['access_token' => 'fake-token']),
|
||||
'https://api.igdb.com/v4/games' => Http::response([], 500),
|
||||
]);
|
||||
|
||||
$scraper = new IGDBCoverScraper();
|
||||
$results = $scraper->search('mario');
|
||||
|
||||
$this->assertTrue($results->isEmpty());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_empty_on_exception(): void
|
||||
{
|
||||
config([
|
||||
'services.igdb.client_id' => 'test-id',
|
||||
'services.igdb.client_secret' => 'test-secret',
|
||||
]);
|
||||
|
||||
Http::fake([
|
||||
'https://id.twitch.tv/oauth2/token' => Http::response(['access_token' => 'fake-token']),
|
||||
'https://api.igdb.com/v4/games' => fn () => throw new \Exception('Network error'),
|
||||
]);
|
||||
|
||||
$scraper = new IGDBCoverScraper();
|
||||
$results = $scraper->search('mario');
|
||||
|
||||
$this->assertTrue($results->isEmpty());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_gets_cover_url(): void
|
||||
{
|
||||
config([
|
||||
'services.igdb.client_id' => 'test-id',
|
||||
'services.igdb.client_secret' => 'test-secret',
|
||||
]);
|
||||
|
||||
Http::fake([
|
||||
'https://id.twitch.tv/oauth2/token' => Http::response(['access_token' => 'fake-token']),
|
||||
'https://api.igdb.com/v4/covers' => Http::response([
|
||||
['url' => 'https://images.igdb.com/igdb/image/upload/thumb/test.jpg'],
|
||||
]),
|
||||
]);
|
||||
|
||||
$scraper = new IGDBCoverScraper();
|
||||
$url = $scraper->getCoverUrl(123);
|
||||
|
||||
$this->assertNotNull($url);
|
||||
$this->assertStringContainsString('cover_big', $url);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_null_cover_when_empty_response(): void
|
||||
{
|
||||
config([
|
||||
'services.igdb.client_id' => 'test-id',
|
||||
'services.igdb.client_secret' => 'test-secret',
|
||||
]);
|
||||
|
||||
Http::fake([
|
||||
'https://id.twitch.tv/oauth2/token' => Http::response(['access_token' => 'fake-token']),
|
||||
'https://api.igdb.com/v4/covers' => Http::response([]),
|
||||
]);
|
||||
|
||||
$scraper = new IGDBCoverScraper();
|
||||
$url = $scraper->getCoverUrl(123);
|
||||
|
||||
$this->assertNull($url);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_gets_screenshot_url(): void
|
||||
{
|
||||
config([
|
||||
'services.igdb.client_id' => 'test-id',
|
||||
'services.igdb.client_secret' => 'test-secret',
|
||||
]);
|
||||
|
||||
Http::fake([
|
||||
'https://id.twitch.tv/oauth2/token' => Http::response(['access_token' => 'fake-token']),
|
||||
'https://api.igdb.com/v4/screenshots' => Http::response([
|
||||
['url' => 'https://images.igdb.com/igdb/image/upload/thumb/screenshot.jpg'],
|
||||
]),
|
||||
]);
|
||||
|
||||
$scraper = new IGDBCoverScraper();
|
||||
$url = $scraper->getScreenshotUrl(123);
|
||||
|
||||
$this->assertNotNull($url);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_null_screenshot_when_empty(): void
|
||||
{
|
||||
config([
|
||||
'services.igdb.client_id' => 'test-id',
|
||||
'services.igdb.client_secret' => 'test-secret',
|
||||
]);
|
||||
|
||||
Http::fake([
|
||||
'https://id.twitch.tv/oauth2/token' => Http::response(['access_token' => 'fake-token']),
|
||||
'https://api.igdb.com/v4/screenshots' => Http::response([]),
|
||||
]);
|
||||
|
||||
$scraper = new IGDBCoverScraper();
|
||||
$url = $scraper->getScreenshotUrl(123);
|
||||
|
||||
$this->assertNull($url);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_gets_images(): void
|
||||
{
|
||||
config([
|
||||
'services.igdb.client_id' => 'test-id',
|
||||
'services.igdb.client_secret' => 'test-secret',
|
||||
]);
|
||||
|
||||
Http::fake([
|
||||
'https://id.twitch.tv/oauth2/token' => Http::response(['access_token' => 'fake-token']),
|
||||
'https://api.igdb.com/v4/covers' => Http::response([
|
||||
['url' => 'https://images.igdb.com/igdb/image/upload/thumb/test.jpg'],
|
||||
]),
|
||||
'https://api.igdb.com/v4/screenshots' => Http::response([
|
||||
['url' => 'https://images.igdb.com/igdb/image/upload/thumb/screenshot.jpg'],
|
||||
]),
|
||||
]);
|
||||
|
||||
$scraper = new IGDBCoverScraper();
|
||||
$images = $scraper->getImages(123);
|
||||
|
||||
$this->assertCount(2, $images);
|
||||
$this->assertArrayHasKey('cover', $images);
|
||||
$this->assertArrayHasKey('screenshot', $images);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user