]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | namespace Shaarli\Config; | |
3 | ||
4 | /** | |
5 | * Class ConfigJsonTest | |
6 | */ | |
7 | class ConfigJsonTest extends \PHPUnit\Framework\TestCase | |
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'); | |
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']); | |
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 | * | |
42 | * @expectedException \Exception | |
43 | * @expectedExceptionMessageRegExp /An error occurred while parsing JSON configuration file \([\w\/\.]+\): error code #4/ | |
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( | |
57 | 'credentials' => array( | |
58 | 'login' => 'root', | |
59 | ), | |
60 | 'resource' => array( | |
61 | 'datastore' => 'data/datastore.php', | |
62 | ), | |
63 | 'redirector' => array( | |
64 | 'url' => 'lala', | |
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 = '{ | |
74 | "credentials": { | |
75 | "login": "root" | |
76 | }, | |
77 | "resource": { | |
78 | "datastore": "data\/datastore.php" | |
79 | }, | |
80 | "redirector": { | |
81 | "url": "lala" | |
82 | }, | |
83 | "plugins": { | |
84 | "WALLABAG_VERSION": "1" | |
85 | } | |
86 | }'; | |
87 | } else { | |
88 | $expected = '{"credentials":{"login":"root"},"resource":{"datastore":"data\/datastore.php"},"redirector":{"url":"lala"},"plugins":{"WALLABAG_VERSION":"1"}}'; | |
89 | } | |
90 | $expected = ConfigJson::getPhpHeaders() . $expected . ConfigJson::getPhpSuffix(); | |
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); | |
104 | $conf['redirector']['url'] = 'blabla'; | |
105 | $this->configIO->write($dest, $conf); | |
106 | $conf = $this->configIO->read($dest); | |
107 | $this->assertEquals('blabla', $conf['redirector']['url']); | |
108 | unlink($dest); | |
109 | } | |
110 | ||
111 | /** | |
112 | * Write to invalid path. | |
113 | * | |
114 | * @expectedException \Shaarli\Exceptions\IOException | |
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 | * | |
125 | * @expectedException \Shaarli\Exceptions\IOException | |
126 | */ | |
127 | public function testWriteInvalidBlank() | |
128 | { | |
129 | $conf = array('conf' => 'value'); | |
130 | @$this->configIO->write('', $conf); | |
131 | } | |
132 | } |