4 * Unit tests for Class ConfigManagerTest
6 * Note: it only test the manager with ConfigJson,
7 * ConfigPhp is only a workaround to handle the transition to JSON type.
9 class ConfigManagerTest
extends PHPUnit_Framework_TestCase
16 public function setUp()
18 $this->conf
= new ConfigManager('tests/utils/config/configJson');
24 * 2. Check settings value.
26 public function testSetGet()
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);
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'));
42 * Set/write/get config test:
44 * 2. Write it to the config file.
46 * 4. Check settings value.
48 public function testSetWriteGet()
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);
56 $this->conf
->setConfigFile('tests/utils/config/configTmp');
57 $this->conf
->write(true);
58 $this->conf
->reload();
59 unlink($this->conf
->getConfigFileExt());
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'));
69 * Test set/write/get with nested keys.
71 public function testSetWriteGetNested()
73 $this->conf
->set('foo.bar.key.stuff', 'testSetWriteGetNested');
75 $this->conf
->setConfigFile('tests/utils/config/configTmp');
76 $this->conf
->write(true);
77 $this->conf
->reload();
78 unlink($this->conf
->getConfigFileExt());
80 $this->assertEquals('testSetWriteGetNested', $this->conf
->get('foo.bar.key.stuff'));
84 * Set with an empty key.
86 * @expectedException Exception
87 * @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*#
89 public function testSetEmptyKey()
91 $this->conf
->set('', 'stuff');
95 * Set with an array key.
97 * @expectedException Exception
98 * @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*#
100 public function testSetArrayKey()
102 $this->conf
->set(array('foo' => 'bar'), 'stuff');
106 * Try to write the config without mandatory parameter (e.g. 'login').
108 * @expectedException MissingFieldConfigException
110 public function testWriteMissingParameter()
112 $this->conf
->setConfigFile('tests/utils/config/configTmp');
113 $this->assertFalse(file_exists($this->conf
->getConfigFileExt()));
114 $this->conf
->reload();
116 $this->conf
->write(true);
120 * Try to get non existent config keys.
122 public function testGetNonExistent()
124 $this->assertEquals('', $this->conf
->get('nope.test'));
125 $this->assertEquals('default', $this->conf
->get('nope.test', 'default'));
129 * Test the 'exists' method with existent values.
131 public function testExistsOk()
133 $this->assertTrue($this->conf
->exists('credentials.login'));
134 $this->assertTrue($this->conf
->exists('config.foo'));
138 * Test the 'exists' method with non existent or invalid values.
140 public function testExistsKo()
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));
149 * Reset the ConfigManager instance.
151 public function testReset()
153 $confIO = $this->conf
->getConfigIO();
154 $this->conf
->reset();
155 $this->assertFalse($confIO === $this->conf
->getConfigIO());
159 * Reload the config from file.
161 public function testReload()
163 $this->conf
->setConfigFile('tests/utils/config/configTmp');
164 $newConf = ConfigJson
::getPhpHeaders() . '{ "key": "value" }';
165 file_put_contents($this->conf
->getConfigFileExt(), $newConf);
166 $this->conf
->reload();
167 unlink($this->conf
->getConfigFileExt());
168 // Previous conf no longer exists, and new values have been loaded.
169 $this->assertFalse($this->conf
->exists('credentials.login'));
170 $this->assertEquals('value', $this->conf
->get('key'));