Nope, this will add hashes to the list ...
[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__)) . '/framework/config.php';
9
10 // Load all include files
11 require $cfg->getConfigEntry('framework_base_path') . 'includes.php';
12
13 // Load all game classes
14 require $cfg->getConfigEntry('framework_base_path') . 'classes.php';
15
16 // Set default application
17 FrameworkConfiguration::getInstance()->setConfigEntry('default_application', 'shipsimu');
18
19 // Set testing mode (no starter.php will be loaded!)
20 define('TEST_MODE', true);
21
22 // Load the PHPUnit framework
23 require '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@shipsimu.org>
31  * @version             0.0.0
32  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
33  * @license             GNU GPL 3.0 or any newer version
34  * @link                http://www.shipsimu.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                         // Now find a configuration variable that does not exist
62                         $dummy = FrameworkConfiguration::getInstance()->getConfigEntry('does_not_exist');
63                 } catch (ConfigEntryNotFoundException $expected) {
64                         // This exception was expected, so it is fine
65                         $testPassed = true;
66                 } catch (FrameworkException $unexptected) {
67                         // This exception was not expected!
68                         $this->fail(sprintf("Unexpected exception %s detected.", $unexpected->__toString()));
69                 }
70
71                 if (!$testPassed) {
72                         // This test went wrong
73                         $this->fail("Test of thrown exception ConfigEntryNotFoundException failed!");
74                 }
75         }
76
77         /**
78          * Tests if the exception ConfigEntryIsEmptyException is being thrown by
79          * "emulating" an empty configuration variable.
80          *
81          * @return      void
82          */
83         public function testConfigEntryIsEmptyExceptionRead () {
84                 // Now try the test
85                 $testPassed = false;
86                 try {
87                         // Try to read an empty configuration variable
88                         $dummy = FrameworkConfiguration::getInstance()->getConfigEntry('');
89                 } catch (ConfigEntryIsEmptyException $expected) {
90                         // This exception was expected, so it is fine
91                         $testPassed = true;
92                 } catch (FrameworkException $unexptected) {
93                         // This exception was not expected!
94                         $this->fail(sprintf("Unexpected exception %s detected.", $unexpected->__toString()));
95                 }
96
97                 if (!$testPassed) {
98                         // This test went wrong
99                         $this->fail("Test of thrown exception ConfigEntryIsEmptyException failed!");
100                 }
101         }
102
103         /**
104          * Tests if the exception ConfigEntryIsEmptyException is being thrown by
105          * "emulating" an empty configuration variable.
106          *
107          * @return      void
108          */
109         public function testConfigEntryIsEmptyExceptionWrite () {
110                 // Now try the test
111                 $testPassed = false;
112                 try {
113                         // Try to read an empty configuration variable
114                         FrameworkConfiguration::getInstance()->setConfigEntry('', 'will_never_be_set');
115                 } catch (ConfigEntryIsEmptyException $expected) {
116                         // This exception was expected, so it is fine
117                         $testPassed = true;
118                 } catch (FrameworkException $unexptected) {
119                         // This exception was not expected!
120                         $this->fail(sprintf("Unexpected exception %s detected.", $unexpected->__toString()));
121                 }
122
123                 if (!$testPassed) {
124                         // This test went wrong
125                         $this->fail("Test of thrown exception ConfigEntryIsEmptyException failed!");
126                 }
127         }
128
129         /**
130          * Tests if a set value can be returned from the configuration sub-system
131          *
132          * @return      void
133          */
134         public function testWritegetConfigEntryEntry () {
135                 // Try it here
136                 $value = "This is a test value";
137                 try {
138                         // Try to read an empty configuration variable
139                         FrameworkConfiguration::getInstance()->setConfigEntry('test_key', "{$value}");
140
141                         // Read the config entry we have just written
142                         $readValue = FrameworkConfiguration::getInstance()->getConfigEntry('test_key');
143
144                         // Now test the values
145                         $this->assertEquals($value, $readValue);
146                 } catch (FrameworkException $unexptected) {
147                         // This exception was not expected!
148                         $this->fail(sprintf("Unexpected exception %s detected.", $unexpected->__toString()));
149                 }
150         }
151 }