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

Commit

Permalink
Merge pull request #494 from fzaninotto/Nicals-lorempixel_randomize
Browse files Browse the repository at this point in the history
add random GET argument for imageUrl faker
  • Loading branch information
fzaninotto committed Jan 4, 2015
2 parents 86eaa6f + f0d4179 commit a09cd81
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
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

0 comments on commit a09cd81

Please sign in to comment.