diff options
Diffstat (limited to 'tests/config/ConfigJsonTest.php')
-rw-r--r-- | tests/config/ConfigJsonTest.php | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/tests/config/ConfigJsonTest.php b/tests/config/ConfigJsonTest.php new file mode 100644 index 00000000..5b3bce46 --- /dev/null +++ b/tests/config/ConfigJsonTest.php | |||
@@ -0,0 +1,125 @@ | |||
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['login']); | ||
27 | $this->assertEquals('lala', $conf['redirector']); | ||
28 | $this->assertEquals('data/datastore.php', $conf['config']['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 occured 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 | 'login' => 'root', | ||
59 | 'redirector' => 'lala', | ||
60 | 'config' => array( | ||
61 | 'DATASTORE' => 'data/datastore.php', | ||
62 | ), | ||
63 | 'plugins' => array( | ||
64 | 'WALLABAG_VERSION' => '1', | ||
65 | ) | ||
66 | ); | ||
67 | $this->configIO->write($dataFile, $data); | ||
68 | // PHP 5.3 doesn't support json pretty print. | ||
69 | if (defined('JSON_PRETTY_PRINT')) { | ||
70 | $expected = '{ | ||
71 | "login": "root", | ||
72 | "redirector": "lala", | ||
73 | "config": { | ||
74 | "DATASTORE": "data\/datastore.php" | ||
75 | }, | ||
76 | "plugins": { | ||
77 | "WALLABAG_VERSION": "1" | ||
78 | } | ||
79 | }'; | ||
80 | } else { | ||
81 | $expected = '{"login":"root","redirector":"lala","config":{"DATASTORE":"data\/datastore.php"},"plugins":{"WALLABAG_VERSION":"1"}}'; | ||
82 | } | ||
83 | $expected = ConfigJson::$PHP_HEADER . $expected; | ||
84 | $this->assertEquals($expected, file_get_contents($dataFile)); | ||
85 | unlink($dataFile); | ||
86 | } | ||
87 | |||
88 | /** | ||
89 | * Overwrite an existing setting. | ||
90 | */ | ||
91 | public function testOverwrite() | ||
92 | { | ||
93 | $source = 'tests/utils/config/configJson.json.php'; | ||
94 | $dest = 'tests/utils/config/configOverwrite.json.php'; | ||
95 | copy($source, $dest); | ||
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']); | ||
101 | unlink($dest); | ||
102 | } | ||
103 | |||
104 | /** | ||
105 | * Write to invalid path. | ||
106 | * | ||
107 | * @expectedException IOException | ||
108 | */ | ||
109 | public function testWriteInvalidArray() | ||
110 | { | ||
111 | $conf = array('conf' => 'value'); | ||
112 | @$this->configIO->write(array(), $conf); | ||
113 | } | ||
114 | |||
115 | /** | ||
116 | * Write to invalid path. | ||
117 | * | ||
118 | * @expectedException IOException | ||
119 | */ | ||
120 | public function testWriteInvalidBlank() | ||
121 | { | ||
122 | $conf = array('conf' => 'value'); | ||
123 | @$this->configIO->write('', $conf); | ||
124 | } | ||
125 | } | ||