2 // Same namespace as target class
3 namespace CoreFramework\Configuration;
5 // Inport framework stuff
6 use CoreFramework\Bootstrap\FrameworkBootstrap;
7 use CoreFramework\Configuration\FrameworkConfiguration;
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/>.
35 class FrameworkConfigurationTest extends TestCase {
38 * The configuration instance being tested
40 private static $configInstance = NULL;
45 public function setUp() {
47 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
53 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
60 public static function setUpBeforeClass() {
62 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
65 parent::setUpBeforeClass();
68 self::$configInstance = FrameworkBootstrap::getConfigurationInstance();
71 * Disable strict naming-convention check in own class loader, because
72 * PHP_Invoker doesn't have namespaces.
74 ClassLoader::enableStrictNamingConventionCheck(FALSE);
77 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
81 * Tests if __toString() returns proper name
83 public function testConfigToStringClassName () {
85 $className = self::$configInstance->__toString();
88 $this->assertEquals('CoreFramework\Configuration\FrameworkConfiguration', $className);
92 * Tests getting a singleton instance
94 public function testGettingSelfConfigInstance () {
96 $dummyInstance = FrameworkBootstrap::getConfigurationInstance();
98 // Should be equal to own instance
99 $this->assertEquals(self::$configInstance, $dummyInstance);
103 * Tests equals() method
105 public function testEqualsConfigInstance () {
107 $dummyInstance = new FrameworkConfiguration();
109 // Should return TRUE
110 $this->assertTrue(self::$configInstance->equals($dummyInstance));
114 * Tests hashCode() method
116 public function testHashCodeConfigInstance () {
118 $dummyInstance = FrameworkBootstrap::getConfigurationInstance();
120 // Get hash code from both
121 $hashCodeExpected = self::$configInstance->hashCode();
122 $hashCodeActual = $dummyInstance->hashCode();
125 $this->assertEquals($hashCodeExpected, $hashCodeActual);
129 * Tests if getting configuration entry returns an array
131 public function testGettingConfigurationArray () {
133 $config = self::$configInstance->getConfigurationArray();
135 // Should be an array
136 $this->assertTrue(is_array($config));
140 * Tests if getting configuration entry returns a filled array
142 public function testGettingfilledConfigurationArray () {
144 $config = self::$configInstance->getConfigurationArray();
146 // Should be an array
147 $this->assertTrue(count($config) > 0);
151 * Tests if getting whole configuration entry is the same for another
154 public function testSameConfigurationArrayGetter () {
156 $dummyInstance = FrameworkBootstrap::getConfigurationInstance();
158 // Get it from both instances
159 $config1 = self::$configInstance->getConfigurationArray();
160 $config2 = $dummyInstance->getConfigurationArray();
162 // Should be the same
163 $this->assertEquals($config1, $config2);
167 * Tests if a proper exception is thrown when check for a NULL key
169 public function testCheckingNullConfigKey () {
170 // Will throw this exception
171 $this->expectException(NullPointerException::class);
174 $dummy = self::$configInstance->isConfigurationEntrySet(NULL);
178 * Tests if a proper exception is thrown when checking a boolean key
180 public function testCheckingBooleanConfigKey () {
181 // Will throw this exception
182 $this->expectException(InvalidArgumentException::class);
185 $dummy = self::$configInstance->isConfigurationEntrySet(FALSE);
189 * Tests if a proper exception is thrown when checking an empty key
191 public function testCheckingEmptyConfigKey () {
192 // Will throw this exception
193 $this->expectException(InvalidArgumentException::class);
196 $dummy = self::$configInstance->isConfigurationEntrySet('');
200 * Tests if a proper exception is thrown when checking an array key
202 public function testCheckingArrayConfigKey () {
203 // Will throw this exception
204 $this->expectException(InvalidArgumentException::class);
207 $dummy = self::$configInstance->isConfigurationEntrySet(array());
211 * Tests if a proper exception is thrown when checking a decimal key
213 public function testCheckingDecimalConfigKey () {
214 // Will throw this exception
215 $this->expectException(InvalidArgumentException::class);
218 $dummy = self::$configInstance->isConfigurationEntrySet(12345);
222 * Tests if a proper exception is thrown when checking a float key
224 public function testCheckingFloatConfigKey () {
225 // Will throw this exception
226 $this->expectException(InvalidArgumentException::class);
229 $dummy = self::$configInstance->isConfigurationEntrySet(123.45);
233 * Tests if a proper exception is thrown when checking an object key
235 public function testCheckingObjectConfigKey () {
236 // Will throw this exception
237 $this->expectException(InvalidArgumentException::class);
240 $dummy = self::$configInstance->isConfigurationEntrySet($this);
244 * Tests if a proper exception is thrown when checking a resource key
246 public function testCheckingResourceConfigKey () {
247 // Will throw this exception
248 $this->expectException(InvalidArgumentException::class);
250 // Init some resource
251 $resource = fopen(__FILE__, 'r');
254 $dummy = self::$configInstance->isConfigurationEntrySet($resource);
258 * Tests if checking an existing (well-known) key can be found and returns
261 public function testCheckingExistingConfigKey () {
262 // Should return TRUE
263 $this->assertTrue(self::$configInstance->isConfigurationEntrySet('application_base_path'));
267 * Tests if checking a non-existing key will return FALSE.
269 public function testCheckingNonExistingConfigKey () {
270 // Should return FALSE
271 $this->assertFalse(self::$configInstance->isConfigurationEntrySet('__non_existing_key__'));
275 * Tests if a proper exception is thrown when getting a NULL key
277 public function testGettingNullConfigKey () {
278 // Will throw this exception
279 $this->expectException(NullPointerException::class);
282 $dummy = self::$configInstance->getConfigEntry(NULL);
286 * Tests if a proper exception is thrown when getting a boolean key
288 public function testGettingBooleanConfigKey () {
289 // Will throw this exception
290 $this->expectException(InvalidArgumentException::class);
293 $dummy = self::$configInstance->getConfigEntry(FALSE);
297 * Tests if a proper exception is thrown when getting an empty key
299 public function testGettingEmptyConfigKey () {
300 // Will throw this exception
301 $this->expectException(InvalidArgumentException::class);
304 $dummy = self::$configInstance->getConfigEntry('');
308 * Tests if a proper exception is thrown when getting a decimal key
310 public function testGettingDecimalConfigKey () {
311 // Will throw this exception
312 $this->expectException(InvalidArgumentException::class);
315 $dummy = self::$configInstance->getConfigEntry(12345);
319 * Tests if a proper exception is thrown when getting a float key
321 public function testGettingFloatConfigKey () {
322 // Will throw this exception
323 $this->expectException(InvalidArgumentException::class);
326 $dummy = self::$configInstance->getConfigEntry(123.45);
330 * Tests if a proper exception is thrown when getting an array key
332 public function testGettingArrayConfigKey () {
333 // Will throw this exception
334 $this->expectException(InvalidArgumentException::class);
337 $dummy = self::$configInstance->getConfigEntry(array());
341 * Tests if a proper exception is thrown when getting an object key
343 public function testGettingObjectConfigKey () {
344 // Will throw this exception
345 $this->expectException(InvalidArgumentException::class);
348 $dummy = self::$configInstance->getConfigEntry($this);
352 * Tests if a proper exception is thrown when getting a resource key
354 public function testGettingResourceConfigKey () {
355 // Will throw this exception
356 $this->expectException(InvalidArgumentException::class);
358 // Init some resource
359 $resource = fopen(__FILE__, 'r');
362 $dummy = self::$configInstance->getConfigEntry($resource);
366 * Tests if getting a non-existing key will cause a proper exception been
369 public function testGettingNonExistingConfigKey () {
370 // Should cause this exception
371 $this->expectException(NoConfigEntryException::class);
373 // Get non-existing key
374 $dummy = self::$configInstance->getConfigEntry('__non_existing_key__');
378 * Tests if a generic key 'application_base_path' can be found.
380 public function testGettingConfigKeyApplicationBasePathExists () {
381 // Get it from config instance
382 $value = self::$configInstance->getConfigEntry('application_base_path');
384 // Is it a readable path?
385 $this->assertDirectoryIsReadable($value);
389 * Tests setting a NULL key (value doesn't matter)
391 public function testSettingNullConfigKey () {
392 // Will throw this exception
393 $this->expectException(NullPointerException::class);
396 self::$configInstance->setConfigEntry(NULL, 'foo');
400 * Tests setting a boolean key (value doesn't matter)
402 public function testSettingBooleanConfigKey () {
403 // Will throw this exception
404 $this->expectException(InvalidArgumentException::class);
407 self::$configInstance->setConfigEntry(FALSE, 'foo');
411 * Tests setting an empty key (value doesn't matter)
413 public function testSettingEmptyConfigKey () {
414 // Will throw this exception
415 $this->expectException(InvalidArgumentException::class);
418 self::$configInstance->setConfigEntry('', 'foo');
422 * Tests setting a decimal key (value doesn't matter)
424 public function testSettingDecimalConfigKey () {
425 // Will throw this exception
426 $this->expectException(InvalidArgumentException::class);
429 self::$configInstance->setConfigEntry(12345, 'foo');
433 * Tests setting a float key (value doesn't matter)
435 public function testSettingFloatConfigKey () {
436 // Will throw this exception
437 $this->expectException(InvalidArgumentException::class);
440 self::$configInstance->setConfigEntry(123.45, 'foo');
444 * Tests setting an array key (value doesn't matter)
446 public function testSettingArrayConfigKey () {
447 // Will throw this exception
448 $this->expectException(InvalidArgumentException::class);
451 self::$configInstance->setConfigEntry(array(), 'foo');
455 * Tests setting an object key (value doesn't matter)
457 public function testSettingObjectConfigKey () {
458 // Will throw this exception
459 $this->expectException(InvalidArgumentException::class);
462 self::$configInstance->setConfigEntry($this, 'foo');
466 * Tests setting a resource key (value doesn't matter)
468 public function testSettingResourceConfigKey () {
469 // Will throw this exception
470 $this->expectException(InvalidArgumentException::class);
472 // Init some resource
473 $resource = fopen(__FILE__, 'r');
476 self::$configInstance->setConfigEntry($resource, 'foo');
480 * Tests setting a valid key but array for value
482 public function testSettingArrayValueConfigKey () {
483 // Will throw this exception
484 $this->expectException(InvalidArgumentException::class);
487 self::$configInstance->setConfigEntry('foo', array());
491 * Tests setting a valid key but object for value
493 public function testSettingObjectValueConfigKey () {
494 // Will throw this exception
495 $this->expectException(InvalidArgumentException::class);
498 self::$configInstance->setConfigEntry('foo', $this);
502 * Tests setting a valid key but resource for value
504 public function testSettingResourceValueConfigKey () {
505 // Will throw this exception
506 $this->expectException(InvalidArgumentException::class);
508 // Init some resource
509 $resource = fopen(__FILE__, 'r');
512 self::$configInstance->setConfigEntry('foo', $resource);
516 * Tests unsetting NULL key
518 public function testUnsettingNullConfigKey () {
519 // Will throw this exception
520 $this->expectException(NullPointerException::class);
523 self::$configInstance->unsetConfigEntry(NULL);
527 * Tests unsetting boolean key
529 public function testUnsettingBooleanConfigKey () {
530 // Will throw this exception
531 $this->expectException(InvalidArgumentException::class);
534 self::$configInstance->unsetConfigEntry(FALSE);
538 * Tests unsetting decimal key
540 public function testUnsettingDecimalConfigKey () {
541 // Will throw this exception
542 $this->expectException(InvalidArgumentException::class);
545 self::$configInstance->unsetConfigEntry(12345);
549 * Tests unsetting float key
551 public function testUnsettingFloatConfigKey () {
552 // Will throw this exception
553 $this->expectException(InvalidArgumentException::class);
556 self::$configInstance->unsetConfigEntry(123.45);
560 * Tests unsetting array key
562 public function testUnsettingArrayConfigKey () {
563 // Will throw this exception
564 $this->expectException(InvalidArgumentException::class);
567 self::$configInstance->unsetConfigEntry(array());
571 * Tests unsetting object key
573 public function testUnsettingObjectConfigKey () {
574 // Will throw this exception
575 $this->expectException(InvalidArgumentException::class);
578 self::$configInstance->unsetConfigEntry($this);
582 * Tests unsetting resource key
584 public function testUnsettingResourceConfigKey () {
585 // Will throw this exception
586 $this->expectException(InvalidArgumentException::class);
588 // Init some resource
589 $resource = fopen(__FILE__, 'r');
592 self::$configInstance->unsetConfigEntry($resource);
596 * Tests unsetting an empty key
598 public function testUnettingEmptyConfigKey () {
599 // Will throw this exception
600 $this->expectException(InvalidArgumentException::class);
603 self::$configInstance->unsetConfigEntry('');
607 * Tests unsetting a non-existing key
609 public function testUnettingNonExistingConfigKey () {
610 // Will throw this exception
611 $this->expectException(NoConfigEntryException::class);
614 self::$configInstance->unsetConfigEntry('__non_existing_key__');
618 * Tests setting and getting a key will return same value of same type
619 * (NULL). This unit test does also unset the then existing key.
621 public function testSettingGettiingNullConfigKey () {
623 self::$configInstance->setConfigEntry('__test_key', NULL);
626 $value = self::$configInstance->getConfigEntry('__test_key');
629 $this->assertEquals(NULL, $value);
632 self::$configInstance->unsetConfigEntry('__test_key');
636 * Tests setting and getting a key will return same value of same type.
637 * This unit test does also unset the then existing key.
639 public function testSettingGettiingConfigKey () {
641 self::$configInstance->setConfigEntry('__test_key', 'foo');
644 $value = self::$configInstance->getConfigEntry('__test_key');
647 $this->assertEquals('foo', $value);
650 self::$configInstance->unsetConfigEntry('__test_key');
654 * Tests if the method getField() is still unsupported in this class. Please
655 * note, that this and isFieldSet() may get removed in the future. So also
656 * these test methods will be gone.
658 public function testConfigGetFieldUnsupported () {
659 // Expect this exception
660 $this->expectException(UnsupportedOperationException::class);
663 $dummy = self::$configInstance->getField('foo');
667 * Tests if the method isFieldSet() is still unsupported in this class. Please
668 * note, that this and getField() may get removed in the future. So also
669 * these test methods will be gone.
671 public function testConfigIsFieldSetUnsupported () {
672 // Expect this exception
673 $this->expectException(UnsupportedOperationException::class);
676 $dummy = self::$configInstance->isFieldSet('foo');