ThumbnailerTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace Shaarli;
  3. use PHPUnit\Framework\TestCase;
  4. use Shaarli\Config\ConfigManager;
  5. use WebThumbnailer\Application\ConfigManager as WTConfigManager;
  6. /**
  7. * Class ThumbnailerTest
  8. *
  9. * We only make 1 thumb test because:
  10. *
  11. * 1. the thumbnailer library is itself tested
  12. * 2. we don't want to make too many external requests during the tests
  13. */
  14. class ThumbnailerTest extends TestCase
  15. {
  16. const WIDTH = 190;
  17. const HEIGHT = 210;
  18. /**
  19. * @var Thumbnailer;
  20. */
  21. protected $thumbnailer;
  22. /**
  23. * @var ConfigManager
  24. */
  25. protected $conf;
  26. public function setUp()
  27. {
  28. $this->conf = new ConfigManager('tests/utils/config/configJson');
  29. $this->conf->set('thumbnails.mode', Thumbnailer::MODE_ALL);
  30. $this->conf->set('thumbnails.width', self::WIDTH);
  31. $this->conf->set('thumbnails.height', self::HEIGHT);
  32. $this->conf->set('dev.debug', true);
  33. $this->thumbnailer = new Thumbnailer($this->conf);
  34. // cache files in the sandbox
  35. WTConfigManager::addFile('tests/utils/config/wt.json');
  36. }
  37. public function tearDown()
  38. {
  39. $this->rrmdirContent('sandbox/');
  40. }
  41. /**
  42. * Test a thumbnail with a custom size in 'all' mode.
  43. */
  44. public function testThumbnailAllValid()
  45. {
  46. $thumb = $this->thumbnailer->get('https://github.com/shaarli/Shaarli/');
  47. $this->assertNotFalse($thumb);
  48. $image = imagecreatefromstring(file_get_contents($thumb));
  49. $this->assertEquals(self::WIDTH, imagesx($image));
  50. $this->assertEquals(self::HEIGHT, imagesy($image));
  51. }
  52. /**
  53. * Test a thumbnail with a custom size in 'common' mode.
  54. */
  55. public function testThumbnailCommonValid()
  56. {
  57. $this->conf->set('thumbnails.mode', Thumbnailer::MODE_COMMON);
  58. $thumb = $this->thumbnailer->get('https://imgur.com/jlFgGpe');
  59. $this->assertNotFalse($thumb);
  60. $image = imagecreatefromstring(file_get_contents($thumb));
  61. $this->assertEquals(self::WIDTH, imagesx($image));
  62. $this->assertEquals(self::HEIGHT, imagesy($image));
  63. }
  64. /**
  65. * Test a thumbnail in 'common' mode which isn't include in common websites.
  66. */
  67. public function testThumbnailCommonInvalid()
  68. {
  69. $this->conf->set('thumbnails.mode', Thumbnailer::MODE_COMMON);
  70. $thumb = $this->thumbnailer->get('https://github.com/shaarli/Shaarli/');
  71. $this->assertFalse($thumb);
  72. }
  73. /**
  74. * Test a thumbnail that can't be retrieved.
  75. */
  76. public function testThumbnailNotValid()
  77. {
  78. $oldlog = ini_get('error_log');
  79. ini_set('error_log', '/dev/null');
  80. $thumbnailer = new Thumbnailer(new ConfigManager());
  81. $thumb = $thumbnailer->get('nope');
  82. $this->assertFalse($thumb);
  83. ini_set('error_log', $oldlog);
  84. }
  85. protected function rrmdirContent($dir)
  86. {
  87. if (is_dir($dir)) {
  88. $objects = scandir($dir);
  89. foreach ($objects as $object) {
  90. if ($object != "." && $object != "..") {
  91. if (is_dir($dir."/".$object)) {
  92. $this->rrmdirContent($dir."/".$object);
  93. } else {
  94. unlink($dir."/".$object);
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }