]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/ThumbnailerTest.php
namespacing: \Shaarli\ApplicationUtils
[github/shaarli/Shaarli.git] / tests / ThumbnailerTest.php
CommitLineData
1b93137e
A
1<?php
2
e85b7a05
A
3namespace Shaarli;
4
5use PHPUnit\Framework\TestCase;
6use Shaarli\Config\ConfigManager;
7use WebThumbnailer\Application\ConfigManager as WTConfigManager;
1b93137e
A
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 */
e85b7a05 17class ThumbnailerTest extends TestCase
1b93137e 18{
e85b7a05
A
19 const WIDTH = 190;
20
21 const HEIGHT = 210;
22
1b93137e 23 /**
e85b7a05 24 * @var Thumbnailer;
1b93137e 25 */
e85b7a05
A
26 protected $thumbnailer;
27
b302b3c5
A
28 /**
29 * @var ConfigManager
30 */
31 protected $conf;
32
e85b7a05 33 public function setUp()
1b93137e 34 {
b302b3c5
A
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);
e85b7a05 40
b302b3c5 41 $this->thumbnailer = new Thumbnailer($this->conf);
e85b7a05
A
42 // cache files in the sandbox
43 WTConfigManager::addFile('tests/utils/config/wt.json');
44 }
45
46 public function tearDown()
47 {
48 $this->rrmdirContent('sandbox/');
49 }
1b93137e 50
e85b7a05 51 /**
b302b3c5 52 * Test a thumbnail with a custom size in 'all' mode.
e85b7a05 53 */
b302b3c5 54 public function testThumbnailAllValid()
e85b7a05
A
55 {
56 $thumb = $this->thumbnailer->get('https://github.com/shaarli/Shaarli/');
1b93137e
A
57 $this->assertNotFalse($thumb);
58 $image = imagecreatefromstring(file_get_contents($thumb));
e85b7a05
A
59 $this->assertEquals(self::WIDTH, imagesx($image));
60 $this->assertEquals(self::HEIGHT, imagesy($image));
1b93137e
A
61 }
62
b302b3c5
A
63 /**
64 * Test a thumbnail with a custom size in 'common' mode.
65 */
66 public function testThumbnailCommonValid()
67 {
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));
74 }
75
76 /**
77 * Test a thumbnail in 'common' mode which isn't include in common websites.
78 */
79 public function testThumbnailCommonInvalid()
80 {
81 $this->conf->set('thumbnails.mode', Thumbnailer::MODE_COMMON);
82 $thumb = $this->thumbnailer->get('https://github.com/shaarli/Shaarli/');
83 $this->assertFalse($thumb);
84 }
85
1b93137e
A
86 /**
87 * Test a thumbnail that can't be retrieved.
1b93137e
A
88 */
89 public function testThumbnailNotValid()
90 {
91 $oldlog = ini_get('error_log');
92 ini_set('error_log', '/dev/null');
93
94 $thumbnailer = new Thumbnailer(new ConfigManager());
95 $thumb = $thumbnailer->get('nope');
96 $this->assertFalse($thumb);
97
98 ini_set('error_log', $oldlog);
99 }
e85b7a05 100
067c2dd8
V
101 protected function rrmdirContent($dir)
102 {
e85b7a05
A
103 if (is_dir($dir)) {
104 $objects = scandir($dir);
105 foreach ($objects as $object) {
106 if ($object != "." && $object != "..") {
067c2dd8 107 if (is_dir($dir."/".$object)) {
e85b7a05 108 $this->rrmdirContent($dir."/".$object);
067c2dd8 109 } else {
e85b7a05 110 unlink($dir."/".$object);
067c2dd8 111 }
e85b7a05
A
112 }
113 }
114 }
115 }
1b93137e 116}