2 // Same namespace as target class
3 namespace Org\Mxchange\CoreFramework\Configuration;
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;
12 // Import PHPUnit stuff
13 use PHPUnit\Framework\TestCase;
16 use \InvalidArgumentException;
17 use \UnexpectedValueException;
20 * Copyright (C) 2017 - 2020 - Core Developer Team
22 * This program is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 class FrameworkConfigurationTest extends TestCase {
38 * The configuration instance being tested
40 private static $configInstance = NULL;
45 public function setUp() {
47 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
53 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
60 public static function setUpBeforeClass() {
62 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
65 parent::setUpBeforeClass();
68 self::$configInstance = FrameworkBootstrap::getConfigurationInstance();
71 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
75 * Tests if __toString() returns proper name
77 public function testConfigToStringClassName () {
79 $className = self::$configInstance->__toString();
82 $this->assertEquals('Org\Mxchange\CoreFramework\Configuration\FrameworkConfiguration', $className);
86 * Tests getting a singleton instance
88 public function testGettingSelfConfigInstance () {
90 $dummyInstance = FrameworkBootstrap::getConfigurationInstance();
92 // Should be equal to own instance
93 $this->assertEquals(self::$configInstance, $dummyInstance);
97 * Tests equals() method
99 public function testEqualsConfigInstance () {
101 $dummyInstance = new FrameworkConfiguration();
103 // Should return TRUE
104 $this->assertTrue(self::$configInstance->equals($dummyInstance));
108 * Tests hashCode() method
110 public function testHashCodeConfigInstance () {
112 $dummyInstance = FrameworkBootstrap::getConfigurationInstance();
114 // Get hash code from both
115 $hashCodeExpected = self::$configInstance->hashCode();
116 $hashCodeActual = $dummyInstance->hashCode();
119 $this->assertEquals($hashCodeExpected, $hashCodeActual);
123 * Tests if getting configuration entry returns an array
125 public function testGettingConfigurationArray () {
127 $config = self::$configInstance->getConfigurationArray();
129 // Should be an array
130 $this->assertTrue(is_array($config));
134 * Tests if getting configuration entry returns a filled array
136 public function testGettingfilledConfigurationArray () {
138 $config = self::$configInstance->getConfigurationArray();
140 // Should be an array
141 $this->assertTrue(count($config) > 0);
145 * Tests if getting whole configuration entry is the same for another
148 public function testSameConfigurationArrayGetter () {
150 $dummyInstance = FrameworkBootstrap::getConfigurationInstance();
152 // Get it from both instances
153 $config1 = self::$configInstance->getConfigurationArray();
154 $config2 = $dummyInstance->getConfigurationArray();
156 // Should be the same
157 $this->assertEquals($config1, $config2);
161 * Tests if a proper exception is thrown when checking an empty key
163 public function testCheckingEmptyConfigKey () {
164 // Will throw this exception
165 $this->expectException(InvalidArgumentException::class);
168 $dummy = self::$configInstance->isConfigurationEntrySet('');
172 * Tests if checking an existing (well-known) key can be found and returns
175 public function testCheckingExistingConfigKey () {
176 // Should return TRUE
177 $this->assertTrue(self::$configInstance->isConfigurationEntrySet('application_base_path'));
181 * Tests if checking a non-existing key will return FALSE.
183 public function testCheckingNonExistingConfigKey () {
184 // Should return FALSE
185 $this->assertFalse(self::$configInstance->isConfigurationEntrySet('__non_existing_key__'));
189 * Tests if a proper exception is thrown when getting an empty key
191 public function testGettingEmptyConfigKey () {
192 // Will throw this exception
193 $this->expectException(InvalidArgumentException::class);
196 $dummy = self::$configInstance->getConfigEntry('');
200 * Tests if getting a non-existing key will cause a proper exception been
203 public function testGettingNonExistingConfigKey () {
204 // Should cause this exception
205 $this->expectException(NoConfigEntryException::class);
207 // Get non-existing key
208 $dummy = self::$configInstance->getConfigEntry('__non_existing_key__');
212 * Tests if a generic key 'application_base_path' can be found.
214 public function testGettingConfigKeyApplicationBasePathExists () {
215 // Get it from config instance
216 $value = self::$configInstance->getConfigEntry('application_base_path');
218 // Is it a readable path?
219 $this->assertDirectoryIsReadable($value);
223 * Tests setting an empty key (value doesn't matter)
225 public function testSettingEmptyConfigKey () {
226 // Will throw this exception
227 $this->expectException(InvalidArgumentException::class);
230 self::$configInstance->setConfigEntry('', 'foo');
234 * Tests setting a valid key but array for value
236 public function testSettingArrayValueConfigKey () {
237 // Will throw this exception
238 $this->expectException(InvalidArgumentException::class);
241 self::$configInstance->setConfigEntry('foo', array());
245 * Tests setting a valid key but object for value
247 public function testSettingObjectValueConfigKey () {
248 // Will throw this exception
249 $this->expectException(InvalidArgumentException::class);
252 self::$configInstance->setConfigEntry('foo', $this);
256 * Tests setting a valid key but resource for value
258 public function testSettingResourceValueConfigKey () {
259 // Will throw this exception
260 $this->expectException(InvalidArgumentException::class);
262 // Init some resource
263 $resource = fopen(__FILE__, 'r');
266 self::$configInstance->setConfigEntry('foo', $resource);
270 * Tests unsetting an empty key
272 public function testUnettingEmptyConfigKey () {
273 // Will throw this exception
274 $this->expectException(InvalidArgumentException::class);
277 self::$configInstance->unsetConfigEntry('');
281 * Tests unsetting a non-existing key
283 public function testUnettingNonExistingConfigKey () {
284 // Will throw this exception
285 $this->expectException(NoConfigEntryException::class);
288 self::$configInstance->unsetConfigEntry('__non_existing_key__');
292 * Tests setting and getting a key will return same value of same type
293 * (NULL). This unit test does also unset the then existing key.
295 public function testSettingGettiingNullConfigKey () {
297 self::$configInstance->setConfigEntry('__test_key', NULL);
300 $value = self::$configInstance->getConfigEntry('__test_key');
303 $this->assertEquals(NULL, $value);
306 self::$configInstance->unsetConfigEntry('__test_key');
310 * Tests setting and getting a key will return same value of same type.
311 * This unit test does also unset the then existing key.
313 public function testSettingGettiingConfigKey () {
315 self::$configInstance->setConfigEntry('__test_key', 'foo');
318 $value = self::$configInstance->getConfigEntry('__test_key');
321 $this->assertEquals('foo', $value);
324 self::$configInstance->unsetConfigEntry('__test_key');
328 * Tests if the method getField() is still unsupported in this class. Please
329 * note, that this and isFieldSet() may get removed in the future. So also
330 * these test methods will be gone.
332 public function testConfigGetFieldUnsupported () {
333 // Expect this exception
334 $this->expectException(UnsupportedOperationException::class);
337 $dummy = self::$configInstance->getField('foo');
341 * Tests if the method isFieldSet() is still unsupported in this class. Please
342 * note, that this and getField() may get removed in the future. So also
343 * these test methods will be gone.
345 public function testConfigIsFieldSetUnsupported () {
346 // Expect this exception
347 $this->expectException(UnsupportedOperationException::class);
350 $dummy = self::$configInstance->isFieldSet('foo');
354 * Tests isEnabled() method being called with empty parameter
356 public function testConfigIsEnabledEmptyString () {
358 $this->expectException(InvalidArgumentException::class);
361 $dummy = self::$configInstance->isEnabled('');
365 * Tests isEnabled() method being called with missing configuration key
367 public function testConfigIsEnabledMissingConfigKey () {
369 $this->expectException(NoConfigEntryException::class);
372 $dummy = self::$configInstance->isEnabled('does_not_exist');
376 * Tests isEnabled() method being called with non-boolean configuration key
378 public function testConfigIsEnabledNonBooleanConfigKey () {
380 $this->expectException(UnexpectedValueException::class);
383 self::$configInstance->setConfigEntry('is_non_boolean_enabled', 'Y');
386 $dummy = self::$configInstance->isEnabled('non_boolean');
390 * Tests isEnabled() method being called with 'single_server'
392 public function testConfigIsEnabledSingleServerConfigKey () {
393 // Check it on known boolean value
394 $this->assertTrue(is_bool(self::$configInstance->isEnabled('single_server')));