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['login']);
27 $this->assertEquals('lala', $conf['redirector']);
28 $this->assertEquals('data/datastore.php', $conf['config']['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 occured 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';
59 'redirector' => 'lala',
61 'DATASTORE' => 'data/datastore.php',
64 'WALLABAG_VERSION' => '1',
67 $this->configIO
->write($dataFile, $data);
68 // PHP 5.3 doesn't support json pretty print.
69 if (defined('JSON_PRETTY_PRINT')) {
74 "DATASTORE": "data\/datastore.php"
77 "WALLABAG_VERSION": "1"
81 $expected = '{"login":"root","redirector":"lala","config":{"DATASTORE":"data\/datastore.php"},"plugins":{"WALLABAG_VERSION":"1"}}';
83 $expected = ConfigJson
::$PHP_HEADER . $expected;
84 $this->assertEquals($expected, file_get_contents($dataFile));
89 * Overwrite an existing setting.
91 public function testOverwrite()
93 $source = 'tests/utils/config/configJson.json.php';
94 $dest = 'tests/utils/config/configOverwrite.json.php';
96 $conf = $this->configIO
->read($dest);
97 $conf['redirector'] = 'blabla';
98 $this->configIO
->write($dest, $conf);
99 $conf = $this->configIO
->read($dest);
100 $this->assertEquals('blabla', $conf['redirector']);
105 * Write to invalid path.
107 * @expectedException IOException
109 public function testWriteInvalidArray()
111 $conf = array('conf' => 'value');
112 @$this->configIO
->write(array(), $conf);
116 * Write to invalid path.
118 * @expectedException IOException
120 public function testWriteInvalidBlank()
122 $conf = array('conf' => 'value');
123 @$this->configIO
->write('', $conf);