2 // Same namespace as target class
3 namespace Org\Mxchange\CoreFramework\Configuration;
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\Loader\ClassLoader;
10 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
11 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
13 // Import PHPUnit stuff
14 use PHPUnit\Framework\Error\Notice;
15 use PHPUnit\Framework\TestCase;
18 use \InvalidArgumentException;
21 * Copyright (C) 2017 - 2020 - Core Developer Team
23 * This program is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 class FrameworkConfigurationTest extends TestCase {
39 * The configuration instance being tested
41 private static $configInstance = NULL;
46 public function setUp() {
48 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
54 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
61 public static function setUpBeforeClass() {
63 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
66 parent::setUpBeforeClass();
69 self::$configInstance = FrameworkBootstrap::getConfigurationInstance();
72 * Disable strict naming-convention check in own class loader, because
73 * PHP_Invoker doesn't have namespaces.
75 ClassLoader::enableStrictNamingConventionCheck(FALSE);
78 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
82 * Tests if __toString() returns proper name
84 public function testConfigToStringClassName () {
86 $className = self::$configInstance->__toString();
89 $this->assertEquals('Org\Mxchange\CoreFramework\Configuration\FrameworkConfiguration', $className);
93 * Tests getting a singleton instance
95 public function testGettingSelfConfigInstance () {
97 $dummyInstance = FrameworkBootstrap::getConfigurationInstance();
99 // Should be equal to own instance
100 $this->assertEquals(self::$configInstance, $dummyInstance);
104 * Tests equals() method
106 public function testEqualsConfigInstance () {
108 $dummyInstance = new FrameworkConfiguration();
110 // Should return TRUE
111 $this->assertTrue(self::$configInstance->equals($dummyInstance));
115 * Tests hashCode() method
117 public function testHashCodeConfigInstance () {
119 $dummyInstance = FrameworkBootstrap::getConfigurationInstance();
121 // Get hash code from both
122 $hashCodeExpected = self::$configInstance->hashCode();
123 $hashCodeActual = $dummyInstance->hashCode();
126 $this->assertEquals($hashCodeExpected, $hashCodeActual);
130 * Tests if getting configuration entry returns an array
132 public function testGettingConfigurationArray () {
134 $config = self::$configInstance->getConfigurationArray();
136 // Should be an array
137 $this->assertTrue(is_array($config));
141 * Tests if getting configuration entry returns a filled array
143 public function testGettingfilledConfigurationArray () {
145 $config = self::$configInstance->getConfigurationArray();
147 // Should be an array
148 $this->assertTrue(count($config) > 0);
152 * Tests if getting whole configuration entry is the same for another
155 public function testSameConfigurationArrayGetter () {
157 $dummyInstance = FrameworkBootstrap::getConfigurationInstance();
159 // Get it from both instances
160 $config1 = self::$configInstance->getConfigurationArray();
161 $config2 = $dummyInstance->getConfigurationArray();
163 // Should be the same
164 $this->assertEquals($config1, $config2);
168 * Tests if a proper exception is thrown when check for a NULL key
170 public function testCheckingNullConfigKey () {
171 // Will throw this exception
172 $this->expectException(NullPointerException::class);
175 $dummy = self::$configInstance->isConfigurationEntrySet(NULL);
179 * Tests if a proper exception is thrown when checking a boolean key
181 public function testCheckingBooleanConfigKey () {
182 // Will throw this exception
183 $this->expectException(InvalidArgumentException::class);
186 $dummy = self::$configInstance->isConfigurationEntrySet(FALSE);
190 * Tests if a proper exception is thrown when checking an empty key
192 public function testCheckingEmptyConfigKey () {
193 // Will throw this exception
194 $this->expectException(InvalidArgumentException::class);
197 $dummy = self::$configInstance->isConfigurationEntrySet('');
201 * Tests if a proper exception is thrown when checking an array key
203 public function testCheckingArrayConfigKey () {
204 // Will throw this exception
205 $this->expectException(InvalidArgumentException::class);
208 $dummy = self::$configInstance->isConfigurationEntrySet(array());
212 * Tests if a proper exception is thrown when checking a decimal key
214 public function testCheckingDecimalConfigKey () {
215 // Will throw this exception
216 $this->expectException(InvalidArgumentException::class);
219 $dummy = self::$configInstance->isConfigurationEntrySet(12345);
223 * Tests if a proper exception is thrown when checking a float key
225 public function testCheckingFloatConfigKey () {
226 // Will throw this exception
227 $this->expectException(InvalidArgumentException::class);
230 $dummy = self::$configInstance->isConfigurationEntrySet(123.45);
234 * Tests if a proper exception is thrown when checking an object key
236 public function testCheckingObjectConfigKey () {
237 // Will throw this exception
238 $this->expectException(InvalidArgumentException::class);
241 $dummy = self::$configInstance->isConfigurationEntrySet($this);
245 * Tests if a proper exception is thrown when checking a resource key
247 public function testCheckingResourceConfigKey () {
248 // Will throw this exception
249 $this->expectException(InvalidArgumentException::class);
251 // Init some resource
252 $resource = fopen(__FILE__, 'r');
255 $dummy = self::$configInstance->isConfigurationEntrySet($resource);
259 * Tests if checking an existing (well-known) key can be found and returns
262 public function testCheckingExistingConfigKey () {
263 // Should return TRUE
264 $this->assertTrue(self::$configInstance->isConfigurationEntrySet('application_base_path'));
268 * Tests if checking a non-existing key will return FALSE.
270 public function testCheckingNonExistingConfigKey () {
271 // Should return FALSE
272 $this->assertFalse(self::$configInstance->isConfigurationEntrySet('__non_existing_key__'));
276 * Tests if a proper exception is thrown when getting a NULL key
278 public function testGettingNullConfigKey () {
279 // Will throw this exception
280 $this->expectException(NullPointerException::class);
283 $dummy = self::$configInstance->getConfigEntry(NULL);
287 * Tests if a proper exception is thrown when getting a boolean key
289 public function testGettingBooleanConfigKey () {
290 // Will throw this exception
291 $this->expectException(InvalidArgumentException::class);
294 $dummy = self::$configInstance->getConfigEntry(FALSE);
298 * Tests if a proper exception is thrown when getting an empty key
300 public function testGettingEmptyConfigKey () {
301 // Will throw this exception
302 $this->expectException(InvalidArgumentException::class);
305 $dummy = self::$configInstance->getConfigEntry('');
309 * Tests if a proper exception is thrown when getting a decimal key
311 public function testGettingDecimalConfigKey () {
312 // Will throw this exception
313 $this->expectException(InvalidArgumentException::class);
316 $dummy = self::$configInstance->getConfigEntry(12345);
320 * Tests if a proper exception is thrown when getting a float key
322 public function testGettingFloatConfigKey () {
323 // Will throw this exception
324 $this->expectException(InvalidArgumentException::class);
327 $dummy = self::$configInstance->getConfigEntry(123.45);
331 * Tests if a proper exception is thrown when getting an array key
333 public function testGettingArrayConfigKey () {
334 // Will throw this exception
335 $this->expectException(InvalidArgumentException::class);
338 $dummy = self::$configInstance->getConfigEntry(array());
342 * Tests if a proper exception is thrown when getting an object key
344 public function testGettingObjectConfigKey () {
345 // Will throw this exception
346 $this->expectException(InvalidArgumentException::class);
349 $dummy = self::$configInstance->getConfigEntry($this);
353 * Tests if a proper exception is thrown when getting a resource key
355 public function testGettingResourceConfigKey () {
356 // Will throw this exception
357 $this->expectException(InvalidArgumentException::class);
359 // Init some resource
360 $resource = fopen(__FILE__, 'r');
363 $dummy = self::$configInstance->getConfigEntry($resource);
367 * Tests if getting a non-existing key will cause a proper exception been
370 public function testGettingNonExistingConfigKey () {
371 // Should cause this exception
372 $this->expectException(NoConfigEntryException::class);
374 // Get non-existing key
375 $dummy = self::$configInstance->getConfigEntry('__non_existing_key__');
379 * Tests if a generic key 'application_base_path' can be found.
381 public function testGettingConfigKeyApplicationBasePathExists () {
382 // Get it from config instance
383 $value = self::$configInstance->getConfigEntry('application_base_path');
385 // Is it a readable path?
386 $this->assertDirectoryIsReadable($value);
390 * Tests setting a NULL key (value doesn't matter)
392 public function testSettingNullConfigKey () {
393 // Will throw this exception
394 $this->expectException(NullPointerException::class);
397 self::$configInstance->setConfigEntry(NULL, 'foo');
401 * Tests setting a boolean key (value doesn't matter)
403 public function testSettingBooleanConfigKey () {
404 // Will throw this exception
405 $this->expectException(InvalidArgumentException::class);
408 self::$configInstance->setConfigEntry(FALSE, 'foo');
412 * Tests setting an empty key (value doesn't matter)
414 public function testSettingEmptyConfigKey () {
415 // Will throw this exception
416 $this->expectException(InvalidArgumentException::class);
419 self::$configInstance->setConfigEntry('', 'foo');
423 * Tests setting a decimal key (value doesn't matter)
425 public function testSettingDecimalConfigKey () {
426 // Will throw this exception
427 $this->expectException(InvalidArgumentException::class);
430 self::$configInstance->setConfigEntry(12345, 'foo');
434 * Tests setting a float key (value doesn't matter)
436 public function testSettingFloatConfigKey () {
437 // Will throw this exception
438 $this->expectException(InvalidArgumentException::class);
441 self::$configInstance->setConfigEntry(123.45, 'foo');
445 * Tests setting an array key (value doesn't matter)
447 public function testSettingArrayConfigKey () {
448 // Will throw this exception
449 $this->expectException(InvalidArgumentException::class);
452 self::$configInstance->setConfigEntry(array(), 'foo');
456 * Tests setting an object key (value doesn't matter)
458 public function testSettingObjectConfigKey () {
459 // Will throw this exception
460 $this->expectException(InvalidArgumentException::class);
463 self::$configInstance->setConfigEntry($this, 'foo');
467 * Tests setting a resource key (value doesn't matter)
469 public function testSettingResourceConfigKey () {
470 // Will throw this exception
471 $this->expectException(InvalidArgumentException::class);
473 // Init some resource
474 $resource = fopen(__FILE__, 'r');
477 self::$configInstance->setConfigEntry($resource, 'foo');
481 * Tests setting a valid key but array for value
483 public function testSettingArrayValueConfigKey () {
484 // Will throw this exception
485 $this->expectException(InvalidArgumentException::class);
488 self::$configInstance->setConfigEntry('foo', array());
492 * Tests setting a valid key but object for value
494 public function testSettingObjectValueConfigKey () {
495 // Will throw this exception
496 $this->expectException(InvalidArgumentException::class);
499 self::$configInstance->setConfigEntry('foo', $this);
503 * Tests setting a valid key but resource for value
505 public function testSettingResourceValueConfigKey () {
506 // Will throw this exception
507 $this->expectException(InvalidArgumentException::class);
509 // Init some resource
510 $resource = fopen(__FILE__, 'r');
513 self::$configInstance->setConfigEntry('foo', $resource);
517 * Tests unsetting NULL key
519 public function testUnsettingNullConfigKey () {
520 // Will throw this exception
521 $this->expectException(NullPointerException::class);
524 self::$configInstance->unsetConfigEntry(NULL);
528 * Tests unsetting boolean key
530 public function testUnsettingBooleanConfigKey () {
531 // Will throw this exception
532 $this->expectException(InvalidArgumentException::class);
535 self::$configInstance->unsetConfigEntry(FALSE);
539 * Tests unsetting decimal key
541 public function testUnsettingDecimalConfigKey () {
542 // Will throw this exception
543 $this->expectException(InvalidArgumentException::class);
546 self::$configInstance->unsetConfigEntry(12345);
550 * Tests unsetting float key
552 public function testUnsettingFloatConfigKey () {
553 // Will throw this exception
554 $this->expectException(InvalidArgumentException::class);
557 self::$configInstance->unsetConfigEntry(123.45);
561 * Tests unsetting array key
563 public function testUnsettingArrayConfigKey () {
564 // Will throw this exception
565 $this->expectException(InvalidArgumentException::class);
568 self::$configInstance->unsetConfigEntry(array());
572 * Tests unsetting object key
574 public function testUnsettingObjectConfigKey () {
575 // Will throw this exception
576 $this->expectException(InvalidArgumentException::class);
579 self::$configInstance->unsetConfigEntry($this);
583 * Tests unsetting resource key
585 public function testUnsettingResourceConfigKey () {
586 // Will throw this exception
587 $this->expectException(InvalidArgumentException::class);
589 // Init some resource
590 $resource = fopen(__FILE__, 'r');
593 self::$configInstance->unsetConfigEntry($resource);
597 * Tests unsetting an empty key
599 public function testUnettingEmptyConfigKey () {
600 // Will throw this exception
601 $this->expectException(InvalidArgumentException::class);
604 self::$configInstance->unsetConfigEntry('');
608 * Tests unsetting a non-existing key
610 public function testUnettingNonExistingConfigKey () {
611 // Will throw this exception
612 $this->expectException(NoConfigEntryException::class);
615 self::$configInstance->unsetConfigEntry('__non_existing_key__');
619 * Tests setting and getting a key will return same value of same type
620 * (NULL). This unit test does also unset the then existing key.
622 public function testSettingGettiingNullConfigKey () {
624 self::$configInstance->setConfigEntry('__test_key', NULL);
627 $value = self::$configInstance->getConfigEntry('__test_key');
630 $this->assertEquals(NULL, $value);
633 self::$configInstance->unsetConfigEntry('__test_key');
637 * Tests setting and getting a key will return same value of same type.
638 * This unit test does also unset the then existing key.
640 public function testSettingGettiingConfigKey () {
642 self::$configInstance->setConfigEntry('__test_key', 'foo');
645 $value = self::$configInstance->getConfigEntry('__test_key');
648 $this->assertEquals('foo', $value);
651 self::$configInstance->unsetConfigEntry('__test_key');
655 * Tests if the method getField() is still unsupported in this class. Please
656 * note, that this and isFieldSet() may get removed in the future. So also
657 * these test methods will be gone.
659 public function testConfigGetFieldUnsupported () {
660 // Expect this exception
661 $this->expectException(UnsupportedOperationException::class);
664 $dummy = self::$configInstance->getField('foo');
668 * Tests if the method isFieldSet() is still unsupported in this class. Please
669 * note, that this and getField() may get removed in the future. So also
670 * these test methods will be gone.
672 public function testConfigIsFieldSetUnsupported () {
673 // Expect this exception
674 $this->expectException(UnsupportedOperationException::class);
677 $dummy = self::$configInstance->isFieldSet('foo');