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 an empty key
540 public function testUnettingEmptyConfigKey () {
541 // Will throw this exception
542 $this->expectException(InvalidArgumentException::class);
545 self::$configInstance->unsetConfigEntry('');
549 * Tests unsetting a non-existing key
551 public function testUnettingNonExistingConfigKey () {
552 // Will throw this exception
553 $this->expectException(NoConfigEntryException::class);
556 self::$configInstance->unsetConfigEntry('__non_existing_key__');
560 * Tests setting and getting a key will return same value of same type
561 * (NULL). This unit test does also unset the then existing key.
563 public function testSettingGettiingNullConfigKey () {
565 self::$configInstance->setConfigEntry('__test_key', NULL);
568 $value = self::$configInstance->getConfigEntry('__test_key');
571 $this->assertEquals(NULL, $value);
574 self::$configInstance->unsetConfigEntry('__test_key');
578 * Tests setting and getting a key will return same value of same type.
579 * This unit test does also unset the then existing key.
581 public function testSettingGettiingConfigKey () {
583 self::$configInstance->setConfigEntry('__test_key', 'foo');
586 $value = self::$configInstance->getConfigEntry('__test_key');
589 $this->assertEquals('foo', $value);
592 self::$configInstance->unsetConfigEntry('__test_key');
596 * Tests setting a NULL default timezone
598 public function testSettingNullDefaultTimezone () {
599 // Will throw this exception
600 $this->expectException(NullPointerException::class);
603 self::$configInstance->setDefaultTimezone(NULL);
607 * Tests setting an empty default timezone
609 public function testSettingEmptyDefaultTimezone () {
610 // Will throw this exception
611 $this->expectException(InvalidArgumentException::class);
614 self::$configInstance->setDefaultTimezone('');
618 * Tests setting invalid timezone
620 public function testSettingInvalidDefaultTimezone () {
622 $this->expectException(Notice::class);
625 self::$configInstance->setDefaultTimezone('!invalid!');
629 * Tests setting valid timezone
631 public function testSettingValidDefaultTimezone () {
633 $this->assertTrue(self::$configInstance->setDefaultTimezone('Europe/Berlin'));
637 * Tests if detectServerAddress is returning what it should for tests.
638 * This will always be 127.0.0.1.
640 public function testConfigDetectServerAddress () {
642 $serverAddress = self::$configInstance->detectServerAddress();
644 // Should be the same
645 $this->assertEquals(self::$ipAddress, $serverAddress);
649 * Re-tests if detectServerAddress is returning what it should for tests.
650 * This will always be 127.0.0.1. This call should not invoke
651 * ConsoleTools's method as the configuration entry is already cached.
653 public function testConfigDetectServerAddressCached () {
655 $serverAddress = self::$configInstance->detectServerAddress();
657 // Should be the same
658 $this->assertEquals(self::$ipAddress, $serverAddress);
662 * Tests setting a NULL server address
664 public function testConfigSettingNullServerAddress () {
665 // Expect this exception
666 $this->expectException(NullPointerException::class);
669 self::$configInstance->setServerAddress(NULL);
673 * Tests setting a boolean server address
675 public function testConfigSettingBooleanServerAddress () {
676 // Expect this exception
677 $this->expectException(InvalidArgumentException::class);
680 self::$configInstance->setServerAddress(FALSE);
684 * Tests setting a decimal server address
686 public function testConfigSettingDecimalServerAddress () {
687 // Expect this exception
688 $this->expectException(InvalidArgumentException::class);
691 self::$configInstance->setServerAddress(12345);
695 * Tests setting a float server address
697 public function testConfigSettingFloatServerAddress () {
698 // Expect this exception
699 $this->expectException(InvalidArgumentException::class);
702 self::$configInstance->setServerAddress(123.45);
706 * Tests setting an array server address
708 public function testConfigSettingArrayServerAddress () {
709 // Expect this exception
710 $this->expectException(InvalidArgumentException::class);
713 self::$configInstance->setServerAddress(array());
717 * Tests setting an object server address
719 public function testConfigSettingObjectServerAddress () {
720 // Expect this exception
721 $this->expectException(InvalidArgumentException::class);
724 self::$configInstance->setServerAddress($this);
728 * Tests setting a resource server address
730 public function testConfigSettingResourceServerAddress () {
731 // Expect this exception
732 $this->expectException(InvalidArgumentException::class);
734 // Init some resource
735 $resource = fopen(__FILE__, 'r');
738 self::$configInstance->setServerAddress($resource);
742 * Tests setting an empty server address
744 public function testConfigSettingEmptyServerAddress () {
745 // Expect this exception
746 $this->expectException(InvalidArgumentException::class);
749 self::$configInstance->setServerAddress('');
753 * Tests setting a valid server address and getting it back
755 public function testConfigGettingValidServerAddress () {
757 self::$configInstance->setServerAddress('127.0.0.1');
760 $serverAddress = self::$configInstance->getServerAddress();
763 $this->assertEquals('127.0.0.1', $serverAddress);
766 self::$configInstance->setServerAddress(self::$ipAddress);