Code merge from latest Ship-Simu code
[mailer.git] / tests / ConfigTest.php
1 <?php
2 // Change directory
3 @chdir("..");
4
5 // Load config file
6 require(dirname(dirname(__FILE__)) . "/inc/config.php");
7
8 // Load all include files
9 require(PATH . "inc/includes.php");
10
11 // Load all game classes
12 require(PATH . "inc/classes.php");
13
14 // Set default application
15 FrameworkConfiguration::getInstance()->setConfigEntry("default_application", "ship-simu");
16
17 // Set testing mode (no starter.php will be loaded!)
18 define('TEST_MODE', true);
19
20 // Load the PHPUnit framework
21 require_once("PHPUnit/Framework.php");
22
23 /**
24  * A test case for the configuration sub system
25  *
26  */
27 class ConfigTest extends PHPUnit_Framework_TestCase {
28         /**
29          * Tests the exception ConfigEntryNotFoundException if it is being thrown.
30          * Else the test shall fail
31          *
32          * @return      void
33          */
34         public function testConfigEntryNotFoundException () {
35                 // Now try the test
36                 $testPassed = false;
37                 try {
38                         // Get a configuration instance
39                         $cfg = FrameworkConfiguration::getInstance();
40
41                         // Now find a configuration variable that does not exist
42                         $dummy = $cfg->readConfig("does_not_exist");
43                 } catch (ConfigEntryNotFoundException $expected) {
44                         // This exception was expected, so it is fine
45                         $testPassed = true;
46                 } catch (FrameworkException $unexptected) {
47                         // This exception was not expected!
48                         $this->fail(sprintf("Unexpected exception %s detected.", $unexpected->__toString()));
49                 }
50
51                 if (!$testPassed) {
52                         // This test went wrong
53                         $this->fail("Test of thrown exception ConfigEntryNotFoundException failed!");
54                 }
55         }
56
57         /**
58          * Tests if the exception ConfigEntryIsEmptyException is being thrown by
59          * "emulating" an empty configuration variable.
60          *
61          * @return      void
62          */
63         public function testConfigEntryIsEmptyExceptionRead () {
64                 // Now try the test
65                 $testPassed = false;
66                 try {
67                         // Get a configuration instance
68                         $cfg = FrameworkConfiguration::getInstance();
69
70                         // Try to read an empty configuration variable
71                         $dummy = $cfg->readConfig("");
72                 } catch (ConfigEntryIsEmptyException $expected) {
73                         // This exception was expected, so it is fine
74                         $testPassed = true;
75                 } catch (FrameworkException $unexptected) {
76                         // This exception was not expected!
77                         $this->fail(sprintf("Unexpected exception %s detected.", $unexpected->__toString()));
78                 }
79
80                 if (!$testPassed) {
81                         // This test went wrong
82                         $this->fail("Test of thrown exception ConfigEntryIsEmptyException failed!");
83                 }
84         }
85
86         /**
87          * Tests if the exception ConfigEntryIsEmptyException is being thrown by
88          * "emulating" an empty configuration variable.
89          *
90          * @return      void
91          */
92         public function testConfigEntryIsEmptyExceptionWrite () {
93                 // Now try the test
94                 $testPassed = false;
95                 try {
96                         // Get a configuration instance
97                         $cfg = FrameworkConfiguration::getInstance();
98
99                         // Try to read an empty configuration variable
100                         $cfg->setConfigEntry("", "will_never_be_set");
101                 } catch (ConfigEntryIsEmptyException $expected) {
102                         // This exception was expected, so it is fine
103                         $testPassed = true;
104                 } catch (FrameworkException $unexptected) {
105                         // This exception was not expected!
106                         $this->fail(sprintf("Unexpected exception %s detected.", $unexpected->__toString()));
107                 }
108
109                 if (!$testPassed) {
110                         // This test went wrong
111                         $this->fail("Test of thrown exception ConfigEntryIsEmptyException failed!");
112                 }
113         }
114
115         /**
116          * Tests if a set value can be returned from the configuration sub-system
117          *
118          * @return      void
119          */
120         public function testWriteReadConfigEntry () {
121                 // Try it here
122                 $value = "This is a test value";
123                 try {
124                         // Get a configuration instance
125                         $cfg = FrameworkConfiguration::getInstance();
126
127                         // Try to read an empty configuration variable
128                         $cfg->setConfigEntry("test_key", "{$value}");
129
130                         // Read the config entry we have just written
131                         $readValue = $cfg->readConfig("test_key");
132
133                         // Now test the values
134                         $this->assertEquals($value, $readValue);
135                 } catch (FrameworkException $unexptected) {
136                         // This exception was not expected!
137                         $this->fail(sprintf("Unexpected exception %s detected.", $unexpected->__toString()));
138                 }
139         }
140 }
141
142 ?>