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;
11 // Import PHPUnit stuff
12 use PHPUnit\Framework\Error\Notice;
13 use PHPUnit\Framework\TestCase;
16 use \InvalidArgumentException;
19 * Copyright (C) 2017 Roland Haeder<roland@mxchange.org>
21 * This program is free software: you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation, either version 3 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 class FrameworkConfigurationTest extends TestCase {
38 * The configuration instance being tested
40 private static $configInstance = NULL;
45 private static $ipAddress = FALSE;
50 public function setUp() {
52 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
58 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
65 public static function setUpBeforeClass() {
67 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
70 parent::setUpBeforeClass();
73 self::$configInstance = FrameworkConfiguration::getSelfInstance();
76 * Disable strict naming-convention check in own class loader, because
77 * PHP_Invoker doesn't have namespaces.
79 ClassLoader::enableStrictNamingConventionCheck(FALSE);
81 // Quiet DNS resolver as this is not wanted here
82 self::$configInstance->setConfigEntry('quiet_dns_resolver', TRUE);
84 // Lookup own IP address
85 self::$ipAddress = ConsoleTools::acquireSelfIpAddress();
88 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
92 * Tests if __toString() returns proper name
94 public function testConfigToStringClassName () {
96 $className = self::$configInstance->__toString();
99 $this->assertEquals('CoreFramework\Configuration\FrameworkConfiguration', $className);
103 * Tests getting a singleton instance
105 public function testGettingSelfConfigInstance () {
107 $dummyInstance = FrameworkConfiguration::getSelfInstance();
109 // Should be equal to own instance
110 $this->assertEquals(self::$configInstance, $dummyInstance);
114 * Tests equals() method
116 public function testEqualsConfigInstance () {
118 $dummyInstance = FrameworkConfiguration::getSelfInstance();
120 // Should return TRUE
121 $this->assertTrue(self::$configInstance->equals($dummyInstance));
125 * Tests hashCode() method
127 public function testHashCodeConfigInstance () {
129 $dummyInstance = FrameworkConfiguration::getSelfInstance();
131 // Get hash code from both
132 $hashCodeExpected = self::$configInstance->hashCode();
133 $hashCodeActual = $dummyInstance->hashCode();
136 $this->assertEquals($hashCodeExpected, $hashCodeActual);
140 * Tests if getting configuration entry returns an array
142 public function testGettingConfigurationArray () {
144 $config = self::$configInstance->getConfigurationArray();
146 // Should be an array
147 $this->assertTrue(is_array($config));
151 * Tests if getting configuration entry returns a filled array
153 public function testGettingfilledConfigurationArray () {
155 $config = self::$configInstance->getConfigurationArray();
157 // Should be an array
158 $this->assertTrue(count($config) > 0);
162 * Tests if getting whole configuration entry is the same for another
165 public function testSameConfigurationArrayGetter () {
167 $dummyInstance = FrameworkConfiguration::getSelfInstance();
169 // Get it from both instances
170 $config1 = self::$configInstance->getConfigurationArray();
171 $config2 = $dummyInstance->getConfigurationArray();
173 // Should be the same
174 $this->assertEquals($config1, $config2);
178 * Tests if a proper exception is thrown when check for a NULL key
180 public function testCheckingNullConfigKey () {
181 // Will throw this exception
182 $this->expectException(NullPointerException::class);
185 $dummy = self::$configInstance->isConfigurationEntrySet(NULL);
189 * Tests if a proper exception is thrown when checking a boolean key
191 public function testCheckingBooleanConfigKey () {
192 // Will throw this exception
193 $this->expectException(InvalidArgumentException::class);
196 $dummy = self::$configInstance->isConfigurationEntrySet(FALSE);
200 * Tests if a proper exception is thrown when checking an empty key
202 public function testCheckingEmptyConfigKey () {
203 // Will throw this exception
204 $this->expectException(InvalidArgumentException::class);
207 $dummy = self::$configInstance->isConfigurationEntrySet('');
211 * Tests if a proper exception is thrown when checking an array key
213 public function testCheckingArrayConfigKey () {
214 // Will throw this exception
215 $this->expectException(InvalidArgumentException::class);
218 $dummy = self::$configInstance->isConfigurationEntrySet(array());
222 * Tests if a proper exception is thrown when checking a decimal key
224 public function testCheckingDecimalConfigKey () {
225 // Will throw this exception
226 $this->expectException(InvalidArgumentException::class);
229 $dummy = self::$configInstance->isConfigurationEntrySet(12345);
233 * Tests if a proper exception is thrown when checking a float key
235 public function testCheckingFloatConfigKey () {
236 // Will throw this exception
237 $this->expectException(InvalidArgumentException::class);
240 $dummy = self::$configInstance->isConfigurationEntrySet(123.45);
244 * Tests if a proper exception is thrown when checking an object key
246 public function testCheckingObjectConfigKey () {
247 // Will throw this exception
248 $this->expectException(InvalidArgumentException::class);
251 $dummy = self::$configInstance->isConfigurationEntrySet($this);
255 * Tests if a proper exception is thrown when checking a resource key
257 public function testCheckingResourceConfigKey () {
258 // Will throw this exception
259 $this->expectException(InvalidArgumentException::class);
261 // Init some resource
262 $resource = fopen(__FILE__, 'r');
265 $dummy = self::$configInstance->isConfigurationEntrySet($resource);
269 * Tests if checking an existing (well-known) key can be found and returns
272 public function testCheckingExistingConfigKey () {
273 // Should return TRUE
274 $this->assertTrue(self::$configInstance->isConfigurationEntrySet('application_base_path'));
278 * Tests if checking a non-existing key will return FALSE.
280 public function testCheckingNonExistingConfigKey () {
281 // Should return FALSE
282 $this->assertFalse(self::$configInstance->isConfigurationEntrySet('__non_existing_key__'));
286 * Tests if a proper exception is thrown when getting a NULL key
288 public function testGettingNullConfigKey () {
289 // Will throw this exception
290 $this->expectException(NullPointerException::class);
293 $dummy = self::$configInstance->getConfigEntry(NULL);
297 * Tests if a proper exception is thrown when getting a boolean key
299 public function testGettingBooleanConfigKey () {
300 // Will throw this exception
301 $this->expectException(InvalidArgumentException::class);
304 $dummy = self::$configInstance->getConfigEntry(FALSE);
308 * Tests if a proper exception is thrown when getting an empty key
310 public function testGettingEmptyConfigKey () {
311 // Will throw this exception
312 $this->expectException(InvalidArgumentException::class);
315 $dummy = self::$configInstance->getConfigEntry('');
319 * Tests if a proper exception is thrown when getting a decimal key
321 public function testGettingDecimalConfigKey () {
322 // Will throw this exception
323 $this->expectException(InvalidArgumentException::class);
326 $dummy = self::$configInstance->getConfigEntry(12345);
330 * Tests if a proper exception is thrown when getting a float key
332 public function testGettingFloatConfigKey () {
333 // Will throw this exception
334 $this->expectException(InvalidArgumentException::class);
337 $dummy = self::$configInstance->getConfigEntry(123.45);
341 * Tests if a proper exception is thrown when getting an array key
343 public function testGettingArrayConfigKey () {
344 // Will throw this exception
345 $this->expectException(InvalidArgumentException::class);
348 $dummy = self::$configInstance->getConfigEntry(array());
352 * Tests if a proper exception is thrown when getting an object key
354 public function testGettingObjectConfigKey () {
355 // Will throw this exception
356 $this->expectException(InvalidArgumentException::class);
359 $dummy = self::$configInstance->getConfigEntry($this);
363 * Tests if a proper exception is thrown when getting a resource key
365 public function testGettingResourceConfigKey () {
366 // Will throw this exception
367 $this->expectException(InvalidArgumentException::class);
369 // Init some resource
370 $resource = fopen(__FILE__, 'r');
373 $dummy = self::$configInstance->getConfigEntry($resource);
377 * Tests if getting a non-existing key will cause a proper exception been
380 public function testGettingNonExistingConfigKey () {
381 // Should cause this exception
382 $this->expectException(NoConfigEntryException::class);
384 // Get non-existing key
385 $dummy = self::$configInstance->getConfigEntry('__non_existing_key__');
389 * Tests if a generic key 'application_base_path' can be found.
391 public function testGettingConfigKeyApplicationBasePathExists () {
392 // Get it from config instance
393 $value = self::$configInstance->getConfigEntry('application_base_path');
395 // Is it a readable path?
396 $this->assertDirectoryIsReadable($value);
400 * Tests setting a NULL key (value doesn't matter)
402 public function testSettingNullConfigKey () {
403 // Will throw this exception
404 $this->expectException(NullPointerException::class);
407 self::$configInstance->setConfigEntry(NULL, 'foo');
411 * Tests setting a boolean key (value doesn't matter)
413 public function testSettingBooleanConfigKey () {
414 // Will throw this exception
415 $this->expectException(InvalidArgumentException::class);
418 self::$configInstance->setConfigEntry(FALSE, 'foo');
422 * Tests setting an empty key (value doesn't matter)
424 public function testSettingEmptyConfigKey () {
425 // Will throw this exception
426 $this->expectException(InvalidArgumentException::class);
429 self::$configInstance->setConfigEntry('', 'foo');
433 * Tests setting a decimal key (value doesn't matter)
435 public function testSettingDecimalConfigKey () {
436 // Will throw this exception
437 $this->expectException(InvalidArgumentException::class);
440 self::$configInstance->setConfigEntry(12345, 'foo');
444 * Tests setting a float key (value doesn't matter)
446 public function testSettingFloatConfigKey () {
447 // Will throw this exception
448 $this->expectException(InvalidArgumentException::class);
451 self::$configInstance->setConfigEntry(123.45, 'foo');
455 * Tests setting an array key (value doesn't matter)
457 public function testSettingArrayConfigKey () {
458 // Will throw this exception
459 $this->expectException(InvalidArgumentException::class);
462 self::$configInstance->setConfigEntry(array(), 'foo');
466 * Tests setting an object key (value doesn't matter)
468 public function testSettingObjectConfigKey () {
469 // Will throw this exception
470 $this->expectException(InvalidArgumentException::class);
473 self::$configInstance->setConfigEntry($this, 'foo');
477 * Tests setting a resource key (value doesn't matter)
479 public function testSettingResourceConfigKey () {
480 // Will throw this exception
481 $this->expectException(InvalidArgumentException::class);
483 // Init some resource
484 $resource = fopen(__FILE__, 'r');
487 self::$configInstance->setConfigEntry($resource, 'foo');
491 * Tests setting a valid key but array for value
493 public function testSettingArrayValueConfigKey () {
494 // Will throw this exception
495 $this->expectException(InvalidArgumentException::class);
498 self::$configInstance->setConfigEntry('foo', array());
502 * Tests setting a valid key but object for value
504 public function testSettingObjectValueConfigKey () {
505 // Will throw this exception
506 $this->expectException(InvalidArgumentException::class);
509 self::$configInstance->setConfigEntry('foo', $this);
513 * Tests setting a valid key but resource for value
515 public function testSettingResourceValueConfigKey () {
516 // Will throw this exception
517 $this->expectException(InvalidArgumentException::class);
519 // Init some resource
520 $resource = fopen(__FILE__, 'r');
523 self::$configInstance->setConfigEntry('foo', $resource);
527 * Tests unsetting NULL key
529 public function testUnsettingNullConfigKey () {
530 // Will throw this exception
531 $this->expectException(NullPointerException::class);
534 self::$configInstance->unsetConfigEntry(NULL);
538 * Tests unsetting boolean key
540 public function testUnsettingBooleanConfigKey () {
541 // Will throw this exception
542 $this->expectException(InvalidArgumentException::class);
545 self::$configInstance->unsetConfigEntry(FALSE);
549 * Tests unsetting decimal key
551 public function testUnsettingDecimalConfigKey () {
552 // Will throw this exception
553 $this->expectException(InvalidArgumentException::class);
556 self::$configInstance->unsetConfigEntry(12345);
560 * Tests unsetting float key
562 public function testUnsettingFloatConfigKey () {
563 // Will throw this exception
564 $this->expectException(InvalidArgumentException::class);
567 self::$configInstance->unsetConfigEntry(123.45);
571 * Tests unsetting array key
573 public function testUnsettingArrayConfigKey () {
574 // Will throw this exception
575 $this->expectException(InvalidArgumentException::class);
578 self::$configInstance->unsetConfigEntry(array());
582 * Tests unsetting object key
584 public function testUnsettingObjectConfigKey () {
585 // Will throw this exception
586 $this->expectException(InvalidArgumentException::class);
589 self::$configInstance->unsetConfigEntry($this);
593 * Tests unsetting resource key
595 public function testUnsettingResourceConfigKey () {
596 // Will throw this exception
597 $this->expectException(InvalidArgumentException::class);
599 // Init some resource
600 $resource = fopen(__FILE__, 'r');
603 self::$configInstance->unsetConfigEntry($resource);
607 * Tests unsetting an empty key
609 public function testUnettingEmptyConfigKey () {
610 // Will throw this exception
611 $this->expectException(InvalidArgumentException::class);
614 self::$configInstance->unsetConfigEntry('');
618 * Tests unsetting a non-existing key
620 public function testUnettingNonExistingConfigKey () {
621 // Will throw this exception
622 $this->expectException(NoConfigEntryException::class);
625 self::$configInstance->unsetConfigEntry('__non_existing_key__');
629 * Tests setting and getting a key will return same value of same type
630 * (NULL). This unit test does also unset the then existing key.
632 public function testSettingGettiingNullConfigKey () {
634 self::$configInstance->setConfigEntry('__test_key', NULL);
637 $value = self::$configInstance->getConfigEntry('__test_key');
640 $this->assertEquals(NULL, $value);
643 self::$configInstance->unsetConfigEntry('__test_key');
647 * Tests setting and getting a key will return same value of same type.
648 * This unit test does also unset the then existing key.
650 public function testSettingGettiingConfigKey () {
652 self::$configInstance->setConfigEntry('__test_key', 'foo');
655 $value = self::$configInstance->getConfigEntry('__test_key');
658 $this->assertEquals('foo', $value);
661 self::$configInstance->unsetConfigEntry('__test_key');
665 * Tests setting a NULL default timezone
667 public function testSettingNullDefaultTimezone () {
668 // Will throw this exception
669 $this->expectException(NullPointerException::class);
672 self::$configInstance->setDefaultTimezone(NULL);
676 * Tests setting a boolean default timezone
678 public function testSettingBooleanDefaultTimezone () {
679 // Will throw this exception
680 $this->expectException(InvalidArgumentException::class);
683 self::$configInstance->setDefaultTimezone(FALSE);
687 * Tests setting a decimal default timezone
689 public function testSettingDecimalDefaultTimezone () {
690 // Will throw this exception
691 $this->expectException(InvalidArgumentException::class);
694 self::$configInstance->setDefaultTimezone(12345);
698 * Tests setting a float default timezone
700 public function testSettingFloatDefaultTimezone () {
701 // Will throw this exception
702 $this->expectException(InvalidArgumentException::class);
705 self::$configInstance->setDefaultTimezone(123.45);
709 * Tests setting an array default timezone
711 public function testSettingArrayDefaultTimezone () {
712 // Will throw this exception
713 $this->expectException(InvalidArgumentException::class);
716 self::$configInstance->setDefaultTimezone(array());
720 * Tests setting an object default timezone
722 public function testSettingObjectDefaultTimezone () {
723 // Will throw this exception
724 $this->expectException(InvalidArgumentException::class);
727 self::$configInstance->setDefaultTimezone($this);
731 * Tests setting a resource default timezone
733 public function testSettingResourceDefaultTimezone () {
734 // Will throw this exception
735 $this->expectException(InvalidArgumentException::class);
737 // Init some resource
738 $resource = fopen(__FILE__, 'r');
741 self::$configInstance->setDefaultTimezone($resource);
745 * Tests setting an empty default timezone
747 public function testSettingEmptyDefaultTimezone () {
748 // Will throw this exception
749 $this->expectException(InvalidArgumentException::class);
752 self::$configInstance->setDefaultTimezone('');
756 * Tests setting invalid timezone
758 public function testSettingInvalidDefaultTimezone () {
760 $this->expectException(Notice::class);
763 self::$configInstance->setDefaultTimezone('!invalid!');
767 * Tests setting valid timezone
769 public function testSettingValidDefaultTimezone () {
771 $this->assertTrue(self::$configInstance->setDefaultTimezone('Europe/Berlin'));
775 * Tests if detectServerAddress is returning what it should for tests.
776 * This will always be 127.0.0.1.
778 public function testConfigDetectServerAddress () {
780 $serverAddress = self::$configInstance->detectServerAddress();
782 // Should be the same
783 $this->assertEquals(self::$ipAddress, $serverAddress);
787 * Re-tests if detectServerAddress is returning what it should for tests.
788 * This will always be 127.0.0.1. This call should not invoke
789 * ConsoleTools's method as the configuration entry is already cached.
791 public function testConfigDetectServerAddressCached () {
793 $serverAddress = self::$configInstance->detectServerAddress();
795 // Should be the same
796 $this->assertEquals(self::$ipAddress, $serverAddress);
800 * Tests setting a NULL server address
802 public function testConfigSettingNullServerAddress () {
803 // Expect this exception
804 $this->expectException(NullPointerException::class);
807 self::$configInstance->setServerAddress(NULL);
811 * Tests setting a boolean server address
813 public function testConfigSettingBooleanServerAddress () {
814 // Expect this exception
815 $this->expectException(InvalidArgumentException::class);
818 self::$configInstance->setServerAddress(FALSE);
822 * Tests setting a decimal server address
824 public function testConfigSettingDecimalServerAddress () {
825 // Expect this exception
826 $this->expectException(InvalidArgumentException::class);
829 self::$configInstance->setServerAddress(12345);
833 * Tests setting a float server address
835 public function testConfigSettingFloatServerAddress () {
836 // Expect this exception
837 $this->expectException(InvalidArgumentException::class);
840 self::$configInstance->setServerAddress(123.45);
844 * Tests setting an array server address
846 public function testConfigSettingArrayServerAddress () {
847 // Expect this exception
848 $this->expectException(InvalidArgumentException::class);
851 self::$configInstance->setServerAddress(array());
855 * Tests setting an object server address
857 public function testConfigSettingObjectServerAddress () {
858 // Expect this exception
859 $this->expectException(InvalidArgumentException::class);
862 self::$configInstance->setServerAddress($this);
866 * Tests setting a resource server address
868 public function testConfigSettingResourceServerAddress () {
869 // Expect this exception
870 $this->expectException(InvalidArgumentException::class);
872 // Init some resource
873 $resource = fopen(__FILE__, 'r');
876 self::$configInstance->setServerAddress($resource);
880 * Tests setting an empty server address
882 public function testConfigSettingEmptyServerAddress () {
883 // Expect this exception
884 $this->expectException(InvalidArgumentException::class);
887 self::$configInstance->setServerAddress('');
891 * Tests setting a valid server address and getting it back
893 public function testConfigGettingValidServerAddress () {
895 self::$configInstance->setServerAddress('127.0.0.1');
898 $serverAddress = self::$configInstance->getServerAddress();
901 $this->assertEquals('127.0.0.1', $serverAddress);
904 self::$configInstance->setServerAddress(self::$ipAddress);