]> git.mxchange.org Git - shipsimu.git/commitdiff
first unit test for configuration sub system added
authorRoland Häder <roland@mxchange.org>
Sat, 15 Mar 2008 18:23:09 +0000 (18:23 +0000)
committerRoland Häder <roland@mxchange.org>
Sat, 15 Mar 2008 18:23:09 +0000 (18:23 +0000)
.gitattributes
inc/classes/exceptions/main/class_ConfigEntryIsEmptyException.php
inc/config/class_FrameworkConfiguration.php
tests/ConfigTest.php [new file with mode: 0644]
tests/old/contract-test.php
tests/old/loader-test.php
tests/old/personell-test.php

index 93335faaff40a272c47539a1615409df4237cf14..4bc507e14986101e44aafeb7608b381857427d4c 100644 (file)
@@ -279,6 +279,7 @@ 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
 tests/old/contract-test.php -text
 tests/old/loader-test.php -text
 tests/old/personell-test.php -text
index df1891566a156d5365e88a3dcd68f092552b5f21..f16e3658073820bb791f0461263a99cb0b13bcb7 100644 (file)
@@ -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()
                );
 
index 957a3537d86a814ca0fe397a3abfa33971215ca3..fc920402b4dc664576b8886f35829cbb9e0c83f8 100644 (file)
@@ -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 (file)
index 0000000..84b245d
--- /dev/null
@@ -0,0 +1,142 @@
+<?php
+// Change directory
+@chdir("..");
+
+// Load config file
+require(dirname(dirname(__FILE__)) . "/inc/config.php");
+
+// Load all include files
+require(PATH . "inc/includes.php");
+
+// Load all game classes
+require(PATH . "inc/classes.php");
+
+// Set default application
+FrameworkConfiguration::getInstance()->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()));
+               }
+       }
+}
+
+?>
index f948c30ba354fe219f0ae3cd18527d438484a549..db00cedc731f96f8d9e92d469e2dfb237e35875c 100644 (file)
@@ -1,5 +1,4 @@
 <?php
-define('TEST', true);
 // Change directory
 @chdir("../..");
 
@@ -18,9 +17,6 @@ FrameworkConfiguration::getInstance()->setConfigEntry("default_application", "sh
 // Set testing mode (no starter.php will be loaded!)
 define('TEST_MODE', true);
 
-// Load the "selector"
-require(PATH . "inc/selector.php");
-
 // Wir tun hier so, als waere schon das Reederei-Objekt generiert und wir wollen
 // jetzt die Personalliste wiederherstellen
 try {
index 0c648d94fb07be0f03f39c418a2cf226b32ea86c..80b75c5a5a20bd78f69e61650f67288806979a3b 100644 (file)
@@ -1,5 +1,4 @@
 <?php
-define('TEST', true);
 // Change directory
 @chdir("../..");
 
@@ -18,9 +17,6 @@ FrameworkConfiguration::getInstance()->setConfigEntry("default_application", "sh
 // Set testing mode (no starter.php will be loaded!)
 define('TEST_MODE', true);
 
-// Load the "selector"
-require(PATH . "inc/selector.php");
-
 // Wir tun hier so, als waere schon das Reederei-Objekt generiert und wir wollen
 // jetzt die Personalliste wiederherstellen
 try {
index 5a8e604a62cb11b1d2e44fb832894f7ccaa9c8bc..c28823797a0d955303c99fe7ba646fdc5605ee10 100644 (file)
@@ -1,5 +1,4 @@
 <?php
-define('TEST', true);
 // Change directory
 @chdir("../..");
 
@@ -18,9 +17,6 @@ FrameworkConfiguration::getInstance()->setConfigEntry("default_application", "sh
 // Set testing mode (no starter.php will be loaded!)
 define('TEST_MODE', true);
 
-// Load the "selector"
-require(PATH . "inc/selector.php");
-
 // Wir tun hier so, als waere schon das Reederei-Objekt generiert und wir wollen
 // jetzt die Personalliste wiederherstellen
 try {