diff --git a/src/Faker/Provider/Image.php b/src/Faker/Provider/Image.php index 30bef88f86..e915ff474a 100644 --- a/src/Faker/Provider/Image.php +++ b/src/Faker/Provider/Image.php @@ -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) { @@ -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; } diff --git a/test/Faker/Provider/ImageTest.php b/test/Faker/Provider/ImageTest.php index 8f64a1a50e..02f2d49071 100644 --- a/test/Faker/Provider/ImageTest.php +++ b/test/Faker/Provider/ImageTest.php @@ -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]); } /**