logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ihabu...@apache.org
Subject svn commit: r1187879 - in /logging/log4php/trunk/src: main/php/helpers/LoggerOptionConverter.php test/php/helpers/LoggerOptionConverterTest.php
Date Sun, 23 Oct 2011 13:07:46 GMT
Author: ihabunek
Date: Sun Oct 23 13:07:46 2011
New Revision: 1187879

URL: http://svn.apache.org/viewvc?rev=1187879&view=rev
Log:
Added new methods for conversion to integer and boolean which throw an exception if a value
cannot be converted. Useful for parsing mandatory options.

Modified:
    logging/log4php/trunk/src/main/php/helpers/LoggerOptionConverter.php
    logging/log4php/trunk/src/test/php/helpers/LoggerOptionConverterTest.php

Modified: logging/log4php/trunk/src/main/php/helpers/LoggerOptionConverter.php
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/main/php/helpers/LoggerOptionConverter.php?rev=1187879&r1=1187878&r2=1187879&view=diff
==============================================================================
--- logging/log4php/trunk/src/main/php/helpers/LoggerOptionConverter.php (original)
+++ logging/log4php/trunk/src/main/php/helpers/LoggerOptionConverter.php Sun Oct 23 13:07:46
2011
@@ -32,7 +32,13 @@ class LoggerOptionConverter {
 	const DELIM_STOP = '}';
 	const DELIM_START_LEN = 2;
 	const DELIM_STOP_LEN = 1;
-
+	
+	/** String values which are converted to boolean TRUE. */
+	private static $trueValues = array('1', 'true', 'yes', 'on');
+	
+	/** String values which are converted to boolean FALSE. */
+	private static $falseValues = array('0', 'false', 'no', 'off');
+	
 	/**
 	 * Read a predefined var.
 	 *
@@ -89,6 +95,21 @@ class LoggerOptionConverter {
 		return $default;
 	}
 
+	/** Converts $value to boolean, or throws an exception if not possible. */
+	public static function toBooleanEx($value) {
+		if (isset($value)) {
+			$value = strtolower(trim($value));
+			if (in_array($value, self::$trueValues)) {
+				return true;
+			}
+			if (in_array($value, self::$falseValues)) {
+				return false;
+			}
+		}
+		
+		throw new LoggerException("Givan value [" . var_export($value, true) . "] cannot be converted
to boolean.");
+	}
+	
 	/**
 	 * @param string $value
 	 * @param integer $default
@@ -102,6 +123,18 @@ class LoggerOptionConverter {
 			return $default;
 		}
 	}
+	
+	
+	/** Converts $value to integer, or throws an exception if not possible. */
+	public static function toIntegerEx($value) {
+		if (!empty($value)) {
+			if (is_numeric($value)) {
+				return (integer) $value;
+			}
+		}
+	
+		throw new LoggerException("Givan value [" . var_export($value, true) . "] cannot be converted
to integer.");
+	}
 
 	/**
 	 * Converts a standard or custom priority level to a Level

Modified: logging/log4php/trunk/src/test/php/helpers/LoggerOptionConverterTest.php
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/test/php/helpers/LoggerOptionConverterTest.php?rev=1187879&r1=1187878&r2=1187879&view=diff
==============================================================================
--- logging/log4php/trunk/src/test/php/helpers/LoggerOptionConverterTest.php (original)
+++ logging/log4php/trunk/src/test/php/helpers/LoggerOptionConverterTest.php Sun Oct 23 13:07:46
2011
@@ -32,23 +32,100 @@ define('MY_CONSTANT_CONSTANT_OTHER', 'DE
 class LoggerOptionConverterTest extends PHPUnit_Framework_TestCase {
 
     public function testToBoolean() {
-        self::assertEquals(true, LoggerOptionConverter::toBoolean(null, true));
-        self::assertEquals(true, LoggerOptionConverter::toBoolean(null));
-        self::assertEquals(true, LoggerOptionConverter::toBoolean(true));
-        self::assertEquals(true, LoggerOptionConverter::toBoolean("1"));
-        self::assertEquals(true, LoggerOptionConverter::toBoolean("true"));
-        self::assertEquals(true, LoggerOptionConverter::toBoolean("on"));
-        self::assertEquals(true, LoggerOptionConverter::toBoolean("yes"));
-        
-        self::assertEquals(false, LoggerOptionConverter::toBoolean(null, false));
-        self::assertEquals(false, LoggerOptionConverter::toBoolean(false));
-        self::assertEquals(false, LoggerOptionConverter::toBoolean(""));
-        self::assertEquals(false, LoggerOptionConverter::toBoolean("0"));
-        self::assertEquals(false, LoggerOptionConverter::toBoolean("false"));
-        self::assertEquals(false, LoggerOptionConverter::toBoolean("off"));
-        self::assertEquals(false, LoggerOptionConverter::toBoolean("no"));
+        self::assertTrue(LoggerOptionConverter::toBoolean(null, true));
+        self::assertTrue(LoggerOptionConverter::toBoolean('foo', true));
+        self::assertTrue(LoggerOptionConverter::toBoolean(null));
+        self::assertTrue(LoggerOptionConverter::toBoolean(true));
+        self::assertTrue(LoggerOptionConverter::toBoolean("1"));
+        self::assertTrue(LoggerOptionConverter::toBoolean(1));
+        self::assertTrue(LoggerOptionConverter::toBoolean("true"));
+        self::assertTrue(LoggerOptionConverter::toBoolean("on"));
+        self::assertTrue(LoggerOptionConverter::toBoolean("yes"));
+        
+        self::assertFalse(LoggerOptionConverter::toBoolean(null, false));
+        self::assertFalse(LoggerOptionConverter::toBoolean('foo', false));
+        self::assertFalse(LoggerOptionConverter::toBoolean(false));
+        self::assertFalse(LoggerOptionConverter::toBoolean(""));
+        self::assertFalse(LoggerOptionConverter::toBoolean("0"));
+        self::assertFalse(LoggerOptionConverter::toBoolean(0));
+        self::assertFalse(LoggerOptionConverter::toBoolean("false"));
+        self::assertFalse(LoggerOptionConverter::toBoolean("off"));
+        self::assertFalse(LoggerOptionConverter::toBoolean("no"));
+        
+        self::assertTrue(LoggerOptionConverter::toBooleanEx("1"));
+        self::assertTrue(LoggerOptionConverter::toBooleanEx("true"));
+        self::assertTrue(LoggerOptionConverter::toBooleanEx("on"));
+        self::assertTrue(LoggerOptionConverter::toBooleanEx("yes"));
+        
+        self::assertFalse(LoggerOptionConverter::toBooleanEx("0"));
+        self::assertFalse(LoggerOptionConverter::toBooleanEx("false"));
+        self::assertFalse(LoggerOptionConverter::toBooleanEx("off"));
+        self::assertFalse(LoggerOptionConverter::toBooleanEx("no"));
+    }
+    
+    /**
+     * Test fail on NULL. 
+ 	 * @expectedException LoggerException
+ 	 * @expectedExceptionMessage Givan value [NULL] cannot be converted to boolean.
+     */
+    public function testToBooleanFailure1() {
+    	LoggerOptionConverter::toBooleanEx(null);
+    }
+    
+    /**
+     * Test fail on empty string.
+     * @expectedException LoggerException
+     * @expectedExceptionMessage Givan value [''] cannot be converted to boolean.
+     */
+    public function testToBooleanFailure2() {
+    	LoggerOptionConverter::toBooleanEx('');
+    }
+    
+    /**
+     * Test fail on invalid string.
+     * @expectedException LoggerException
+     * @expectedExceptionMessage Givan value ['foo'] cannot be converted to boolean.
+     */
+    public function testToBooleanFailure3() {
+    	LoggerOptionConverter::toBooleanEx('foo');
+    }
+    
+    public function testToInteger() {
+    	self::assertSame(1, LoggerOptionConverter::toInt('1', 0));
+    	self::assertSame(-11, LoggerOptionConverter::toInt('-11', 0));
+    	self::assertSame(-10, LoggerOptionConverter::toInt(null, -10));
+    	self::assertSame(-10, LoggerOptionConverter::toInt('', -10));
+    	self::assertSame(-10, LoggerOptionConverter::toInt('foo', -10));
+    	
+    	self::assertSame(1, LoggerOptionConverter::toIntegerEx('1'));
+    }
+    
+    /**
+    * Test fail on NULL.
+    * @expectedException LoggerException
+    * @expectedExceptionMessage Givan value [NULL] cannot be converted to integer.
+    */
+    public function testToIntegerFailure1() {
+    	LoggerOptionConverter::toIntegerEx(null);
+    }
+    
+    /**
+     * Test fail on empty string.
+     * @expectedException LoggerException
+     * @expectedExceptionMessage Givan value [''] cannot be converted to integer.
+     */
+    public function testToIntegerFailure2() {
+    	LoggerOptionConverter::toIntegerEx('');
     }
     
+    /**
+     * Test fail on invalid string.
+     * @expectedException LoggerException
+     * @expectedExceptionMessage Givan value ['foo'] cannot be converted to integer.
+     */
+    public function testToIntegerFailure3() {
+    	LoggerOptionConverter::toIntegerEx('foo');
+    }
     
     public function testSubstituteVars() {
     	$props['OTHER_CONSTANT'] = "OTHER";



Mime
View raw message