Comment header fixed
[shipsimu.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  * @author              Roland Haeder <webmaster@ship-simu.org>
27  * @version             0.0.0
28  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
29  * @license             GNU GPL 3.0 or any newer version
30  * @link                http://www.ship-simu.org
31  * @see                 http://www.phpunit.de
32  *
33  * This program is free software: you can redistribute it and/or modify
34  * it under the terms of the GNU General Public License as published by
35  * the Free Software Foundation, either version 3 of the License, or
36  * (at your option) any later version.
37  *
38  * This program is distributed in the hope that it will be useful,
39  * but WITHOUT ANY WARRANTY; without even the implied warranty of
40  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
41  * GNU General Public License for more details.
42  *
43  * You should have received a copy of the GNU General Public License
44  * along with this program. If not, see <http://www.gnu.org/licenses/>.
45  */
46 class ConfigTest extends PHPUnit_Framework_TestCase {
47         /**
48          * Tests the exception ConfigEntryNotFoundException if it is being thrown.
49          * Else the test shall fail
50          *
51          * @return      void
52          */
53         public function testConfigEntryNotFoundException () {
54                 // Now try the test
55                 $testPassed = false;
56                 try {
57                         // Get a configuration instance
58                         $cfg = FrameworkConfiguration::getInstance();
59
60                         // Now find a configuration variable that does not exist
61                         $dummy = $cfg->readConfig('does_not_exist');
62                 } catch (ConfigEntryNotFoundException $expected) {
63                         // This exception was expected, so it is fine
64                         $testPassed = true;
65                 } catch (FrameworkException $unexptected) {
66                         // This exception was not expected!
67                         $this->fail(sprintf("Unexpected exception %s detected.", $unexpected->__toString()));
68                 }
69
70                 if (!$testPassed) {
71                         // This test went wrong
72                         $this->fail("Test of thrown exception ConfigEntryNotFoundException failed!");
73                 }
74         }
75
76         /**
77          * Tests if the exception ConfigEntryIsEmptyException is being thrown by
78          * "emulating" an empty configuration variable.
79          *
80          * @return      void
81          */
82         public function testConfigEntryIsEmptyExceptionRead () {
83                 // Now try the test
84                 $testPassed = false;
85                 try {
86                         // Get a configuration instance
87                         $cfg = FrameworkConfiguration::getInstance();
88
89                         // Try to read an empty configuration variable
90                         $dummy = $cfg->readConfig("");
91                 } catch (ConfigEntryIsEmptyException $expected) {
92                         // This exception was expected, so it is fine
93                         $testPassed = true;
94                 } catch (FrameworkException $unexptected) {
95                         // This exception was not expected!
96                         $this->fail(sprintf("Unexpected exception %s detected.", $unexpected->__toString()));
97                 }
98
99                 if (!$testPassed) {
100                         // This test went wrong
101                         $this->fail("Test of thrown exception ConfigEntryIsEmptyException failed!");
102                 }
103         }
104
105         /**
106          * Tests if the exception ConfigEntryIsEmptyException is being thrown by
107          * "emulating" an empty configuration variable.
108          *
109          * @return      void
110          */
111         public function testConfigEntryIsEmptyExceptionWrite () {
112                 // Now try the test
113                 $testPassed = false;
114                 try {
115                         // Get a configuration instance
116                         $cfg = FrameworkConfiguration::getInstance();
117
118                         // Try to read an empty configuration variable
119                         $cfg->setConfigEntry("", 'will_never_be_set');
120                 } catch (ConfigEntryIsEmptyException $expected) {
121                         // This exception was expected, so it is fine
122                         $testPassed = true;
123                 } catch (FrameworkException $unexptected) {
124                         // This exception was not expected!
125                         $this->fail(sprintf("Unexpected exception %s detected.", $unexpected->__toString()));
126                 }
127
128                 if (!$testPassed) {
129                         // This test went wrong
130                         $this->fail("Test of thrown exception ConfigEntryIsEmptyException failed!");
131                 }
132         }
133
134         /**
135          * Tests if a set value can be returned from the configuration sub-system
136          *
137          * @return      void
138          */
139         public function testWriteReadConfigEntry () {
140                 // Try it here
141                 $value = "This is a test value";
142                 try {
143                         // Get a configuration instance
144                         $cfg = FrameworkConfiguration::getInstance();
145
146                         // Try to read an empty configuration variable
147                         $cfg->setConfigEntry('test_key', "{$value}");
148
149                         // Read the config entry we have just written
150                         $readValue = $cfg->readConfig('test_key');
151
152                         // Now test the values
153                         $this->assertEquals($value, $readValue);
154                 } catch (FrameworkException $unexptected) {
155                         // This exception was not expected!
156                         $this->fail(sprintf("Unexpected exception %s detected.", $unexpected->__toString()));
157                 }
158         }
159 }
160
161 ?>