]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/config/ConfigManagerTest.php
Merge pull request #1698 from ArthurHoaro/feature/plugins-search-filter
[github/shaarli/Shaarli.git] / tests / config / ConfigManagerTest.php
CommitLineData
59404d79 1<?php
3c66e564 2namespace Shaarli\Config;
59404d79
A
3
4/**
5 * Unit tests for Class ConfigManagerTest
6 *
7 * Note: it only test the manager with ConfigJson,
8 * ConfigPhp is only a workaround to handle the transition to JSON type.
9 */
a5a9cf23 10class ConfigManagerTest extends \Shaarli\TestCase
59404d79
A
11{
12 /**
13 * @var ConfigManager
14 */
15 protected $conf;
16
8f60e120 17 protected function setUp(): void
59404d79 18 {
278d9ee2 19 $this->conf = new ConfigManager('tests/utils/config/configJson');
59404d79
A
20 }
21
b74b96bf
A
22 /**
23 * Simple config test:
24 * 1. Set settings.
25 * 2. Check settings value.
26 */
27 public function testSetGet()
59404d79 28 {
b74b96bf
A
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'));
59404d79
A
40 }
41
b74b96bf
A
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 */
59404d79
A
49 public function testSetWriteGet()
50 {
59404d79
A
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);
56
278d9ee2 57 $this->conf->setConfigFile('tests/utils/config/configTmp');
59404d79
A
58 $this->conf->write(true);
59 $this->conf->reload();
278d9ee2 60 unlink($this->conf->getConfigFileExt());
59404d79
A
61
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'));
67 }
b74b96bf
A
68
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
278d9ee2 76 $this->conf->setConfigFile('tests/utils/config/configTmp');
b74b96bf
A
77 $this->conf->write(true);
78 $this->conf->reload();
278d9ee2 79 unlink($this->conf->getConfigFileExt());
b74b96bf
A
80
81 $this->assertEquals('testSetWriteGetNested', $this->conf->get('foo.bar.key.stuff'));
82 }
83
a3724717
A
84 public function testSetDeleteNested()
85 {
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'));
90
91 $this->conf->remove('foo.bar');
92 $this->assertFalse($this->conf->exists('foo.bar.key.stuff'));
93 $this->assertFalse($this->conf->exists('foo.bar'));
94 }
95
b74b96bf
A
96 /**
97 * Set with an empty key.
b74b96bf
A
98 */
99 public function testSetEmptyKey()
100 {
b1baca99
A
101 $this->expectException(\Exception::class);
102 $this->expectExceptionMessageRegExp('#^Invalid setting key parameter. String expected, got.*#');
103
b74b96bf
A
104 $this->conf->set('', 'stuff');
105 }
106
107 /**
108 * Set with an array key.
b74b96bf
A
109 */
110 public function testSetArrayKey()
111 {
b1baca99
A
112 $this->expectException(\Exception::class);
113 $this->expectExceptionMessageRegExp('#^Invalid setting key parameter. String expected, got.*#');
114
b74b96bf
A
115 $this->conf->set(array('foo' => 'bar'), 'stuff');
116 }
117
a3724717
A
118 /**
119 * Remove with an empty key.
a3724717
A
120 */
121 public function testRmoveEmptyKey()
122 {
b1baca99
A
123 $this->expectException(\Exception::class);
124 $this->expectExceptionMessageRegExp('#^Invalid setting key parameter. String expected, got.*#');
125
a3724717
A
126 $this->conf->remove('');
127 }
128
b74b96bf
A
129 /**
130 * Try to write the config without mandatory parameter (e.g. 'login').
b74b96bf
A
131 */
132 public function testWriteMissingParameter()
133 {
b1baca99
A
134 $this->expectException(\Shaarli\Config\Exception\MissingFieldConfigException::class);
135
278d9ee2
A
136 $this->conf->setConfigFile('tests/utils/config/configTmp');
137 $this->assertFalse(file_exists($this->conf->getConfigFileExt()));
b74b96bf
A
138 $this->conf->reload();
139
140 $this->conf->write(true);
141 }
142
143 /**
144 * Try to get non existent config keys.
145 */
146 public function testGetNonExistent()
147 {
148 $this->assertEquals('', $this->conf->get('nope.test'));
149 $this->assertEquals('default', $this->conf->get('nope.test', 'default'));
150 }
151
152 /**
153 * Test the 'exists' method with existent values.
154 */
155 public function testExistsOk()
156 {
da10377b 157 $this->assertTrue($this->conf->exists('credentials.login'));
b74b96bf
A
158 $this->assertTrue($this->conf->exists('config.foo'));
159 }
160
161 /**
162 * Test the 'exists' method with non existent or invalid values.
163 */
164 public function testExistsKo()
165 {
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));
170 }
171
172 /**
173 * Reset the ConfigManager instance.
174 */
175 public function testReset()
176 {
278d9ee2
A
177 $confIO = $this->conf->getConfigIO();
178 $this->conf->reset();
179 $this->assertFalse($confIO === $this->conf->getConfigIO());
b74b96bf
A
180 }
181
182 /**
183 * Reload the config from file.
184 */
185 public function testReload()
186 {
278d9ee2 187 $this->conf->setConfigFile('tests/utils/config/configTmp');
da10377b 188 $newConf = ConfigJson::getPhpHeaders() . '{ "key": "value" }';
278d9ee2 189 file_put_contents($this->conf->getConfigFileExt(), $newConf);
b74b96bf 190 $this->conf->reload();
278d9ee2 191 unlink($this->conf->getConfigFileExt());
b74b96bf 192 // Previous conf no longer exists, and new values have been loaded.
da10377b 193 $this->assertFalse($this->conf->exists('credentials.login'));
b74b96bf
A
194 $this->assertEquals('value', $this->conf->get('key'));
195 }
196}