2017-01-16 12:30:18 +01:00
|
|
|
<?php
|
|
|
|
|
2018-12-03 00:59:21 +01:00
|
|
|
namespace Shaarli;
|
2018-12-02 23:31:40 +01:00
|
|
|
|
2018-12-03 00:59:21 +01:00
|
|
|
use Exception;
|
2017-01-16 12:30:18 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class FileUtilsTest
|
|
|
|
*
|
|
|
|
* Test file utility class.
|
|
|
|
*/
|
2018-12-03 00:59:21 +01:00
|
|
|
class FileUtilsTest extends \PHPUnit\Framework\TestCase
|
2017-01-16 12:30:18 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string Test file path.
|
|
|
|
*/
|
|
|
|
protected static $file = 'sandbox/flat.db';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete test file after every test.
|
|
|
|
*/
|
2020-09-26 15:08:39 +02:00
|
|
|
protected function tearDown(): void
|
2017-01-16 12:30:18 +01:00
|
|
|
{
|
|
|
|
@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.
|
|
|
|
*
|
2018-12-02 23:31:40 +01:00
|
|
|
* @expectedException Shaarli\Exceptions\IOException
|
2017-01-16 12:30:18 +01:00
|
|
|
* @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.
|
|
|
|
*
|
2018-12-02 23:31:40 +01:00
|
|
|
* @expectedException Shaarli\Exceptions\IOException
|
2017-01-16 12:30:18 +01:00
|
|
|
* @expectedExceptionMessage Error accessing "nopefolder"
|
|
|
|
*/
|
|
|
|
public function testWriteFolderDoesNotExist()
|
|
|
|
{
|
|
|
|
FileUtils::writeFlatDB('nopefolder/file', null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Folder non writable: raise an exception.
|
|
|
|
*
|
2018-12-02 23:31:40 +01:00
|
|
|
* @expectedException Shaarli\Exceptions\IOException
|
2017-01-16 12:30:18 +01:00
|
|
|
* @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']));
|
|
|
|
}
|
|
|
|
}
|