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 protected function tearDown(): void
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 public function testWriteWithoutPermission()
55 $this->expectException(\Shaarli\Exceptions\IOException
::class);
56 $this->expectExceptionMessage('Error accessing "sandbox/flat.db"');
59 chmod(self
::$file, 0440);
60 FileUtils
::writeFlatDB(self
::$file, null);
64 * Folder non existent: raise an exception.
66 public function testWriteFolderDoesNotExist()
68 $this->expectException(\Shaarli\Exceptions\IOException
::class);
69 $this->expectExceptionMessage('Error accessing "nopefolder"');
71 FileUtils
::writeFlatDB('nopefolder/file', null);
75 * Folder non writable: raise an exception.
77 public function testWriteFolderPermission()
79 $this->expectException(\Shaarli\Exceptions\IOException
::class);
80 $this->expectExceptionMessage('Error accessing "sandbox"');
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']));