From 572a852a605606b256201203aab2d37f879d4d15 Mon Sep 17 00:00:00 2001 From: Nicolas Appriou Date: Fri, 18 Jul 2014 09:50:25 +0200 Subject: [PATCH 1/4] add random GET argument for imageUrl faker --- src/Faker/Provider/Image.php | 8 +++++++- test/Faker/Provider/ImageTest.php | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Faker/Provider/Image.php b/src/Faker/Provider/Image.php index 30bef88f86..3073ef4e19 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 * + * Set randomize to false to add a random GET parameter at the end of the url. + * * @example 'http://lorempixel.com/640/480/' */ - public static function imageUrl($width = 640, $height = 480, $category = null) + public static function imageUrl($width = 640, $height = 480, $category = null, $randomize=false) { $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..3bb678ddcc 100644 --- a/test/Faker/Provider/ImageTest.php +++ b/test/Faker/Provider/ImageTest.php @@ -21,6 +21,15 @@ public function testUrlWithDimensionsAndCategory() $this->assertEquals(Image::imageUrl(800, 400, 'nature'), 'http://lorempixel.com/800/400/nature/'); } + public function testRandomizedUrl() + { + $url = Image::imageUrl(800, 400, null, true); + $splitUrl = preg_split('/\?/', $url); + + $this->assertEquals(count($splitUrl), 2); + $this->assertEquals($splitUrl[0], 'http://lorempixel.com/800/400/'); + } + /** * @expectedException \InvalidArgumentException */ From 2f779436de60b9d2813e228d42f560759b70be7f Mon Sep 17 00:00:00 2001 From: Nicolas Appriou Date: Fri, 18 Jul 2014 10:21:58 +0200 Subject: [PATCH 2/4] fix coding standarts --- src/Faker/Provider/Image.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Faker/Provider/Image.php b/src/Faker/Provider/Image.php index 3073ef4e19..fa002c1e1c 100644 --- a/src/Faker/Provider/Image.php +++ b/src/Faker/Provider/Image.php @@ -19,7 +19,7 @@ class Image extends Base * * @example 'http://lorempixel.com/640/480/' */ - public static function imageUrl($width = 640, $height = 480, $category = null, $randomize=false) + public static function imageUrl($width = 640, $height = 480, $category = null, $randomize = false) { $url = "http://lorempixel.com/{$width}/{$height}/"; if ($category) { From cb512ae189da3112351a69e4983b6af677ca92f4 Mon Sep 17 00:00:00 2001 From: Nicolas Appriou Date: Tue, 26 Aug 2014 11:42:20 +0200 Subject: [PATCH 3/4] update readme.md --- readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/readme.md b/readme.md index 8f521f686c..d28cf6c034 100644 --- a/readme.md +++ b/readme.md @@ -221,6 +221,8 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle imageUrl($width, $height, 'cats') // 'http://lorempixel.com/800/600/cats/' image($dir = '/tmp', $width = 640, $height = 480) // '/tmp/13b73edae8443990be1aa8f1a483bc27.jpg' image($dir, $width, $height, 'cats') // 'tmp/13b73edae8443990be1aa8f1a483bc27.jpg' it's a cat! + image($dir, $width, $height, 'cats', true) // 'tmp/13b73edae8443990be1aa8f1a483bc27.jpg?52345' it's a cat with a five digit random GET argument, + // thus, each generated image within a single web page will be different (no client cache use). ### `Faker\Provider\Uuid` From f0d41792c919820097b2f54eb4ed82816998c4da Mon Sep 17 00:00:00 2001 From: Francois Zaninotto Date: Mon, 5 Jan 2015 00:22:18 +0100 Subject: [PATCH 4/4] Make randomize true the default --- readme.md | 2 -- src/Faker/Provider/Image.php | 6 +++--- test/Faker/Provider/ImageTest.php | 18 +++++++++--------- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/readme.md b/readme.md index d28cf6c034..8f521f686c 100644 --- a/readme.md +++ b/readme.md @@ -221,8 +221,6 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle imageUrl($width, $height, 'cats') // 'http://lorempixel.com/800/600/cats/' image($dir = '/tmp', $width = 640, $height = 480) // '/tmp/13b73edae8443990be1aa8f1a483bc27.jpg' image($dir, $width, $height, 'cats') // 'tmp/13b73edae8443990be1aa8f1a483bc27.jpg' it's a cat! - image($dir, $width, $height, 'cats', true) // 'tmp/13b73edae8443990be1aa8f1a483bc27.jpg?52345' it's a cat with a five digit random GET argument, - // thus, each generated image within a single web page will be different (no client cache use). ### `Faker\Provider\Uuid` diff --git a/src/Faker/Provider/Image.php b/src/Faker/Provider/Image.php index fa002c1e1c..e915ff474a 100644 --- a/src/Faker/Provider/Image.php +++ b/src/Faker/Provider/Image.php @@ -15,11 +15,11 @@ class Image extends Base /** * Generate the URL that will return a random image * - * Set randomize to false to add a random GET parameter at the end of the url. + * Set randomize to false to remove the random GET parameter at the end of the url. * - * @example 'http://lorempixel.com/640/480/' + * @example 'http://lorempixel.com/640/480/?12345' */ - public static function imageUrl($width = 640, $height = 480, $category = null, $randomize = false) + public static function imageUrl($width = 640, $height = 480, $category = null, $randomize = true) { $url = "http://lorempixel.com/{$width}/{$height}/"; if ($category) { diff --git a/test/Faker/Provider/ImageTest.php b/test/Faker/Provider/ImageTest.php index 3bb678ddcc..02f2d49071 100644 --- a/test/Faker/Provider/ImageTest.php +++ b/test/Faker/Provider/ImageTest.php @@ -6,28 +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 testRandomizedUrl() + public function testImageUrlAddsARandomGetParameterByDefault() { - $url = Image::imageUrl(800, 400, null, true); + $url = Image::imageUrl(800, 400); $splitUrl = preg_split('/\?/', $url); $this->assertEquals(count($splitUrl), 2); - $this->assertEquals($splitUrl[0], 'http://lorempixel.com/800/400/'); + $this->assertRegexp('#\d{5}#', $splitUrl[1]); } /**