2 namespace Shaarli\Config
;
5 * Unit tests for Class ConfigManagerTest
7 * Note: it only test the manager with ConfigJson,
8 * ConfigPhp is only a workaround to handle the transition to JSON type.
10 class ConfigManagerTest
extends \PHPUnit_Framework_TestCase
17 public function setUp()
19 $this->conf
= new ConfigManager('tests/utils/config/configJson');
25 * 2. Check settings value.
27 public function testSetGet()
29 $this->conf
->set('paramInt', 42);
30 $this->conf
->set('paramString', 'value1');
31 $this->conf
->set('paramBool', false);
32 $this->conf
->set('paramArray', array('foo' => 'bar'));
33 $this->conf
->set('paramNull', null);
35 $this->assertEquals(42, $this->conf
->get('paramInt'));
36 $this->assertEquals('value1', $this->conf
->get('paramString'));
37 $this->assertFalse($this->conf
->get('paramBool'));
38 $this->assertEquals(array('foo' => 'bar'), $this->conf
->get('paramArray'));
39 $this->assertEquals(null, $this->conf
->get('paramNull'));
43 * Set/write/get config test:
45 * 2. Write it to the config file.
47 * 4. Check settings value.
49 public function testSetWriteGet()
51 $this->conf
->set('paramInt', 42);
52 $this->conf
->set('paramString', 'value1');
53 $this->conf
->set('paramBool', false);
54 $this->conf
->set('paramArray', array('foo' => 'bar'));
55 $this->conf
->set('paramNull', null);
57 $this->conf
->setConfigFile('tests/utils/config/configTmp');
58 $this->conf
->write(true);
59 $this->conf
->reload();
60 unlink($this->conf
->getConfigFileExt());
62 $this->assertEquals(42, $this->conf
->get('paramInt'));
63 $this->assertEquals('value1', $this->conf
->get('paramString'));
64 $this->assertFalse($this->conf
->get('paramBool'));
65 $this->assertEquals(array('foo' => 'bar'), $this->conf
->get('paramArray'));
66 $this->assertEquals(null, $this->conf
->get('paramNull'));
70 * Test set/write/get with nested keys.
72 public function testSetWriteGetNested()
74 $this->conf
->set('foo.bar.key.stuff', 'testSetWriteGetNested');
76 $this->conf
->setConfigFile('tests/utils/config/configTmp');
77 $this->conf
->write(true);
78 $this->conf
->reload();
79 unlink($this->conf
->getConfigFileExt());
81 $this->assertEquals('testSetWriteGetNested', $this->conf
->get('foo.bar.key.stuff'));
84 public function testSetDeleteNested()
86 $this->conf
->set('foo.bar.key.stuff', 'testSetDeleteNested');
87 $this->assertTrue($this->conf
->exists('foo.bar'));
88 $this->assertTrue($this->conf
->exists('foo.bar.key.stuff'));
89 $this->assertEquals('testSetDeleteNested', $this->conf
->get('foo.bar.key.stuff'));
91 $this->conf
->remove('foo.bar');
92 $this->assertFalse($this->conf
->exists('foo.bar.key.stuff'));
93 $this->assertFalse($this->conf
->exists('foo.bar'));
97 * Set with an empty key.
99 * @expectedException \Exception
100 * @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*#
102 public function testSetEmptyKey()
104 $this->conf
->set('', 'stuff');
108 * Set with an array key.
110 * @expectedException \Exception
111 * @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*#
113 public function testSetArrayKey()
115 $this->conf
->set(array('foo' => 'bar'), 'stuff');
119 * Remove with an empty key.
121 * @expectedException \Exception
122 * @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*#
124 public function testRmoveEmptyKey()
126 $this->conf
->remove('');
130 * Try to write the config without mandatory parameter (e.g. 'login').
132 * @expectedException Shaarli\Config\Exception\MissingFieldConfigException
134 public function testWriteMissingParameter()
136 $this->conf
->setConfigFile('tests/utils/config/configTmp');
137 $this->assertFalse(file_exists($this->conf
->getConfigFileExt()));
138 $this->conf
->reload();
140 $this->conf
->write(true);
144 * Try to get non existent config keys.
146 public function testGetNonExistent()
148 $this->assertEquals('', $this->conf
->get('nope.test'));
149 $this->assertEquals('default', $this->conf
->get('nope.test', 'default'));
153 * Test the 'exists' method with existent values.
155 public function testExistsOk()
157 $this->assertTrue($this->conf
->exists('credentials.login'));
158 $this->assertTrue($this->conf
->exists('config.foo'));
162 * Test the 'exists' method with non existent or invalid values.
164 public function testExistsKo()
166 $this->assertFalse($this->conf
->exists('nope'));
167 $this->assertFalse($this->conf
->exists('nope.nope'));
168 $this->assertFalse($this->conf
->exists(''));
169 $this->assertFalse($this->conf
->exists(false));
173 * Reset the ConfigManager instance.
175 public function testReset()
177 $confIO = $this->conf
->getConfigIO();
178 $this->conf
->reset();
179 $this->assertFalse($confIO === $this->conf
->getConfigIO());
183 * Reload the config from file.
185 public function testReload()
187 $this->conf
->setConfigFile('tests/utils/config/configTmp');
188 $newConf = ConfigJson
::getPhpHeaders() . '{ "key": "value" }';
189 file_put_contents($this->conf
->getConfigFileExt(), $newConf);
190 $this->conf
->reload();
191 unlink($this->conf
->getConfigFileExt());
192 // Previous conf no longer exists, and new values have been loaded.
193 $this->assertFalse($this->conf
->exists('credentials.login'));
194 $this->assertEquals('value', $this->conf
->get('key'));