3 // Same namespace as target class
4 namespace CoreFramework\Configuration;
6 // Inport framework stuff
7 use CoreFramework\Loader\ClassLoader;
8 use CoreFramework\Generic\NullPointerException;
9 use CoreFramework\Generic\UnsupportedOperationException;
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/>.
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 boolean key
528 public function testUnsettingBooleanConfigKey () {
529 // Will throw this exception
530 $this->expectException(InvalidArgumentException::class);
533 self::$configInstance->unsetConfigEntry(FALSE);
537 * Tests unsetting decimal key
539 public function testUnsettingDecimalConfigKey () {
540 // Will throw this exception
541 $this->expectException(InvalidArgumentException::class);
544 self::$configInstance->unsetConfigEntry(12345);
548 * Tests unsetting float key
550 public function testUnsettingFloatConfigKey () {
551 // Will throw this exception
552 $this->expectException(InvalidArgumentException::class);
555 self::$configInstance->unsetConfigEntry(123.45);
559 * Tests unsetting array key
561 public function testUnsettingArrayConfigKey () {
562 // Will throw this exception
563 $this->expectException(InvalidArgumentException::class);
566 self::$configInstance->unsetConfigEntry(array());
570 * Tests unsetting object key
572 public function testUnsettingObjectConfigKey () {
573 // Will throw this exception
574 $this->expectException(InvalidArgumentException::class);
577 self::$configInstance->unsetConfigEntry($this);
581 * Tests unsetting resource key
583 public function testUnsettingResourceConfigKey () {
584 // Will throw this exception
585 $this->expectException(InvalidArgumentException::class);
587 // Init some resource
588 $resource = fopen(__FILE__, 'r');
591 self::$configInstance->unsetConfigEntry($resource);
595 * Tests unsetting an empty key
597 public function testUnettingEmptyConfigKey () {
598 // Will throw this exception
599 $this->expectException(InvalidArgumentException::class);
602 self::$configInstance->unsetConfigEntry('');
606 * Tests unsetting a non-existing key
608 public function testUnettingNonExistingConfigKey () {
609 // Will throw this exception
610 $this->expectException(NoConfigEntryException::class);
613 self::$configInstance->unsetConfigEntry('__non_existing_key__');
617 * Tests setting and getting a key will return same value of same type
618 * (NULL). This unit test does also unset the then existing key.
620 public function testSettingGettiingNullConfigKey () {
622 self::$configInstance->setConfigEntry('__test_key', NULL);
625 $value = self::$configInstance->getConfigEntry('__test_key');
628 $this->assertEquals(NULL, $value);
631 self::$configInstance->unsetConfigEntry('__test_key');
635 * Tests setting and getting a key will return same value of same type.
636 * This unit test does also unset the then existing key.
638 public function testSettingGettiingConfigKey () {
640 self::$configInstance->setConfigEntry('__test_key', 'foo');
643 $value = self::$configInstance->getConfigEntry('__test_key');
646 $this->assertEquals('foo', $value);
649 self::$configInstance->unsetConfigEntry('__test_key');
653 * Tests if the method getField() is still unsupported in this class. Please
654 * note, that this and isFieldSet() may get removed in the future. So also
655 * these test methods will be gone.
657 public function testConfigGetFieldUnsupported () {
658 // Expect this exception
659 $this->expectException(UnsupportedOperationException::class);
662 $dummy = self::$configInstance->getField('foo');
666 * Tests if the method isFieldSet() is still unsupported in this class. Please
667 * note, that this and getField() may get removed in the future. So also
668 * these test methods will be gone.
670 public function testConfigIsFieldSetUnsupported () {
671 // Expect this exception
672 $this->expectException(UnsupportedOperationException::class);
675 $dummy = self::$configInstance->isFieldSet('foo');