3 // Same namespace as target class
4 namespace CoreFramework\Configuration;
6 // Inport framework stuff
7 use CoreFramework\Loader\ClassLoader;
8 use CoreFramework\Generic\NullPointerException;
10 // Import PHPUnit stuff
11 use PHPUnit\Framework\Error\Notice;
12 use PHPUnit\Framework\TestCase;
15 use \InvalidArgumentException;
18 * Copyright (C) 2017 Roland Haeder<roland@mxchange.org>
20 * This program is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 class FrameworkConfigurationTest extends TestCase {
37 * The configuration instance being tested
39 private static $configInstance = NULL;
44 public function setUp() {
46 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
52 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
59 public static function setUpBeforeClass() {
61 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
64 parent::setUpBeforeClass();
67 self::$configInstance = FrameworkConfiguration::getSelfInstance();
70 * Disable strict naming-convention check in own class loader, because
71 * PHP_Invoker doesn't have namespaces.
73 ClassLoader::enableStrictNamingConventionCheck(FALSE);
76 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
80 * Tests if __toString() returns proper name
82 public function testConfigToStringClassName () {
84 $className = self::$configInstance->__toString();
87 $this->assertEquals('CoreFramework\Configuration\FrameworkConfiguration', $className);
91 * Tests getting a singleton instance
93 public function testGettingSelfConfigInstance () {
95 $dummyInstance = FrameworkConfiguration::getSelfInstance();
97 // Should be equal to own instance
98 $this->assertEquals(self::$configInstance, $dummyInstance);
102 * Tests equals() method
104 public function testEqualsConfigInstance () {
106 $dummyInstance = FrameworkConfiguration::getSelfInstance();
108 // Should return TRUE
109 $this->assertTrue(self::$configInstance->equals($dummyInstance));
113 * Tests hashCode() method
115 public function testHashCodeConfigInstance () {
117 $dummyInstance = FrameworkConfiguration::getSelfInstance();
119 // Get hash code from both
120 $hashCodeExpected = self::$configInstance->hashCode();
121 $hashCodeActual = $dummyInstance->hashCode();
124 $this->assertEquals($hashCodeExpected, $hashCodeActual);
128 * Tests if getting configuration entry returns an array
130 public function testGettingConfigurationArray () {
132 $config = self::$configInstance->getConfigurationArray();
134 // Should be an array
135 $this->assertTrue(is_array($config));
139 * Tests if getting configuration entry returns a filled array
141 public function testGettingfilledConfigurationArray () {
143 $config = self::$configInstance->getConfigurationArray();
145 // Should be an array
146 $this->assertTrue(count($config) > 0);
150 * Tests if getting whole configuration entry is the same for another
153 public function testSameConfigurationArrayGetter () {
155 $dummyInstance = FrameworkConfiguration::getSelfInstance();
157 // Get it from both instances
158 $config1 = self::$configInstance->getConfigurationArray();
159 $config2 = $dummyInstance->getConfigurationArray();
161 // Should be the same
162 $this->assertEquals($config1, $config2);
166 * Tests if a proper exception is thrown when check for a NULL key
168 public function testCheckingNullConfigKey () {
169 // Will throw this exception
170 $this->expectException(NullPointerException::class);
173 $dummy = self::$configInstance->isConfigurationEntrySet(NULL);
177 * Tests if a proper exception is thrown when checking a boolean key
179 public function testCheckingBooleanConfigKey () {
180 // Will throw this exception
181 $this->expectException(InvalidArgumentException::class);
184 $dummy = self::$configInstance->isConfigurationEntrySet(FALSE);
188 * Tests if a proper exception is thrown when checking an empty key
190 public function testCheckingEmptyConfigKey () {
191 // Will throw this exception
192 $this->expectException(InvalidArgumentException::class);
195 $dummy = self::$configInstance->isConfigurationEntrySet('');
199 * Tests if a proper exception is thrown when checking an array key
201 public function testCheckingArrayConfigKey () {
202 // Will throw this exception
203 $this->expectException(InvalidArgumentException::class);
206 $dummy = self::$configInstance->isConfigurationEntrySet(array());
210 * Tests if a proper exception is thrown when checking a decimal key
212 public function testCheckingDecimalConfigKey () {
213 // Will throw this exception
214 $this->expectException(InvalidArgumentException::class);
217 $dummy = self::$configInstance->isConfigurationEntrySet(12345);
221 * Tests if a proper exception is thrown when checking a float key
223 public function testCheckingFloatConfigKey () {
224 // Will throw this exception
225 $this->expectException(InvalidArgumentException::class);
228 $dummy = self::$configInstance->isConfigurationEntrySet(123.45);
232 * Tests if a proper exception is thrown when checking an object key
234 public function testCheckingObjectConfigKey () {
235 // Will throw this exception
236 $this->expectException(InvalidArgumentException::class);
239 $dummy = self::$configInstance->isConfigurationEntrySet($this);
243 * Tests if a proper exception is thrown when checking a resource key
245 public function testCheckingResourceConfigKey () {
246 // Will throw this exception
247 $this->expectException(InvalidArgumentException::class);
249 // Init some resource
250 $resource = fopen(__FILE__, 'r');
253 $dummy = self::$configInstance->isConfigurationEntrySet($resource);
257 * Tests if checking an existing (well-known) key can be found and returns
260 public function testCheckingExistingConfigKey () {
261 // Should return TRUE
262 $this->assertTrue(self::$configInstance->isConfigurationEntrySet('application_base_path'));
266 * Tests if checking a non-existing key will return FALSE.
268 public function testCheckingNonExistingConfigKey () {
269 // Should return FALSE
270 $this->assertFalse(self::$configInstance->isConfigurationEntrySet('__non_existing_key__'));
274 * Tests if a proper exception is thrown when getting a NULL key
276 public function testGettingNullConfigKey () {
277 // Will throw this exception
278 $this->expectException(NullPointerException::class);
281 $dummy = self::$configInstance->getConfigEntry(NULL);
285 * Tests if a proper exception is thrown when getting a boolean key
287 public function testGettingBooleanConfigKey () {
288 // Will throw this exception
289 $this->expectException(InvalidArgumentException::class);
292 $dummy = self::$configInstance->getConfigEntry(FALSE);
296 * Tests if a proper exception is thrown when getting an empty key
298 public function testGettingEmptyConfigKey () {
299 // Will throw this exception
300 $this->expectException(InvalidArgumentException::class);
303 $dummy = self::$configInstance->getConfigEntry('');
307 * Tests if a proper exception is thrown when getting a decimal key
309 public function testGettingDecimalConfigKey () {
310 // Will throw this exception
311 $this->expectException(InvalidArgumentException::class);
314 $dummy = self::$configInstance->getConfigEntry(12345);
318 * Tests if a proper exception is thrown when getting a float key
320 public function testGettingFloatConfigKey () {
321 // Will throw this exception
322 $this->expectException(InvalidArgumentException::class);
325 $dummy = self::$configInstance->getConfigEntry(123.45);
329 * Tests if a proper exception is thrown when getting an array key
331 public function testGettingArrayConfigKey () {
332 // Will throw this exception
333 $this->expectException(InvalidArgumentException::class);
336 $dummy = self::$configInstance->getConfigEntry(array());
340 * Tests if a proper exception is thrown when getting an object key
342 public function testGettingObjectConfigKey () {
343 // Will throw this exception
344 $this->expectException(InvalidArgumentException::class);
347 $dummy = self::$configInstance->getConfigEntry($this);
351 * Tests if a proper exception is thrown when getting a resource key
353 public function testGettingResourceConfigKey () {
354 // Will throw this exception
355 $this->expectException(InvalidArgumentException::class);
357 // Init some resource
358 $resource = fopen(__FILE__, 'r');
361 $dummy = self::$configInstance->getConfigEntry($resource);
365 * Tests if getting a non-existing key will cause a proper exception been
368 public function testGettingNonExistingConfigKey () {
369 // Should cause this exception
370 $this->expectException(NoConfigEntryException::class);
372 // Get non-existing key
373 $dummy = self::$configInstance->getConfigEntry('__non_existing_key__');
377 * Tests if a generic key 'application_base_path' can be found.
379 public function testGettingConfigKeyApplicationBasePathExists () {
380 // Get it from config instance
381 $value = self::$configInstance->getConfigEntry('application_base_path');
383 // Is it a readable path?
384 $this->assertDirectoryIsReadable($value);
388 * Tests setting a NULL key (value doesn't matter)
390 public function testSettingNullConfigKey () {
391 // Will throw this exception
392 $this->expectException(NullPointerException::class);
395 self::$configInstance->setConfigEntry(NULL, 'foo');
399 * Tests setting a boolean key (value doesn't matter)
401 public function testSettingBooleanConfigKey () {
402 // Will throw this exception
403 $this->expectException(InvalidArgumentException::class);
406 self::$configInstance->setConfigEntry(FALSE, 'foo');
410 * Tests setting an empty key (value doesn't matter)
412 public function testSettingEmptyConfigKey () {
413 // Will throw this exception
414 $this->expectException(InvalidArgumentException::class);
417 self::$configInstance->setConfigEntry('', 'foo');
421 * Tests setting a decimal key (value doesn't matter)
423 public function testSettingDecimalConfigKey () {
424 // Will throw this exception
425 $this->expectException(InvalidArgumentException::class);
428 self::$configInstance->setConfigEntry(12345, 'foo');
432 * Tests setting a float key (value doesn't matter)
434 public function testSettingFloatConfigKey () {
435 // Will throw this exception
436 $this->expectException(InvalidArgumentException::class);
439 self::$configInstance->setConfigEntry(123.45, 'foo');
443 * Tests setting an array key (value doesn't matter)
445 public function testSettingArrayConfigKey () {
446 // Will throw this exception
447 $this->expectException(InvalidArgumentException::class);
450 self::$configInstance->setConfigEntry(array(), 'foo');
454 * Tests setting an object key (value doesn't matter)
456 public function testSettingObjectConfigKey () {
457 // Will throw this exception
458 $this->expectException(InvalidArgumentException::class);
461 self::$configInstance->setConfigEntry($this, 'foo');
465 * Tests setting a resource key (value doesn't matter)
467 public function testSettingResourceConfigKey () {
468 // Will throw this exception
469 $this->expectException(InvalidArgumentException::class);
471 // Init some resource
472 $resource = fopen(__FILE__, 'r');
475 self::$configInstance->setConfigEntry($resource, 'foo');
479 * Tests setting a valid key but array for value
481 public function testSettingArrayValueConfigKey () {
482 // Will throw this exception
483 $this->expectException(InvalidArgumentException::class);
486 self::$configInstance->setConfigEntry('foo', array());
490 * Tests setting a valid key but object for value
492 public function testSettingObjectValueConfigKey () {
493 // Will throw this exception
494 $this->expectException(InvalidArgumentException::class);
497 self::$configInstance->setConfigEntry('foo', $this);
501 * Tests setting a valid key but resource for value
503 public function testSettingResourceValueConfigKey () {
504 // Will throw this exception
505 $this->expectException(InvalidArgumentException::class);
507 // Init some resource
508 $resource = fopen(__FILE__, 'r');
511 self::$configInstance->setConfigEntry('foo', $resource);
515 * Tests unsetting NULL key
517 public function testUnsettingNullConfigKey () {
518 // Will throw this exception
519 $this->expectException(NullPointerException::class);
522 self::$configInstance->unsetConfigEntry(NULL);
526 * Tests unsetting an empty key
528 public function testUnettingEmptyConfigKey () {
529 // Will throw this exception
530 $this->expectException(InvalidArgumentException::class);
533 self::$configInstance->unsetConfigEntry('');
537 * Tests unsetting a non-existing key
539 public function testUnettingNonExistingConfigKey () {
540 // Will throw this exception
541 $this->expectException(NoConfigEntryException::class);
544 self::$configInstance->unsetConfigEntry('__non_existing_key__');
548 * Tests setting and getting a key will return same value of same type
549 * (NULL). This unit test does also unset the then existing key.
551 public function testSettingGettiingNullConfigKey () {
553 self::$configInstance->setConfigEntry('__test_key', NULL);
556 $value = self::$configInstance->getConfigEntry('__test_key');
559 $this->assertEquals(NULL, $value);
562 self::$configInstance->unsetConfigEntry('__test_key');
566 * Tests setting and getting a key will return same value of same type.
567 * This unit test does also unset the then existing key.
569 public function testSettingGettiingConfigKey () {
571 self::$configInstance->setConfigEntry('__test_key', 'foo');
574 $value = self::$configInstance->getConfigEntry('__test_key');
577 $this->assertEquals('foo', $value);
580 self::$configInstance->unsetConfigEntry('__test_key');
584 * Tests setting a NULL default timezone
586 public function testSettingNullDefaultTimezone () {
587 // Will throw this exception
588 $this->expectException(NullPointerException::class);
591 self::$configInstance->setDefaultTimezone(NULL);
595 * Tests setting an empty default timezone
597 public function testSettingEmptyDefaultTimezone () {
598 // Will throw this exception
599 $this->expectException(InvalidArgumentException::class);
602 self::$configInstance->setDefaultTimezone('');
606 * Tests setting invalid timezone
608 public function testSettingInvalidDefaultTimezone () {
610 $this->expectException(Notice::class);
613 self::$configInstance->setDefaultTimezone('!invalid!');
617 * Tests setting valid timezone
619 public function testSettingValidDefaultTimezone () {
621 $this->assertTrue(self::$configInstance->setDefaultTimezone('Europe/Berlin'));
625 * Tests if detectServerAddress is returning what it should for tests.
626 * This will always be 127.0.0.1.
628 public function testConfigDetectServerAddress () {
630 $serverAddress = self::$configInstance->detectServerAddress();
632 // Should be the same
633 $this->assertEquals('127.0.0.1', $serverAddress);
637 * Re-tests if detectServerAddress is returning what it should for tests.
638 * This will always be 127.0.0.1. This call should not invoke
639 * ConsoleTools's method as the configuration entry is already cached.
641 public function testConfigDetectServerAddressCached () {
643 $serverAddress = self::$configInstance->detectServerAddress();
645 // Should be the same
646 $this->assertEquals('127.0.0.1', $serverAddress);
650 * Tests setting a NULL server address
652 public function testConfigSettingNullServerAddress () {
653 // Expect this exception
654 $this->expectException(NullPointerException::class);
657 self::$configInstance->setServerAddress(NULL);
661 * Tests setting a boolean server address
663 public function testConfigSettingBooleanServerAddress () {
664 // Expect this exception
665 $this->expectException(InvalidArgumentException::class);
668 self::$configInstance->setServerAddress(FALSE);
672 * Tests setting a decimal server address
674 public function testConfigSettingDecimalServerAddress () {
675 // Expect this exception
676 $this->expectException(InvalidArgumentException::class);
679 self::$configInstance->setServerAddress(12345);
683 * Tests setting a float server address
685 public function testConfigSettingFloatServerAddress () {
686 // Expect this exception
687 $this->expectException(InvalidArgumentException::class);
690 self::$configInstance->setServerAddress(123.45);
694 * Tests setting an array server address
696 public function testConfigSettingArrayServerAddress () {
697 // Expect this exception
698 $this->expectException(InvalidArgumentException::class);
701 self::$configInstance->setServerAddress(array());
705 * Tests setting an object server address
707 public function testConfigSettingObjectServerAddress () {
708 // Expect this exception
709 $this->expectException(InvalidArgumentException::class);
712 self::$configInstance->setServerAddress($this);
716 * Tests setting a resource server address
718 public function testConfigSettingResourceServerAddress () {
719 // Expect this exception
720 $this->expectException(InvalidArgumentException::class);
722 // Init some resource
723 $resource = fopen(__FILE__, 'r');
726 self::$configInstance->setServerAddress($resource);
730 * Tests setting an empty server address
732 public function testConfigSettingEmptyServerAddress () {
733 // Expect this exception
734 $this->expectException(InvalidArgumentException::class);
737 self::$configInstance->setServerAddress('');
741 * Tests setting a valid server address and getting it back
743 public function testConfigGettingValidServerAddress () {
745 self::$configInstance->setServerAddress('127.0.0.1');
748 $serverAddress = self::$configInstance->getServerAddress();
751 $this->assertEquals('127.0.0.1', $serverAddress);