logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ihabu...@apache.org
Subject [3/3] git commit: Improvements to LoggerAppenderMail
Date Mon, 31 Dec 2012 10:56:33 GMT
Updated Branches:
  refs/heads/develop 7375b297d -> 3d4cc6030


Improvements to LoggerAppenderMail

* added checking of required parameters (to, from)
* removed deprecated dry mode which is no longer used for testing
* improved tests


Project: http://git-wip-us.apache.org/repos/asf/logging-log4php/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4php/commit/3d4cc603
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4php/tree/3d4cc603
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4php/diff/3d4cc603

Branch: refs/heads/develop
Commit: 3d4cc60304d494a4fb004cac05320a2aa8980ed7
Parents: ae11f08
Author: Ivan Habunek <ihabunek@apache.org>
Authored: Mon Dec 31 11:54:51 2012 +0100
Committer: Ivan Habunek <ihabunek@apache.org>
Committed: Mon Dec 31 11:54:51 2012 +0100

----------------------------------------------------------------------
 src/main/php/appenders/LoggerAppenderMail.php     |   57 +++++++---------
 src/test/php/appenders/LoggerAppenderMailTest.php |   24 +++++++
 2 files changed, 48 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4php/blob/3d4cc603/src/main/php/appenders/LoggerAppenderMail.php
----------------------------------------------------------------------
diff --git a/src/main/php/appenders/LoggerAppenderMail.php b/src/main/php/appenders/LoggerAppenderMail.php
index 29d88c2..fe26e1a 100644
--- a/src/main/php/appenders/LoggerAppenderMail.php
+++ b/src/main/php/appenders/LoggerAppenderMail.php
@@ -45,7 +45,7 @@ class LoggerAppenderMail extends LoggerAppender {
 	 * Email address to put in From field of the email.
 	 * @var string
 	 */
-	protected $from = null;
+	protected $from;
 
 	/** 
 	 * The subject of the email.
@@ -57,14 +57,7 @@ class LoggerAppenderMail extends LoggerAppender {
 	 * One or more comma separated email addresses to which to send the email. 
 	 * @var string
 	 */
-	protected $to = null;
-
-	/** 
-	 * Indiciates whether this appender should run in dry mode.
-	 * @deprecated
-	 * @var boolean 
-	 */
-	protected $dry = false;
+	protected $to;
 
 	/** 
 	 * Buffer which holds the email contents before it is sent. 
@@ -73,29 +66,32 @@ class LoggerAppenderMail extends LoggerAppender {
 	protected $body = '';
 	
 	public function append(LoggerLoggingEvent $event) {
-		if($this->layout !== null) {
-			$this->body .= $this->layout->format($event);
+		$this->body .= $this->layout->format($event);
+	}
+
+	public function activateOptions() {
+		if (empty($this->from)) {
+			$this->warn("Required parameter 'from' not set. Closing appender.");
+			$this->closed = true;
+			return;
+		}
+		if (empty($this->to)) {
+			$this->warn("Required parameter 'to' not set. Closing appender.");
+			$this->closed = true;
+			return;
 		}
 	}
 	
 	public function close() {
-		if($this->closed != true) {
-			$from = $this->from;
-			$to = $this->to;
-	
-			if(!empty($this->body) and $from !== null and $to !== null and $this->layout !==
null) {
-				if(!$this->dry) {
-					$message = $this->layout->getHeader() . $this->body . $this->layout->getFooter();
-					$subject = $this->subject;
-					$contentType = $this->layout->getContentType();
-					
-					$headers = "From: {$from}\r\n";
-					$headers .= "Content-Type: {$contentType}\r\n";
-					
-					mail($to, $subject, $message, $headers);
-				} else {
-				    echo "DRY MODE OF MAIL APP.: Send mail to: ".$to." with content: ".$this->body;
-				}
+		if(!$this->closed) {
+			if(!empty($this->body)) {
+				$message = $this->layout->getHeader() . $this->body . $this->layout->getFooter();
+				$contentType = $this->layout->getContentType();
+
+				$headers = "From: {$this->from}\r\n";
+				$headers .= "Content-Type: {$contentType}\r\n";
+
+				mail($this->to, $this->subject, $message, $headers);
 			}
 			$this->closed = true;
 		}
@@ -130,9 +126,4 @@ class LoggerAppenderMail extends LoggerAppender {
 	public function getFrom() {
 		return $this->from;
 	}
-
-	/** Enables or disables dry mode. */
-	public function setDry($dry) {
-		$this->setBoolean('dry', $dry);
-	}
 }

http://git-wip-us.apache.org/repos/asf/logging-log4php/blob/3d4cc603/src/test/php/appenders/LoggerAppenderMailTest.php
----------------------------------------------------------------------
diff --git a/src/test/php/appenders/LoggerAppenderMailTest.php b/src/test/php/appenders/LoggerAppenderMailTest.php
index a08c4c2..a949258 100644
--- a/src/test/php/appenders/LoggerAppenderMailTest.php
+++ b/src/test/php/appenders/LoggerAppenderMailTest.php
@@ -64,4 +64,28 @@ class LoggerAppenderMailTest extends PHPUnit_Framework_TestCase {
 		$appender->append(LoggerTestHelper::getFatalEvent('fatality!'));
 		$appender->close();
 	}
+
+	/**
+	 * @expectedException PHPUnit_Framework_Error
+	 * @expectedExceptionMessage Required parameter 'to' not set.
+	 */
+	public function testErrorMissingTo() {
+		$appender = new LoggerAppenderMail("testAppender");
+		$appender->setLayout(new LoggerLayoutHtml());
+		$appender->setFrom('log4php@localhost');
+		$appender->setSubject("Testing text/html " . date('Y-m-d H:i:s'));
+		$appender->activateOptions();
+	}
+
+	/**
+	 * @expectedException PHPUnit_Framework_Error
+	 * @expectedExceptionMessage Required parameter 'from' not set.
+	 */
+	public function testErrorMissingFrom() {
+		$appender = new LoggerAppenderMail("testAppender");
+		$appender->setLayout(new LoggerLayoutHtml());
+		$appender->setTo('log4php@gmail.com');
+		$appender->setSubject("Testing text/html " . date('Y-m-d H:i:s'));
+		$appender->activateOptions();
+	}
 }


Mime
View raw message