]> git.mxchange.org Git - core.git/blob - tests/framework/config/FrameworkConfigurationTest.php
Continued:
[core.git] / tests / framework / config / FrameworkConfigurationTest.php
1 <?php
2 // Same namespace as target class
3 namespace Org\Mxchange\CoreFramework\Configuration;
4
5 // Inport framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Configuration\FrameworkConfiguration;
8 use Org\Mxchange\CoreFramework\Configuration\NoConfigEntryException;
9 use Org\Mxchange\CoreFramework\Loader\ClassLoader;
10 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
11 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
12
13 // Import PHPUnit stuff
14 use PHPUnit\Framework\Error\Notice;
15 use PHPUnit\Framework\TestCase;
16
17 // Import SPL stuff
18 use \InvalidArgumentException;
19
20 /*
21  * Copyright (C) 2017 - 2020 - Core Developer Team
22  *
23  * This program is free software: you can redistribute it and/or modify
24  * it under the terms of the GNU General Public License as published by
25  * the Free Software Foundation, either version 3 of the License, or
26  * (at your option) any later version.
27  *
28  * This program is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31  * GNU General Public License for more details.
32  *
33  * You should have received a copy of the GNU General Public License
34  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35  */
36 class FrameworkConfigurationTest extends TestCase {
37
38         /**
39          * The configuration instance being tested
40          */
41         private static $configInstance = NULL;
42
43         /**
44          * Setup test case
45          */
46         public function setUp() {
47                 // Trace message
48                 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
49
50                 // Call parent method
51                 parent::setUp();
52
53                 // Trace message
54                 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
55
56         }
57
58         /**
59          * Setup test case
60          */
61         public static function setUpBeforeClass() {
62                 // Trace message
63                 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
64
65                 // Call parent method
66                 parent::setUpBeforeClass();
67
68                 // Init instance
69                 self::$configInstance = FrameworkBootstrap::getConfigurationInstance();
70
71                 /*
72                  * Disable strict naming-convention check in own class loader, because
73                  * PHP_Invoker doesn't have namespaces.
74                  */
75                 ClassLoader::enableStrictNamingConventionCheck(FALSE);
76
77                 // Trace message
78                 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
79         }
80
81         /**
82          * Tests if __toString() returns proper name
83          */
84         public function testConfigToStringClassName () {
85                 // Get it
86                 $className = self::$configInstance->__toString();
87
88                 // Should be equal
89                 $this->assertEquals('Org\Mxchange\CoreFramework\Configuration\FrameworkConfiguration', $className);
90         }
91
92         /**
93          * Tests getting a singleton instance
94          */
95         public function testGettingSelfConfigInstance () {
96                 // Get instance
97                 $dummyInstance = FrameworkBootstrap::getConfigurationInstance();
98
99                 // Should be equal to own instance
100                 $this->assertEquals(self::$configInstance, $dummyInstance);
101         }
102
103         /**
104          * Tests equals() method
105          */
106         public function testEqualsConfigInstance () {
107                 // Get instance
108                 $dummyInstance = new FrameworkConfiguration();
109
110                 // Should return TRUE
111                 $this->assertTrue(self::$configInstance->equals($dummyInstance));
112         }
113
114         /**
115          * Tests hashCode() method
116          */
117         public function testHashCodeConfigInstance () {
118                 // Get instance
119                 $dummyInstance = FrameworkBootstrap::getConfigurationInstance();
120
121                 // Get hash code from both
122                 $hashCodeExpected = self::$configInstance->hashCode();
123                 $hashCodeActual = $dummyInstance->hashCode();
124
125                 // Should be equal
126                 $this->assertEquals($hashCodeExpected, $hashCodeActual);
127         }
128
129         /**
130          * Tests if getting configuration entry returns an array
131          */
132         public function testGettingConfigurationArray () {
133                 // Get it
134                 $config = self::$configInstance->getConfigurationArray();
135
136                 // Should be an array
137                 $this->assertTrue(is_array($config));
138         }
139
140         /**
141          * Tests if getting configuration entry returns a filled array
142          */
143         public function testGettingfilledConfigurationArray () {
144                 // Get it
145                 $config = self::$configInstance->getConfigurationArray();
146
147                 // Should be an array
148                 $this->assertTrue(count($config) > 0);
149         }
150
151         /**
152          * Tests if getting whole configuration entry is the same for another
153          * singleton instance
154          */
155         public function testSameConfigurationArrayGetter () {
156                 // Get instance
157                 $dummyInstance = FrameworkBootstrap::getConfigurationInstance();
158
159                 // Get it from both instances
160                 $config1 = self::$configInstance->getConfigurationArray();
161                 $config2 = $dummyInstance->getConfigurationArray();
162
163                 // Should be the same
164                 $this->assertEquals($config1, $config2);
165         }
166
167         /**
168          * Tests if a proper exception is thrown when checking an empty key
169          */
170         public function testCheckingEmptyConfigKey () {
171                 // Will throw this exception
172                 $this->expectException(InvalidArgumentException::class);
173
174                 // Test it
175                 $dummy = self::$configInstance->isConfigurationEntrySet('');
176         }
177
178         /**
179          * Tests if checking an existing (well-known) key can be found and returns
180          * TRUE.
181          */
182         public function testCheckingExistingConfigKey () {
183                 // Should return TRUE
184                 $this->assertTrue(self::$configInstance->isConfigurationEntrySet('application_base_path'));
185         }
186
187         /**
188          * Tests if checking a non-existing key will return FALSE.
189          */
190         public function testCheckingNonExistingConfigKey () {
191                 // Should return FALSE
192                 $this->assertFalse(self::$configInstance->isConfigurationEntrySet('__non_existing_key__'));
193         }
194
195         /**
196          * Tests if a proper exception is thrown when getting an empty key
197          */
198         public function testGettingEmptyConfigKey () {
199                 // Will throw this exception
200                 $this->expectException(InvalidArgumentException::class);
201
202                 // Test it
203                 $dummy = self::$configInstance->getConfigEntry('');
204         }
205
206         /**
207          * Tests if getting a non-existing key will cause a proper exception been
208          * thrown.
209          */
210         public function testGettingNonExistingConfigKey () {
211                 // Should cause this exception
212                 $this->expectException(NoConfigEntryException::class);
213
214                 // Get non-existing key
215                 $dummy = self::$configInstance->getConfigEntry('__non_existing_key__');
216         }
217
218         /**
219          * Tests if a generic key 'application_base_path' can be found.
220          */
221         public function testGettingConfigKeyApplicationBasePathExists () {
222                 // Get it from config instance
223                 $value = self::$configInstance->getConfigEntry('application_base_path');
224
225                 // Is it a readable path?
226                 $this->assertDirectoryIsReadable($value);
227         }
228
229         /**
230          * Tests setting an empty key (value doesn't matter)
231          */
232         public function testSettingEmptyConfigKey () {
233                 // Will throw this exception
234                 $this->expectException(InvalidArgumentException::class);
235
236                 // Test it
237                 self::$configInstance->setConfigEntry('', 'foo');
238         }
239
240         /**
241          * Tests setting a valid key but array for value
242          */
243         public function testSettingArrayValueConfigKey () {
244                 // Will throw this exception
245                 $this->expectException(InvalidArgumentException::class);
246
247                 // Test it
248                 self::$configInstance->setConfigEntry('foo', array());
249         }
250
251         /**
252          * Tests setting a valid key but object for value
253          */
254         public function testSettingObjectValueConfigKey () {
255                 // Will throw this exception
256                 $this->expectException(InvalidArgumentException::class);
257
258                 // Test it
259                 self::$configInstance->setConfigEntry('foo', $this);
260         }
261
262         /**
263          * Tests setting a valid key but resource for value
264          */
265         public function testSettingResourceValueConfigKey () {
266                 // Will throw this exception
267                 $this->expectException(InvalidArgumentException::class);
268
269                 // Init some resource
270                 $resource = fopen(__FILE__, 'r');
271
272                 // Test it
273                 self::$configInstance->setConfigEntry('foo', $resource);
274         }
275
276         /**
277          * Tests unsetting an empty key
278          */
279         public function testUnettingEmptyConfigKey () {
280                 // Will throw this exception
281                 $this->expectException(InvalidArgumentException::class);
282
283                 // Test it
284                 self::$configInstance->unsetConfigEntry('');
285         }
286
287         /**
288          * Tests unsetting a non-existing key
289          */
290         public function testUnettingNonExistingConfigKey () {
291                 // Will throw this exception
292                 $this->expectException(NoConfigEntryException::class);
293
294                 // Test it
295                 self::$configInstance->unsetConfigEntry('__non_existing_key__');
296         }
297
298         /**
299          * Tests setting and getting a key will return same value of same type
300          * (NULL). This unit test does also unset the then existing key.
301          */
302         public function testSettingGettiingNullConfigKey () {
303                 // Set test value
304                 self::$configInstance->setConfigEntry('__test_key', NULL);
305
306                 // Get it
307                 $value = self::$configInstance->getConfigEntry('__test_key');
308
309                 // Must be equal
310                 $this->assertEquals(NULL, $value);
311
312                 // Unset it
313                 self::$configInstance->unsetConfigEntry('__test_key');
314         }
315
316         /**
317          * Tests setting and getting a key will return same value of same type.
318          * This unit test does also unset the then existing key.
319          */
320         public function testSettingGettiingConfigKey () {
321                 // Set test value
322                 self::$configInstance->setConfigEntry('__test_key', 'foo');
323
324                 // Get it
325                 $value = self::$configInstance->getConfigEntry('__test_key');
326
327                 // Must be equal
328                 $this->assertEquals('foo', $value);
329
330                 // Unset it
331                 self::$configInstance->unsetConfigEntry('__test_key');
332         }
333
334         /**
335          * Tests if the method getField() is still unsupported in this class. Please
336          * note, that this and isFieldSet() may get removed in the future. So also
337          * these test methods will be gone.
338          */
339         public function testConfigGetFieldUnsupported () {
340                 // Expect this exception
341                 $this->expectException(UnsupportedOperationException::class);
342
343                 // Test it
344                 $dummy = self::$configInstance->getField('foo');
345         }
346
347         /**
348          * Tests if the method isFieldSet() is still unsupported in this class. Please
349          * note, that this and getField() may get removed in the future. So also
350          * these test methods will be gone.
351          */
352         public function testConfigIsFieldSetUnsupported () {
353                 // Expect this exception
354                 $this->expectException(UnsupportedOperationException::class);
355
356                 // Test it
357                 $dummy = self::$configInstance->isFieldSet('foo');
358         }
359
360 }