diff options
Diffstat (limited to 'tests/ThumbnailerTest.php')
-rw-r--r-- | tests/ThumbnailerTest.php | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/tests/ThumbnailerTest.php b/tests/ThumbnailerTest.php new file mode 100644 index 00000000..c01849f7 --- /dev/null +++ b/tests/ThumbnailerTest.php | |||
@@ -0,0 +1,116 @@ | |||
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 | /** | ||
29 | * @var ConfigManager | ||
30 | */ | ||
31 | protected $conf; | ||
32 | |||
33 | public function setUp() | ||
34 | { | ||
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); | ||
40 | |||
41 | $this->thumbnailer = new Thumbnailer($this->conf); | ||
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 | } | ||
50 | |||
51 | /** | ||
52 | * Test a thumbnail with a custom size in 'all' mode. | ||
53 | */ | ||
54 | public function testThumbnailAllValid() | ||
55 | { | ||
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)); | ||
61 | } | ||
62 | |||
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 | |||
86 | /** | ||
87 | * Test a thumbnail that can't be retrieved. | ||
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 | } | ||
100 | |||
101 | protected function rrmdirContent($dir) | ||
102 | { | ||
103 | if (is_dir($dir)) { | ||
104 | $objects = scandir($dir); | ||
105 | foreach ($objects as $object) { | ||
106 | if ($object != "." && $object != "..") { | ||
107 | if (is_dir($dir."/".$object)) { | ||
108 | $this->rrmdirContent($dir."/".$object); | ||
109 | } else { | ||
110 | unlink($dir."/".$object); | ||
111 | } | ||
112 | } | ||
113 | } | ||
114 | } | ||
115 | } | ||
116 | } | ||