No controller instances are now passed to filters
[core.git] / tests / ConfigTest.php
1 <?php
2 print (basename(__FILE__).": Init...\n");
3
4 // Change directory
5 @chdir("..");
6
7 // Load config file
8 require(dirname(dirname(__FILE__)) . '/inc/config.php');
9
10 // Load all include files
11 require($cfg->readConfig('base_path') . 'inc/includes.php');
12
13 // Load all game classes
14 require($cfg->readConfig('base_path') . 'inc/classes.php');
15
16 // Set default application
17 FrameworkConfiguration::getInstance()->setConfigEntry('default_application', 'ship-simu');
18
19 // Set testing mode (no starter.php will be loaded!)
20 define('TEST_MODE', true);
21
22 // Load the PHPUnit framework
23 require_once('PHPUnit/Framework.php');
24
25 print (basename(__FILE__).": Init completed.\n\n");
26
27 /**
28  * A test case for the configuration sub system
29  *
30  * @author              Roland Haeder <webmaster@ship-simu.org>
31  * @version             0.0.0
32  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, this is free software
33  * @license             GNU GPL 3.0 or any newer version
34  * @link                http://www.ship-simu.org
35  * @see                 http://www.phpunit.de
36  *
37  * This program is free software: you can redistribute it and/or modify
38  * it under the terms of the GNU General Public License as published by
39  * the Free Software Foundation, either version 3 of the License, or
40  * (at your option) any later version.
41  *
42  * This program is distributed in the hope that it will be useful,
43  * but WITHOUT ANY WARRANTY; without even the implied warranty of
44  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
45  * GNU General Public License for more details.
46  *
47  * You should have received a copy of the GNU General Public License
48  * along with this program. If not, see <http://www.gnu.org/licenses/>.
49  */
50 class ConfigTest extends PHPUnit_Framework_TestCase {
51         /**
52          * Tests the exception ConfigEntryNotFoundException if it is being thrown.
53          * Else the test shall fail
54          *
55          * @return      void
56          */
57         public function testConfigEntryNotFoundException () {
58                 // Now try the test
59                 $testPassed = false;
60                 try {
61                         // Get a configuration instance
62                         $cfg = FrameworkConfiguration::getInstance();
63
64                         // Now find a configuration variable that does not exist
65                         $dummy = $cfg->readConfig('does_not_exist');
66                 } catch (ConfigEntryNotFoundException $expected) {
67                         // This exception was expected, so it is fine
68                         $testPassed = true;
69                 } catch (FrameworkException $unexptected) {
70                         // This exception was not expected!
71                         $this->fail(sprintf("Unexpected exception %s detected.", $unexpected->__toString()));
72                 }
73
74                 if (!$testPassed) {
75                         // This test went wrong
76                         $this->fail("Test of thrown exception ConfigEntryNotFoundException failed!");
77                 }
78         }
79
80         /**
81          * Tests if the exception ConfigEntryIsEmptyException is being thrown by
82          * "emulating" an empty configuration variable.
83          *
84          * @return      void
85          */
86         public function testConfigEntryIsEmptyExceptionRead () {
87                 // Now try the test
88                 $testPassed = false;
89                 try {
90                         // Get a configuration instance
91                         $cfg = FrameworkConfiguration::getInstance();
92
93                         // Try to read an empty configuration variable
94                         $dummy = $cfg->readConfig("");
95                 } catch (ConfigEntryIsEmptyException $expected) {
96                         // This exception was expected, so it is fine
97                         $testPassed = true;
98                 } catch (FrameworkException $unexptected) {
99                         // This exception was not expected!
100                         $this->fail(sprintf("Unexpected exception %s detected.", $unexpected->__toString()));
101                 }
102
103                 if (!$testPassed) {
104                         // This test went wrong
105                         $this->fail("Test of thrown exception ConfigEntryIsEmptyException failed!");
106                 }
107         }
108
109         /**
110          * Tests if the exception ConfigEntryIsEmptyException is being thrown by
111          * "emulating" an empty configuration variable.
112          *
113          * @return      void
114          */
115         public function testConfigEntryIsEmptyExceptionWrite () {
116                 // Now try the test
117                 $testPassed = false;
118                 try {
119                         // Get a configuration instance
120                         $cfg = FrameworkConfiguration::getInstance();
121
122                         // Try to read an empty configuration variable
123                         $cfg->setConfigEntry("", 'will_never_be_set');
124                 } catch (ConfigEntryIsEmptyException $expected) {
125                         // This exception was expected, so it is fine
126                         $testPassed = true;
127                 } catch (FrameworkException $unexptected) {
128                         // This exception was not expected!
129                         $this->fail(sprintf("Unexpected exception %s detected.", $unexpected->__toString()));
130                 }
131
132                 if (!$testPassed) {
133                         // This test went wrong
134                         $this->fail("Test of thrown exception ConfigEntryIsEmptyException failed!");
135                 }
136         }
137
138         /**
139          * Tests if a set value can be returned from the configuration sub-system
140          *
141          * @return      void
142          */
143         public function testWriteReadConfigEntry () {
144                 // Try it here
145                 $value = "This is a test value";
146                 try {
147                         // Get a configuration instance
148                         $cfg = FrameworkConfiguration::getInstance();
149
150                         // Try to read an empty configuration variable
151                         $cfg->setConfigEntry('test_key', "{$value}");
152
153                         // Read the config entry we have just written
154                         $readValue = $cfg->readConfig('test_key');
155
156                         // Now test the values
157                         $this->assertEquals($value, $readValue);
158                 } catch (FrameworkException $unexptected) {
159                         // This exception was not expected!
160                         $this->fail(sprintf("Unexpected exception %s detected.", $unexpected->__toString()));
161                 }
162         }
163 }
164
165 ?>