3 require_once 'application/FileUtils.php';
8 * Test file utility class.
10 class FileUtilsTest
extends PHPUnit_Framework_TestCase
13 * @var string Test file path.
15 protected static $file = 'sandbox/flat.db';
18 * Delete test file after every test.
20 public function tearDown()
26 * Test writeDB, then readDB with different data.
28 public function testSimpleWriteRead()
30 $data = ['blue', 'red'];
31 $this->assertTrue(FileUtils
::writeFlatDB(self
::$file, $data) > 0);
32 $this->assertTrue(startsWith(file_get_contents(self
::$file), '<?php /*'));
33 $this->assertEquals($data, FileUtils
::readFlatDB(self
::$file));
36 $this->assertTrue(FileUtils
::writeFlatDB(self
::$file, $data) > 0);
37 $this->assertEquals($data, FileUtils
::readFlatDB(self
::$file));
40 $this->assertTrue(FileUtils
::writeFlatDB(self
::$file, $data) > 0);
41 $this->assertEquals($data, FileUtils
::readFlatDB(self
::$file));
44 $this->assertTrue(FileUtils
::writeFlatDB(self
::$file, $data) > 0);
45 $this->assertEquals($data, FileUtils
::readFlatDB(self
::$file));
49 * File not writable: raise an exception.
51 * @expectedException IOException
52 * @expectedExceptionMessage Error accessing "sandbox/flat.db"
54 public function testWriteWithoutPermission()
57 chmod(self
::$file, 0440);
58 FileUtils
::writeFlatDB(self
::$file, null);
62 * Folder non existent: raise an exception.
64 * @expectedException IOException
65 * @expectedExceptionMessage Error accessing "nopefolder"
67 public function testWriteFolderDoesNotExist()
69 FileUtils
::writeFlatDB('nopefolder/file', null);
73 * Folder non writable: raise an exception.
75 * @expectedException IOException
76 * @expectedExceptionMessage Error accessing "sandbox"
78 public function testWriteFolderPermission()
80 chmod(dirname(self
::$file), 0555);
82 FileUtils
::writeFlatDB(self
::$file, null);
83 } catch (Exception
$e) {
84 chmod(dirname(self
::$file), 0755);
90 * Read non existent file, use default parameter.
92 public function testReadNotExistentFile()
94 $this->assertEquals(null, FileUtils
::readFlatDB(self
::$file));
95 $this->assertEquals(['test'], FileUtils
::readFlatDB(self
::$file, ['test']));
99 * Read non readable file, use default parameter.
101 public function testReadNotReadable()
104 chmod(self
::$file, 0220);
105 $this->assertEquals(null, FileUtils
::readFlatDB(self
::$file));
106 $this->assertEquals(['test'], FileUtils
::readFlatDB(self
::$file, ['test']));