]> git.mxchange.org Git - core.git/blob - tests/framework/config/FrameworkConfigurationTest.php
3a2db60bb81d37b8b314dcbe08a7db76571cc067
[core.git] / tests / framework / config / FrameworkConfigurationTest.php
1 <?php
2 // Same namespace as target class
3 namespace Org\Mxchange\CoreFramework\Configuration;
4
5 // Inport framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Configuration\FrameworkConfiguration;
8 use Org\Mxchange\CoreFramework\Configuration\NoConfigEntryException;
9 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
10 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
11
12 // Import PHPUnit stuff
13 use PHPUnit\Framework\TestCase;
14
15 // Import SPL stuff
16 use \InvalidArgumentException;
17 use \UnexpectedValueException;
18
19 /*
20  * Copyright (C) 2017 - 2020 - Core Developer Team
21  *
22  * This program is free software: you can redistribute it and/or modify
23  * it under the terms of the GNU General Public License as published by
24  * the Free Software Foundation, either version 3 of the License, or
25  * (at your option) any later version.
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30  * GNU General Public License for more details.
31  *
32  * You should have received a copy of the GNU General Public License
33  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34  */
35 class FrameworkConfigurationTest extends TestCase {
36
37         /**
38          * The configuration instance being tested
39          */
40         private static $configInstance = NULL;
41
42         /**
43          * Setup test case
44          */
45         public function setUp() {
46                 // Trace message
47                 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
48
49                 // Call parent method
50                 parent::setUp();
51
52                 // Trace message
53                 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
54
55         }
56
57         /**
58          * Setup test case
59          */
60         public static function setUpBeforeClass() {
61                 // Trace message
62                 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
63
64                 // Call parent method
65                 parent::setUpBeforeClass();
66
67                 // Init instance
68                 self::$configInstance = FrameworkBootstrap::getConfigurationInstance();
69
70                 // Trace message
71                 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
72         }
73
74         /**
75          * Tests if __toString() returns proper name
76          */
77         public function testConfigToStringClassName () {
78                 // Get it
79                 $className = self::$configInstance->__toString();
80
81                 // Should be equal
82                 $this->assertEquals('Org\Mxchange\CoreFramework\Configuration\FrameworkConfiguration', $className);
83         }
84
85         /**
86          * Tests getting a singleton instance
87          */
88         public function testGettingSelfConfigInstance () {
89                 // Get instance
90                 $dummyInstance = FrameworkBootstrap::getConfigurationInstance();
91
92                 // Should be equal to own instance
93                 $this->assertEquals(self::$configInstance, $dummyInstance);
94         }
95
96         /**
97          * Tests equals() method
98          */
99         public function testEqualsConfigInstance () {
100                 // Get instance
101                 $dummyInstance = new FrameworkConfiguration();
102
103                 // Should return TRUE
104                 $this->assertTrue(self::$configInstance->equals($dummyInstance));
105         }
106
107         /**
108          * Tests hashCode() method
109          */
110         public function testHashCodeConfigInstance () {
111                 // Get instance
112                 $dummyInstance = FrameworkBootstrap::getConfigurationInstance();
113
114                 // Get hash code from both
115                 $hashCodeExpected = self::$configInstance->hashCode();
116                 $hashCodeActual = $dummyInstance->hashCode();
117
118                 // Should be equal
119                 $this->assertEquals($hashCodeExpected, $hashCodeActual);
120         }
121
122         /**
123          * Tests if getting configuration entry returns an array
124          */
125         public function testGettingConfigurationArray () {
126                 // Get it
127                 $config = self::$configInstance->getConfigurationArray();
128
129                 // Should be an array
130                 $this->assertTrue(is_array($config));
131         }
132
133         /**
134          * Tests if getting configuration entry returns a filled array
135          */
136         public function testGettingfilledConfigurationArray () {
137                 // Get it
138                 $config = self::$configInstance->getConfigurationArray();
139
140                 // Should be an array
141                 $this->assertTrue(count($config) > 0);
142         }
143
144         /**
145          * Tests if getting whole configuration entry is the same for another
146          * singleton instance
147          */
148         public function testSameConfigurationArrayGetter () {
149                 // Get instance
150                 $dummyInstance = FrameworkBootstrap::getConfigurationInstance();
151
152                 // Get it from both instances
153                 $config1 = self::$configInstance->getConfigurationArray();
154                 $config2 = $dummyInstance->getConfigurationArray();
155
156                 // Should be the same
157                 $this->assertEquals($config1, $config2);
158         }
159
160         /**
161          * Tests if a proper exception is thrown when checking an empty key
162          */
163         public function testCheckingEmptyConfigKey () {
164                 // Will throw this exception
165                 $this->expectException(InvalidArgumentException::class);
166
167                 // Test it
168                 $dummy = self::$configInstance->isConfigurationEntrySet('');
169         }
170
171         /**
172          * Tests if checking an existing (well-known) key can be found and returns
173          * TRUE.
174          */
175         public function testCheckingExistingConfigKey () {
176                 // Should return TRUE
177                 $this->assertTrue(self::$configInstance->isConfigurationEntrySet('application_base_path'));
178         }
179
180         /**
181          * Tests if checking a non-existing key will return FALSE.
182          */
183         public function testCheckingNonExistingConfigKey () {
184                 // Should return FALSE
185                 $this->assertFalse(self::$configInstance->isConfigurationEntrySet('__non_existing_key__'));
186         }
187
188         /**
189          * Tests if a proper exception is thrown when getting an empty key
190          */
191         public function testGettingEmptyConfigKey () {
192                 // Will throw this exception
193                 $this->expectException(InvalidArgumentException::class);
194
195                 // Test it
196                 $dummy = self::$configInstance->getConfigEntry('');
197         }
198
199         /**
200          * Tests if getting a non-existing key will cause a proper exception been
201          * thrown.
202          */
203         public function testGettingNonExistingConfigKey () {
204                 // Should cause this exception
205                 $this->expectException(NoConfigEntryException::class);
206
207                 // Get non-existing key
208                 $dummy = self::$configInstance->getConfigEntry('__non_existing_key__');
209         }
210
211         /**
212          * Tests if a generic key 'application_base_path' can be found.
213          */
214         public function testGettingConfigKeyApplicationBasePathExists () {
215                 // Get it from config instance
216                 $value = self::$configInstance->getConfigEntry('application_base_path');
217
218                 // Is it a readable path?
219                 $this->assertDirectoryIsReadable($value);
220         }
221
222         /**
223          * Tests setting an empty key (value doesn't matter)
224          */
225         public function testSettingEmptyConfigKey () {
226                 // Will throw this exception
227                 $this->expectException(InvalidArgumentException::class);
228
229                 // Test it
230                 self::$configInstance->setConfigEntry('', 'foo');
231         }
232
233         /**
234          * Tests setting a valid key but array for value
235          */
236         public function testSettingArrayValueConfigKey () {
237                 // Will throw this exception
238                 $this->expectException(InvalidArgumentException::class);
239
240                 // Test it
241                 self::$configInstance->setConfigEntry('foo', array());
242         }
243
244         /**
245          * Tests setting a valid key but object for value
246          */
247         public function testSettingObjectValueConfigKey () {
248                 // Will throw this exception
249                 $this->expectException(InvalidArgumentException::class);
250
251                 // Test it
252                 self::$configInstance->setConfigEntry('foo', $this);
253         }
254
255         /**
256          * Tests setting a valid key but resource for value
257          */
258         public function testSettingResourceValueConfigKey () {
259                 // Will throw this exception
260                 $this->expectException(InvalidArgumentException::class);
261
262                 // Init some resource
263                 $resource = fopen(__FILE__, 'r');
264
265                 // Test it
266                 self::$configInstance->setConfigEntry('foo', $resource);
267         }
268
269         /**
270          * Tests unsetting an empty key
271          */
272         public function testUnettingEmptyConfigKey () {
273                 // Will throw this exception
274                 $this->expectException(InvalidArgumentException::class);
275
276                 // Test it
277                 self::$configInstance->unsetConfigEntry('');
278         }
279
280         /**
281          * Tests unsetting a non-existing key
282          */
283         public function testUnettingNonExistingConfigKey () {
284                 // Will throw this exception
285                 $this->expectException(NoConfigEntryException::class);
286
287                 // Test it
288                 self::$configInstance->unsetConfigEntry('__non_existing_key__');
289         }
290
291         /**
292          * Tests setting and getting a key will return same value of same type
293          * (NULL). This unit test does also unset the then existing key.
294          */
295         public function testSettingGettiingNullConfigKey () {
296                 // Set test value
297                 self::$configInstance->setConfigEntry('__test_key', NULL);
298
299                 // Get it
300                 $value = self::$configInstance->getConfigEntry('__test_key');
301
302                 // Must be equal
303                 $this->assertEquals(NULL, $value);
304
305                 // Unset it
306                 self::$configInstance->unsetConfigEntry('__test_key');
307         }
308
309         /**
310          * Tests setting and getting a key will return same value of same type.
311          * This unit test does also unset the then existing key.
312          */
313         public function testSettingGettiingConfigKey () {
314                 // Set test value
315                 self::$configInstance->setConfigEntry('__test_key', 'foo');
316
317                 // Get it
318                 $value = self::$configInstance->getConfigEntry('__test_key');
319
320                 // Must be equal
321                 $this->assertEquals('foo', $value);
322
323                 // Unset it
324                 self::$configInstance->unsetConfigEntry('__test_key');
325         }
326
327         /**
328          * Tests if the method getField() is still unsupported in this class. Please
329          * note, that this and isFieldSet() may get removed in the future. So also
330          * these test methods will be gone.
331          */
332         public function testConfigGetFieldUnsupported () {
333                 // Expect this exception
334                 $this->expectException(UnsupportedOperationException::class);
335
336                 // Test it
337                 $dummy = self::$configInstance->getField('foo');
338         }
339
340         /**
341          * Tests if the method isFieldSet() is still unsupported in this class. Please
342          * note, that this and getField() may get removed in the future. So also
343          * these test methods will be gone.
344          */
345         public function testConfigIsFieldSetUnsupported () {
346                 // Expect this exception
347                 $this->expectException(UnsupportedOperationException::class);
348
349                 // Test it
350                 $dummy = self::$configInstance->isFieldSet('foo');
351         }
352
353         /**
354          * Tests isEnabled() method being called with empty parameter
355          */
356         public function testConfigIsEnabledEmptyString () {
357                 // Expect IAE
358                 $this->expectException(InvalidArgumentException::class);
359
360                 // Just invoke it
361                 $dummy = self::$configInstance->isEnabled('');
362         }
363
364         /**
365          * Tests isEnabled() method being called with missing configuration key
366          */
367         public function testConfigIsEnabledMissingConfigKey () {
368                 // Expect NoConfig
369                 $this->expectException(NoConfigEntryException::class);
370
371                 // Just invoke it
372                 $dummy = self::$configInstance->isEnabled('does_not_exist');
373         }
374
375         /**
376          * Tests isEnabled() method being called with non-boolean configuration key
377          */
378         public function testConfigIsEnabledNonBooleanConfigKey () {
379                 // Expect UVE
380                 $this->expectException(UnexpectedValueException::class);
381
382                 // Set it temporary
383                 self::$configInstance->setConfigEntry('is_non_boolean_enabled', 'Y');
384
385                 // Just invoke it
386                 $dummy = self::$configInstance->isEnabled('non_boolean');
387         }
388
389         /**
390          * Tests isEnabled() method being called with 'single_server'
391          */
392         public function testConfigIsEnabledSingleServerConfigKey () {
393                 // Check it on known boolean value
394                 $this->assertTrue(is_bool(self::$configInstance->isEnabled('single_server')));
395         }
396
397         /**
398          * Tests if sorting the configuration array is always returning the same
399          * array (but sorted) back.
400          */
401         public function testConfigSortConfigurationArray () {
402                 // First get configuration array
403                 /* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
404                 $config = self::$configInstance->getConfigurationArray();
405
406                 // Run sort method
407                 /* NOISY-DEBUG: */ printf('[%s:%d]: config()=%d' . PHP_EOL, __METHOD__, __LINE__, count($config));
408                 self::$configInstance->sortConfigurationArray();
409
410                 // This should be an empty array
411                 $diff = array_diff($config, self::$configInstance->getConfigurationArray());
412
413                 // Check condition
414                 /* NOISY-DEBUG: */ printf('[%s:%d]: diff()=%d' . PHP_EOL, __METHOD__, __LINE__, count($diff));
415                 $this->assertTrue(count($diff) === 0);
416
417                 // Trace message
418                 /* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
419         }
420
421 }