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