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\Loader\ClassLoader;
10 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
11 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
13 // Import PHPUnit stuff
14 use PHPUnit\Framework\Error\Notice;
15 use PHPUnit\Framework\TestCase;
18 use \InvalidArgumentException;
21 * Copyright (C) 2017 - 2020 - Core Developer Team
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.
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.
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/>.
36 class FrameworkConfigurationTest extends TestCase {
39 * The configuration instance being tested
41 private static $configInstance = NULL;
46 public function setUp() {
48 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
54 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
61 public static function setUpBeforeClass() {
63 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
66 parent::setUpBeforeClass();
69 self::$configInstance = FrameworkBootstrap::getConfigurationInstance();
72 * Disable strict naming-convention check in own class loader, because
73 * PHP_Invoker doesn't have namespaces.
75 ClassLoader::enableStrictNamingConventionCheck(FALSE);
78 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
82 * Tests if __toString() returns proper name
84 public function testConfigToStringClassName () {
86 $className = self::$configInstance->__toString();
89 $this->assertEquals('Org\Mxchange\CoreFramework\Configuration\FrameworkConfiguration', $className);
93 * Tests getting a singleton instance
95 public function testGettingSelfConfigInstance () {
97 $dummyInstance = FrameworkBootstrap::getConfigurationInstance();
99 // Should be equal to own instance
100 $this->assertEquals(self::$configInstance, $dummyInstance);
104 * Tests equals() method
106 public function testEqualsConfigInstance () {
108 $dummyInstance = new FrameworkConfiguration();
110 // Should return TRUE
111 $this->assertTrue(self::$configInstance->equals($dummyInstance));
115 * Tests hashCode() method
117 public function testHashCodeConfigInstance () {
119 $dummyInstance = FrameworkBootstrap::getConfigurationInstance();
121 // Get hash code from both
122 $hashCodeExpected = self::$configInstance->hashCode();
123 $hashCodeActual = $dummyInstance->hashCode();
126 $this->assertEquals($hashCodeExpected, $hashCodeActual);
130 * Tests if getting configuration entry returns an array
132 public function testGettingConfigurationArray () {
134 $config = self::$configInstance->getConfigurationArray();
136 // Should be an array
137 $this->assertTrue(is_array($config));
141 * Tests if getting configuration entry returns a filled array
143 public function testGettingfilledConfigurationArray () {
145 $config = self::$configInstance->getConfigurationArray();
147 // Should be an array
148 $this->assertTrue(count($config) > 0);
152 * Tests if getting whole configuration entry is the same for another
155 public function testSameConfigurationArrayGetter () {
157 $dummyInstance = FrameworkBootstrap::getConfigurationInstance();
159 // Get it from both instances
160 $config1 = self::$configInstance->getConfigurationArray();
161 $config2 = $dummyInstance->getConfigurationArray();
163 // Should be the same
164 $this->assertEquals($config1, $config2);
168 * Tests if a proper exception is thrown when checking an empty key
170 public function testCheckingEmptyConfigKey () {
171 // Will throw this exception
172 $this->expectException(InvalidArgumentException::class);
175 $dummy = self::$configInstance->isConfigurationEntrySet('');
179 * Tests if checking an existing (well-known) key can be found and returns
182 public function testCheckingExistingConfigKey () {
183 // Should return TRUE
184 $this->assertTrue(self::$configInstance->isConfigurationEntrySet('application_base_path'));
188 * Tests if checking a non-existing key will return FALSE.
190 public function testCheckingNonExistingConfigKey () {
191 // Should return FALSE
192 $this->assertFalse(self::$configInstance->isConfigurationEntrySet('__non_existing_key__'));
196 * Tests if a proper exception is thrown when getting an empty key
198 public function testGettingEmptyConfigKey () {
199 // Will throw this exception
200 $this->expectException(InvalidArgumentException::class);
203 $dummy = self::$configInstance->getConfigEntry('');
207 * Tests if getting a non-existing key will cause a proper exception been
210 public function testGettingNonExistingConfigKey () {
211 // Should cause this exception
212 $this->expectException(NoConfigEntryException::class);
214 // Get non-existing key
215 $dummy = self::$configInstance->getConfigEntry('__non_existing_key__');
219 * Tests if a generic key 'application_base_path' can be found.
221 public function testGettingConfigKeyApplicationBasePathExists () {
222 // Get it from config instance
223 $value = self::$configInstance->getConfigEntry('application_base_path');
225 // Is it a readable path?
226 $this->assertDirectoryIsReadable($value);
230 * Tests setting an empty key (value doesn't matter)
232 public function testSettingEmptyConfigKey () {
233 // Will throw this exception
234 $this->expectException(InvalidArgumentException::class);
237 self::$configInstance->setConfigEntry('', 'foo');
241 * Tests setting a valid key but array for value
243 public function testSettingArrayValueConfigKey () {
244 // Will throw this exception
245 $this->expectException(InvalidArgumentException::class);
248 self::$configInstance->setConfigEntry('foo', array());
252 * Tests setting a valid key but object for value
254 public function testSettingObjectValueConfigKey () {
255 // Will throw this exception
256 $this->expectException(InvalidArgumentException::class);
259 self::$configInstance->setConfigEntry('foo', $this);
263 * Tests setting a valid key but resource for value
265 public function testSettingResourceValueConfigKey () {
266 // Will throw this exception
267 $this->expectException(InvalidArgumentException::class);
269 // Init some resource
270 $resource = fopen(__FILE__, 'r');
273 self::$configInstance->setConfigEntry('foo', $resource);
277 * Tests unsetting an empty key
279 public function testUnettingEmptyConfigKey () {
280 // Will throw this exception
281 $this->expectException(InvalidArgumentException::class);
284 self::$configInstance->unsetConfigEntry('');
288 * Tests unsetting a non-existing key
290 public function testUnettingNonExistingConfigKey () {
291 // Will throw this exception
292 $this->expectException(NoConfigEntryException::class);
295 self::$configInstance->unsetConfigEntry('__non_existing_key__');
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.
302 public function testSettingGettiingNullConfigKey () {
304 self::$configInstance->setConfigEntry('__test_key', NULL);
307 $value = self::$configInstance->getConfigEntry('__test_key');
310 $this->assertEquals(NULL, $value);
313 self::$configInstance->unsetConfigEntry('__test_key');
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.
320 public function testSettingGettiingConfigKey () {
322 self::$configInstance->setConfigEntry('__test_key', 'foo');
325 $value = self::$configInstance->getConfigEntry('__test_key');
328 $this->assertEquals('foo', $value);
331 self::$configInstance->unsetConfigEntry('__test_key');
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.
339 public function testConfigGetFieldUnsupported () {
340 // Expect this exception
341 $this->expectException(UnsupportedOperationException::class);
344 $dummy = self::$configInstance->getField('foo');
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.
352 public function testConfigIsFieldSetUnsupported () {
353 // Expect this exception
354 $this->expectException(UnsupportedOperationException::class);
357 $dummy = self::$configInstance->isFieldSet('foo');