Browse Source

Adding ConfigCacheLoaderTest

tags/2019.03^2
Philipp Holzer 6 months ago
parent
commit
199ceeadbf
No account linked to committer's email address

+ 0
- 8
src/Core/Config/ConfigCacheLoader.php View File

@@ -160,10 +160,6 @@ class ConfigCacheLoader
160 160
 	 */
161 161
 	private function loadINIConfigFile($filepath)
162 162
 	{
163
-		if (!file_exists($filepath)) {
164
-			throw new \Exception('Error parsing non-existent INI config file ' . $filepath);
165
-		}
166
-
167 163
 		$contents = include($filepath);
168 164
 
169 165
 		$config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
@@ -193,10 +189,6 @@ class ConfigCacheLoader
193 189
 	 */
194 190
 	private function loadConfigFile($filepath)
195 191
 	{
196
-		if (!file_exists($filepath)) {
197
-			throw new \Exception('Error loading non-existent config file ' . $filepath);
198
-		}
199
-
200 192
 		$config = include($filepath);
201 193
 
202 194
 		if (!is_array($config)) {

+ 1
- 1
tests/Util/VFSTrait.php View File

@@ -60,7 +60,7 @@ trait VFSTrait
60 60
 	protected function delConfigFile($filename)
61 61
 	{
62 62
 		if ($this->root->hasChild('config/' . $filename)) {
63
-			$this->root->removeChild('config/' . $filename);
63
+			$this->root->getChild('config')->removeChild($filename);
64 64
 		}
65 65
 	}
66 66
 }

+ 13
- 0
tests/datasets/config/.htconfig.test.php View File

@@ -0,0 +1,13 @@
1
+<?php
2
+/**
3
+ * A test .htconfig file
4
+ */
5
+
6
+$db_host = 'testhost';
7
+$db_user = 'testuser';
8
+$db_pass = 'testpw';
9
+$db_data = 'testdb';
10
+
11
+$pidfile = '/var/run/friendica.pid';
12
+$default_timezone = 'Europe/Berlin';
13
+$lang = 'fr';

+ 27
- 0
tests/datasets/config/local.config.php View File

@@ -0,0 +1,27 @@
1
+<?php
2
+
3
+/**
4
+ * A test file for local configuration
5
+ *
6
+ */
7
+
8
+return [
9
+	'database' => [
10
+		'hostname' => 'testhost',
11
+		'username' => 'testuser',
12
+		'password' => 'testpw',
13
+		'database' => 'testdb',
14
+		'charset' => 'utf8mb4',
15
+	],
16
+
17
+	'config' => [
18
+		'admin_email' => 'admin@test.it',
19
+		'sitename' => 'Friendica Social Network',
20
+		'register_policy' => \Friendica\Module\Register::OPEN,
21
+		'register_text' => '',
22
+	],
23
+	'system' => [
24
+		'default_timezone' => 'UTC',
25
+		'language' => 'en',
26
+	],
27
+];

+ 16
- 0
tests/datasets/config/local.ini.php View File

@@ -0,0 +1,16 @@
1
+<?php
2
+/**
3
+ * A test local ini file
4
+ */
5
+
6
+return <<<INI
7
+
8
+[database]
9
+hostname = testhost
10
+username = testuser
11
+password = testpw
12
+database = testdb
13
+
14
+[config]
15
+admin_email = admin@test.it
16
+INI;

+ 184
- 0
tests/src/Core/Config/ConfigCacheLoaderTest.php View File

@@ -0,0 +1,184 @@
1
+<?php
2
+
3
+namespace Friendica\Test\Core\Config;
4
+
5
+use Friendica\Core\Config\ConfigCache;
6
+use Friendica\Core\Config\ConfigCacheLoader;
7
+use Friendica\Test\MockedTest;
8
+use Friendica\Test\Util\VFSTrait;
9
+use org\bovigo\vfs\vfsStream;
10
+
11
+class ConfigCacheLoaderTest extends MockedTest
12
+{
13
+	use VFSTrait;
14
+
15
+	protected function setUp()
16
+	{
17
+		parent::setUp();
18
+
19
+		$this->setUpVfsDir();
20
+	}
21
+
22
+	/**
23
+	 * Test the loadConfigFiles() method with default values
24
+	 */
25
+	public function testLoadConfigFiles()
26
+	{
27
+		$configCacheLoader = new ConfigCacheLoader($this->root->url());
28
+		$configCache = new ConfigCache();
29
+
30
+		$configCacheLoader->loadConfigFiles($configCache);
31
+
32
+		$this->assertEquals($this->root->url(), $configCache->get('system', 'basepath'));
33
+	}
34
+
35
+	/**
36
+	 * Test the loadConfigFiles() method with a wrong local.config.php
37
+	 * @expectedException \Exception
38
+	 * @expectedExceptionMessageRegExp /Error loading config file \w+/
39
+	 */
40
+	public function testLoadConfigWrong()
41
+	{
42
+		$this->delConfigFile('local.config.php');
43
+
44
+		vfsStream::newFile('local.config.php')
45
+			->at($this->root->getChild('config'))
46
+			->setContent('<?php return true;');
47
+
48
+		$configCacheLoader = new ConfigCacheLoader($this->root->url());
49
+		$configCache = new ConfigCache();
50
+
51
+		$configCacheLoader->loadConfigFiles($configCache);
52
+	}
53
+
54
+	/**
55
+	 * Test the loadConfigFiles() method with a local.config.php file
56
+	 */
57
+	public function testLoadConfigFilesLocal()
58
+	{
59
+		$this->delConfigFile('local.config.php');
60
+
61
+		$file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
62
+			'..' . DIRECTORY_SEPARATOR .
63
+			'..' . DIRECTORY_SEPARATOR .
64
+			'datasets' . DIRECTORY_SEPARATOR .
65
+			'config' . DIRECTORY_SEPARATOR .
66
+			'local.config.php';
67
+
68
+		vfsStream::newFile('local.config.php')
69
+			->at($this->root->getChild('config'))
70
+			->setContent(file_get_contents($file));
71
+
72
+		$configCacheLoader = new ConfigCacheLoader($this->root->url());
73
+		$configCache = new ConfigCache();
74
+
75
+		$configCacheLoader->loadConfigFiles($configCache);
76
+
77
+		$this->assertEquals('testhost', $configCache->get('database', 'hostname'));
78
+		$this->assertEquals('testuser', $configCache->get('database', 'username'));
79
+		$this->assertEquals('testpw', $configCache->get('database', 'password'));
80
+		$this->assertEquals('testdb', $configCache->get('database', 'database'));
81
+
82
+		$this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
83
+		$this->assertEquals('Friendica Social Network', $configCache->get('config', 'sitename'));
84
+	}
85
+
86
+	/**
87
+	 * Test the loadConfigFile() method with a local.ini.php file
88
+	 */
89
+	public function testLoadConfigFilesINI()
90
+	{
91
+		$this->delConfigFile('local.config.php');
92
+
93
+		$file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
94
+			'..' . DIRECTORY_SEPARATOR .
95
+			'..' . DIRECTORY_SEPARATOR .
96
+			'datasets' . DIRECTORY_SEPARATOR .
97
+			'config' . DIRECTORY_SEPARATOR .
98
+			'local.ini.php';
99
+
100
+		vfsStream::newFile('local.ini.php')
101
+			->at($this->root->getChild('config'))
102
+			->setContent(file_get_contents($file));
103
+
104
+		$configCacheLoader = new ConfigCacheLoader($this->root->url());
105
+		$configCache = new ConfigCache();
106
+
107
+		$configCacheLoader->loadConfigFiles($configCache);
108
+
109
+		$this->assertEquals('testhost', $configCache->get('database', 'hostname'));
110
+		$this->assertEquals('testuser', $configCache->get('database', 'username'));
111
+		$this->assertEquals('testpw', $configCache->get('database', 'password'));
112
+		$this->assertEquals('testdb', $configCache->get('database', 'database'));
113
+
114
+		$this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
115
+	}
116
+
117
+	/**
118
+	 * Test the loadConfigFile() method with a .htconfig.php file
119
+	 */
120
+	public function testLoadConfigFilesHtconfig()
121
+	{
122
+		$this->delConfigFile('local.config.php');
123
+
124
+		$file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
125
+			'..' . DIRECTORY_SEPARATOR .
126
+			'..' . DIRECTORY_SEPARATOR .
127
+			'datasets' . DIRECTORY_SEPARATOR .
128
+			'config' . DIRECTORY_SEPARATOR .
129
+			'.htconfig.test.php';
130
+
131
+		vfsStream::newFile('.htconfig.php')
132
+			->at($this->root)
133
+			->setContent(file_get_contents($file));
134
+
135
+		$configCacheLoader = new ConfigCacheLoader($this->root->url());
136
+		$configCache = new ConfigCache();
137
+
138
+		$configCacheLoader->loadConfigFiles($configCache);
139
+
140
+		$this->assertEquals('testhost', $configCache->get('database', 'hostname'));
141
+		$this->assertEquals('testuser', $configCache->get('database', 'username'));
142
+		$this->assertEquals('testpw', $configCache->get('database', 'password'));
143
+		$this->assertEquals('testdb', $configCache->get('database', 'database'));
144
+
145
+		$this->assertEquals('/var/run/friendica.pid', $configCache->get('system', 'pidfile'));
146
+		$this->assertEquals('Europe/Berlin', $configCache->get('system', 'default_timezone'));
147
+		$this->assertEquals('fr', $configCache->get('system', 'language'));
148
+	}
149
+
150
+	public function testLoadAddonConfig()
151
+	{
152
+		$structure = [
153
+			'addon' => [
154
+				'test' => [
155
+					'config' => [],
156
+				],
157
+			],
158
+		];
159
+
160
+		vfsStream::create($structure, $this->root);
161
+
162
+		$file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
163
+			'..' . DIRECTORY_SEPARATOR .
164
+			'..' . DIRECTORY_SEPARATOR .
165
+			'datasets' . DIRECTORY_SEPARATOR .
166
+			'config' . DIRECTORY_SEPARATOR .
167
+			'local.config.php';
168
+
169
+		vfsStream::newFile('test.config.php')
170
+			->at($this->root->getChild('addon')->getChild('test')->getChild('config'))
171
+			->setContent(file_get_contents($file));
172
+
173
+		$configCacheLoader = new ConfigCacheLoader($this->root->url());
174
+
175
+		$conf = $configCacheLoader->loadAddonConfig('test');
176
+
177
+		$this->assertEquals('testhost', $conf['database']['hostname']);
178
+		$this->assertEquals('testuser', $conf['database']['username']);
179
+		$this->assertEquals('testpw', $conf['database']['password']);
180
+		$this->assertEquals('testdb', $conf['database']['database']);
181
+
182
+		$this->assertEquals('admin@test.it', $conf['config']['admin_email']);
183
+	}
184
+}

Loading…
Cancel
Save