ImageTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace Faker\Test\Provider;
  3. use Faker\Provider\Image;
  4. class ImageTest extends \PHPUnit_Framework_TestCase
  5. {
  6. public function testImageUrlUses640x680AsTheDefaultSize()
  7. {
  8. $this->assertRegExp('#^http://lorempixel.com/640/480/#', Image::imageUrl());
  9. }
  10. public function testImageUrlAcceptsCustomWidthAndHeight()
  11. {
  12. $this->assertRegExp('#^http://lorempixel.com/800/400/#', Image::imageUrl(800, 400));
  13. }
  14. public function testImageUrlAcceptsCustomCategory()
  15. {
  16. $this->assertRegExp('#^http://lorempixel.com/800/400/nature/#', Image::imageUrl(800, 400, 'nature'));
  17. }
  18. public function testImageUrlAcceptsCustomText()
  19. {
  20. $this->assertRegExp('#^http://lorempixel.com/800/400/nature/Faker#', Image::imageUrl(800, 400, 'nature', false, 'Faker'));
  21. }
  22. public function testImageUrlAddsARandomGetParameterByDefault()
  23. {
  24. $url = Image::imageUrl(800, 400);
  25. $splitUrl = preg_split('/\?/', $url);
  26. $this->assertEquals(count($splitUrl), 2);
  27. $this->assertRegexp('#\d{5}#', $splitUrl[1]);
  28. }
  29. /**
  30. * @expectedException \InvalidArgumentException
  31. */
  32. public function testUrlWithDimensionsAndBadCategory()
  33. {
  34. Image::imageUrl(800, 400, 'bullhonky');
  35. }
  36. public function testDownloadWithDefaults()
  37. {
  38. $url = "http://www.lorempixel.com/";
  39. $curlPing = curl_init($url);
  40. curl_setopt($curlPing, CURLOPT_TIMEOUT, 5);
  41. curl_setopt($curlPing, CURLOPT_CONNECTTIMEOUT, 5);
  42. curl_setopt($curlPing, CURLOPT_RETURNTRANSFER, true);
  43. $data = curl_exec($curlPing);
  44. $httpCode = curl_getinfo($curlPing, CURLINFO_HTTP_CODE);
  45. curl_close($curlPing);
  46. if ($httpCode < 200 | $httpCode > 300) {
  47. $this->markTestSkipped("LoremPixel is offline, skipping image download");
  48. }
  49. $file = Image::image(sys_get_temp_dir());
  50. $this->assertFileExists($file);
  51. if (function_exists('getimagesize')) {
  52. list($width, $height, $type, $attr) = getimagesize($file);
  53. $this->assertEquals(640, $width);
  54. $this->assertEquals(480, $height);
  55. $this->assertEquals(constant('IMAGETYPE_JPEG'), $type);
  56. } else {
  57. $this->assertEquals('jpg', pathinfo($file, PATHINFO_EXTENSION));
  58. }
  59. if (file_exists($file)) {
  60. unlink($file);
  61. }
  62. }
  63. }