ThumbnailerTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. public function setUp()
  23. {
  24. $conf = new ConfigManager('tests/utils/config/configJson');
  25. $conf->set('thumbnails.width', self::WIDTH);
  26. $conf->set('thumbnails.height', self::HEIGHT);
  27. $conf->set('dev.debug', true);
  28. $this->thumbnailer = new Thumbnailer($conf);
  29. // cache files in the sandbox
  30. WTConfigManager::addFile('tests/utils/config/wt.json');
  31. }
  32. public function tearDown()
  33. {
  34. $this->rrmdirContent('sandbox/');
  35. }
  36. /**
  37. * Test a thumbnail with a custom size.
  38. */
  39. public function testThumbnailValid()
  40. {
  41. $thumb = $this->thumbnailer->get('https://github.com/shaarli/Shaarli/');
  42. $this->assertNotFalse($thumb);
  43. $image = imagecreatefromstring(file_get_contents($thumb));
  44. $this->assertEquals(self::WIDTH, imagesx($image));
  45. $this->assertEquals(self::HEIGHT, imagesy($image));
  46. }
  47. /**
  48. * Test a thumbnail that can't be retrieved.
  49. */
  50. public function testThumbnailNotValid()
  51. {
  52. $oldlog = ini_get('error_log');
  53. ini_set('error_log', '/dev/null');
  54. $thumbnailer = new Thumbnailer(new ConfigManager());
  55. $thumb = $thumbnailer->get('nope');
  56. $this->assertFalse($thumb);
  57. ini_set('error_log', $oldlog);
  58. }
  59. protected function rrmdirContent($dir) {
  60. if (is_dir($dir)) {
  61. $objects = scandir($dir);
  62. foreach ($objects as $object) {
  63. if ($object != "." && $object != "..") {
  64. if (is_dir($dir."/".$object))
  65. $this->rrmdirContent($dir."/".$object);
  66. else
  67. unlink($dir."/".$object);
  68. }
  69. }
  70. }
  71. }
  72. }