3 // Same namespace as target class
4 namespace CoreFramework\Configuration;
6 // Inport framework stuff
7 use CoreFramework\Console\Tools\ConsoleTools;
8 use CoreFramework\Loader\ClassLoader;
9 use CoreFramework\Generic\NullPointerException;
10 use CoreFramework\Generic\UnsupportedOperationException;
12 // Import PHPUnit stuff
13 use PHPUnit\Framework\Error\Notice;
14 use PHPUnit\Framework\TestCase;
17 use \InvalidArgumentException;
20 * Copyright (C) 2017 Roland Haeder<roland@mxchange.org>
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.
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.
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/>.
36 class FrameworkConfigurationTest extends TestCase {
39 * The configuration instance being tested
41 private static $configInstance = NULL;
46 private static $ipAddress = FALSE;
51 public function setUp() {
53 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
59 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
66 public static function setUpBeforeClass() {
68 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
71 parent::setUpBeforeClass();
74 self::$configInstance = FrameworkConfiguration::getSelfInstance();
77 * Disable strict naming-convention check in own class loader, because
78 * PHP_Invoker doesn't have namespaces.
80 ClassLoader::enableStrictNamingConventionCheck(FALSE);
82 // Quiet DNS resolver as this is not wanted here
83 self::$configInstance->setConfigEntry('quiet_dns_resolver', TRUE);
85 // Lookup own IP address
86 self::$ipAddress = ConsoleTools::acquireSelfIpAddress();
89 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
93 * Tests if __toString() returns proper name
95 public function testConfigToStringClassName () {
97 $className = self::$configInstance->__toString();
100 $this->assertEquals('CoreFramework\Configuration\FrameworkConfiguration', $className);
104 * Tests getting a singleton instance
106 public function testGettingSelfConfigInstance () {
108 $dummyInstance = FrameworkConfiguration::getSelfInstance();
110 // Should be equal to own instance
111 $this->assertEquals(self::$configInstance, $dummyInstance);
115 * Tests equals() method
117 public function testEqualsConfigInstance () {
119 $dummyInstance = FrameworkConfiguration::getSelfInstance();
121 // Should return TRUE
122 $this->assertTrue(self::$configInstance->equals($dummyInstance));
126 * Tests hashCode() method
128 public function testHashCodeConfigInstance () {
130 $dummyInstance = FrameworkConfiguration::getSelfInstance();
132 // Get hash code from both
133 $hashCodeExpected = self::$configInstance->hashCode();
134 $hashCodeActual = $dummyInstance->hashCode();
137 $this->assertEquals($hashCodeExpected, $hashCodeActual);
141 * Tests if getting configuration entry returns an array
143 public function testGettingConfigurationArray () {
145 $config = self::$configInstance->getConfigurationArray();
147 // Should be an array
148 $this->assertTrue(is_array($config));
152 * Tests if getting configuration entry returns a filled array
154 public function testGettingfilledConfigurationArray () {
156 $config = self::$configInstance->getConfigurationArray();
158 // Should be an array
159 $this->assertTrue(count($config) > 0);
163 * Tests if getting whole configuration entry is the same for another
166 public function testSameConfigurationArrayGetter () {
168 $dummyInstance = FrameworkConfiguration::getSelfInstance();
170 // Get it from both instances
171 $config1 = self::$configInstance->getConfigurationArray();
172 $config2 = $dummyInstance->getConfigurationArray();
174 // Should be the same
175 $this->assertEquals($config1, $config2);
179 * Tests if a proper exception is thrown when check for a NULL key
181 public function testCheckingNullConfigKey () {
182 // Will throw this exception
183 $this->expectException(NullPointerException::class);
186 $dummy = self::$configInstance->isConfigurationEntrySet(NULL);
190 * Tests if a proper exception is thrown when checking a boolean key
192 public function testCheckingBooleanConfigKey () {
193 // Will throw this exception
194 $this->expectException(InvalidArgumentException::class);
197 $dummy = self::$configInstance->isConfigurationEntrySet(FALSE);
201 * Tests if a proper exception is thrown when checking an empty key
203 public function testCheckingEmptyConfigKey () {
204 // Will throw this exception
205 $this->expectException(InvalidArgumentException::class);
208 $dummy = self::$configInstance->isConfigurationEntrySet('');
212 * Tests if a proper exception is thrown when checking an array key
214 public function testCheckingArrayConfigKey () {
215 // Will throw this exception
216 $this->expectException(InvalidArgumentException::class);
219 $dummy = self::$configInstance->isConfigurationEntrySet(array());
223 * Tests if a proper exception is thrown when checking a decimal key
225 public function testCheckingDecimalConfigKey () {
226 // Will throw this exception
227 $this->expectException(InvalidArgumentException::class);
230 $dummy = self::$configInstance->isConfigurationEntrySet(12345);
234 * Tests if a proper exception is thrown when checking a float key
236 public function testCheckingFloatConfigKey () {
237 // Will throw this exception
238 $this->expectException(InvalidArgumentException::class);
241 $dummy = self::$configInstance->isConfigurationEntrySet(123.45);
245 * Tests if a proper exception is thrown when checking an object key
247 public function testCheckingObjectConfigKey () {
248 // Will throw this exception
249 $this->expectException(InvalidArgumentException::class);
252 $dummy = self::$configInstance->isConfigurationEntrySet($this);
256 * Tests if a proper exception is thrown when checking a resource key
258 public function testCheckingResourceConfigKey () {
259 // Will throw this exception
260 $this->expectException(InvalidArgumentException::class);
262 // Init some resource
263 $resource = fopen(__FILE__, 'r');
266 $dummy = self::$configInstance->isConfigurationEntrySet($resource);
270 * Tests if checking an existing (well-known) key can be found and returns
273 public function testCheckingExistingConfigKey () {
274 // Should return TRUE
275 $this->assertTrue(self::$configInstance->isConfigurationEntrySet('application_base_path'));
279 * Tests if checking a non-existing key will return FALSE.
281 public function testCheckingNonExistingConfigKey () {
282 // Should return FALSE
283 $this->assertFalse(self::$configInstance->isConfigurationEntrySet('__non_existing_key__'));
287 * Tests if a proper exception is thrown when getting a NULL key
289 public function testGettingNullConfigKey () {
290 // Will throw this exception
291 $this->expectException(NullPointerException::class);
294 $dummy = self::$configInstance->getConfigEntry(NULL);
298 * Tests if a proper exception is thrown when getting a boolean key
300 public function testGettingBooleanConfigKey () {
301 // Will throw this exception
302 $this->expectException(InvalidArgumentException::class);
305 $dummy = self::$configInstance->getConfigEntry(FALSE);
309 * Tests if a proper exception is thrown when getting an empty key
311 public function testGettingEmptyConfigKey () {
312 // Will throw this exception
313 $this->expectException(InvalidArgumentException::class);
316 $dummy = self::$configInstance->getConfigEntry('');
320 * Tests if a proper exception is thrown when getting a decimal key
322 public function testGettingDecimalConfigKey () {
323 // Will throw this exception
324 $this->expectException(InvalidArgumentException::class);
327 $dummy = self::$configInstance->getConfigEntry(12345);
331 * Tests if a proper exception is thrown when getting a float key
333 public function testGettingFloatConfigKey () {
334 // Will throw this exception
335 $this->expectException(InvalidArgumentException::class);
338 $dummy = self::$configInstance->getConfigEntry(123.45);
342 * Tests if a proper exception is thrown when getting an array key
344 public function testGettingArrayConfigKey () {
345 // Will throw this exception
346 $this->expectException(InvalidArgumentException::class);
349 $dummy = self::$configInstance->getConfigEntry(array());
353 * Tests if a proper exception is thrown when getting an object key
355 public function testGettingObjectConfigKey () {
356 // Will throw this exception
357 $this->expectException(InvalidArgumentException::class);
360 $dummy = self::$configInstance->getConfigEntry($this);
364 * Tests if a proper exception is thrown when getting a resource key
366 public function testGettingResourceConfigKey () {
367 // Will throw this exception
368 $this->expectException(InvalidArgumentException::class);
370 // Init some resource
371 $resource = fopen(__FILE__, 'r');
374 $dummy = self::$configInstance->getConfigEntry($resource);
378 * Tests if getting a non-existing key will cause a proper exception been
381 public function testGettingNonExistingConfigKey () {
382 // Should cause this exception
383 $this->expectException(NoConfigEntryException::class);
385 // Get non-existing key
386 $dummy = self::$configInstance->getConfigEntry('__non_existing_key__');
390 * Tests if a generic key 'application_base_path' can be found.
392 public function testGettingConfigKeyApplicationBasePathExists () {
393 // Get it from config instance
394 $value = self::$configInstance->getConfigEntry('application_base_path');
396 // Is it a readable path?
397 $this->assertDirectoryIsReadable($value);
401 * Tests setting a NULL key (value doesn't matter)
403 public function testSettingNullConfigKey () {
404 // Will throw this exception
405 $this->expectException(NullPointerException::class);
408 self::$configInstance->setConfigEntry(NULL, 'foo');
412 * Tests setting a boolean key (value doesn't matter)
414 public function testSettingBooleanConfigKey () {
415 // Will throw this exception
416 $this->expectException(InvalidArgumentException::class);
419 self::$configInstance->setConfigEntry(FALSE, 'foo');
423 * Tests setting an empty key (value doesn't matter)
425 public function testSettingEmptyConfigKey () {
426 // Will throw this exception
427 $this->expectException(InvalidArgumentException::class);
430 self::$configInstance->setConfigEntry('', 'foo');
434 * Tests setting a decimal key (value doesn't matter)
436 public function testSettingDecimalConfigKey () {
437 // Will throw this exception
438 $this->expectException(InvalidArgumentException::class);
441 self::$configInstance->setConfigEntry(12345, 'foo');
445 * Tests setting a float key (value doesn't matter)
447 public function testSettingFloatConfigKey () {
448 // Will throw this exception
449 $this->expectException(InvalidArgumentException::class);
452 self::$configInstance->setConfigEntry(123.45, 'foo');
456 * Tests setting an array key (value doesn't matter)
458 public function testSettingArrayConfigKey () {
459 // Will throw this exception
460 $this->expectException(InvalidArgumentException::class);
463 self::$configInstance->setConfigEntry(array(), 'foo');
467 * Tests setting an object key (value doesn't matter)
469 public function testSettingObjectConfigKey () {
470 // Will throw this exception
471 $this->expectException(InvalidArgumentException::class);
474 self::$configInstance->setConfigEntry($this, 'foo');
478 * Tests setting a resource key (value doesn't matter)
480 public function testSettingResourceConfigKey () {
481 // Will throw this exception
482 $this->expectException(InvalidArgumentException::class);
484 // Init some resource
485 $resource = fopen(__FILE__, 'r');
488 self::$configInstance->setConfigEntry($resource, 'foo');
492 * Tests setting a valid key but array for value
494 public function testSettingArrayValueConfigKey () {
495 // Will throw this exception
496 $this->expectException(InvalidArgumentException::class);
499 self::$configInstance->setConfigEntry('foo', array());
503 * Tests setting a valid key but object for value
505 public function testSettingObjectValueConfigKey () {
506 // Will throw this exception
507 $this->expectException(InvalidArgumentException::class);
510 self::$configInstance->setConfigEntry('foo', $this);
514 * Tests setting a valid key but resource for value
516 public function testSettingResourceValueConfigKey () {
517 // Will throw this exception
518 $this->expectException(InvalidArgumentException::class);
520 // Init some resource
521 $resource = fopen(__FILE__, 'r');
524 self::$configInstance->setConfigEntry('foo', $resource);
528 * Tests unsetting NULL key
530 public function testUnsettingNullConfigKey () {
531 // Will throw this exception
532 $this->expectException(NullPointerException::class);
535 self::$configInstance->unsetConfigEntry(NULL);
539 * Tests unsetting boolean key
541 public function testUnsettingBooleanConfigKey () {
542 // Will throw this exception
543 $this->expectException(InvalidArgumentException::class);
546 self::$configInstance->unsetConfigEntry(FALSE);
550 * Tests unsetting decimal key
552 public function testUnsettingDecimalConfigKey () {
553 // Will throw this exception
554 $this->expectException(InvalidArgumentException::class);
557 self::$configInstance->unsetConfigEntry(12345);
561 * Tests unsetting float key
563 public function testUnsettingFloatConfigKey () {
564 // Will throw this exception
565 $this->expectException(InvalidArgumentException::class);
568 self::$configInstance->unsetConfigEntry(123.45);
572 * Tests unsetting array key
574 public function testUnsettingArrayConfigKey () {
575 // Will throw this exception
576 $this->expectException(InvalidArgumentException::class);
579 self::$configInstance->unsetConfigEntry(array());
583 * Tests unsetting object key
585 public function testUnsettingObjectConfigKey () {
586 // Will throw this exception
587 $this->expectException(InvalidArgumentException::class);
590 self::$configInstance->unsetConfigEntry($this);
594 * Tests unsetting resource key
596 public function testUnsettingResourceConfigKey () {
597 // Will throw this exception
598 $this->expectException(InvalidArgumentException::class);
600 // Init some resource
601 $resource = fopen(__FILE__, 'r');
604 self::$configInstance->unsetConfigEntry($resource);
608 * Tests unsetting an empty key
610 public function testUnettingEmptyConfigKey () {
611 // Will throw this exception
612 $this->expectException(InvalidArgumentException::class);
615 self::$configInstance->unsetConfigEntry('');
619 * Tests unsetting a non-existing key
621 public function testUnettingNonExistingConfigKey () {
622 // Will throw this exception
623 $this->expectException(NoConfigEntryException::class);
626 self::$configInstance->unsetConfigEntry('__non_existing_key__');
630 * Tests setting and getting a key will return same value of same type
631 * (NULL). This unit test does also unset the then existing key.
633 public function testSettingGettiingNullConfigKey () {
635 self::$configInstance->setConfigEntry('__test_key', NULL);
638 $value = self::$configInstance->getConfigEntry('__test_key');
641 $this->assertEquals(NULL, $value);
644 self::$configInstance->unsetConfigEntry('__test_key');
648 * Tests setting and getting a key will return same value of same type.
649 * This unit test does also unset the then existing key.
651 public function testSettingGettiingConfigKey () {
653 self::$configInstance->setConfigEntry('__test_key', 'foo');
656 $value = self::$configInstance->getConfigEntry('__test_key');
659 $this->assertEquals('foo', $value);
662 self::$configInstance->unsetConfigEntry('__test_key');
666 * Tests setting a NULL default timezone
668 public function testSettingNullDefaultTimezone () {
669 // Will throw this exception
670 $this->expectException(NullPointerException::class);
673 self::$configInstance->setDefaultTimezone(NULL);
677 * Tests setting a boolean default timezone
679 public function testSettingBooleanDefaultTimezone () {
680 // Will throw this exception
681 $this->expectException(InvalidArgumentException::class);
684 self::$configInstance->setDefaultTimezone(FALSE);
688 * Tests setting a decimal default timezone
690 public function testSettingDecimalDefaultTimezone () {
691 // Will throw this exception
692 $this->expectException(InvalidArgumentException::class);
695 self::$configInstance->setDefaultTimezone(12345);
699 * Tests setting a float default timezone
701 public function testSettingFloatDefaultTimezone () {
702 // Will throw this exception
703 $this->expectException(InvalidArgumentException::class);
706 self::$configInstance->setDefaultTimezone(123.45);
710 * Tests setting an array default timezone
712 public function testSettingArrayDefaultTimezone () {
713 // Will throw this exception
714 $this->expectException(InvalidArgumentException::class);
717 self::$configInstance->setDefaultTimezone(array());
721 * Tests setting an object default timezone
723 public function testSettingObjectDefaultTimezone () {
724 // Will throw this exception
725 $this->expectException(InvalidArgumentException::class);
728 self::$configInstance->setDefaultTimezone($this);
732 * Tests setting a resource default timezone
734 public function testSettingResourceDefaultTimezone () {
735 // Will throw this exception
736 $this->expectException(InvalidArgumentException::class);
738 // Init some resource
739 $resource = fopen(__FILE__, 'r');
742 self::$configInstance->setDefaultTimezone($resource);
746 * Tests setting an empty default timezone
748 public function testSettingEmptyDefaultTimezone () {
749 // Will throw this exception
750 $this->expectException(InvalidArgumentException::class);
753 self::$configInstance->setDefaultTimezone('');
757 * Tests setting invalid timezone
759 public function testSettingInvalidDefaultTimezone () {
761 $this->expectException(Notice::class);
764 self::$configInstance->setDefaultTimezone('!invalid!');
768 * Tests setting valid timezone
770 public function testSettingValidDefaultTimezone () {
772 $this->assertTrue(self::$configInstance->setDefaultTimezone('Europe/Berlin'));
776 * Tests if detectServerAddress is returning what it should for tests.
777 * This will always be 127.0.0.1.
779 public function testConfigDetectServerAddress () {
781 $serverAddress = self::$configInstance->detectServerAddress();
783 // Should be the same
784 $this->assertEquals(self::$ipAddress, $serverAddress);
788 * Re-tests if detectServerAddress is returning what it should for tests.
789 * This will always be 127.0.0.1. This call should not invoke
790 * ConsoleTools's method as the configuration entry is already cached.
792 public function testConfigDetectServerAddressCached () {
794 $serverAddress = self::$configInstance->detectServerAddress();
796 // Should be the same
797 $this->assertEquals(self::$ipAddress, $serverAddress);
801 * Tests setting a NULL server address
803 public function testConfigSettingNullServerAddress () {
804 // Expect this exception
805 $this->expectException(NullPointerException::class);
808 self::$configInstance->setServerAddress(NULL);
812 * Tests setting a boolean server address
814 public function testConfigSettingBooleanServerAddress () {
815 // Expect this exception
816 $this->expectException(InvalidArgumentException::class);
819 self::$configInstance->setServerAddress(FALSE);
823 * Tests setting a decimal server address
825 public function testConfigSettingDecimalServerAddress () {
826 // Expect this exception
827 $this->expectException(InvalidArgumentException::class);
830 self::$configInstance->setServerAddress(12345);
834 * Tests setting a float server address
836 public function testConfigSettingFloatServerAddress () {
837 // Expect this exception
838 $this->expectException(InvalidArgumentException::class);
841 self::$configInstance->setServerAddress(123.45);
845 * Tests setting an array server address
847 public function testConfigSettingArrayServerAddress () {
848 // Expect this exception
849 $this->expectException(InvalidArgumentException::class);
852 self::$configInstance->setServerAddress(array());
856 * Tests setting an object server address
858 public function testConfigSettingObjectServerAddress () {
859 // Expect this exception
860 $this->expectException(InvalidArgumentException::class);
863 self::$configInstance->setServerAddress($this);
867 * Tests setting a resource server address
869 public function testConfigSettingResourceServerAddress () {
870 // Expect this exception
871 $this->expectException(InvalidArgumentException::class);
873 // Init some resource
874 $resource = fopen(__FILE__, 'r');
877 self::$configInstance->setServerAddress($resource);
881 * Tests setting an empty server address
883 public function testConfigSettingEmptyServerAddress () {
884 // Expect this exception
885 $this->expectException(InvalidArgumentException::class);
888 self::$configInstance->setServerAddress('');
892 * Tests setting a valid server address and getting it back
894 public function testConfigGettingValidServerAddress () {
896 self::$configInstance->setServerAddress('127.0.0.1');
899 $serverAddress = self::$configInstance->getServerAddress();
902 $this->assertEquals('127.0.0.1', $serverAddress);
905 self::$configInstance->setServerAddress(self::$ipAddress);
909 * Tests if the method getField() is still unsupported in this class. Please
910 * note, that this and isFieldSet() may get removed in the future. So also
911 * these test methods will be gone.
913 public function testConfigGetFieldUnsupported () {
914 // Expect this exception
915 $this->expectException(UnsupportedOperationException::class);
918 $dummy = self::$configInstance->getField('foo');
922 * Tests if the method isFieldSet() is still unsupported in this class. Please
923 * note, that this and getField() may get removed in the future. So also
924 * these test methods will be gone.
926 public function testConfigIsFieldSetUnsupported () {
927 // Expect this exception
928 $this->expectException(UnsupportedOperationException::class);
931 $dummy = self::$configInstance->isFieldSet('foo');