2 namespace Shaarli\Config
;
7 class ConfigJsonTest
extends \PHPUnit_Framework_TestCase
14 public function setUp()
16 $this->configIO
= new ConfigJson();
20 * Read a simple existing config file.
22 public function testRead()
24 $conf = $this->configIO
->read('tests/utils/config/configJson.json.php');
25 $this->assertEquals('root', $conf['credentials']['login']);
26 $this->assertEquals('lala', $conf['redirector']['url']);
27 $this->assertEquals('tests/utils/config/datastore.php', $conf['resource']['datastore']);
28 $this->assertEquals('1', $conf['plugins']['WALLABAG_VERSION']);
32 * Read a non existent config file -> empty array.
34 public function testReadNonExistent()
36 $this->assertEquals(array(), $this->configIO
->read('nope'));
40 * Read a non existent config file -> empty array.
42 * @expectedException \Exception
43 * @expectedExceptionMessageRegExp /An error occurred while parsing JSON configuration file \([\w\/\.]+\): error code #4/
45 public function testReadInvalidJson()
47 $this->configIO
->read('tests/utils/config/configInvalid.json.php');
51 * Write a new config file.
53 public function testWriteNew()
55 $dataFile = 'tests/utils/config/configWrite.json.php';
57 'credentials' => array(
61 'datastore' => 'data/datastore.php',
63 'redirector' => array(
67 'WALLABAG_VERSION' => '1',
70 $this->configIO
->write($dataFile, $data);
71 // PHP 5.3 doesn't support json pretty print.
72 if (defined('JSON_PRETTY_PRINT')) {
78 "datastore": "data\/datastore.php"
84 "WALLABAG_VERSION": "1"
88 $expected = '{"credentials":{"login":"root"},"resource":{"datastore":"data\/datastore.php"},"redirector":{"url":"lala"},"plugins":{"WALLABAG_VERSION":"1"}}';
90 $expected = ConfigJson
::getPhpHeaders() . $expected . ConfigJson
::getPhpSuffix();
91 $this->assertEquals($expected, file_get_contents($dataFile));
96 * Overwrite an existing setting.
98 public function testOverwrite()
100 $source = 'tests/utils/config/configJson.json.php';
101 $dest = 'tests/utils/config/configOverwrite.json.php';
102 copy($source, $dest);
103 $conf = $this->configIO
->read($dest);
104 $conf['redirector']['url'] = 'blabla';
105 $this->configIO
->write($dest, $conf);
106 $conf = $this->configIO
->read($dest);
107 $this->assertEquals('blabla', $conf['redirector']['url']);
112 * Write to invalid path.
114 * @expectedException \IOException
116 public function testWriteInvalidArray()
118 $conf = array('conf' => 'value');
119 @$this->configIO
->write(array(), $conf);
123 * Write to invalid path.
125 * @expectedException \IOException
127 public function testWriteInvalidBlank()
129 $conf = array('conf' => 'value');
130 @$this->configIO
->write('', $conf);