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