]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/config/ConfigManagerTest.php
ConfigManager no longer uses singleton pattern
[github/shaarli/Shaarli.git] / tests / config / ConfigManagerTest.php
CommitLineData
59404d79
A
1<?php
2
3/**
4 * Unit tests for Class ConfigManagerTest
5 *
6 * Note: it only test the manager with ConfigJson,
7 * ConfigPhp is only a workaround to handle the transition to JSON type.
8 */
b74b96bf 9class ConfigManagerTest extends PHPUnit_Framework_TestCase
59404d79
A
10{
11 /**
12 * @var ConfigManager
13 */
14 protected $conf;
15
16 public function setUp()
17 {
278d9ee2 18 $this->conf = new ConfigManager('tests/utils/config/configJson');
59404d79
A
19 }
20
b74b96bf
A
21 /**
22 * Simple config test:
23 * 1. Set settings.
24 * 2. Check settings value.
25 */
26 public function testSetGet()
59404d79 27 {
b74b96bf
A
28 $this->conf->set('paramInt', 42);
29 $this->conf->set('paramString', 'value1');
30 $this->conf->set('paramBool', false);
31 $this->conf->set('paramArray', array('foo' => 'bar'));
32 $this->conf->set('paramNull', null);
33
34 $this->assertEquals(42, $this->conf->get('paramInt'));
35 $this->assertEquals('value1', $this->conf->get('paramString'));
36 $this->assertFalse($this->conf->get('paramBool'));
37 $this->assertEquals(array('foo' => 'bar'), $this->conf->get('paramArray'));
38 $this->assertEquals(null, $this->conf->get('paramNull'));
59404d79
A
39 }
40
b74b96bf
A
41 /**
42 * Set/write/get config test:
43 * 1. Set settings.
44 * 2. Write it to the config file.
45 * 3. Read the file.
46 * 4. Check settings value.
47 */
59404d79
A
48 public function testSetWriteGet()
49 {
59404d79
A
50 $this->conf->set('paramInt', 42);
51 $this->conf->set('paramString', 'value1');
52 $this->conf->set('paramBool', false);
53 $this->conf->set('paramArray', array('foo' => 'bar'));
54 $this->conf->set('paramNull', null);
55
278d9ee2 56 $this->conf->setConfigFile('tests/utils/config/configTmp');
59404d79
A
57 $this->conf->write(true);
58 $this->conf->reload();
278d9ee2 59 unlink($this->conf->getConfigFileExt());
59404d79
A
60
61 $this->assertEquals(42, $this->conf->get('paramInt'));
62 $this->assertEquals('value1', $this->conf->get('paramString'));
63 $this->assertFalse($this->conf->get('paramBool'));
64 $this->assertEquals(array('foo' => 'bar'), $this->conf->get('paramArray'));
65 $this->assertEquals(null, $this->conf->get('paramNull'));
66 }
b74b96bf
A
67
68 /**
69 * Test set/write/get with nested keys.
70 */
71 public function testSetWriteGetNested()
72 {
73 $this->conf->set('foo.bar.key.stuff', 'testSetWriteGetNested');
74
278d9ee2 75 $this->conf->setConfigFile('tests/utils/config/configTmp');
b74b96bf
A
76 $this->conf->write(true);
77 $this->conf->reload();
278d9ee2 78 unlink($this->conf->getConfigFileExt());
b74b96bf
A
79
80 $this->assertEquals('testSetWriteGetNested', $this->conf->get('foo.bar.key.stuff'));
81 }
82
83 /**
84 * Set with an empty key.
85 *
86 * @expectedException Exception
87 * @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*#
88 */
89 public function testSetEmptyKey()
90 {
91 $this->conf->set('', 'stuff');
92 }
93
94 /**
95 * Set with an array key.
96 *
97 * @expectedException Exception
98 * @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*#
99 */
100 public function testSetArrayKey()
101 {
102 $this->conf->set(array('foo' => 'bar'), 'stuff');
103 }
104
105 /**
106 * Try to write the config without mandatory parameter (e.g. 'login').
107 *
108 * @expectedException MissingFieldConfigException
109 */
110 public function testWriteMissingParameter()
111 {
278d9ee2
A
112 $this->conf->setConfigFile('tests/utils/config/configTmp');
113 $this->assertFalse(file_exists($this->conf->getConfigFileExt()));
b74b96bf
A
114 $this->conf->reload();
115
116 $this->conf->write(true);
117 }
118
119 /**
120 * Try to get non existent config keys.
121 */
122 public function testGetNonExistent()
123 {
124 $this->assertEquals('', $this->conf->get('nope.test'));
125 $this->assertEquals('default', $this->conf->get('nope.test', 'default'));
126 }
127
128 /**
129 * Test the 'exists' method with existent values.
130 */
131 public function testExistsOk()
132 {
da10377b 133 $this->assertTrue($this->conf->exists('credentials.login'));
b74b96bf
A
134 $this->assertTrue($this->conf->exists('config.foo'));
135 }
136
137 /**
138 * Test the 'exists' method with non existent or invalid values.
139 */
140 public function testExistsKo()
141 {
142 $this->assertFalse($this->conf->exists('nope'));
143 $this->assertFalse($this->conf->exists('nope.nope'));
144 $this->assertFalse($this->conf->exists(''));
145 $this->assertFalse($this->conf->exists(false));
146 }
147
148 /**
149 * Reset the ConfigManager instance.
150 */
151 public function testReset()
152 {
278d9ee2
A
153 $confIO = $this->conf->getConfigIO();
154 $this->conf->reset();
155 $this->assertFalse($confIO === $this->conf->getConfigIO());
b74b96bf
A
156 }
157
158 /**
159 * Reload the config from file.
160 */
161 public function testReload()
162 {
278d9ee2 163 $this->conf->setConfigFile('tests/utils/config/configTmp');
da10377b 164 $newConf = ConfigJson::getPhpHeaders() . '{ "key": "value" }';
278d9ee2 165 file_put_contents($this->conf->getConfigFileExt(), $newConf);
b74b96bf 166 $this->conf->reload();
278d9ee2 167 unlink($this->conf->getConfigFileExt());
b74b96bf 168 // Previous conf no longer exists, and new values have been loaded.
da10377b 169 $this->assertFalse($this->conf->exists('credentials.login'));
b74b96bf
A
170 $this->assertEquals('value', $this->conf->get('key'));
171 }
172}