]> git.mxchange.org Git - core.git/commitdiff
Added first PHPUnit-based unit test. This will test some of the methods in the
authorRoland Häder <roland@mxchange.org>
Sun, 16 Jul 2017 16:19:49 +0000 (18:19 +0200)
committerRoland Häder <roland@mxchange.org>
Sun, 16 Jul 2017 16:19:49 +0000 (18:19 +0200)
configuration class 'FrameworkConfiguration'. Strangely, not all I have tested
have 100% coverage ... Why? :-(

Signed-off-by: Roland Häder <roland@mxchange.org>
tests/framework/config/FrameworkConfigurationTest.php [new file with mode: 0644]

diff --git a/tests/framework/config/FrameworkConfigurationTest.php b/tests/framework/config/FrameworkConfigurationTest.php
new file mode 100644 (file)
index 0000000..8e33a28
--- /dev/null
@@ -0,0 +1,754 @@
+<?php
+
+// Same namespace as target class
+namespace CoreFramework\Configuration;
+
+// Inport framework stuff
+use CoreFramework\Loader\ClassLoader;
+use CoreFramework\Generic\NullPointerException;
+
+// Import PHPUnit stuff
+use PHPUnit\Framework\Error\Notice;
+use PHPUnit\Framework\TestCase;
+
+// Import SPL stuff
+use \InvalidArgumentException;
+
+/*
+ * Copyright (C) 2017 Roland Haeder<roland@mxchange.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+class FrameworkConfigurationTest extends TestCase {
+
+       /**
+        * The configuration instance being tested
+        */
+       private static $configInstance = NULL;
+
+       /**
+        * Setup test case
+        */
+       public function setUp() {
+               // Trace message
+               //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
+
+               // Call parent method
+               parent::setUp();
+
+               // Trace message
+               //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
+
+       }
+
+       /**
+        * Setup test case
+        */
+       public static function setUpBeforeClass() {
+               // Trace message
+               //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
+
+               // Call parent method
+               parent::setUpBeforeClass();
+
+               // Init instance
+               self::$configInstance = FrameworkConfiguration::getSelfInstance();
+
+               /*
+                * Disable strict naming-convention check in own class loader, because
+                * PHP_Invoker doesn't have namespaces.
+                */
+               ClassLoader::enableStrictNamingConventionCheck(FALSE);
+
+               // Trace message
+               //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
+       }
+
+       /**
+        * Tests if __toString() returns proper name
+        */
+       public function testConfigToStringClassName () {
+               // Get it
+               $className = self::$configInstance->__toString();
+
+               // Should be equal
+               $this->assertEquals('CoreFramework\Configuration\FrameworkConfiguration', $className);
+       }
+
+       /**
+        * Tests getting a singleton instance
+        */
+       public function testGettingSelfConfigInstance () {
+               // Get instance
+               $dummyInstance = FrameworkConfiguration::getSelfInstance();
+
+               // Should be equal to own instance
+               $this->assertEquals(self::$configInstance, $dummyInstance);
+       }
+
+       /**
+        * Tests equals() method
+        */
+       public function testEqualsConfigInstance () {
+               // Get instance
+               $dummyInstance = FrameworkConfiguration::getSelfInstance();
+
+               // Should return TRUE
+               $this->assertTrue(self::$configInstance->equals($dummyInstance));
+       }
+
+       /**
+        * Tests hashCode() method
+        */
+       public function testHashCodeConfigInstance () {
+               // Get instance
+               $dummyInstance = FrameworkConfiguration::getSelfInstance();
+
+               // Get hash code from both
+               $hashCodeExpected = self::$configInstance->hashCode();
+               $hashCodeActual = $dummyInstance->hashCode();
+
+               // Should be equal
+               $this->assertEquals($hashCodeExpected, $hashCodeActual);
+       }
+
+       /**
+        * Tests if getting configuration entry returns an array
+        */
+       public function testGettingConfigurationArray () {
+               // Get it
+               $config = self::$configInstance->getConfigurationArray();
+
+               // Should be an array
+               $this->assertTrue(is_array($config));
+       }
+
+       /**
+        * Tests if getting configuration entry returns a filled array
+        */
+       public function testGettingfilledConfigurationArray () {
+               // Get it
+               $config = self::$configInstance->getConfigurationArray();
+
+               // Should be an array
+               $this->assertTrue(count($config) > 0);
+       }
+
+       /**
+        * Tests if getting whole configuration entry is the same for another
+        * singleton instance
+        */
+       public function testSameConfigurationArrayGetter () {
+               // Get instance
+               $dummyInstance = FrameworkConfiguration::getSelfInstance();
+
+               // Get it from both instances
+               $config1 = self::$configInstance->getConfigurationArray();
+               $config2 = $dummyInstance->getConfigurationArray();
+
+               // Should be the same
+               $this->assertEquals($config1, $config2);
+       }
+
+       /**
+        * Tests if a proper exception is thrown when check for a NULL key
+        */
+       public function testCheckingNullConfigKey () {
+               // Will throw this exception
+               $this->expectException(NullPointerException::class);
+
+               // Test it
+               $dummy = self::$configInstance->isConfigurationEntrySet(NULL);
+       }
+
+       /**
+        * Tests if a proper exception is thrown when checking a boolean key
+        */
+       public function testCheckingBooleanConfigKey () {
+               // Will throw this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Test it
+               $dummy = self::$configInstance->isConfigurationEntrySet(FALSE);
+       }
+
+       /**
+        * Tests if a proper exception is thrown when checking an empty key
+        */
+       public function testCheckingEmptyConfigKey () {
+               // Will throw this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Test it
+               $dummy = self::$configInstance->isConfigurationEntrySet('');
+       }
+
+       /**
+        * Tests if a proper exception is thrown when checking an array key
+        */
+       public function testCheckingArrayConfigKey () {
+               // Will throw this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Test it
+               $dummy = self::$configInstance->isConfigurationEntrySet(array());
+       }
+
+       /**
+        * Tests if a proper exception is thrown when checking a decimal key
+        */
+       public function testCheckingDecimalConfigKey () {
+               // Will throw this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Test it
+               $dummy = self::$configInstance->isConfigurationEntrySet(12345);
+       }
+
+       /**
+        * Tests if a proper exception is thrown when checking a float key
+        */
+       public function testCheckingFloatConfigKey () {
+               // Will throw this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Test it
+               $dummy = self::$configInstance->isConfigurationEntrySet(123.45);
+       }
+
+       /**
+        * Tests if a proper exception is thrown when checking an object key
+        */
+       public function testCheckingObjectConfigKey () {
+               // Will throw this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Test it
+               $dummy = self::$configInstance->isConfigurationEntrySet($this);
+       }
+
+       /**
+        * Tests if a proper exception is thrown when checking a resource key
+        */
+       public function testCheckingResourceConfigKey () {
+               // Will throw this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Init some resource
+               $resource = fopen(__FILE__, 'r');
+
+               // Test it
+               $dummy = self::$configInstance->isConfigurationEntrySet($resource);
+       }
+
+       /**
+        * Tests if checking an existing (well-known) key can be found and returns
+        * TRUE.
+        */
+       public function testCheckingExistingConfigKey () {
+               // Should return TRUE
+               $this->assertTrue(self::$configInstance->isConfigurationEntrySet('application_base_path'));
+       }
+
+       /**
+        * Tests if checking a non-existing key will return FALSE.
+        */
+       public function testCheckingNonExistingConfigKey () {
+               // Should return FALSE
+               $this->assertFalse(self::$configInstance->isConfigurationEntrySet('__non_existing_key__'));
+       }
+
+       /**
+        * Tests if a proper exception is thrown when getting a NULL key
+        */
+       public function testGettingNullConfigKey () {
+               // Will throw this exception
+               $this->expectException(NullPointerException::class);
+
+               // Test it
+               $dummy = self::$configInstance->getConfigEntry(NULL);
+       }
+
+       /**
+        * Tests if a proper exception is thrown when getting a boolean key
+        */
+       public function testGettingBooleanConfigKey () {
+               // Will throw this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Test it
+               $dummy = self::$configInstance->getConfigEntry(FALSE);
+       }
+
+       /**
+        * Tests if a proper exception is thrown when getting an empty key
+        */
+       public function testGettingEmptyConfigKey () {
+               // Will throw this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Test it
+               $dummy = self::$configInstance->getConfigEntry('');
+       }
+
+       /**
+        * Tests if a proper exception is thrown when getting a decimal key
+        */
+       public function testGettingDecimalConfigKey () {
+               // Will throw this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Test it
+               $dummy = self::$configInstance->getConfigEntry(12345);
+       }
+
+       /**
+        * Tests if a proper exception is thrown when getting a float key
+        */
+       public function testGettingFloatConfigKey () {
+               // Will throw this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Test it
+               $dummy = self::$configInstance->getConfigEntry(123.45);
+       }
+
+       /**
+        * Tests if a proper exception is thrown when getting an array key
+        */
+       public function testGettingArrayConfigKey () {
+               // Will throw this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Test it
+               $dummy = self::$configInstance->getConfigEntry(array());
+       }
+
+       /**
+        * Tests if a proper exception is thrown when getting an object key
+        */
+       public function testGettingObjectConfigKey () {
+               // Will throw this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Test it
+               $dummy = self::$configInstance->getConfigEntry($this);
+       }
+
+       /**
+        * Tests if a proper exception is thrown when getting a resource key
+        */
+       public function testGettingResourceConfigKey () {
+               // Will throw this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Init some resource
+               $resource = fopen(__FILE__, 'r');
+
+               // Test it
+               $dummy = self::$configInstance->getConfigEntry($resource);
+       }
+
+       /**
+        * Tests if getting a non-existing key will cause a proper exception been
+        * thrown.
+        */
+       public function testGettingNonExistingConfigKey () {
+               // Should cause this exception
+               $this->expectException(NoConfigEntryException::class);
+
+               // Get non-existing key
+               $dummy = self::$configInstance->getConfigEntry('__non_existing_key__');
+       }
+
+       /**
+        * Tests if a generic key 'application_base_path' can be found.
+        */
+       public function testGettingConfigKeyApplicationBasePathExists () {
+               // Get it from config instance
+               $value = self::$configInstance->getConfigEntry('application_base_path');
+
+               // Is it a readable path?
+               $this->assertDirectoryIsReadable($value);
+       }
+
+       /**
+        * Tests setting a NULL key (value doesn't matter)
+        */
+       public function testSettingNullConfigKey () {
+               // Will throw this exception
+               $this->expectException(NullPointerException::class);
+
+               // Test it
+               self::$configInstance->setConfigEntry(NULL, 'foo');
+       }
+
+       /**
+        * Tests setting a boolean key (value doesn't matter)
+        */
+       public function testSettingBooleanConfigKey () {
+               // Will throw this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Test it
+               self::$configInstance->setConfigEntry(FALSE, 'foo');
+       }
+
+       /**
+        * Tests setting an empty key (value doesn't matter)
+        */
+       public function testSettingEmptyConfigKey () {
+               // Will throw this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Test it
+               self::$configInstance->setConfigEntry('', 'foo');
+       }
+
+       /**
+        * Tests setting a decimal key (value doesn't matter)
+        */
+       public function testSettingDecimalConfigKey () {
+               // Will throw this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Test it
+               self::$configInstance->setConfigEntry(12345, 'foo');
+       }
+
+       /**
+        * Tests setting a float key (value doesn't matter)
+        */
+       public function testSettingFloatConfigKey () {
+               // Will throw this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Test it
+               self::$configInstance->setConfigEntry(123.45, 'foo');
+       }
+
+       /**
+        * Tests setting an array key (value doesn't matter)
+        */
+       public function testSettingArrayConfigKey () {
+               // Will throw this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Test it
+               self::$configInstance->setConfigEntry(array(), 'foo');
+       }
+
+       /**
+        * Tests setting an object key (value doesn't matter)
+        */
+       public function testSettingObjectConfigKey () {
+               // Will throw this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Test it
+               self::$configInstance->setConfigEntry($this, 'foo');
+       }
+
+       /**
+        * Tests setting a resource key (value doesn't matter)
+        */
+       public function testSettingResourceConfigKey () {
+               // Will throw this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Init some resource
+               $resource = fopen(__FILE__, 'r');
+
+               // Test it
+               self::$configInstance->setConfigEntry($resource, 'foo');
+       }
+
+       /**
+        * Tests setting a valid key but array for value
+        */
+       public function testSettingArrayValueConfigKey () {
+               // Will throw this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Test it
+               self::$configInstance->setConfigEntry('foo', array());
+       }
+
+       /**
+        * Tests setting a valid key but object for value
+        */
+       public function testSettingObjectValueConfigKey () {
+               // Will throw this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Test it
+               self::$configInstance->setConfigEntry('foo', $this);
+       }
+
+       /**
+        * Tests setting a valid key but resource for value
+        */
+       public function testSettingResourceValueConfigKey () {
+               // Will throw this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Init some resource
+               $resource = fopen(__FILE__, 'r');
+
+               // Test it
+               self::$configInstance->setConfigEntry('foo', $resource);
+       }
+
+       /**
+        * Tests unsetting NULL key
+        */
+       public function testUnsettingNullConfigKey () {
+               // Will throw this exception
+               $this->expectException(NullPointerException::class);
+
+               // Test it
+               self::$configInstance->unsetConfigEntry(NULL);
+       }
+
+       /**
+        * Tests unsetting an empty key
+        */
+       public function testUnettingEmptyConfigKey () {
+               // Will throw this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Test it
+               self::$configInstance->unsetConfigEntry('');
+       }
+
+       /**
+        * Tests unsetting a non-existing key
+        */
+       public function testUnettingNonExistingConfigKey () {
+               // Will throw this exception
+               $this->expectException(NoConfigEntryException::class);
+
+               // Test it
+               self::$configInstance->unsetConfigEntry('__non_existing_key__');
+       }
+
+       /**
+        * Tests setting and getting a key will return same value of same type
+        * (NULL). This unit test does also unset the then existing key.
+        */
+       public function testSettingGettiingNullConfigKey () {
+               // Set test value
+               self::$configInstance->setConfigEntry('__test_key', NULL);
+
+               // Get it
+               $value = self::$configInstance->getConfigEntry('__test_key');
+
+               // Must be equal
+               $this->assertEquals(NULL, $value);
+
+               // Unset it
+               self::$configInstance->unsetConfigEntry('__test_key');
+       }
+
+       /**
+        * Tests setting and getting a key will return same value of same type.
+        * This unit test does also unset the then existing key.
+        */
+       public function testSettingGettiingConfigKey () {
+               // Set test value
+               self::$configInstance->setConfigEntry('__test_key', 'foo');
+
+               // Get it
+               $value = self::$configInstance->getConfigEntry('__test_key');
+
+               // Must be equal
+               $this->assertEquals('foo', $value);
+
+               // Unset it
+               self::$configInstance->unsetConfigEntry('__test_key');
+       }
+
+       /**
+        * Tests setting a NULL default timezone
+        */
+       public function testSettingNullDefaultTimezone () {
+               // Will throw this exception
+               $this->expectException(NullPointerException::class);
+
+               // Test it
+               self::$configInstance->setDefaultTimezone(NULL);
+       }
+
+       /**
+        * Tests setting an empty default timezone
+        */
+       public function testSettingEmptyDefaultTimezone () {
+               // Will throw this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Test it
+               self::$configInstance->setDefaultTimezone('');
+       }
+
+       /**
+        * Tests setting invalid timezone
+        */
+       public function testSettingInvalidDefaultTimezone () {
+               // Expect Notice
+               $this->expectException(Notice::class);
+
+               // Try to set it
+               self::$configInstance->setDefaultTimezone('!invalid!');
+       }
+
+       /**
+        * Tests setting valid timezone
+        */
+       public function testSettingValidDefaultTimezone () {
+               // Will be true
+               $this->assertTrue(self::$configInstance->setDefaultTimezone('Europe/Berlin'));
+       }
+
+       /**
+        * Tests if detectServerAddress is returning what it should for tests.
+        * This will always be 127.0.0.1.
+        */
+       public function testConfigDetectServerAddress () {
+               // Call it
+               $serverAddress = self::$configInstance->detectServerAddress();
+
+               // Should be the same
+               $this->assertEquals('127.0.0.1', $serverAddress);
+       }
+
+       /**
+        * Re-tests if detectServerAddress is returning what it should for tests.
+        * This will always be 127.0.0.1. This call should not invoke
+        * ConsoleTools's method as the configuration entry is already cached.
+        */
+       public function testConfigDetectServerAddressCached () {
+               // Call it
+               $serverAddress = self::$configInstance->detectServerAddress();
+
+               // Should be the same
+               $this->assertEquals('127.0.0.1', $serverAddress);
+       }
+
+       /**
+        * Tests setting a NULL server address
+        */
+       public function testConfigSettingNullServerAddress () {
+               // Expect this exception
+               $this->expectException(NullPointerException::class);
+
+               // Test it
+               self::$configInstance->setServerAddress(NULL);
+       }
+
+       /**
+        * Tests setting a boolean server address
+        */
+       public function testConfigSettingBooleanServerAddress () {
+               // Expect this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Test it
+               self::$configInstance->setServerAddress(FALSE);
+       }
+
+       /**
+        * Tests setting a decimal server address
+        */
+       public function testConfigSettingDecimalServerAddress () {
+               // Expect this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Test it
+               self::$configInstance->setServerAddress(12345);
+       }
+
+       /**
+        * Tests setting a float server address
+        */
+       public function testConfigSettingFloatServerAddress () {
+               // Expect this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Test it
+               self::$configInstance->setServerAddress(123.45);
+       }
+
+       /**
+        * Tests setting an array server address
+        */
+       public function testConfigSettingArrayServerAddress () {
+               // Expect this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Test it
+               self::$configInstance->setServerAddress(array());
+       }
+
+       /**
+        * Tests setting an object server address
+        */
+       public function testConfigSettingObjectServerAddress () {
+               // Expect this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Test it
+               self::$configInstance->setServerAddress($this);
+       }
+
+       /**
+        * Tests setting a resource server address
+        */
+       public function testConfigSettingResourceServerAddress () {
+               // Expect this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Init some resource
+               $resource = fopen(__FILE__, 'r');
+
+               // Test it
+               self::$configInstance->setServerAddress($resource);
+       }
+
+       /**
+        * Tests setting an empty server address
+        */
+       public function testConfigSettingEmptyServerAddress () {
+               // Expect this exception
+               $this->expectException(InvalidArgumentException::class);
+
+               // Test it
+               self::$configInstance->setServerAddress('');
+       }
+
+       /**
+        * Tests setting a valid server address and getting it back
+        */
+       public function testConfigGettingValidServerAddress () {
+               // Test it
+               self::$configInstance->setServerAddress('127.0.0.1');
+
+               // Get it back
+               $serverAddress = self::$configInstance->getServerAddress();
+
+               // Should be equal
+               $this->assertEquals('127.0.0.1', $serverAddress);
+       }
+
+}