]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Move database read/write to FileUtils class + additional unit tests
authorArthurHoaro <arthur@hoa.ro>
Mon, 16 Jan 2017 11:30:18 +0000 (12:30 +0100)
committerArthurHoaro <arthur@hoa.ro>
Tue, 21 Mar 2017 19:16:26 +0000 (20:16 +0100)
application/FileUtils.php
application/LinkDB.php
application/exceptions/IOException.php [new file with mode: 0644]
tests/FileUtilsTest.php [new file with mode: 0644]
tests/LinkDBTest.php

index 6cac9825de24a1ac4aec018cc54017e822064ef7..b8ad897029f7f0318feaf91fd76b728dc4d4ea12 100644 (file)
@@ -1,21 +1,76 @@
 <?php
+
+require_once 'exceptions/IOException.php';
+
 /**
- * Exception class thrown when a filesystem access failure happens
+ * Class FileUtils
+ *
+ * Utility class for file manipulation.
  */
-class IOException extends Exception
+class FileUtils
 {
-    private $path;
+    /**
+     * @var string
+     */
+    protected static $phpPrefix = '<?php /* ';
+
+    /**
+     * @var string
+     */
+    protected static $phpSuffix = ' */ ?>';
 
     /**
-     * Construct a new IOException
+     * Write data into a file (Shaarli database format).
+     * The data is stored in a PHP file, as a comment, in compressed base64 format.
+     *
+     * The file will be created if it doesn't exist.
+     *
+     * @param string $file    File path.
+     * @param string $content Content to write.
+     *
+     * @return int|bool Number of bytes written or false if it fails.
      *
-     * @param string $path    path to the resource that cannot be accessed
-     * @param string $message Custom exception message.
+     * @throws IOException The destination file can't be written.
      */
-    public function __construct($path, $message = '')
+    public static function writeFlatDB($file, $content)
     {
-        $this->path = $path;
-        $this->message = empty($message) ? 'Error accessing' : $message;
-        $this->message .= PHP_EOL . $this->path;
+        if (is_file($file) && !is_writeable($file)) {
+            // The datastore exists but is not writeable
+            throw new IOException($file);
+        } else if (!is_file($file) && !is_writeable(dirname($file))) {
+            // The datastore does not exist and its parent directory is not writeable
+            throw new IOException(dirname($file));
+        }
+
+        return file_put_contents(
+            $file,
+            self::$phpPrefix.base64_encode(gzdeflate(serialize($content))).self::$phpSuffix
+        );
+    }
+
+    /**
+     * Read data from a file containing Shaarli database format content.
+     * If the file isn't readable or doesn't exists, default data will be returned.
+     *
+     * @param string $file    File path.
+     * @param mixed  $default The default value to return if the file isn't readable.
+     *
+     * @return mixed The content unserialized, or default if the file isn't readable, or false if it fails.
+     */
+    public static function readFlatDB($file, $default = null)
+    {
+        // Note that gzinflate is faster than gzuncompress.
+        // See: http://www.php.net/manual/en/function.gzdeflate.php#96439
+        if (is_readable($file)) {
+            return unserialize(
+                gzinflate(
+                    base64_decode(
+                        substr(file_get_contents($file), strlen(self::$phpPrefix), -strlen(self::$phpSuffix))
+                    )
+                )
+            );
+        }
+
+        return $default;
     }
 }
index 4cee2af9942559fd122189687058ddddc1fc4ebf..2fb15040321339cd31aa127aad692416f33e5110 100644 (file)
@@ -50,12 +50,6 @@ class LinkDB implements Iterator, Countable, ArrayAccess
     // Link date storage format
     const LINK_DATE_FORMAT = 'Ymd_His';
 
-    // Datastore PHP prefix
-    protected static $phpPrefix = '<?php /* ';
-
-    // Datastore PHP suffix
-    protected static $phpSuffix = ' */ ?>';
-
     // List of links (associative array)
     //  - key:   link date (e.g. "20110823_124546"),
     //  - value: associative array (keys: title, description...)
@@ -295,16 +289,7 @@ You use the community supported version of the original Shaarli project, by Seba
             return;
         }
 
