From 0098a8bfbf8a72aa497626aa76a88e590944682d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sat, 15 Mar 2008 18:23:07 +0000 Subject: [PATCH] first unit test for configuration sub system added --- .gitattributes | 1 + .../class_ConfigEntryIsEmptyException.php | 8 +- .../classes/class_LocalFileDatabase.php | 4 +- inc/config/class_FrameworkConfiguration.php | 6 +- tests/ConfigTest.php | 142 ++++++++++++++++++ 5 files changed, 152 insertions(+), 9 deletions(-) create mode 100644 tests/ConfigTest.php diff --git a/.gitattributes b/.gitattributes index 36ba42873..9771ed95d 100644 --- a/.gitattributes +++ b/.gitattributes @@ -206,3 +206,4 @@ templates/de/code/emergency_exit.ctp -text templates/de/code/footer_msg.ctp -text templates/de/code/header.ctp -text templates/de/html/.htaccess -text +tests/ConfigTest.php -text diff --git a/inc/classes/exceptions/main/class_ConfigEntryIsEmptyException.php b/inc/classes/exceptions/main/class_ConfigEntryIsEmptyException.php index df1891566..f16e36580 100644 --- a/inc/classes/exceptions/main/class_ConfigEntryIsEmptyException.php +++ b/inc/classes/exceptions/main/class_ConfigEntryIsEmptyException.php @@ -7,7 +7,7 @@ * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software * @license GNU GPL 3.0 or any newer version * @link http://www.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 @@ -25,14 +25,14 @@ class ConfigEntryIsEmptyException extends FrameworkException { /** * The constructor * - * @param $message Message from the exception + * @param $message Message from the exception * @param $code Code number for the exception * @return void */ - public function __construct (BaseFrameworkSystem $class, $code) { + public function __construct (FrameworkConfiguration $class, $code) { // Add a message around the missing class $message = sprintf("[%s:%d] Leeren Konfigurationseintrag angegeben!", - $class, + $class->__toString(), $this->getLine() ); diff --git a/inc/classes/main/database/classes/class_LocalFileDatabase.php b/inc/classes/main/database/classes/class_LocalFileDatabase.php index 0dc66c680..ea4a8e19e 100644 --- a/inc/classes/main/database/classes/class_LocalFileDatabase.php +++ b/inc/classes/main/database/classes/class_LocalFileDatabase.php @@ -316,7 +316,7 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend * @param $fqfn The FQFN of the last read file * @return void */ - private function setLastFile ($fqfn) { + private final function setLastFile ($fqfn) { // Cast string $fqfn = (string) $fqfn; $this->lastFile = $fqfn; @@ -337,7 +337,7 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend * @param $contents An array with header and data elements * @return void */ - private function setLastFileContents ($contents) { + private final function setLastFileContents ($contents) { // Cast array $contents = (array) $contents; $this->lastContents = $contents; diff --git a/inc/config/class_FrameworkConfiguration.php b/inc/config/class_FrameworkConfiguration.php index 957a3537d..fc920402b 100644 --- a/inc/config/class_FrameworkConfiguration.php +++ b/inc/config/class_FrameworkConfiguration.php @@ -116,7 +116,7 @@ class FrameworkConfiguration { /** * A private include loader * - * @param $arrayObject The array object with all include files + * @param $arrayObject The array object with all include files * @return void */ private function loadIncludes (ArrayObject $arrayObject) { @@ -212,7 +212,7 @@ class FrameworkConfiguration { // Is a valid configuration entry provided? if (empty($cfgEntry)) { // Entry is empty - throw new ConfigEntryIsEmptyException(__CLASS__, self::EXCEPTION_CONFIG_ENTRY_IS_EMPTY); + throw new ConfigEntryIsEmptyException($this, self::EXCEPTION_CONFIG_ENTRY_IS_EMPTY); } elseif (!isset($this->config[$cfgEntry])) { // Entry was not found! throw new ConfigEntryNotFoundException(array(__CLASS__, $cfgEntry), self::EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND); @@ -243,7 +243,7 @@ class FrameworkConfiguration { // Is a valid configuration entry provided? if (empty($cfgEntry)) { // Entry is empty - throw new ConfigEntryIsEmptyException(__CLASS__, self::EXCEPTION_CONFIG_ENTRY_IS_EMPTY); + throw new ConfigEntryIsEmptyException($this, self::EXCEPTION_CONFIG_ENTRY_IS_EMPTY); } // Set the configuration value diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php new file mode 100644 index 000000000..84b245d49 --- /dev/null +++ b/tests/ConfigTest.php @@ -0,0 +1,142 @@ +setConfigEntry("default_application", "ship-simu"); + +// Set testing mode (no starter.php will be loaded!) +define('TEST_MODE', true); + +// Load the PHPUnit framework +require_once("PHPUnit/Framework.php"); + +/** + * A test case for the configuration sub system + * + */ +class ConfigTest extends PHPUnit_Framework_TestCase { + /** + * Tests the exception ConfigEntryNotFoundException if it is being thrown. + * Else the test shall fail + * + * @return void + */ + public function testConfigEntryNotFoundException () { + // Now try the test + $testPassed = false; + try { + // Get a configuration instance + $cfg = FrameworkConfiguration::getInstance(); + + // Now find a configuration variable that does not exist + $dummy = $cfg->readConfig("does_not_exist"); + } catch (ConfigEntryNotFoundException $expected) { + // This exception was expected, so it is fine + $testPassed = true; + } catch (FrameworkException $unexptected) { + // This exception was not expected! + $this->fail(sprintf("Unexpected exception %s detected.", $unexpected->__toString())); + } + + if (!$testPassed) { + // This test went wrong + $this->fail("Test of thrown exception ConfigEntryNotFoundException failed!"); + } + } + + /** + * Tests if the exception ConfigEntryIsEmptyException is being thrown by + * "emulating" an empty configuration variable. + * + * @return void + */ + public function testConfigEntryIsEmptyExceptionRead () { + // Now try the test + $testPassed = false; + try { + // Get a configuration instance + $cfg = FrameworkConfiguration::getInstance(); + + // Try to read an empty configuration variable + $dummy = $cfg->readConfig(""); + } catch (ConfigEntryIsEmptyException $expected) { + // This exception was expected, so it is fine + $testPassed = true; + } catch (FrameworkException $unexptected) { + // This exception was not expected! + $this->fail(sprintf("Unexpected exception %s detected.", $unexpected->__toString())); + } + + if (!$testPassed) { + // This test went wrong + $this->fail("Test of thrown exception ConfigEntryIsEmptyException failed!"); + } + } + + /** + * Tests if the exception ConfigEntryIsEmptyException is being thrown by + * "emulating" an empty configuration variable. + * + * @return void + */ + public function testConfigEntryIsEmptyExceptionWrite () { + // Now try the test + $testPassed = false; + try { + // Get a configuration instance + $cfg = FrameworkConfiguration::getInstance(); + + // Try to read an empty configuration variable + $cfg->setConfigEntry("", "will_never_be_set"); + } catch (ConfigEntryIsEmptyException $expected) { + // This exception was expected, so it is fine + $testPassed = true; + } catch (FrameworkException $unexptected) { + // This exception was not expected! + $this->fail(sprintf("Unexpected exception %s detected.", $unexpected->__toString())); + } + + if (!$testPassed) { + // This test went wrong + $this->fail("Test of thrown exception ConfigEntryIsEmptyException failed!"); + } + } + + /** + * Tests if a set value can be returned from the configuration sub-system + * + * @return void + */ + public function testWriteReadConfigEntry () { + // Try it here + $value = "This is a test value"; + try { + // Get a configuration instance + $cfg = FrameworkConfiguration::getInstance(); + + // Try to read an empty configuration variable + $cfg->setConfigEntry("test_key", "{$value}"); + + // Read the config entry we have just written + $readValue = $cfg->readConfig("test_key"); + + // Now test the values + $this->assertEquals($value, $readValue); + } catch (FrameworkException $unexptected) { + // This exception was not expected! + $this->fail(sprintf("Unexpected exception %s detected.", $unexpected->__toString())); + } + } +} + +?> -- 2.39.5