-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
40f49b8
commit e57c454
Showing
10 changed files
with
174 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace MarcReichel\IGDBLaravel\Enums\Image; | ||
|
||
class Size | ||
{ | ||
public const COVER_SMALL = 'cover_small'; | ||
public const SCREENSHOT_MEDIUM = 'screenshot_med'; | ||
public const COVER_BIG = 'cover_big'; | ||
public const LOGO_MEDIUM = 'logo_med'; | ||
public const SCREENSHOT_BIG = 'screenshot_big'; | ||
public const SCREENSHOT_HUGE = 'screenshot_huge'; | ||
public const THUMBNAIL = 'thumb'; | ||
public const MICRO = 'micro'; | ||
public const HD_READY = '720p'; | ||
public const HD = '1080p'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
namespace MarcReichel\IGDBLaravel\Models; | ||
|
||
class Artwork extends Model | ||
class Artwork extends Image | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
namespace MarcReichel\IGDBLaravel\Models; | ||
|
||
class CompanyLogo extends Model | ||
class CompanyLogo extends Image | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
namespace MarcReichel\IGDBLaravel\Models; | ||
|
||
class Cover extends Model | ||
class Cover extends Image | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace MarcReichel\IGDBLaravel\Models; | ||
|
||
use Illuminate\Support\Str; | ||
use InvalidArgumentException; | ||
use MarcReichel\IGDBLaravel\Enums\Image\Size; | ||
use ReflectionClass; | ||
|
||
abstract class Image extends Model | ||
{ | ||
protected const IMAGE_BASE_PATH = '//images.igdb.com/igdb/image/upload'; | ||
|
||
public function getUrl(string $size = 'thumb', bool $retina = false): string | ||
{ | ||
$availableSizes = new ReflectionClass(Size::class); | ||
$constants = collect($availableSizes->getConstants()); | ||
$sizeFromEnum = $constants->first(function($value) use ($size) { | ||
return $value === $size; | ||
}); | ||
|
||
if (is_null($sizeFromEnum)) { | ||
throw new InvalidArgumentException("[$size] is not a valid image size."); | ||
} | ||
|
||
$basePath = static::IMAGE_BASE_PATH; | ||
$id = $this->getAttribute('image_id'); | ||
|
||
if ($retina) { | ||
$sizeFromEnum = Str::finish($sizeFromEnum, '_2x'); | ||
} | ||
|
||
return "$basePath/t_$sizeFromEnum/$id.jpg"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
namespace MarcReichel\IGDBLaravel\Models; | ||
|
||
class Screenshot extends Model | ||
class Screenshot extends Image | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
|
||
namespace MarcReichel\IGDBLaravel\Tests; | ||
|
||
use Illuminate\Support\Facades\Cache; | ||
use Illuminate\Support\Facades\Http; | ||
use InvalidArgumentException; | ||
use MarcReichel\IGDBLaravel\Enums\Image\Size; | ||
use MarcReichel\IGDBLaravel\Models\Artwork; | ||
use MarcReichel\IGDBLaravel\Models\CharacterMugShot; | ||
use MarcReichel\IGDBLaravel\Models\CompanyLogo; | ||
use MarcReichel\IGDBLaravel\Models\Cover; | ||
use MarcReichel\IGDBLaravel\Models\GameEngineLogo; | ||
use MarcReichel\IGDBLaravel\Models\Image; | ||
use MarcReichel\IGDBLaravel\Models\PlatformLogo; | ||
use MarcReichel\IGDBLaravel\Models\Screenshot; | ||
|
||
class ImageTest extends TestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Cache::put('igdb_cache.access_token', 'some-token'); | ||
|
||
Http::fake([ | ||
'*' => Http::response([ | ||
[ | ||
'id' => 1, | ||
'alpha_channel' => false, | ||
'animated' => false, | ||
'checksum' => 'abc', | ||
'height' => 100, | ||
'image_id' => 'abc', | ||
'url' => '//images.igdb.com/igdb/image/upload/t_thumb/abc.jpg', | ||
'width' => 100, | ||
], | ||
]), | ||
]); | ||
} | ||
|
||
/** @test */ | ||
public function artwork_should_be_mapped_as_instance_of_image(): void | ||
{ | ||
self::assertInstanceOf(Image::class, Artwork::first()); | ||
} | ||
|
||
/** @test */ | ||
public function character_mug_shot_should_be_mapped_as_instance_of_image(): void | ||
{ | ||
self::assertInstanceOf(Image::class, CharacterMugShot::first()); | ||
} | ||
|
||
/** @test */ | ||
public function company_logo_should_be_mapped_as_instance_of_image(): void | ||
{ | ||
self::assertInstanceOf(Image::class, CompanyLogo::first()); | ||
} | ||
|
||
/** @test */ | ||
public function cover_should_be_mapped_as_instance_of_image(): void | ||
{ | ||
self::assertInstanceOf(Image::class, Cover::first()); | ||
} | ||
|
||
/** @test */ | ||
public function game_engine_logo_should_be_mapped_as_instance_of_image(): void | ||
{ | ||
self::assertInstanceOf(Image::class, GameEngineLogo::first()); | ||
} | ||
|
||
/** @test */ | ||
public function platform_logo_should_be_mapped_as_instance_of_image(): void | ||
{ | ||
self::assertInstanceOf(Image::class, PlatformLogo::first()); | ||
} | ||
|
||
/** @test */ | ||
public function screenshot_should_be_mapped_as_instance_of_image(): void | ||
{ | ||
self::assertInstanceOf(Image::class, Screenshot::first()); | ||
} | ||
|
||
/** @test */ | ||
public function it_should_generate_default_image_url_without_attributes(): void | ||
{ | ||
$url = Artwork::first()->getUrl(); | ||
|
||
self::assertEquals('//images.igdb.com/igdb/image/upload/t_thumb/abc.jpg', $url); | ||
} | ||
|
||
/** @test */ | ||
public function it_should_generate_desired_image_url_with_parameter(): void | ||
{ | ||
$url = Artwork::first()->getUrl(Size::COVER_BIG); | ||
|
||
self::assertEquals('//images.igdb.com/igdb/image/upload/t_cover_big/abc.jpg', $url); | ||
} | ||
|
||
/** @test */ | ||
public function it_should_generate_retina_image_url(): void | ||
{ | ||
$url = Artwork::first()->getUrl(Size::COVER_BIG, true); | ||
|
||
self::assertEquals('//images.igdb.com/igdb/image/upload/t_cover_big_2x/abc.jpg', $url); | ||
} | ||
|
||
/** @test */ | ||
public function it_should_throw_exception_with_invalid_image_size(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
|
||
Artwork::first()->getUrl('foo'); | ||
} | ||
} |