From 579d515ef83b5835d5e4920edc98026f20b4cf61 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 16 Jul 2017 22:47:31 +0200 Subject: [PATCH] Continued a bit: - better checks allowing more coverage - added some sanity-checks MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../config/class_FrameworkConfiguration.php | 17 ++- .../config/FrameworkConfigurationTest.php | 138 ++++++++++++++++++ 2 files changed, 148 insertions(+), 7 deletions(-) diff --git a/framework/config/class_FrameworkConfiguration.php b/framework/config/class_FrameworkConfiguration.php index 16ebaf86..9f158c36 100644 --- a/framework/config/class_FrameworkConfiguration.php +++ b/framework/config/class_FrameworkConfiguration.php @@ -112,7 +112,10 @@ class FrameworkConfiguration implements Registerable { if (is_null($str)) { // Throw NPE throw new NullPointerException($this, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER); - } elseif (empty($str)) { + } elseif (!is_string($str)) { + // Entry is empty + throw new InvalidArgumentException(sprintf('str[]=%s is not a string', gettype($str)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY); + } elseif ((is_string($str)) && (empty($str))) { // Entry is empty throw new InvalidArgumentException('str is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY); } @@ -140,7 +143,7 @@ class FrameworkConfiguration implements Registerable { } elseif (!is_string($timezone)) { // Is not a string throw new InvalidArgumentException(sprintf('timezone[]=%s is not a string', gettype($timezone)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY); - } elseif (empty($timezone)) { + } elseif ((is_string($timezone)) && (empty($timezone))) { // Entry is empty throw new InvalidArgumentException('timezone is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY); } @@ -174,7 +177,7 @@ class FrameworkConfiguration implements Registerable { } elseif (!is_string($configKey)) { // Is not a string throw new InvalidArgumentException(sprintf('configKey[]=%s is not a string', gettype($configKey)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY); - } elseif (empty($configKey)) { + } elseif ((is_string($configKey)) && (empty($configKey))) { // Entry is empty throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY); } @@ -203,7 +206,7 @@ class FrameworkConfiguration implements Registerable { } elseif (!is_string($configKey)) { // Is not a string throw new InvalidArgumentException(sprintf('configKey[]=%s is not a string', gettype($configKey)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY); - } elseif (empty($configKey)) { + } elseif ((is_string($configKey)) && (empty($configKey))) { // Entry is empty throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY); } @@ -239,7 +242,7 @@ class FrameworkConfiguration implements Registerable { } elseif (!is_string($configKey)) { // Is not a string throw new InvalidArgumentException(sprintf('configKey[]=%s is not a string', gettype($configKey)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY); - } elseif (empty($configKey)) { + } elseif ((is_string($configKey)) && (empty($configKey))) { // Entry is empty throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY); } elseif ((is_array($configValue)) || (is_object($configValue)) || (is_resource($configValue))) { @@ -286,7 +289,7 @@ class FrameworkConfiguration implements Registerable { } elseif (!is_string($configKey)) { // Entry is empty throw new InvalidArgumentException(sprintf('configKey[]=%s is not a string', gettype($configKey)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY); - } elseif (empty($configKey)) { + } elseif ((is_string($configKey)) && (empty($configKey))) { // Entry is empty throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY); } @@ -360,7 +363,7 @@ class FrameworkConfiguration implements Registerable { } elseif (!is_string($serverAddress)) { // Is not a string throw new InvalidArgumentException(sprintf('serverAddress[]=%s is not a string', gettype($serverAddress)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY); - } elseif (empty($serverAddress)) { + } elseif ((is_string($serverAddress)) && (empty($serverAddress))) { // Entry is empty throw new InvalidArgumentException('serverAddress is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY); } diff --git a/tests/framework/config/FrameworkConfigurationTest.php b/tests/framework/config/FrameworkConfigurationTest.php index 324a8fe0..d3e8ccd5 100644 --- a/tests/framework/config/FrameworkConfigurationTest.php +++ b/tests/framework/config/FrameworkConfigurationTest.php @@ -534,6 +534,75 @@ class FrameworkConfigurationTest extends TestCase { self::$configInstance->unsetConfigEntry(NULL); } + /** + * Tests unsetting boolean key + */ + public function testUnsettingBooleanConfigKey () { + // Will throw this exception + $this->expectException(InvalidArgumentException::class); + + // Test it + self::$configInstance->unsetConfigEntry(FALSE); + } + + /** + * Tests unsetting decimal key + */ + public function testUnsettingDecimalConfigKey () { + // Will throw this exception + $this->expectException(InvalidArgumentException::class); + + // Test it + self::$configInstance->unsetConfigEntry(12345); + } + + /** + * Tests unsetting float key + */ + public function testUnsettingFloatConfigKey () { + // Will throw this exception + $this->expectException(InvalidArgumentException::class); + + // Test it + self::$configInstance->unsetConfigEntry(123.45); + } + + /** + * Tests unsetting array key + */ + public function testUnsettingArrayConfigKey () { + // Will throw this exception + $this->expectException(InvalidArgumentException::class); + + // Test it + self::$configInstance->unsetConfigEntry(array()); + } + + /** + * Tests unsetting object key + */ + public function testUnsettingObjectConfigKey () { + // Will throw this exception + $this->expectException(InvalidArgumentException::class); + + // Test it + self::$configInstance->unsetConfigEntry($this); + } + + /** + * Tests unsetting resource key + */ + public function testUnsettingResourceConfigKey () { + // Will throw this exception + $this->expectException(InvalidArgumentException::class); + + // Init some resource + $resource = fopen(__FILE__, 'r'); + + // Test it + self::$configInstance->unsetConfigEntry($resource); + } + /** * Tests unsetting an empty key */ @@ -603,6 +672,75 @@ class FrameworkConfigurationTest extends TestCase { self::$configInstance->setDefaultTimezone(NULL); } + /** + * Tests setting a boolean default timezone + */ + public function testSettingBooleanDefaultTimezone () { + // Will throw this exception + $this->expectException(InvalidArgumentException::class); + + // Test it + self::$configInstance->setDefaultTimezone(FALSE); + } + + /** + * Tests setting a decimal default timezone + */ + public function testSettingDecimalDefaultTimezone () { + // Will throw this exception + $this->expectException(InvalidArgumentException::class); + + // Test it + self::$configInstance->setDefaultTimezone(12345); + } + + /** + * Tests setting a float default timezone + */ + public function testSettingFloatDefaultTimezone () { + // Will throw this exception + $this->expectException(InvalidArgumentException::class); + + // Test it + self::$configInstance->setDefaultTimezone(123.45); + } + + /** + * Tests setting an array default timezone + */ + public function testSettingArrayDefaultTimezone () { + // Will throw this exception + $this->expectException(InvalidArgumentException::class); + + // Test it + self::$configInstance->setDefaultTimezone(array()); + } + + /** + * Tests setting an object default timezone + */ + public function testSettingObjectDefaultTimezone () { + // Will throw this exception + $this->expectException(InvalidArgumentException::class); + + // Test it + self::$configInstance->setDefaultTimezone($this); + } + + /** + * Tests setting a resource default timezone + */ + public function testSettingResourceDefaultTimezone () { + // Will throw this exception + $this->expectException(InvalidArgumentException::class); + + // Init some resource + $resource = fopen(__FILE__, 'r'); + + // Test it + self::$configInstance->setDefaultTimezone($resource); + } + /** * Tests setting an empty default timezone */ -- 2.39.5