Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

add random GET argument for imageUrl faker #494

Merged
merged 4 commits into from
Jan 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Faker/Provider/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ class Image extends Base
/**
* Generate the URL that will return a random image
*
* @example 'http://lorempixel.com/640/480/'
* Set randomize to false to remove the random GET parameter at the end of the url.
*
* @example 'http://lorempixel.com/640/480/?12345'
*/
public static function imageUrl($width = 640, $height = 480, $category = null)
public static function imageUrl($width = 640, $height = 480, $category = null, $randomize = true)
{
$url = "http://lorempixel.com/{$width}/{$height}/";
if ($category) {
Expand All @@ -27,6 +29,10 @@ public static function imageUrl($width = 640, $height = 480, $category = null)
$url .= "{$category}/";
}

if ($randomize) {
$url .= '?' . static::randomNumber(5, true);
}

return $url;
}

Expand Down
21 changes: 15 additions & 6 deletions test/Faker/Provider/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,28 @@

class ImageTest extends \PHPUnit_Framework_TestCase
{
public function testUrlWithDefaults()
public function testImageUrlUses640x680AsTheDefaultSize()
{
$this->assertEquals(Image::imageUrl(), 'http://lorempixel.com/640/480/');
$this->assertRegExp('#^http://lorempixel.com/640/480/#', Image::imageUrl());
}

public function testUrlWithDimensions()
public function testImageUrlAcceptsCustomWidthAndHeight()
{
$this->assertEquals(Image::imageUrl(800, 400), 'http://lorempixel.com/800/400/');
$this->assertRegExp('#^http://lorempixel.com/800/400/#', Image::imageUrl(800, 400));
}

public function testUrlWithDimensionsAndCategory()
public function testImageUrlAcceptsCustomCategory()
{
$this->assertEquals(Image::imageUrl(800, 400, 'nature'), 'http://lorempixel.com/800/400/nature/');
$this->assertRegExp('#^http://lorempixel.com/800/400/nature/#', Image::imageUrl(800, 400, 'nature'));
}

public function testImageUrlAddsARandomGetParameterByDefault()
{
$url = Image::imageUrl(800, 400);
$splitUrl = preg_split('/\?/', $url);

$this->assertEquals(count($splitUrl), 2);
$this->assertRegexp('#\d{5}#', $splitUrl[1]);
}

/**
Expand Down