]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - tests/ThumbnailerTest.php
Merge pull request #1234 from virtualtam/lint
[github/shaarli/Shaarli.git] / tests / ThumbnailerTest.php
index db10932114a6f9f078ed99407a2b8273f3dee047..c01849f7f089e45e1ca17af6dcb0ee0a3c2e5daf 100644 (file)
@@ -1,7 +1,10 @@
 <?php
 
-require_once 'application/Thumbnailer.php';
-require_once 'application/config/ConfigManager.php';
+namespace Shaarli;
+
+use PHPUnit\Framework\TestCase;
+use Shaarli\Config\ConfigManager;
+use WebThumbnailer\Application\ConfigManager as WTConfigManager;
 
 /**
  * Class ThumbnailerTest
@@ -11,31 +14,77 @@ require_once 'application/config/ConfigManager.php';
  *   1. the thumbnailer library is itself tested
  *   2. we don't want to make too many external requests during the tests
  */
-class ThumbnailerTest extends PHPUnit_Framework_TestCase
+class ThumbnailerTest extends TestCase
 {
+    const WIDTH = 190;
+
+    const HEIGHT = 210;
+
+    /**
+     * @var Thumbnailer;
+     */
+    protected $thumbnailer;
+
+    /**
+     * @var ConfigManager
+     */
+    protected $conf;
+
+    public function setUp()
+    {
+        $this->conf = new ConfigManager('tests/utils/config/configJson');
+        $this->conf->set('thumbnails.mode', Thumbnailer::MODE_ALL);
+        $this->conf->set('thumbnails.width', self::WIDTH);
+        $this->conf->set('thumbnails.height', self::HEIGHT);
+        $this->conf->set('dev.debug', true);
+
+        $this->thumbnailer = new Thumbnailer($this->conf);
+        // cache files in the sandbox
+        WTConfigManager::addFile('tests/utils/config/wt.json');
+    }
+
+    public function tearDown()
+    {
+        $this->rrmdirContent('sandbox/');
+    }
+
     /**
-     * Test a thumbnail with a custom size.
+     * Test a thumbnail with a custom size in 'all' mode.
      */
-    public function testThumbnailValid()
+    public function testThumbnailAllValid()
     {
-        $conf = new ConfigManager('tests/utils/config/configJson');
-        $width = 200;
-        $height = 200;
-        $conf->set('thumbnails.width', $width);
-        $conf->set('thumbnails.height', $height);
-
-        $thumbnailer = new Thumbnailer($conf);
-        $thumb = $thumbnailer->get('https://github.com/shaarli/Shaarli/');
+        $thumb = $this->thumbnailer->get('https://github.com/shaarli/Shaarli/');
         $this->assertNotFalse($thumb);
         $image = imagecreatefromstring(file_get_contents($thumb));
-        $this->assertEquals($width, imagesx($image));
-        $this->assertEquals($height, imagesy($image));
+        $this->assertEquals(self::WIDTH, imagesx($image));
+        $this->assertEquals(self::HEIGHT, imagesy($image));
+    }
+
+    /**
+     * Test a thumbnail with a custom size in 'common' mode.
+     */
+    public function testThumbnailCommonValid()
+    {
+        $this->conf->set('thumbnails.mode', Thumbnailer::MODE_COMMON);
+        $thumb = $this->thumbnailer->get('https://imgur.com/jlFgGpe');
+        $this->assertNotFalse($thumb);
+        $image = imagecreatefromstring(file_get_contents($thumb));
+        $this->assertEquals(self::WIDTH, imagesx($image));
+        $this->assertEquals(self::HEIGHT, imagesy($image));
+    }
+
+    /**
+     * Test a thumbnail in 'common' mode which isn't include in common websites.
+     */
+    public function testThumbnailCommonInvalid()
+    {
+        $this->conf->set('thumbnails.mode', Thumbnailer::MODE_COMMON);
+        $thumb = $this->thumbnailer->get('https://github.com/shaarli/Shaarli/');
+        $this->assertFalse($thumb);
     }
 
     /**
      * Test a thumbnail that can't be retrieved.
-     *
-     * @expectedException WebThumbnailer\Exception\ThumbnailNotFoundException
      */
     public function testThumbnailNotValid()
     {
@@ -48,4 +97,20 @@ class ThumbnailerTest extends PHPUnit_Framework_TestCase
 
         ini_set('error_log', $oldlog);
     }
+
+    protected function rrmdirContent($dir)
+    {
+        if (is_dir($dir)) {
+            $objects = scandir($dir);
+            foreach ($objects as $object) {
+                if ($object != "." && $object != "..") {
+                    if (is_dir($dir."/".$object)) {
+                        $this->rrmdirContent($dir."/".$object);
+                    } else {
+                        unlink($dir."/".$object);
+                    }
+                }
+            }
+        }
+    }
 }