ImageTest.php 2.4 KB

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