Author: grobmeier
Date: Fri Aug 28 05:35:30 2009
New Revision: 808761

URL: http://svn.apache.org/viewvc?rev=808761&view=rev
Log:
introduced "dry mode" to enable unit tests

Added:
    incubator/log4php/trunk/src/test/php/appenders/LoggerAppenderMailTest.php
Modified:
    incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderMail.php

Modified: incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderMail.php
URL: 
http://svn.apache.org/viewvc/incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderMail.php?rev=808761&r1=808760&r2=808761&view=diff
==============================================================================
--- incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderMail.php 
(original)
+++ incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderMail.php Fri 
Aug 28 05:35:30 2009
@@ -32,25 +32,19 @@
  */
 class LoggerAppenderMail extends LoggerAppender {
 
-       /**
-        * @var string 'from' field
-        */
+       /** @var string 'from' field */
        private $from = null;
 
-       /**
-        * @var string 'subject' field
-        */
+       /** @var string 'subject' field */
        private $subject = 'Log4php Report';
        
-       /**
-        * @var string 'to' field
-        */
+       /** @var string 'to' field */
        private $to = null;
 
-       /**
-        * @var string used to create mail body
-        * @access private
-        */
+       /** @var indiciates if this appender should run in dry mode */
+       private $dry = false;
+
+       /** @var string used to create mail body */
        private $body = '';
        
        /**
@@ -77,12 +71,15 @@
                        $to = $this->to;
        
                        if(!empty($this->body) and $from !== null and $to !== 
null and $this->layout !== null) {
-                                                       $subject = 
$this->subject;
-                               mail(
-                                       $to, $subject, 
-                                       $this->layout->getHeader() . 
$this->body . $this->layout->getFooter(),
-                                       "From: {$from}\r\n"
-                               );
+                               $subject = $this->subject;
+                               if(!$this->dry) {
+                                       mail(
+                                               $to, $subject, 
+                                               $this->layout->getHeader() . 
$this->body . $this->layout->getFooter(),
+                                               "From: {$from}\r\n");
+                               } else {
+                                   echo "DRY MODE OF MAIL APP.: Send mail to: 
".$to." with content: ".$this->body;
+                               }
                        }
                        $this->closed = true;
                }
@@ -100,6 +97,10 @@
                $this->from = $from;
        }  
 
+       public function setDry($dry) {
+               $this->dry = $dry;
+       }
+       
        public function append($event) {
                if($this->layout !== null) {
                        $this->body .= $this->layout->format($event);

Added: incubator/log4php/trunk/src/test/php/appenders/LoggerAppenderMailTest.php
URL: 
http://svn.apache.org/viewvc/incubator/log4php/trunk/src/test/php/appenders/LoggerAppenderMailTest.php?rev=808761&view=auto
==============================================================================
--- incubator/log4php/trunk/src/test/php/appenders/LoggerAppenderMailTest.php 
(added)
+++ incubator/log4php/trunk/src/test/php/appenders/LoggerAppenderMailTest.php 
Fri Aug 28 05:35:30 2009
@@ -0,0 +1,52 @@
+<?php
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * 
+ * @category   tests   
+ * @package    log4php
+ * @subpackage appenders
+ * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache License, 
Version 2.0
+ * @version    SVN: $Id$
+ * @link       http://logging.apache.org/log4php
+ */
+
+class LoggerAppenderMailTest extends PHPUnit_Framework_TestCase {
+        
+       public function testMail() {
+               $appender = new LoggerAppenderMail("myname ");
+               
+               $layout = new LoggerLayoutSimple();
+               $appender->setLayout($layout);
+               $appender->setDry(true);
+               $appender->setTo('[email protected]');
+               $appender->setFrom('Testsender');
+               
+               $appender->activateOptions();
+               $event = new LoggerLoggingEvent("LoggerAppenderEchoTest", new 
Logger("TEST"), LoggerLevel::getLevelError(), "testmessage");
+               $event2 = new LoggerLoggingEvent("LoggerAppenderEchoTest", new 
Logger("TEST"), LoggerLevel::getLevelError(), "testmessage2");
+                
+               ob_start();
+               $appender->append($event);
+               $appender->append($event2);
+               $appender->close();
+               $v = ob_get_contents();
+               ob_end_clean();
+
+               $e = "DRY MODE OF MAIL APP.: Send mail to: [email protected] 
with content: ERROR - testmessage".PHP_EOL."ERROR - testmessage2".PHP_EOL;
+               self::assertEquals($e, $v);
+    }
+    
+}


Reply via email to