]> git.mxchange.org Git - shipsimu.git/blobdiff - tests/ConfigTest.php
first unit test for configuration sub system added
[shipsimu.git] / tests / ConfigTest.php
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()));
+               }
+       }
+}
+
+?>