From: Roland Häder Date: Sun, 16 Jul 2017 21:46:53 +0000 (+0200) Subject: Continued with unit tests: X-Git-Url: https://git.mxchange.org/?p=core.git;a=commitdiff_plain;h=5da8f717122568335b8a8ab230fa0de17e983fab Continued with unit tests: - some refacturing: all detectFoo() methods are now moved from configuration to bootstrap class as this seems proper place - also they can only be called static - DNS resolver is now globally quieted in tests/bootstrap.php - created new unit test for FrameworkBootstrap class (partly) - this origins from configuration unit test Signed-off-by: Roland Häder --- diff --git a/framework/bootstrap/class_FrameworkBootstrap.php b/framework/bootstrap/class_FrameworkBootstrap.php index 16e5863d..2af601e0 100644 --- a/framework/bootstrap/class_FrameworkBootstrap.php +++ b/framework/bootstrap/class_FrameworkBootstrap.php @@ -6,8 +6,10 @@ namespace CoreFramework\Bootstrap; use CoreFramework\Configuration\FrameworkConfiguration; use CoreFramework\Connection\Database\DatabaseConnection; use CoreFramework\Connector\Database\DatabaseConnector; +use CoreFramework\Console\Tools\ConsoleTools; use CoreFramework\EntryPoint\ApplicationEntryPoint; use CoreFramework\Factory\ObjectFactory; +use CoreFramework\Generic\NullPointerException; use CoreFramework\Helper\Application\ApplicationHelper; use CoreFramework\Loader\ClassLoader; use CoreFramework\Manager\ManageableApplication; @@ -19,6 +21,7 @@ use CoreFramework\Response\Responseable; // Import SPL stuff use \BadMethodCallException; +use \InvalidArgumentException; /** * A framework-bootstrap class which helps the frameworks to bootstrap ... ;-) @@ -44,6 +47,11 @@ use \BadMethodCallException; */ final class FrameworkBootstrap { + /** + * Detected server address + */ + private static $serverAddress = NULL; + /** * Instance of a Requestable class */ @@ -381,6 +389,169 @@ final class FrameworkBootstrap { $applicationInstance->setDatabaseInstance($connectionInstance); } + /** + * Detects the server address (SERVER_ADDR) and set it in configuration + * + * @return $serverAddress The detected server address + * @throws UnknownHostnameException If SERVER_NAME cannot be resolved to an IP address + * @todo Have to check some more entries from $_SERVER here + */ + public static function detectServerAddress () { + // Is the entry set? + if (!isset(self::$serverAddress)) { + // Is it set in $_SERVER? + if (!empty($_SERVER['SERVER_ADDR'])) { + // Set it from $_SERVER + self::$serverAddress = $_SERVER['SERVER_ADDR']; + } elseif (isset($_SERVER['SERVER_NAME'])) { + // Resolve IP address + $serverIp = ConsoleTools::resolveIpAddress($_SERVER['SERVER_NAME']); + + // Is it valid? + if ($serverIp === false) { + /* + * Why is gethostbyname() returning the host name and not + * false as many other PHP functions are doing? ;-( + */ + throw new UnknownHostnameException(sprintf('Cannot resolve "%s" to an IP address. Please fix your setup.', $_SERVER['SERVER_NAME'])); + } // END - if + + // Al fine, set it + self::$serverAddress = $serverIp; + } else { + // Run auto-detecting through console tools lib + self::$serverAddress = ConsoleTools::acquireSelfIpAddress(); + } + } // END - if + + // Return it + return self::$serverAddress; + } + + /** + * Setter for default time zone (must be correct!) + * + * @param $timezone The timezone string (e.g. Europe/Berlin) + * @return $success If timezone was accepted + * @throws NullPointerException If $timezone is NULL + * @throws InvalidArgumentException If $timezone is empty + */ + public static function setDefaultTimezone ($timezone) { + // Is it null? + if (is_null($timezone)) { + // Throw NPE + throw new NullPointerException(NULL, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER); + } elseif (!is_string($timezone)) { + // Is not a string + throw new InvalidArgumentException(sprintf('timezone[]=%s is not a string', gettype($timezone))); + } elseif ((is_string($timezone)) && (empty($timezone))) { + // Entry is empty + throw new InvalidArgumentException('timezone is empty'); + } + + // Default success + $success = FALSE; + + /* + * Set desired time zone to prevent date() and related functions to + * issue an E_WARNING. + */ + $success = date_default_timezone_set($timezone); + + // Return status + return $success; + } + + /** + * Detects the HTTPS flag + * + * @return $https The detected HTTPS flag or null if failed + */ + public static function detectHttpSecured () { + // Default is null + $https = NULL; + + // Is HTTPS set? + if (self::isHttpSecured()) { + // Then use it + $https = $_SERVER['HTTPS']; + } // END - if + + // Return it + return $https; + } + + /** + * Checks whether HTTPS is set in $_SERVER + * + * @return $isset Whether HTTPS is set + * @todo Test more fields + */ + public static function isHttpSecured () { + return (isset($_SERVER['HTTPS'])); + } + + /** + * Dectect and return the base URL for all URLs and forms + * + * @return $baseUrl Detected base URL + */ + public static function detectBaseUrl () { + // Initialize the URL + $protocol = 'http'; + + // Do we have HTTPS? + if (self::isHttpSecured()) { + // Add the >s< for HTTPS + $protocol = 's'; + } // END - if + + // Construct the full URL and secure it against CSRF attacks + $baseUrl = $protocol . '://' . self::detectDomain() . self::detectScriptPath(); + + // Return the URL + return $baseUrl; + } + + /** + * Detect safely and return the full domain where this script is installed + * + * @return $fullDomain The detected full domain + */ + public static function detectDomain () { + // Full domain is localnet.invalid by default + $fullDomain = 'localnet.invalid'; + + // Is the server name there? + if (isset($_SERVER['SERVER_NAME'])) { + // Detect the full domain + $fullDomain = htmlentities(strip_tags($_SERVER['SERVER_NAME']), ENT_QUOTES); + } // END - if + + // Return it + return $fullDomain; + } + + /** + * Detect safely the script path without trailing slash which is the glue + * between "http://your-domain.invalid/" and "script-name.php" + * + * @return $scriptPath The script path extracted from $_SERVER['SCRIPT_NAME'] + */ + public static function detectScriptPath () { + // Default is empty + $scriptPath = ''; + + // Is the scriptname set? + if (isset($_SERVER['SCRIPT_NAME'])) { + // Get dirname from it and replace back-slashes with slashes for lame OSes... + $scriptPath = str_replace("\\", '/', dirname($_SERVER['SCRIPT_NAME'])); + } // END - if + + // Return it + return $scriptPath; + } + /** * 1) Loads class scanner and scans all framework's classes and interfaces. * This method also registers the class loader's method autoLoad() for the diff --git a/framework/config-global.php b/framework/config-global.php index 31a7fdc0..bf734267 100644 --- a/framework/config-global.php +++ b/framework/config-global.php @@ -39,7 +39,7 @@ $cfg->setConfigEntry('root_base_path', ApplicationEntryPoint::getRootPath() . DI $cfg->setConfigEntry('framework_base_path', ApplicationEntryPoint::detectFrameworkPath()); // CFG: BASE-URL -$cfg->setConfigEntry('base_url', $cfg->detectBaseUrl()); +$cfg->setConfigEntry('base_url', FrameworkBootstrap::detectBaseUrl()); // CFG: DATABASE-TYPE $cfg->setConfigEntry('database_type', 'local_file_database'); @@ -47,8 +47,8 @@ $cfg->setConfigEntry('database_type', 'local_file_database'); // CFG: LOCAL-DATABASE-PATH $cfg->setConfigEntry('local_database_path', $cfg->getConfigEntry('root_base_path') . 'db/'); -// CFG: TIME-ZONE -$cfg->setDefaultTimezone('Europe/Berlin'); +// Default timezone +FrameworkBootstrap::setDefaultTimezone('Europe/Berlin'); // CFG: CLASS-PREFIX $cfg->setConfigEntry('class_prefix', 'class_'); @@ -294,13 +294,13 @@ $cfg->setConfigEntry('guest_class', 'CoreFramework\User\Guest\Guest'); $cfg->setConfigEntry('cookie_expire', (60*60*2)); // Two hours! // CFG: COOKIE-PATH -$cfg->setConfigEntry('cookie_path', $cfg->detectScriptPath() . DIRECTORY_SEPARATOR); +$cfg->setConfigEntry('cookie_path', FrameworkBootstrap::detectScriptPath() . DIRECTORY_SEPARATOR); // CFG: COOKIE-DOMAIN -$cfg->setConfigEntry('cookie_domain', $cfg->detectDomain()); // Is mostly the same... +$cfg->setConfigEntry('cookie_domain', FrameworkBootstrap::detectDomain()); // Is mostly the same... // CFG: COOKIE-SSL -$cfg->setConfigEntry('cookie_ssl', $cfg->isHttpSecured()); +$cfg->setConfigEntry('cookie_ssl', FrameworkBootstrap::isHttpSecured()); // CFG: CRYPT-FIXED-SALT $cfg->setConfigEntry('crypt_fixed_salt', 'N'); diff --git a/framework/config/class_FrameworkConfiguration.php b/framework/config/class_FrameworkConfiguration.php index 26c47d42..2521e3c9 100644 --- a/framework/config/class_FrameworkConfiguration.php +++ b/framework/config/class_FrameworkConfiguration.php @@ -127,40 +127,6 @@ class FrameworkConfiguration implements Registerable { return $str; } - /** - * Setter for default time zone (must be correct!) - * - * @param $timezone The timezone string (e.g. Europe/Berlin) - * @return $success If timezone was accepted - * @throws NullPointerException If $timezone is NULL - * @throws InvalidArgumentException If $timezone is empty - */ - public final function setDefaultTimezone ($timezone) { - // Is it null? - if (is_null($timezone)) { - // Throw NPE - throw new NullPointerException($this, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER); - } elseif (!is_string($timezone)) { - // Is not a string - throw new InvalidArgumentException(sprintf('timezone[]=%s is not a string', gettype($timezone)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY); - } elseif ((is_string($timezone)) && (empty($timezone))) { - // Entry is empty - throw new InvalidArgumentException('timezone is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY); - } - - // Default success - $success = FALSE; - - /* - * Set desired time zone to prevent date() and related functions to - * issue an E_WARNING. - */ - $success = date_default_timezone_set($timezone); - - // Return status - return $success; - } - /** * Checks whether the given configuration key is set * @@ -212,7 +178,7 @@ class FrameworkConfiguration implements Registerable { } // Convert dashes to underscore - $configKey = self::convertDashesToUnderscores($configKey); + $configKey = $this->convertDashesToUnderscores($configKey); // Is a valid configuration key provided? if (!$this->isConfigurationEntrySet($configKey)) { @@ -251,7 +217,7 @@ class FrameworkConfiguration implements Registerable { } // Cast to string - $configKey = self::convertDashesToUnderscores($configKey); + $configKey = $this->convertDashesToUnderscores($configKey); // Set the configuration value //* NOISY-DEBUG: */ print(__METHOD__ . ':configEntry=' . $configKey . ',configValue[' . gettype($configValue) . ']=' . $configValue . PHP_EOL); @@ -295,7 +261,7 @@ class FrameworkConfiguration implements Registerable { } // Convert dashes to underscore - $configKey = self::convertDashesToUnderscores($configKey); + $configKey = $this->convertDashesToUnderscores($configKey); // Is the configuration key there? if (!$this->isConfigurationEntrySet($configKey)) { @@ -307,170 +273,6 @@ class FrameworkConfiguration implements Registerable { unset($this->config[$configKey]); } - /** - * Detects the server address (SERVER_ADDR) and set it in configuration - * - * @return $serverAddress The detected server address - * @throws UnknownHostnameException If SERVER_NAME cannot be resolved to an IP address - * @todo Have to check some more entries from $_SERVER here - */ - public function detectServerAddress () { - // Is the entry set? - if (!$this->isConfigurationEntrySet('server_addr')) { - // Is it set in $_SERVER? - if (isset($_SERVER['SERVER_ADDR'])) { - // Set it from $_SERVER - $this->setServerAddress($_SERVER['SERVER_ADDR']); - } elseif (isset($_SERVER['SERVER_NAME'])) { - // Resolve IP address - $serverIp = ConsoleTools::resolveIpAddress($_SERVER['SERVER_NAME']); - - // Is it valid? - if ($serverIp === false) { - /* - * Why is gethostbyname() returning the host name and not - * false as many other PHP functions are doing? ;-( - */ - throw new UnknownHostnameException(sprintf('Cannot resolve "%s" to an IP address. Please fix your setup.', $_SERVER['SERVER_NAME'])); - } // END - if - - // Al fine, set it - $this->setServerAddress($serverIp); - } else { - // Run auto-detecting through console tools lib - ConsoleTools::acquireSelfIpAddress(); - } - } // END - if - - // Now get it from configuration - $serverAddress = $this->getServerAddress(); - - // Return it - return $serverAddress; - } - - /** - * Setter for SERVER_ADDR - * - * @param $serverAddress New SERVER_ADDR value to set - * @return void - */ - public function setServerAddress ($serverAddress) { - // Is a valid configuration key key provided? - if (is_null($serverAddress)) { - // Configuration key is null - throw new NullPointerException($this, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER); - } elseif (!is_string($serverAddress)) { - // Is not a string - throw new InvalidArgumentException(sprintf('serverAddress[]=%s is not a string', gettype($serverAddress)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY); - } elseif ((is_string($serverAddress)) && (empty($serverAddress))) { - // Entry is empty - throw new InvalidArgumentException('serverAddress is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY); - } - - // Set it, please don't do it yourself here - $this->setConfigEntry('server_addr', (string) $serverAddress); - } - - /** - * Getter for SERVER_ADDR - * - * @return $serverAddress New SERVER_ADDR value to set - */ - public function getServerAddress () { - return $this->getConfigEntry('server_addr'); - } - - /** - * Detects the HTTPS flag - * - * @return $https The detected HTTPS flag or null if failed - */ - public function detectHttpSecured () { - // Default is null - $https = NULL; - - // Is HTTPS set? - if ($this->isHttpSecured()) { - // Then use it - $https = $_SERVER['HTTPS']; - } // END - if - - // Return it - return $https; - } - - /** - * Checks whether HTTPS is set in $_SERVER - * - * @return $isset Whether HTTPS is set - * @todo Test more fields - */ - public function isHttpSecured () { - return (isset($_SERVER['HTTPS'])); - } - - /** - * Dectect and return the base URL for all URLs and forms - * - * @return $baseUrl Detected base URL - */ - public function detectBaseUrl () { - // Initialize the URL - $protocol = 'http'; - - // Do we have HTTPS? - if ($this->isHttpSecured()) { - // Add the >s< for HTTPS - $protocol = 's'; - } // END - if - - // Construct the full URL and secure it against CSRF attacks - $baseUrl = $protocol . '://' . $this->detectDomain() . $this->detectScriptPath(); - - // Return the URL - return $baseUrl; - } - - /** - * Detect safely and return the full domain where this script is installed - * - * @return $fullDomain The detected full domain - */ - public function detectDomain () { - // Full domain is localnet.invalid by default - $fullDomain = 'localnet.invalid'; - - // Is the server name there? - if (isset($_SERVER['SERVER_NAME'])) { - // Detect the full domain - $fullDomain = htmlentities(strip_tags($_SERVER['SERVER_NAME']), ENT_QUOTES); - } // END - if - - // Return it - return $fullDomain; - } - - /** - * Detect safely the script path without trailing slash which is the glue - * between "http://your-domain.invalid/" and "script-name.php" - * - * @return $scriptPath The script path extracted from $_SERVER['SCRIPT_NAME'] - */ - public function detectScriptPath () { - // Default is empty - $scriptPath = ''; - - // Is the scriptname set? - if (isset($_SERVER['SCRIPT_NAME'])) { - // Get dirname from it and replace back-slashes with slashes for lame OSes... - $scriptPath = str_replace("\\", '/', dirname($_SERVER['SCRIPT_NAME'])); - } // END - if - - // Return it - return $scriptPath; - } - /** * Getter for field name * diff --git a/framework/main/classes/rng/class_RandomNumberGenerator.php b/framework/main/classes/rng/class_RandomNumberGenerator.php index d2229285..31bc3c0a 100644 --- a/framework/main/classes/rng/class_RandomNumberGenerator.php +++ b/framework/main/classes/rng/class_RandomNumberGenerator.php @@ -3,6 +3,7 @@ namespace CoreFramework\Crypto\RandomNumber; // Import framework stuff +use CoreFramework\Bootstrap\FrameworkBootstrap; use CoreFramework\Object\BaseFrameworkSystem; use CoreFramework\Generic\FrameworkInterface; @@ -120,7 +121,7 @@ class RandomNumberGenerator extends BaseFrameworkSystem { // Do we have a single server? if ($this->getConfigInstance()->getConfigEntry('is_single_server') == 'Y') { // Then use that IP for extra security - $serverIp = $this->getConfigInstance()->detectServerAddress(); + $serverIp = FrameworkBootstrap::detectServerAddress(); } // END - if // Yet-another fixed salt. This is not dependend on server software or date diff --git a/framework/main/classes/tools/console/class_ConsoleTools.php b/framework/main/classes/tools/console/class_ConsoleTools.php index 8b5a2a45..90216410 100644 --- a/framework/main/classes/tools/console/class_ConsoleTools.php +++ b/framework/main/classes/tools/console/class_ConsoleTools.php @@ -281,9 +281,6 @@ class ConsoleTools extends BaseFrameworkSystem { )); } - // Set it in configuration - FrameworkConfiguration::getSelfInstance()->setServerAddress($ipAddress); - // Return it return $ipAddress; } diff --git a/framework/main/exceptions/main/class_NullPointerException.php b/framework/main/exceptions/main/class_NullPointerException.php index f8fb0eee..ecb7bac4 100644 --- a/framework/main/exceptions/main/class_NullPointerException.php +++ b/framework/main/exceptions/main/class_NullPointerException.php @@ -36,12 +36,20 @@ class NullPointerException extends FrameworkException { * @param $code Code number for the exception * @return void */ - public function __construct (FrameworkInterface $class, $code) { - // Add a message around the missing class - $message = sprintf('[%s:%d] An object instance is set to null.', - $class->__toString(), - $this->getLine() - ); + public function __construct (FrameworkInterface $class = NULL, $code) { + // Is the instance set? + if (is_null($class)) { + // Add a message around the missing class + $message = sprintf('[unknown:%d] An object instance is set to null.', + $this->getLine() + ); + } else { + // Add a message around the missing class + $message = sprintf('[%s:%d] An object instance is set to null.', + $class->__toString(), + $this->getLine() + ); + } // Call parent constructor parent::__construct($message, $code); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 3d2f1aee..de3ab8ee 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,5 +1,8 @@ * @@ -40,3 +43,6 @@ if (extension_loaded('xdebug')) { // Autoload more stuff require dirname(__DIR__) . '/vendor/autoload.php'; + +// Quiet DNS resolver as this is not wanted here +FrameworkConfiguration::getSelfInstance()->setConfigEntry('quiet_dns_resolver', TRUE); diff --git a/tests/framework/.htaccess b/tests/framework/.htaccess new file mode 100644 index 00000000..3a428827 --- /dev/null +++ b/tests/framework/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/tests/framework/bootstrap/.htaccess b/tests/framework/bootstrap/.htaccess new file mode 100644 index 00000000..3a428827 --- /dev/null +++ b/tests/framework/bootstrap/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/tests/framework/bootstrap/class_FrameworkBootstrapTest.php b/tests/framework/bootstrap/class_FrameworkBootstrapTest.php new file mode 100644 index 00000000..95a847c1 --- /dev/null +++ b/tests/framework/bootstrap/class_FrameworkBootstrapTest.php @@ -0,0 +1,215 @@ + + * + * 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 . + */ +class FrameworkBootstrapTest extends TestCase { + + /** + * Own IP address + */ + private static $ipAddress = FALSE; + + /** + * 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(); + + /* + * Disable strict naming-convention check in own class loader, because + * PHP_Invoker doesn't have namespaces. + */ + ClassLoader::enableStrictNamingConventionCheck(FALSE); + + // Lookup own IP address + self::$ipAddress = ConsoleTools::acquireSelfIpAddress(); + + // Trace message + //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__); + } + + /** + * Tests setting a NULL default timezone + */ + public function testSettingNullDefaultTimezone () { + // Will throw this exception + $this->expectException(NullPointerException::class); + + // Test it + FrameworkBootstrap::setDefaultTimezone(NULL); + } + + /** + * Tests setting a boolean default timezone + */ + public function testSettingBooleanDefaultTimezone () { + // Will throw this exception + $this->expectException(InvalidArgumentException::class); + + // Test it + FrameworkBootstrap::setDefaultTimezone(FALSE); + } + + /** + * Tests setting a decimal default timezone + */ + public function testSettingDecimalDefaultTimezone () { + // Will throw this exception + $this->expectException(InvalidArgumentException::class); + + // Test it + FrameworkBootstrap::setDefaultTimezone(12345); + } + + /** + * Tests setting a float default timezone + */ + public function testSettingFloatDefaultTimezone () { + // Will throw this exception + $this->expectException(InvalidArgumentException::class); + + // Test it + FrameworkBootstrap::setDefaultTimezone(123.45); + } + + /** + * Tests setting an array default timezone + */ + public function testSettingArrayDefaultTimezone () { + // Will throw this exception + $this->expectException(InvalidArgumentException::class); + + // Test it + FrameworkBootstrap::setDefaultTimezone(array()); + } + + /** + * Tests setting an object default timezone + */ + public function testSettingObjectDefaultTimezone () { + // Will throw this exception + $this->expectException(InvalidArgumentException::class); + + // Test it + FrameworkBootstrap::setDefaultTimezone($this); + } + + /** + * Tests setting a resource default timezone + */ + public function testSettingResourceDefaultTimezone () { + // Will throw this exception + $this->expectException(InvalidArgumentException::class); + + // Init some resource + $resource = fopen(__FILE__, 'r'); + + // Test it + FrameworkBootstrap::setDefaultTimezone($resource); + } + + /** + * Tests setting an empty default timezone + */ + public function testSettingEmptyDefaultTimezone () { + // Will throw this exception + $this->expectException(InvalidArgumentException::class); + + // Test it + FrameworkBootstrap::setDefaultTimezone(''); + } + + /** + * Tests setting invalid timezone + */ + public function testSettingInvalidDefaultTimezone () { + // Expect Notice + $this->expectException(Notice::class); + + // Try to set it + FrameworkBootstrap::setDefaultTimezone('!invalid!'); + } + + /** + * Tests setting valid timezone + */ + public function testSettingValidDefaultTimezone () { + // Will be true + $this->assertTrue(FrameworkBootstrap::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 = FrameworkBootstrap::detectServerAddress(); + + // Should be the same + $this->assertEquals(self::$ipAddress, $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 = FrameworkBootstrap::detectServerAddress(); + + // Should be the same + $this->assertEquals(self::$ipAddress, $serverAddress); + } + +} diff --git a/tests/framework/config/.htaccess b/tests/framework/config/.htaccess new file mode 100644 index 00000000..3a428827 --- /dev/null +++ b/tests/framework/config/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/tests/framework/config/FrameworkConfigurationTest.php b/tests/framework/config/FrameworkConfigurationTest.php index 329e01ca..4de7bbcc 100644 --- a/tests/framework/config/FrameworkConfigurationTest.php +++ b/tests/framework/config/FrameworkConfigurationTest.php @@ -4,7 +4,6 @@ namespace CoreFramework\Configuration; // Inport framework stuff -use CoreFramework\Console\Tools\ConsoleTools; use CoreFramework\Loader\ClassLoader; use CoreFramework\Generic\NullPointerException; use CoreFramework\Generic\UnsupportedOperationException; @@ -32,7 +31,6 @@ use \InvalidArgumentException; * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - class FrameworkConfigurationTest extends TestCase { /** @@ -40,11 +38,6 @@ class FrameworkConfigurationTest extends TestCase { */ private static $configInstance = NULL; - /** - * Own IP address - */ - private static $ipAddress = FALSE; - /** * Setup test case */ @@ -79,12 +72,6 @@ class FrameworkConfigurationTest extends TestCase { */ ClassLoader::enableStrictNamingConventionCheck(FALSE); - // Quiet DNS resolver as this is not wanted here - self::$configInstance->setConfigEntry('quiet_dns_resolver', TRUE); - - // Lookup own IP address - self::$ipAddress = ConsoleTools::acquireSelfIpAddress(); - // Trace message //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__); } @@ -662,249 +649,6 @@ class FrameworkConfigurationTest extends TestCase { 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 a boolean default timezone - */ - public function testSettingBooleanDefaultTimezone () { - // Will throw this exception - $this->expectException(InvalidArgumentException::class); - - // Test it - self::$configInstance->setDefaultTimezone(FALSE); - } - - /** - * Tests setting a decimal default timezone - */ - public function testSettingDecimalDefaultTimezone () { - // Will throw this exception - $this->expectException(InvalidArgumentException::class); - - // Test it - self::$configInstance->setDefaultTimezone(12345); - } - - /** - * Tests setting a float default timezone - */ - public function testSettingFloatDefaultTimezone () { - // Will throw this exception - $this->expectException(InvalidArgumentException::class); - - // Test it - self::$configInstance->setDefaultTimezone(123.45); - } - - /** - * Tests setting an array default timezone - */ - public function testSettingArrayDefaultTimezone () { - // Will throw this exception - $this->expectException(InvalidArgumentException::class); - - // Test it - self::$configInstance->setDefaultTimezone(array()); - } - - /** - * Tests setting an object default timezone - */ - public function testSettingObjectDefaultTimezone () { - // Will throw this exception - $this->expectException(InvalidArgumentException::class); - - // Test it - self::$configInstance->setDefaultTimezone($this); - } - - /** - * Tests setting a resource default timezone - */ - public function testSettingResourceDefaultTimezone () { - // Will throw this exception - $this->expectException(InvalidArgumentException::class); - - // Init some resource - $resource = fopen(__FILE__, 'r'); - - // Test it - self::$configInstance->setDefaultTimezone($resource); - } - - /** - * 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(self::$ipAddress, $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(self::$ipAddress, $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); - - // Set old back - self::$configInstance->setServerAddress(self::$ipAddress); - } - /** * Tests if the method getField() is still unsupported in this class. Please * note, that this and isFieldSet() may get removed in the future. So also