3 require_once 'application/config/ConfigJson.php';
8 class ConfigJsonTest
extends PHPUnit_Framework_TestCase
15 public function setUp()
17 $this->configIO
= new ConfigJson();
21 * Read a simple existing config file.
23 public function testRead()
25 $conf = $this->configIO
->read('tests/utils/config/configJson.json.php');
26 $this->assertEquals('root', $conf['credentials']['login']);
27 $this->assertEquals('lala', $conf['redirector']['url']);
28 $this->assertEquals('tests/utils/config/datastore.php', $conf['resource']['datastore']);
29 $this->assertEquals('1', $conf['plugins']['WALLABAG_VERSION']);
33 * Read a non existent config file -> empty array.
35 public function testReadNonExistent()
37 $this->assertEquals(array(), $this->configIO
->read('nope'));
41 * Read a non existent config file -> empty array.
43 * @expectedException Exception
44 * @expectedExceptionMessage An error occurred while parsing JSON file: error code #4
46 public function testReadInvalidJson()
48 $this->configIO
->read('tests/utils/config/configInvalid.json.php');
52 * Write a new config file.
54 public function testWriteNew()
56 $dataFile = 'tests/utils/config/configWrite.json.php';
58 'credentials' => array(
62 'datastore' => 'data/datastore.php',
64 'redirector' => array(
68 'WALLABAG_VERSION' => '1',
71 $this->configIO
->write($dataFile, $data);
72 // PHP 5.3 doesn't support json pretty print.
73 if (defined('JSON_PRETTY_PRINT')) {
79 "datastore": "data\/datastore.php"
85 "WALLABAG_VERSION": "1"
89 $expected = '{"credentials":{"login":"root"},"resource":{"datastore":"data\/datastore.php"},"redirector":{"url":"lala"},"plugins":{"WALLABAG_VERSION":"1"}}';
91 $expected = ConfigJson
::getPhpHeaders() . $expected . ConfigJson
::getPhpSuffix();
92 $this->assertEquals($expected, file_get_contents($dataFile));
97 * Overwrite an existing setting.
99 public function testOverwrite()
101 $source = 'tests/utils/config/configJson.json.php';
102 $dest = 'tests/utils/config/configOverwrite.json.php';
103 copy($source, $dest);
104 $conf = $this->configIO
->read($dest);
105 $conf['redirector']['url'] = 'blabla';
106 $this->configIO
->write($dest, $conf);
107 $conf = $this->configIO
->read($dest);
108 $this->assertEquals('blabla', $conf['redirector']['url']);
113 * Write to invalid path.
115 * @expectedException IOException
117 public function testWriteInvalidArray()
119 $conf = array('conf' => 'value');
120 @$this->configIO
->write(array(), $conf);
124 * Write to invalid path.
126 * @expectedException IOException
128 public function testWriteInvalidBlank()
130 $conf = array('conf' => 'value');
131 @$this->configIO
->write('', $conf);