Move database read/write to FileUtils class + additional unit tests
This commit is contained in:
parent
c4c655d9bf
commit
b2306b0c78
5 changed files with 199 additions and 41 deletions
108
tests/FileUtilsTest.php
Normal file
108
tests/FileUtilsTest.php
Normal file
|
@ -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']));
|
||||
}
|
||||
}
|
|
@ -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()
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue