2016-05-29 12:32:14 +02:00
|
|
|
<?php
|
2017-03-03 23:06:12 +01:00
|
|
|
namespace Shaarli\Config;
|
2016-05-29 12:32:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ConfigJsonTest
|
|
|
|
*/
|
2017-03-03 23:06:12 +01:00
|
|
|
class ConfigJsonTest extends \PHPUnit_Framework_TestCase
|
2016-05-29 12:32:14 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var ConfigJson
|
|
|
|
*/
|
|
|
|
protected $configIO;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->configIO = new ConfigJson();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a simple existing config file.
|
|
|
|
*/
|
|
|
|
public function testRead()
|
|
|
|
{
|
|
|
|
$conf = $this->configIO->read('tests/utils/config/configJson.json.php');
|
2016-05-29 16:10:32 +02:00
|
|
|
$this->assertEquals('root', $conf['credentials']['login']);
|
2016-06-11 09:08:02 +02:00
|
|
|
$this->assertEquals('lala', $conf['redirector']['url']);
|
|
|
|
$this->assertEquals('tests/utils/config/datastore.php', $conf['resource']['datastore']);
|
2016-05-29 12:32:14 +02:00
|
|
|
$this->assertEquals('1', $conf['plugins']['WALLABAG_VERSION']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a non existent config file -> empty array.
|
|
|
|
*/
|
|
|
|
public function testReadNonExistent()
|
|
|
|
{
|
|
|
|
$this->assertEquals(array(), $this->configIO->read('nope'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a non existent config file -> empty array.
|
|
|
|
*
|
2017-03-03 23:06:12 +01:00
|
|
|
* @expectedException \Exception
|
2016-10-20 11:31:52 +02:00
|
|
|
* @expectedExceptionMessage An error occurred while parsing JSON file: error code #4
|
2016-05-29 12:32:14 +02:00
|
|
|
*/
|
|
|
|
public function testReadInvalidJson()
|
|
|
|
{
|
|
|
|
$this->configIO->read('tests/utils/config/configInvalid.json.php');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write a new config file.
|
|
|
|
*/
|
|
|
|
public function testWriteNew()
|
|
|
|
{
|
|
|
|
$dataFile = 'tests/utils/config/configWrite.json.php';
|
|
|
|
$data = array(
|
2016-05-29 16:10:32 +02:00
|
|
|
'credentials' => array(
|
|
|
|
'login' => 'root',
|
|
|
|
),
|
2016-06-11 09:08:02 +02:00
|
|
|
'resource' => array(
|
2016-05-29 16:10:32 +02:00
|
|
|
'datastore' => 'data/datastore.php',
|
|
|
|
),
|
2016-06-11 09:08:02 +02:00
|
|
|
'redirector' => array(
|
|
|
|
'url' => 'lala',
|
2016-05-29 12:32:14 +02:00
|
|
|
),
|
|
|
|
'plugins' => array(
|
|
|
|
'WALLABAG_VERSION' => '1',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->configIO->write($dataFile, $data);
|
|
|
|
// PHP 5.3 doesn't support json pretty print.
|
|
|
|
if (defined('JSON_PRETTY_PRINT')) {
|
|
|
|
$expected = '{
|
2016-05-29 16:10:32 +02:00
|
|
|
"credentials": {
|
|
|
|
"login": "root"
|
|
|
|
},
|
2016-06-11 09:08:02 +02:00
|
|
|
"resource": {
|
2016-05-29 16:10:32 +02:00
|
|
|
"datastore": "data\/datastore.php"
|
|
|
|
},
|
2016-06-11 09:08:02 +02:00
|
|
|
"redirector": {
|
|
|
|
"url": "lala"
|
2016-05-29 12:32:14 +02:00
|
|
|
},
|
|
|
|
"plugins": {
|
|
|
|
"WALLABAG_VERSION": "1"
|
|
|
|
}
|
|
|
|
}';
|
|
|
|
} else {
|
2016-06-11 09:08:02 +02:00
|
|
|
$expected = '{"credentials":{"login":"root"},"resource":{"datastore":"data\/datastore.php"},"redirector":{"url":"lala"},"plugins":{"WALLABAG_VERSION":"1"}}';
|
2016-05-29 12:32:14 +02:00
|
|
|
}
|
2016-06-20 18:30:37 +02:00
|
|
|
$expected = ConfigJson::getPhpHeaders() . $expected . ConfigJson::getPhpSuffix();
|
2016-05-29 12:32:14 +02:00
|
|
|
$this->assertEquals($expected, file_get_contents($dataFile));
|
|
|
|
unlink($dataFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Overwrite an existing setting.
|
|
|
|
*/
|
|
|
|
public function testOverwrite()
|
|
|
|
{
|
|
|
|
$source = 'tests/utils/config/configJson.json.php';
|
|
|
|
$dest = 'tests/utils/config/configOverwrite.json.php';
|
|
|
|
copy($source, $dest);
|
|
|
|
$conf = $this->configIO->read($dest);
|
2016-06-11 09:08:02 +02:00
|
|
|
$conf['redirector']['url'] = 'blabla';
|
2016-05-29 12:32:14 +02:00
|
|
|
$this->configIO->write($dest, $conf);
|
|
|
|
$conf = $this->configIO->read($dest);
|
2016-06-11 09:08:02 +02:00
|
|
|
$this->assertEquals('blabla', $conf['redirector']['url']);
|
2016-05-29 12:32:14 +02:00
|
|
|
unlink($dest);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write to invalid path.
|
|
|
|
*
|
2017-03-03 23:06:12 +01:00
|
|
|
* @expectedException \IOException
|
2016-05-29 12:32:14 +02:00
|
|
|
*/
|
|
|
|
public function testWriteInvalidArray()
|
|
|
|
{
|
|
|
|
$conf = array('conf' => 'value');
|
|
|
|
@$this->configIO->write(array(), $conf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write to invalid path.
|
|
|
|
*
|
2017-03-03 23:06:12 +01:00
|
|
|
* @expectedException \IOException
|
2016-05-29 12:32:14 +02:00
|
|
|
*/
|
|
|
|
public function testWriteInvalidBlank()
|
|
|
|
{
|
|
|
|
$conf = array('conf' => 'value');
|
|
|
|
@$this->configIO->write('', $conf);
|
|
|
|
}
|
|
|
|
}
|