5 use PHPUnit\Framework\TestCase
;
6 use Shaarli\Config\ConfigManager
;
7 use WebThumbnailer\Application\ConfigManager
as WTConfigManager
;
10 * Class ThumbnailerTest
12 * We only make 1 thumb test because:
14 * 1. the thumbnailer library is itself tested
15 * 2. we don't want to make too many external requests during the tests
17 class ThumbnailerTest
extends TestCase
26 protected $thumbnailer;
33 public function setUp()
35 $this->conf
= new ConfigManager('tests/utils/config/configJson');
36 $this->conf
->set('thumbnails.mode', Thumbnailer
::MODE_ALL
);
37 $this->conf
->set('thumbnails.width', self
::WIDTH
);
38 $this->conf
->set('thumbnails.height', self
::HEIGHT
);
39 $this->conf
->set('dev.debug', true);
41 $this->thumbnailer
= new Thumbnailer($this->conf
);
42 // cache files in the sandbox
43 WTConfigManager
::addFile('tests/utils/config/wt.json');
46 public function tearDown()
48 $this->rrmdirContent('sandbox/');
52 * Test a thumbnail with a custom size in 'all' mode.
54 public function testThumbnailAllValid()
56 $thumb = $this->thumbnailer
->get('https://github.com/shaarli/Shaarli/');
57 $this->assertNotFalse($thumb);
58 $image = imagecreatefromstring(file_get_contents($thumb));
59 $this->assertEquals(self
::WIDTH
, imagesx($image));
60 $this->assertEquals(self
::HEIGHT
, imagesy($image));
64 * Test a thumbnail with a custom size in 'common' mode.
66 public function testThumbnailCommonValid()
68 $this->conf
->set('thumbnails.mode', Thumbnailer
::MODE_COMMON
);
69 $thumb = $this->thumbnailer
->get('https://imgur.com/jlFgGpe');
70 $this->assertNotFalse($thumb);
71 $image = imagecreatefromstring(file_get_contents($thumb));
72 $this->assertEquals(self
::WIDTH
, imagesx($image));
73 $this->assertEquals(self
::HEIGHT
, imagesy($image));
77 * Test a thumbnail in 'common' mode which isn't include in common websites.
79 public function testThumbnailCommonInvalid()
81 $this->conf
->set('thumbnails.mode', Thumbnailer
::MODE_COMMON
);
82 $thumb = $this->thumbnailer
->get('https://github.com/shaarli/Shaarli/');
83 $this->assertFalse($thumb);
87 * Test a thumbnail that can't be retrieved.
89 public function testThumbnailNotValid()
91 $oldlog = ini_get('error_log');
92 ini_set('error_log', '/dev/null');
94 $thumbnailer = new Thumbnailer(new ConfigManager());
95 $thumb = $thumbnailer->get('nope');
96 $this->assertFalse($thumb);
98 ini_set('error_log', $oldlog);
101 protected function rrmdirContent($dir) {
103 $objects = scandir($dir);
104 foreach ($objects as $object) {
105 if ($object != "." && $object != "..") {
106 if (is_dir($dir."/".$object))
107 $this->rrmdirContent($dir."/".$object);
109 unlink($dir."/".$object);