Skip to content

Commit

Permalink
Add support for self-hosted Lorem Space
Browse files Browse the repository at this point in the history
  • Loading branch information
morawskim committed Oct 1, 2023
1 parent a59d770 commit b1d8782
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 52 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ $path = $faker->picsum(null, 400, 400, true); // /tmp/72c04225dd87efc261d29d3a05
// picsum($dir = null, $width = 640, $height = 480, $fullPath = true, $id = null, $randomize = true, $gray = false, $blur = null, $imageExtension)

// lorem space
// you can use a self owned service, if there are problems with default one
// LoremSpaceProvider::setApiUrl('https://my-self-owned-lorem-space-service.example.com/image/');
$url = $faker->loremSpaceUrl(\Mmo\Faker\LoremSpaceProvider::CATEGORY_FACE); // https://api.lorem.space/image/face?w=640&h=480
// download image to tmp dir
$path = $faker->loremSpace(\Mmo\Faker\LoremSpaceProvider::CATEGORY_FACE); // /tmp/fd3646c544a9a46bd16d1d097e737ee4.jpg
Expand Down
32 changes: 13 additions & 19 deletions src/LoremSpaceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Mmo\Faker;

use Faker\Provider\Base as BaseProvider;
use InvalidArgumentException;

class LoremSpaceProvider extends BaseProvider
{
Expand All @@ -18,18 +17,17 @@ class LoremSpaceProvider extends BaseProvider
public const CATEGORY_FURNITURE = 'furniture';
public const CATEGORY_CAR = 'car';

private static $CATEGORIES = [
self::CATEGORY_MOVIE,
self::CATEGORY_GAME,
self::CATEGORY_ALBUM,
self::CATEGORY_BOOK,
self::CATEGORY_FACE,
self::CATEGORY_FASHION,
self::CATEGORY_SHOES,
self::CATEGORY_WATCH,
self::CATEGORY_FURNITURE,
self::CATEGORY_CAR,
];
private static $API_URL = 'https://api.lorem.space/image/';

public static function setApiUrl(string $url)
{
self::$API_URL = rtrim($url, '/') . '/';
}

public static function getApiUrl()
{
return self::$API_URL;
}

public static function loremSpaceUrl($category, $width = 640, $height = 480)
{
Expand All @@ -52,13 +50,9 @@ private static function buildQueryString(int $width, int $height)
return '?' . http_build_query($queryParams);
}

private static function buildLoremSpaceUrl($category, $queryString)
protected static function buildLoremSpaceUrl($category, $queryString)
{
if (!in_array($category, self::$CATEGORIES, true)) {
throw new InvalidArgumentException(sprintf('Invalid image category "%s"', $category));
}

$baseUrl = 'https://api.lorem.space/image/';
$baseUrl = self::getApiUrl();

return $baseUrl . $category . $queryString;
}
Expand Down
80 changes: 47 additions & 33 deletions test/Faker/LoremSpaceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,33 @@

namespace Mmo\Faker\Test;

use InvalidArgumentException;
use Mmo\Faker\LoremSpaceProvider;
use PHPUnit\Framework\TestCase;

class LoremSpaceProviderTest extends TestCase
{
/**
* @after
*/
protected function resetApiUrl()
{
LoremSpaceProvider::setApiUrl('https://api.lorem.space/image/');
}

public function testGetterAndSetterApiUrl()
{
$apiUrl = LoremSpaceProvider::getApiUrl();
$this->assertSame('https://api.lorem.space/image/', $apiUrl);

LoremSpaceProvider::setApiUrl('https://loremspace.example.com/image/');
$apiUrl = LoremSpaceProvider::getApiUrl();
$this->assertSame('https://loremspace.example.com/image/', $apiUrl);
$this->assertSame(
'https://loremspace.example.com/image/album?w=640&h=480',
LoremSpaceProvider::loremSpaceUrl(LoremSpaceProvider::CATEGORY_ALBUM)
);
}

public function testLoremSpaceUrlUsesDefaultParameters()
{
$this->assertSame(
Expand All @@ -24,30 +45,6 @@ public function testLoremSpaceUrlAcceptsCustomWidthAndHeight()
);
}

public function testNotSupportedCategory()
{
$this->expectException(InvalidArgumentException::class);
if (method_exists($this, 'expectExceptionMessageMatches')) {
$this->expectExceptionMessageMatches('/^Invalid image category/');
} else {
$this->expectExceptionMessageRegExp('/^Invalid image category/');
}

LoremSpaceProvider::loremSpaceUrl('foo');
}

public function testNotSupportedCategoryForDownload()
{
$this->expectException(InvalidArgumentException::class);
if (method_exists($this, 'expectExceptionMessageMatches')) {
$this->expectExceptionMessageMatches('/^Invalid image category/');
} else {
$this->expectExceptionMessageRegExp('/^Invalid image category/');
}

LoremSpaceProvider::loremSpace('foo');
}

/**
* @dataProvider normalizeImageSizeProvider
*/
Expand All @@ -64,11 +61,34 @@ public function testNormalizeImageSizeValue(int $width, int $height, int $expect
);
}

public function testPicsumDownloadWithDefaults()
public function normalizeImageSizeProvider(): iterable
{
yield 'too_small' => [0, 0, 8];
yield 'too_big' => [3000, 3000, 2000];
}

public function testLoremSpaceDownloadWithDefaults()
{
$file = LoremSpaceProvider::loremSpace(LoremSpaceProvider::CATEGORY_FACE, sys_get_temp_dir());
$this->assertFileExists($file);
$this->checkDownloadedFile($file);
}

public function testSelfOwnedLoremSpaceUrl()
{
$selfOwnedLoremSpaceUrl = getenv('LOREM_SPACE_SELF_OWNED_URL');

if (empty($selfOwnedLoremSpaceUrl)) {
self::markTestSkipped(sprintf('The env variable "%s" is not set', 'LOREM_SPACE_SELF_OWNED_URL'));
}

LoremSpaceProvider::setApiUrl($selfOwnedLoremSpaceUrl);
$file = LoremSpaceProvider::loremSpace('mycategory', sys_get_temp_dir());
$this->checkDownloadedFile($file);
}

private function checkDownloadedFile($file): void
{
$this->assertFileExists($file);
if (function_exists('getimagesize')) {
list($width, $height, $type) = getimagesize($file);
$this->assertEquals(640, $width);
Expand All @@ -81,10 +101,4 @@ public function testPicsumDownloadWithDefaults()
unlink($file);
}
}

public function normalizeImageSizeProvider(): iterable
{
yield 'too_small' => [0, 0, 8];
yield 'too_big' => [3000, 3000, 2000];
}
}

0 comments on commit b1d8782

Please sign in to comment.