Author: grobmeier
Date: Fri Aug 28 05:42:47 2009
New Revision: 808762
URL: http://svn.apache.org/viewvc?rev=808762&view=rev
Log:
introduced "dry mode" to enable unit tests
Added:
incubator/log4php/trunk/src/test/php/appenders/LoggerAppenderMailEventTest.php
Modified:
incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderMailEvent.php
Modified:
incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderMailEvent.php
URL:
http://svn.apache.org/viewvc/incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderMailEvent.php?rev=808762&r1=808761&r2=808762&view=diff
==============================================================================
--- incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderMailEvent.php
(original)
+++ incubator/log4php/trunk/src/main/php/appenders/LoggerAppenderMailEvent.php
Fri Aug 28 05:42:47 2009
@@ -67,6 +67,9 @@
*/
protected $requiresLayout = true;
+ /** @var indiciates if this appender should run in dry mode */
+ private $dry = false;
+
/**
* Constructor.
*
@@ -108,14 +111,18 @@
$this->to = $to;
}
+ public function setDry($dry) {
+ $this->dry = $dry;
+ }
+
public function append($event) {
- $from = $this->getFrom();
- $to = $this->getTo();
+ $from = $this->from;
+ $to = $this->to;
if(empty($from) or empty($to)) {
return;
}
- $smtpHost = $this->getSmtpHost();
+ $smtpHost = $this->smtpHost;
$prevSmtpHost = ini_get('SMTP');
if(!empty($smtpHost)) {
ini_set('SMTP', $smtpHost);
@@ -123,7 +130,7 @@
$smtpHost = $prevSmtpHost;
}
- $smtpPort = $this->getPort();
+ $smtpPort = $this->port;
$prevSmtpPort= ini_get('smtp_port');
if($smtpPort > 0 and $smtpPort < 65535) {
ini_set('smtp_port', $smtpPort);
@@ -131,9 +138,13 @@
$smtpPort = $prevSmtpPort;
}
- @mail($to, $this->getSubject(),
- $this->layout->getHeader() .
$this->layout->format($event) . $this->layout->getFooter($event),
- "From: {$from}\r\n");
+ if(!$this->dry) {
+ @mail($to, $this->getSubject(),
+ $this->layout->getHeader() .
$this->layout->format($event) . $this->layout->getFooter($event),
+ "From: {$from}\r\n");
+ } else {
+ echo "DRY MODE OF MAIL APP.: Send mail to: ".$to." with
content: ".$this->layout->format($event);
+ }
ini_set('SMTP', $prevSmtpHost);
ini_set('smtp_port', $prevSmtpPort);
Added:
incubator/log4php/trunk/src/test/php/appenders/LoggerAppenderMailEventTest.php
URL:
http://svn.apache.org/viewvc/incubator/log4php/trunk/src/test/php/appenders/LoggerAppenderMailEventTest.php?rev=808762&view=auto
==============================================================================
---
incubator/log4php/trunk/src/test/php/appenders/LoggerAppenderMailEventTest.php
(added)
+++
incubator/log4php/trunk/src/test/php/appenders/LoggerAppenderMailEventTest.php
Fri Aug 28 05:42:47 2009
@@ -0,0 +1,50 @@
+<?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 LoggerAppenderMailEventTest extends PHPUnit_Framework_TestCase {
+
+ public function testMail() {
+ $appender = new LoggerAppenderMailEvent("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");
+
+ ob_start();
+ $appender->append($event);
+ $v = ob_get_contents();
+ ob_end_clean();
+
+ $e = "DRY MODE OF MAIL APP.: Send mail to: [email protected]
with content: ERROR - testmessage".PHP_EOL;
+ self::assertEquals($e, $v);
+ $appender->close();
+ }
+
+}