Author: grobmeier
Date: Fri Aug 28 06:11:27 2009
New Revision: 808765

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

Added:
    incubator/log4php/trunk/src/test/php/appenders/LoggerAppenderSocketTest.php
Modified:
    incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderSocket.php

Modified: 
incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderSocket.php
URL: 
http://svn.apache.org/viewvc/incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderSocket.php?rev=808765&r1=808764&r2=808765&view=diff
==============================================================================
--- incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderSocket.php 
(original)
+++ incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderSocket.php Fri 
Aug 28 06:11:27 2009
@@ -74,6 +74,9 @@
         */
        private $xmlLayout = null;
        
+       /** @var indiciates if this appender should run in dry mode */
+       private $dry = false;
+       
        public function __destruct() {
        $this->close();
        }
@@ -84,7 +87,9 @@
        public function activateOptions() {
                $errno = 0;
                $errstr = '';
-               $this->sp = @fsockopen($this->getRemoteHost(), 
$this->getPort(), $errno, $errstr, $this->getTimeout());
+               if(!$this->dry) {
+                       $this->sp = @fsockopen($this->getRemoteHost(), 
$this->getPort(), $errno, $errstr, $this->getTimeout());
+               }
                if($errno) {
                        $this->closed = true;
                } else {
@@ -104,11 +109,17 @@
        
        public function close() {
                if($this->closed != true) {
-                       fclose($this->sp);
+                       if(!$this->dry) {
+                               fclose($this->sp);
+                       }
                        $this->closed = true;
                }
        }
 
+       public function setDry($dry) {
+               $this->dry = $dry;
+       }
+       
        /**
         * @return string
         */
@@ -209,20 +220,30 @@
         * @param LoggerLoggingEvent
         */
        public function append($event) {
-               if($this->sp) {
+               if($this->sp || $this->dry) {
                        if($this->getLocationInfo()) {
                                $event->getLocationInformation();
                        }
                
                        if(!$this->getUseXml()) {
                                $sEvent = serialize($event);
-                               fwrite($this->sp, $sEvent, strlen($sEvent));
+                               if(!$this->dry) {
+                                       fwrite($this->sp, $sEvent, 
strlen($sEvent));
+                               } else {
+                                   echo "DRY MODE OF SOCKET APPENDER: 
".$sEvent;
+                               }
                        } else {
-                               fwrite($this->sp, 
$this->xmlLayout->format($event));
+                               if(!$this->dry) {
+                                       fwrite($this->sp, 
$this->xmlLayout->format($event));
+                               } else {
+                                   echo "DRY MODE OF SOCKET APPENDER: ".$event;
+                               }
                        }                        
 
                        // not sure about it...
-                       fflush($this->sp);
+                       if(!$this->dry) {
+                               fflush($this->sp);
+                       }
                } 
        }
 }

Added: 
incubator/log4php/trunk/src/test/php/appenders/LoggerAppenderSocketTest.php
URL: 
http://svn.apache.org/viewvc/incubator/log4php/trunk/src/test/php/appenders/LoggerAppenderSocketTest.php?rev=808765&view=auto
==============================================================================
--- incubator/log4php/trunk/src/test/php/appenders/LoggerAppenderSocketTest.php 
(added)
+++ incubator/log4php/trunk/src/test/php/appenders/LoggerAppenderSocketTest.php 
Fri Aug 28 06:11:27 2009
@@ -0,0 +1,48 @@
+<?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 LoggerAppenderSyslogTest extends PHPUnit_Framework_TestCase {
+        
+       public function testSyslog() {
+               $appender = new LoggerAppenderSocket("myname ");
+               
+               $layout = new LoggerLayoutSimple();
+               $appender->setLayout($layout);
+               $appender->setDry(true);
+//             $appender->setUseXml(true);
+               $appender->activateOptions();
+               $event = new LoggerLoggingEvent("LoggerAppenderSocketTest", new 
Logger("TEST"), LoggerLevel::getLevelError(), "testmessage");
+                
+               ob_start();
+               $appender->append($event);
+               $v = ob_get_contents();
+               ob_end_clean();
+               $s = serialize($event);
+               
+               $e = "DRY MODE OF SOCKET APPENDER: ".$s;
+               self::assertEquals($v, $e);
+    }
+    
+}


Reply via email to