Skip to content

Commit

Permalink
✨ Introduce image url generation
Browse files Browse the repository at this point in the history
  • Loading branch information
marcreichel committed Nov 7, 2021
1 parent 40f49b8 commit e57c454
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 7 deletions.
17 changes: 17 additions & 0 deletions src/Enums/Image/Size.php
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';
}
2 changes: 1 addition & 1 deletion src/Models/Artwork.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MarcReichel\IGDBLaravel\Models;

class Artwork extends Model
class Artwork extends Image
{
//
}
2 changes: 1 addition & 1 deletion src/Models/CharacterMugShot.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MarcReichel\IGDBLaravel\Models;

class CharacterMugShot extends Model
class CharacterMugShot extends Image
{
//
}
2 changes: 1 addition & 1 deletion src/Models/CompanyLogo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MarcReichel\IGDBLaravel\Models;

class CompanyLogo extends Model
class CompanyLogo extends Image
{
//
}
2 changes: 1 addition & 1 deletion src/Models/Cover.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MarcReichel\IGDBLaravel\Models;

class Cover extends Model
class Cover extends Image
{
//
}
2 changes: 1 addition & 1 deletion src/Models/GameEngineLogo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MarcReichel\IGDBLaravel\Models;

class GameEngineLogo extends Model
class GameEngineLogo extends Image
{
//
}
35 changes: 35 additions & 0 deletions src/Models/Image.php
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";
}
}
2 changes: 1 addition & 1 deletion src/Models/PlatformLogo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MarcReichel\IGDBLaravel\Models;

class PlatformLogo extends Model
class PlatformLogo extends Image
{
//
}
2 changes: 1 addition & 1 deletion src/Models/Screenshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MarcReichel\IGDBLaravel\Models;

class Screenshot extends Model
class Screenshot extends Image
{
//
}
115 changes: 115 additions & 0 deletions tests/ImageTest.php
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');
}
}

0 comments on commit e57c454

Please sign in to comment.