. */ class FrameworkConfigurationTest extends TestCase { /** * The configuration instance being tested */ private static $configInstance = NULL; /** * Setup test case */ public function setUp() { // Trace message //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__); // Call parent method parent::setUp(); // Trace message //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__); } /** * Setup test case */ public static function setUpBeforeClass() { // Trace message //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__); // Call parent method parent::setUpBeforeClass(); // Init instance self::$configInstance = FrameworkBootstrap::getConfigurationInstance(); // Trace message //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__); } /** * Tests if __toString() returns proper name */ public function testConfigToStringClassName () { // Get it $className = self::$configInstance->__toString(); // Should be equal $this->assertEquals('Org\Mxchange\CoreFramework\Configuration\FrameworkConfiguration', $className); } /** * Tests getting a singleton instance */ public function testGettingSelfConfigInstance () { // Get instance $dummyInstance = FrameworkBootstrap::getConfigurationInstance(); // Should be equal to own instance $this->assertEquals(self::$configInstance, $dummyInstance); } /** * Tests equals() method */ public function testEqualsConfigInstance () { // Get instance $dummyInstance = new FrameworkConfiguration(); // Should return TRUE $this->assertTrue(self::$configInstance->equals($dummyInstance)); } /** * Tests hashCode() method */ public function testHashCodeConfigInstance () { // Get instance $dummyInstance = FrameworkBootstrap::getConfigurationInstance(); // Get hash code from both $hashCodeExpected = self::$configInstance->hashCode(); $hashCodeActual = $dummyInstance->hashCode(); // Should be equal $this->assertEquals($hashCodeExpected, $hashCodeActual); } /** * Tests if getting configuration entry returns an array */ public function testGettingConfigurationArray () { // Get it $config = self::$configInstance->getConfigurationArray(); // Should be an array $this->assertTrue(is_array($config)); } /** * Tests if getting configuration entry returns a filled array */ public function testGettingfilledConfigurationArray () { // Get it $config = self::$configInstance->getConfigurationArray(); // Should be an array $this->assertTrue(count($config) > 0); } /** * Tests if getting whole configuration entry is the same for another * singleton instance */ public function testSameConfigurationArrayGetter () { // Get instance $dummyInstance = FrameworkBootstrap::getConfigurationInstance(); // Get it from both instances $config1 = self::$configInstance->getConfigurationArray(); $config2 = $dummyInstance->getConfigurationArray(); // Should be the same $this->assertEquals($config1, $config2); } /** * Tests if a proper exception is thrown when checking an empty key */ public function testCheckingEmptyConfigKey () { // Will throw this exception $this->expectException(InvalidArgumentException::class); // Test it $dummy = self::$configInstance->isConfigurationEntrySet(''); } /** * Tests if checking an existing (well-known) key can be found and returns * TRUE. */ public function testCheckingExistingConfigKey () { // Should return TRUE $this->assertTrue(self::$configInstance->isConfigurationEntrySet('application_base_path')); } /** * Tests if checking a non-existing key will return FALSE. */ public function testCheckingNonExistingConfigKey () { // Should return FALSE $this->assertFalse(self::$configInstance->isConfigurationEntrySet('__non_existing_key__')); } /** * Tests if a proper exception is thrown when getting an empty key */ public function testGettingEmptyConfigKey () { // Will throw this exception $this->expectException(InvalidArgumentException::class); // Test it $dummy = self::$configInstance->getConfigEntry(''); } /** * Tests if getting a non-existing key will cause a proper exception been * thrown. */ public function testGettingNonExistingConfigKey () { // Should cause this exception $this->expectException(NoConfigEntryException::class); // Get non-existing key $dummy = self::$configInstance->getConfigEntry('__non_existing_key__'); } /** * Tests if a generic key 'application_base_path' can be found. */ public function testGettingConfigKeyApplicationBasePathExists () { // Get it from config instance $value = self::$configInstance->getConfigEntry('application_base_path'); // Is it a readable path? $this->assertDirectoryIsReadable($value); } /** * Tests setting an empty key (value doesn't matter) */ public function testSettingEmptyConfigKey () { // Will throw this exception $this->expectException(InvalidArgumentException::class); // Test it self::$configInstance->setConfigEntry('', 'foo'); } /** * Tests setting a valid key but array for value */ public function testSettingArrayValueConfigKey () { // Will throw this exception $this->expectException(InvalidArgumentException::class); // Test it self::$configInstance->setConfigEntry('foo', array()); } /** * Tests setting a valid key but object for value */ public function testSettingObjectValueConfigKey () { // Will throw this exception $this->expectException(InvalidArgumentException::class); // Test it self::$configInstance->setConfigEntry('foo', $this); } /** * Tests setting a valid key but resource for value */ public function testSettingResourceValueConfigKey () { // Will throw this exception $this->expectException(InvalidArgumentException::class); // Init some resource $resource = fopen(__FILE__, 'r'); // Test it self::$configInstance->setConfigEntry('foo', $resource); } /** * Tests unsetting an empty key */ public function testUnettingEmptyConfigKey () { // Will throw this exception $this->expectException(InvalidArgumentException::class); // Test it self::$configInstance->unsetConfigEntry(''); } /** * Tests unsetting a non-existing key */ public function testUnettingNonExistingConfigKey () { // Will throw this exception $this->expectException(NoConfigEntryException::class); // Test it self::$configInstance->unsetConfigEntry('__non_existing_key__'); } /** * Tests setting and getting a key will return same value of same type * (NULL). This unit test does also unset the then existing key. */ public function testSettingGettiingNullConfigKey () { // Set test value self::$configInstance->setConfigEntry('__test_key', NULL); // Get it $value = self::$configInstance->getConfigEntry('__test_key'); // Must be equal $this->assertEquals(NULL, $value); // Unset it self::$configInstance->unsetConfigEntry('__test_key'); } /** * Tests setting and getting a key will return same value of same type. * This unit test does also unset the then existing key. */ public function testSettingGettiingConfigKey () { // Set test value self::$configInstance->setConfigEntry('__test_key', 'foo'); // Get it $value = self::$configInstance->getConfigEntry('__test_key'); // Must be equal $this->assertEquals('foo', $value); // Unset it self::$configInstance->unsetConfigEntry('__test_key'); } /** * Tests if the method getField() is still unsupported in this class. Please * note, that this and isFieldSet() may get removed in the future. So also * these test methods will be gone. */ public function testConfigGetFieldUnsupported () { // Expect this exception $this->expectException(UnsupportedOperationException::class); // Test it $dummy = self::$configInstance->getField('foo'); } /** * Tests if the method isFieldSet() is still unsupported in this class. Please * note, that this and getField() may get removed in the future. So also * these test methods will be gone. */ public function testConfigIsFieldSetUnsupported () { // Expect this exception $this->expectException(UnsupportedOperationException::class); // Test it $dummy = self::$configInstance->isFieldSet('foo'); } /** * Tests isEnabled() method being called with empty parameter */ public function testConfigIsEnabledEmptyString () { // Expect IAE $this->expectException(InvalidArgumentException::class); // Just invoke it $dummy = self::$configInstance->isEnabled(''); } /** * Tests isEnabled() method being called with missing configuration key */ public function testConfigIsEnabledMissingConfigKey () { // Expect NoConfig $this->expectException(NoConfigEntryException::class); // Just invoke it $dummy = self::$configInstance->isEnabled('does_not_exist'); } /** * Tests isEnabled() method being called with non-boolean configuration key */ public function testConfigIsEnabledNonBooleanConfigKey () { // Expect UVE $this->expectException(UnexpectedValueException::class); // Set it temporary self::$configInstance->setConfigEntry('is_non_boolean_enabled', 'Y'); // Just invoke it $dummy = self::$configInstance->isEnabled('non_boolean'); } /** * Tests isEnabled() method being called with 'single_server' */ public function testConfigIsEnabledSingleServerConfigKey () { // Check it on known boolean value $this->assertTrue(is_bool(self::$configInstance->isEnabled('single_server'))); } }