]>
Commit | Line | Data |
---|---|---|
b74b96bf | 1 | <?php |
3c66e564 | 2 | namespace Shaarli\Config; |
b74b96bf A |
3 | |
4 | /** | |
5 | * Class ConfigJsonTest | |
6 | */ | |
3c66e564 | 7 | class ConfigJsonTest extends \PHPUnit_Framework_TestCase |
b74b96bf A |
8 | { |
9 | /** | |
10 | * @var ConfigJson | |
11 | */ | |
12 | protected $configIO; | |
13 | ||
14 | public function setUp() | |
15 | { | |
16 | $this->configIO = new ConfigJson(); | |
17 | } | |
18 | ||
19 | /** | |
20 | * Read a simple existing config file. | |
21 | */ | |
22 | public function testRead() | |
23 | { | |
24 | $conf = $this->configIO->read('tests/utils/config/configJson.json.php'); | |
da10377b | 25 | $this->assertEquals('root', $conf['credentials']['login']); |
894a3c4b A |
26 | $this->assertEquals('lala', $conf['redirector']['url']); |
27 | $this->assertEquals('tests/utils/config/datastore.php', $conf['resource']['datastore']); | |
b74b96bf A |
28 | $this->assertEquals('1', $conf['plugins']['WALLABAG_VERSION']); |
29 | } | |
30 | ||
31 | /** | |
32 | * Read a non existent config file -> empty array. | |
33 | */ | |
34 | public function testReadNonExistent() | |
35 | { | |
36 | $this->assertEquals(array(), $this->configIO->read('nope')); | |
37 | } | |
38 | ||
39 | /** | |
40 | * Read a non existent config file -> empty array. | |
41 | * | |
3c66e564 | 42 | * @expectedException \Exception |
c6a4c288 | 43 | * @expectedExceptionMessageRegExp /An error occurred while parsing JSON configuration file \([\w\/\.]+\): error code #4/ |
b74b96bf A |
44 | */ |
45 | public function testReadInvalidJson() | |
46 | { | |
47 | $this->configIO->read('tests/utils/config/configInvalid.json.php'); | |
48 | } | |
49 | ||
50 | /** | |
51 | * Write a new config file. | |
52 | */ | |
53 | public function testWriteNew() | |
54 | { | |
55 | $dataFile = 'tests/utils/config/configWrite.json.php'; | |
56 | $data = array( | |
da10377b A |
57 | 'credentials' => array( |
58 | 'login' => 'root', | |
59 | ), | |
894a3c4b | 60 | 'resource' => array( |
da10377b A |
61 | 'datastore' => 'data/datastore.php', |
62 | ), | |
894a3c4b A |
63 | 'redirector' => array( |
64 | 'url' => 'lala', | |
b74b96bf A |
65 | ), |
66 | 'plugins' => array( | |
67 | 'WALLABAG_VERSION' => '1', | |
68 | ) | |
69 | ); | |
70 | $this->configIO->write($dataFile, $data); | |
71 | // PHP 5.3 doesn't support json pretty print. | |
72 | if (defined('JSON_PRETTY_PRINT')) { | |
73 | $expected = '{ | |
da10377b A |
74 | "credentials": { |
75 | "login": "root" | |
76 | }, | |
894a3c4b | 77 | "resource": { |
da10377b A |
78 | "datastore": "data\/datastore.php" |
79 | }, | |
894a3c4b A |
80 | "redirector": { |
81 | "url": "lala" | |
b74b96bf A |
82 | }, |
83 | "plugins": { | |
84 | "WALLABAG_VERSION": "1" | |
85 | } | |
86 | }'; | |
87 | } else { | |
894a3c4b | 88 | $expected = '{"credentials":{"login":"root"},"resource":{"datastore":"data\/datastore.php"},"redirector":{"url":"lala"},"plugins":{"WALLABAG_VERSION":"1"}}'; |
b74b96bf | 89 | } |
5ff23f02 | 90 | $expected = ConfigJson::getPhpHeaders() . $expected . ConfigJson::getPhpSuffix(); |
b74b96bf A |
91 | $this->assertEquals($expected, file_get_contents($dataFile)); |
92 | unlink($dataFile); | |
93 | } | |
94 | ||
95 | /** | |
96 | * Overwrite an existing setting. | |
97 | */ | |
98 | public function testOverwrite() | |
99 | { | |
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); | |
894a3c4b | 104 | $conf['redirector']['url'] = 'blabla'; |
b74b96bf A |
105 | $this->configIO->write($dest, $conf); |
106 | $conf = $this->configIO->read($dest); | |
894a3c4b | 107 | $this->assertEquals('blabla', $conf['redirector']['url']); |
b74b96bf A |
108 | unlink($dest); |
109 | } | |
110 | ||
111 | /** | |
112 | * Write to invalid path. | |
113 | * | |
3c66e564 | 114 | * @expectedException \IOException |
b74b96bf A |
115 | */ |
116 | public function testWriteInvalidArray() | |
117 | { | |
118 | $conf = array('conf' => 'value'); | |
119 | @$this->configIO->write(array(), $conf); | |
120 | } | |
121 | ||
122 | /** | |
123 | * Write to invalid path. | |
124 | * | |
3c66e564 | 125 | * @expectedException \IOException |
b74b96bf A |
126 | */ |
127 | public function testWriteInvalidBlank() | |
128 | { | |
129 | $conf = array('conf' => 'value'); | |
130 | @$this->configIO->write('', $conf); | |
131 | } | |
132 | } |