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