diff options
author | Arthur <arthur@hoa.ro> | 2016-07-09 07:19:48 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-09 07:19:48 +0200 |
commit | 649af5b501d2a90448242f53764ff693e9854039 (patch) | |
tree | 23cde80a7ee2949e552c48939ae22fa462cfa0fc /tests/config/ConfigManagerTest.php | |
parent | a9cfa38df92bd2e1e2c00a67b6ac1516a2116ade (diff) | |
parent | 5ff23f02b80ec6ddee28dee869171ee8e3656b7c (diff) | |
download | Shaarli-649af5b501d2a90448242f53764ff693e9854039.tar.gz Shaarli-649af5b501d2a90448242f53764ff693e9854039.tar.zst Shaarli-649af5b501d2a90448242f53764ff693e9854039.zip |
Merge pull request #570 from ArthurHoaro/config-manager
Introduce a configuration manager
Diffstat (limited to 'tests/config/ConfigManagerTest.php')
-rw-r--r-- | tests/config/ConfigManagerTest.php | 172 |
1 files changed, 172 insertions, 0 deletions
diff --git a/tests/config/ConfigManagerTest.php b/tests/config/ConfigManagerTest.php new file mode 100644 index 00000000..436e3d67 --- /dev/null +++ b/tests/config/ConfigManagerTest.php | |||
@@ -0,0 +1,172 @@ | |||
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 | */ | ||
9 | class ConfigManagerTest extends PHPUnit_Framework_TestCase | ||
10 | { | ||
11 | /** | ||
12 | * @var ConfigManager | ||
13 | */ | ||
14 | protected $conf; | ||
15 | |||
16 | public function setUp() | ||
17 | { | ||
18 | $this->conf = new ConfigManager('tests/utils/config/configJson'); | ||
19 | } | ||
20 | |||
21 | /** | ||
22 | * Simple config test: | ||
23 | * 1. Set settings. | ||
24 | * 2. Check settings value. | ||
25 | */ | ||
26 | public function testSetGet() | ||
27 | { | ||
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')); | ||
39 | } | ||
40 | |||
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 | */ | ||
48 | public function testSetWriteGet() | ||
49 | { | ||
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 | |||
56 | $this->conf->setConfigFile('tests/utils/config/configTmp'); | ||
57 | $this->conf->write(true); | ||
58 | $this->conf->reload(); | ||
59 | unlink($this->conf->getConfigFileExt()); | ||
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 | } | ||
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 | |||
75 | $this->conf->setConfigFile('tests/utils/config/configTmp'); | ||
76 | $this->conf->write(true); | ||
77 | $this->conf->reload(); | ||
78 | unlink($this->conf->getConfigFileExt()); | ||
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 | { | ||
112 | $this->conf->setConfigFile('tests/utils/config/configTmp'); | ||
113 | $this->assertFalse(file_exists($this->conf->getConfigFileExt())); | ||
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 | { | ||
133 | $this->assertTrue($this->conf->exists('credentials.login')); | ||
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 | { | ||
153 | $confIO = $this->conf->getConfigIO(); | ||
154 | $this->conf->reset(); | ||
155 | $this->assertFalse($confIO === $this->conf->getConfigIO()); | ||
156 | } | ||
157 | |||
158 | /** | ||
159 | * Reload the config from file. | ||
160 | */ | ||
161 | public function testReload() | ||
162 | { | ||
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')); | ||
171 | } | ||
172 | } | ||