-        // Read data
-        // Note that gzinflate is faster than gzuncompress.
-        // See: http://www.php.net/manual/en/function.gzdeflate.php#96439
-        $this->links = array();
-
-        if (file_exists($this->datastore)) {
-            $this->links = unserialize(gzinflate(base64_decode(
-                substr(file_get_contents($this->datastore),
-                       strlen(self::$phpPrefix), -strlen(self::$phpSuffix)))));
-        }
+        $this->links = FileUtils::readFlatDB($this->datastore, []);
 
         $toremove = array();
         foreach ($this->links as $key => &$link) {
@@ -361,19 +346,7 @@ You use the community supported version of the original Shaarli project, by Seba
      */
     private function write()
     {
-        if (is_file($this->datastore) && !is_writeable($this->datastore)) {
-            // The datastore exists but is not writeable
-            throw new IOException($this->datastore);
-        } else if (!is_file($this->datastore) && !is_writeable(dirname($this->datastore))) {
-            // The datastore does not exist and its parent directory is not writeable
-            throw new IOException(dirname($this->datastore));
-        }
-
-        file_put_contents(
-            $this->datastore,
-            self::$phpPrefix.base64_encode(gzdeflate(serialize($this->links))).self::$phpSuffix
-        );
-
+        FileUtils::writeFlatDB($this->datastore, $this->links);
     }
 
     /**
diff --git a/application/exceptions/IOException.php b/application/exceptions/IOException.php
new file mode 100644 (file)
index 0000000..b563b23
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+
+/**
+ * Exception class thrown when a filesystem access failure happens
+ */
+class IOException extends Exception
+{
+    private $path;
+
+    /**
+     * Construct a new IOException
+     *
+     * @param string $path    path to the resource that cannot be accessed
+     * @param string $message Custom exception message.
+     */
+    public function __construct($path, $message = '')
+    {
+        $this->path = $path;
+        $this->message = empty($message) ? 'Error accessing' : $message;
+        $this->message .= ' "' . $this->path .'"';
+    }
+}
diff --git a/tests/FileUtilsTest.php b/tests/FileUtilsTest.php
new file mode 100644 (file)
index 0000000..d764e49
--- /dev/null
@@ -0,0 +1,108 @@
+<?php
+
+require_once 'application/FileUtils.php';
+
+/**
+ * Class FileUtilsTest
+ *
+ * Test file utility class.
+ */
+class FileUtilsTest extends PHPUnit_Framework_TestCase
+{
+    /**
+     * @var string Test file path.
+     */
+    protected static $file = 'sandbox/flat.db';
+
+    /**
+     * Delete test file after every test.
+     */
+    public function tearDown()
+    {
+        @unlink(self::$file);
+    }
+
+    /**
+     * Test writeDB, then readDB with different data.
+     */
+    public function testSimpleWriteRead()
+    {
+        $data = ['blue', 'red'];
+        $this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0);
+        $this->assertTrue(startsWith(file_get_contents(self::$file), '<?php /*'));
+        $this->assertEquals($data, FileUtils::readFlatDB(self::$file));
+
+        $data = 0;
+        $this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0);
+        $this->assertEquals($data, FileUtils::readFlatDB(self::$file));
+
+        $data = null;
+        $this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0);
+        $this->assertEquals($data, FileUtils::readFlatDB(self::$file));
+
+        $data = false;
+        $this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0);
+        $this->assertEquals($data, FileUtils::readFlatDB(self::$file));
+    }
+
+    /**
+     * File not writable: raise an exception.
+     *
+     * @expectedException IOException
+     * @expectedExceptionMessage Error accessing "sandbox/flat.db"
+     */
+    public function testWriteWithoutPermission()
+    {
+        touch(self::$file);
+        chmod(self::$file, 0440);
+        FileUtils::writeFlatDB(self::$file, null);
+    }
+
+    /**
+     * Folder non existent: raise an exception.
+     *
+     * @expectedException IOException
+     * @expectedExceptionMessage Error accessing "nopefolder"
+     */
+    public function testWriteFolderDoesNotExist()
+    {
+        FileUtils::writeFlatDB('nopefolder/file', null);
+    }
+
+    /**
+     * Folder non writable: raise an exception.
+     *
+     * @expectedException IOException
+     * @expectedExceptionMessage Error accessing "sandbox"
+     */
+    public function testWriteFolderPermission()
+    {
+        chmod(dirname(self::$file), 0555);
+        try {
+            FileUtils::writeFlatDB(self::$file, null);
+        } catch (Exception $e) {
+            chmod(dirname(self::$file), 0755);
+            throw $e;
+        }
+    }
+
+    /**
+     * Read non existent file, use default parameter.
+     */
+    public function testReadNotExistentFile()
+    {
+        $this->assertEquals(null, FileUtils::readFlatDB(self::$file));
+        $this->assertEquals(['test'], FileUtils::readFlatDB(self::$file, ['test']));
+    }
+
+    /**
+     * Read non readable file, use default parameter.
+     */
+    public function testReadNotReadable()
+    {
+        touch(self::$file);
+        chmod(self::$file, 0220);
+        $this->assertEquals(null, FileUtils::readFlatDB(self::$file));
+        $this->assertEquals(['test'], FileUtils::readFlatDB(self::$file, ['test']));
+    }
+}
index 1f62a34a160ea5e17c31bbaa0117ef7785ff4202..7bf98f92101d572de71e345bf55080457b5aae7f 100644 (file)
@@ -101,7 +101,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
      * Attempt to instantiate a LinkDB whereas the datastore is not writable
      *
      * @expectedException              IOException
-     * @expectedExceptionMessageRegExp /Error accessing\nnull/
+     * @expectedExceptionMessageRegExp /Error accessing "null"/
      */
     public function testConstructDatastoreNotWriteable()
     {