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;
19 * Copyright (C) 2017 - 2020 - Core Developer Team
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.
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.
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/>.
34 class FrameworkConfigurationTest extends TestCase {
37 * The configuration instance being tested
39 private static $configInstance = NULL;
44 public function setUp() {
46 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
52 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
59 public static function setUpBeforeClass() {
61 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
64 parent::setUpBeforeClass();
67 self::$configInstance = FrameworkBootstrap::getConfigurationInstance();
70 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
74 * Tests if __toString() returns proper name
76 public function testConfigToStringClassName () {
78 $className = self::$configInstance->__toString();
81 $this->assertEquals('Org\Mxchange\CoreFramework\Configuration\FrameworkConfiguration', $className);
85 * Tests getting a singleton instance
87 public function testGettingSelfConfigInstance () {
89 $dummyInstance = FrameworkBootstrap::getConfigurationInstance();
91 // Should be equal to own instance
92 $this->assertEquals(self::$configInstance, $dummyInstance);
96 * Tests equals() method
98 public function testEqualsConfigInstance () {
100 $dummyInstance = new FrameworkConfiguration();
102 // Should return TRUE
103 $this->assertTrue(self::$configInstance->equals($dummyInstance));
107 * Tests hashCode() method
109 public function testHashCodeConfigInstance () {
111 $dummyInstance = FrameworkBootstrap::getConfigurationInstance();
113 // Get hash code from both
114 $hashCodeExpected = self::$configInstance->hashCode();
115 $hashCodeActual = $dummyInstance->hashCode();
118 $this->assertEquals($hashCodeExpected, $hashCodeActual);
122 * Tests if getting configuration entry returns an array
124 public function testGettingConfigurationArray () {
126 $config = self::$configInstance->getConfigurationArray();
128 // Should be an array
129 $this->assertTrue(is_array($config));
133 * Tests if getting configuration entry returns a filled array
135 public function testGettingfilledConfigurationArray () {
137 $config = self::$configInstance->getConfigurationArray();
139 // Should be an array
140 $this->assertTrue(count($config) > 0);
144 * Tests if getting whole configuration entry is the same for another
147 public function testSameConfigurationArrayGetter () {
149 $dummyInstance = FrameworkBootstrap::getConfigurationInstance();
151 // Get it from both instances
152 $config1 = self::$configInstance->getConfigurationArray();
153 $config2 = $dummyInstance->getConfigurationArray();
155 // Should be the same
156 $this->assertEquals($config1, $config2);
160 * Tests if a proper exception is thrown when checking an empty key
162 public function testCheckingEmptyConfigKey () {
163 // Will throw this exception
164 $this->expectException(InvalidArgumentException::class);
167 $dummy = self::$configInstance->isConfigurationEntrySet('');
171 * Tests if checking an existing (well-known) key can be found and returns
174 public function testCheckingExistingConfigKey () {
175 // Should return TRUE
176 $this->assertTrue(self::$configInstance->isConfigurationEntrySet('application_base_path'));
180 * Tests if checking a non-existing key will return FALSE.
182 public function testCheckingNonExistingConfigKey () {
183 // Should return FALSE
184 $this->assertFalse(self::$configInstance->isConfigurationEntrySet('__non_existing_key__'));
188 * Tests if a proper exception is thrown when getting an empty key
190 public function testGettingEmptyConfigKey () {
191 // Will throw this exception
192 $this->expectException(InvalidArgumentException::class);
195 $dummy = self::$configInstance->getConfigEntry('');
199 * Tests if getting a non-existing key will cause a proper exception been
202 public function testGettingNonExistingConfigKey () {
203 // Should cause this exception
204 $this->expectException(NoConfigEntryException::class);
206 // Get non-existing key
207 $dummy = self::$configInstance->getConfigEntry('__non_existing_key__');
211 * Tests if a generic key 'application_base_path' can be found.
213 public function testGettingConfigKeyApplicationBasePathExists () {
214 // Get it from config instance
215 $value = self::$configInstance->getConfigEntry('application_base_path');
217 // Is it a readable path?
218 $this->assertDirectoryIsReadable($value);
222 * Tests setting an empty key (value doesn't matter)
224 public function testSettingEmptyConfigKey () {
225 // Will throw this exception
226 $this->expectException(InvalidArgumentException::class);
229 self::$configInstance->setConfigEntry('', 'foo');
233 * Tests setting a valid key but array for value
235 public function testSettingArrayValueConfigKey () {
236 // Will throw this exception
237 $this->expectException(InvalidArgumentException::class);
240 self::$configInstance->setConfigEntry('foo', array());
244 * Tests setting a valid key but object for value
246 public function testSettingObjectValueConfigKey () {
247 // Will throw this exception
248 $this->expectException(InvalidArgumentException::class);
251 self::$configInstance->setConfigEntry('foo', $this);
255 * Tests setting a valid key but resource for value
257 public function testSettingResourceValueConfigKey () {
258 // Will throw this exception
259 $this->expectException(InvalidArgumentException::class);
261 // Init some resource
262 $resource = fopen(__FILE__, 'r');
265 self::$configInstance->setConfigEntry('foo', $resource);
269 * Tests unsetting an empty key
271 public function testUnettingEmptyConfigKey () {
272 // Will throw this exception
273 $this->expectException(InvalidArgumentException::class);
276 self::$configInstance->unsetConfigEntry('');
280 * Tests unsetting a non-existing key
282 public function testUnettingNonExistingConfigKey () {
283 // Will throw this exception
284 $this->expectException(NoConfigEntryException::class);
287 self::$configInstance->unsetConfigEntry('__non_existing_key__');
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.
294 public function testSettingGettiingNullConfigKey () {
296 self::$configInstance->setConfigEntry('__test_key', NULL);
299 $value = self::$configInstance->getConfigEntry('__test_key');
302 $this->assertEquals(NULL, $value);
305 self::$configInstance->unsetConfigEntry('__test_key');
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.
312 public function testSettingGettiingConfigKey () {
314 self::$configInstance->setConfigEntry('__test_key', 'foo');
317 $value = self::$configInstance->getConfigEntry('__test_key');
320 $this->assertEquals('foo', $value);
323 self::$configInstance->unsetConfigEntry('__test_key');
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.
331 public function testConfigGetFieldUnsupported () {
332 // Expect this exception
333 $this->expectException(UnsupportedOperationException::class);
336 $dummy = self::$configInstance->getField('foo');
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.
344 public function testConfigIsFieldSetUnsupported () {
345 // Expect this exception
346 $this->expectException(UnsupportedOperationException::class);
349 $dummy = self::$configInstance->isFieldSet('foo');