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