10 * Test file utility class.
12 class FileUtilsTest
extends \PHPUnit\Framework\TestCase
15 * @var string Test file path.
17 protected static $file = 'sandbox/flat.db';
20 * Delete test file after every test.
22 public function tearDown()
28 * Test writeDB, then readDB with different data.
30 public function testSimpleWriteRead()
32 $data = ['blue', 'red'];
33 $this->assertTrue(FileUtils
::writeFlatDB(self
::$file, $data) > 0);
34 $this->assertTrue(startsWith(file_get_contents(self
::$file), '<?php /*'));
35 $this->assertEquals($data, FileUtils
::readFlatDB(self
::$file));
38 $this->assertTrue(FileUtils
::writeFlatDB(self
::$file, $data) > 0);
39 $this->assertEquals($data, FileUtils
::readFlatDB(self
::$file));
42 $this->assertTrue(FileUtils
::writeFlatDB(self
::$file, $data) > 0);
43 $this->assertEquals($data, FileUtils
::readFlatDB(self
::$file));
46 $this->assertTrue(FileUtils
::writeFlatDB(self
::$file, $data) > 0);
47 $this->assertEquals($data, FileUtils
::readFlatDB(self
::$file));
51 * File not writable: raise an exception.
53 * @expectedException Shaarli\Exceptions\IOException
54 * @expectedExceptionMessage Error accessing "sandbox/flat.db"
56 public function testWriteWithoutPermission()
59 chmod(self
::$file, 0440);
60 FileUtils
::writeFlatDB(self
::$file, null);
64 * Folder non existent: raise an exception.
66 * @expectedException Shaarli\Exceptions\IOException
67 * @expectedExceptionMessage Error accessing "nopefolder"
69 public function testWriteFolderDoesNotExist()
71 FileUtils
::writeFlatDB('nopefolder/file', null);
75 * Folder non writable: raise an exception.
77 * @expectedException Shaarli\Exceptions\IOException
78 * @expectedExceptionMessage Error accessing "sandbox"
80 public function testWriteFolderPermission()
82 chmod(dirname(self
::$file), 0555);
84 FileUtils
::writeFlatDB(self
::$file, null);
85 } catch (Exception
$e) {
86 chmod(dirname(self
::$file), 0755);
92 * Read non existent file, use default parameter.
94 public function testReadNotExistentFile()
96 $this->assertEquals(null, FileUtils
::readFlatDB(self
::$file));
97 $this->assertEquals(['test'], FileUtils
::readFlatDB(self
::$file, ['test']));
101 * Read non readable file, use default parameter.
103 public function testReadNotReadable()
106 chmod(self
::$file, 0220);
107 $this->assertEquals(null, FileUtils
::readFlatDB(self
::$file));
108 $this->assertEquals(['test'], FileUtils
::readFlatDB(self
::$file, ['test']));