* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Log
*
* @method void LOGLEVEL() LOGLEVEL($event) Log an event at LOGLEVEL, where LOGLEVEL has been added with addLevel() or already exists
* @method void emerg() emerg($event) Log an event at the EMERG log level
* @method void alert() alert($event) Log an event at the ALERT log level
* @method void crit() crit($event) Log an event at the CRIT log level
* @method void err() err($event) Log an event at the ERR log level
* @method void warn() warn($event) Log an event at the WARN log level
* @method void notice() notice($event) Log an event at the NOTICE log level
* @method void info() info($event) Log an event at the INFO log level
* @method void debug() debug($event) Log an event at the DEBUG log level
*/
class Horde_Log_Logger implements Serializable
{
/* Serialize version. */
const VERSION = 1;
/**
* Log levels where the keys are the level priorities and the values are
* the level names.
*
* @var array
*/
private $_levels = array();
/**
* Horde_Log_Handler_Base objects.
*
* @var array
*/
private $_handlers = array();
/**
* Horde_Log_Filter objects.
*
* @var array
*/
private $_filters = array();
/**
* Constructor.
*
* @param Horde_Log_Handler_Base|null $handler Default handler.
*/
public function __construct($handler = null)
{
if (!is_null($handler)) {
$this->addHandler($handler);
}
$this->_init();
}
/**
* Serialize.
*
* @return string Serialized representation of this object.
*/
public function serialize()
{
return serialize(array(
self::VERSION,
$this->_filters,
$this->_handlers
));
}
/**
* Unserialize.
*
* @param string $data Serialized data.
*
* @throws Exception
*/
public function unserialize($data)
{
$data = @unserialize($data);
if (!is_array($data) ||
!isset($data[0]) ||
($data[0] != self::VERSION)) {
throw new Exception('Cache version change');
}
$this->_filters = $data[1];
$this->_handlers = $data[2];
$this->_init();
}
/**
* Initialization tasks.
*/
protected function _init()
{
$r = new ReflectionClass('Horde_Log');
$this->_levels = array_flip($r->getConstants());
}
/**
* Undefined method handler allows a shortcut:
*
* $log->levelName('message');
* instead of
* $log->log('message', Horde_Log_LEVELNAME);
*
*
* @param string $method Log level name.
* @param string $params Message to log.
*/
public function __call($method, $params)
{
$levelName = Horde_String::upper($method);
if (($level = array_search($levelName, $this->_levels)) !== false) {
$this->log(array_shift($params), $level);
} else {
throw new Horde_Log_Exception('Bad log level ' . $levelName);
}
}
/**
* Log a message at a level
*
* @param mixed $event Message to log, either an array or a string.
* @param integer $level Log level of message, required if $message is a
* string.
*/
public function log($event, $level = null)
{
if (empty($this->_handlers)) {
throw new Horde_Log_Exception('No handlers were added');
}
// Create an event array from the given arguments.
if (is_array($event)) {
// If we are passed an array, it must contain 'message'
// and 'level' indices.
if (!isset($event['message'])) {
throw new Horde_Log_Exception('Event array did not contain a message');
}
if (!isset($event['level'])) {
if (is_null($level)) {
throw new Horde_Log_Exception('Event array did not contain a log level');
}
$event['level'] = $level;
}
} else {
// Create an event array from the message and level
// arguments.
$event = array('message' => $event, 'level' => $level);
}
if (!isset($this->_levels[$event['level']]) ||
!is_string($this->_levels[$event['level']])) {
throw new Horde_Log_Exception('Bad log level: ' . $event['level']);
}
// Fill in the level name and timestamp for filters, formatters,
// handlers.
$event['levelName'] = $this->_levels[$event['level']];
if (!isset($event['timestamp'])) {
$event['timestamp'] = date('c');
}
// If any global filter rejects the event, don't log it.
foreach ($this->_filters as $filter) {
if (!$filter->accept($event)) {
return;
}
}
foreach ($this->_handlers as $handler) {
$handler->log($event);
}
}
/**
* Does this logger have the level $name already?
*
* @param string $name The level name to check for.
*
* @return boolean Whether the logger already has the specific level
* name.
*/
public function hasLevel($name)
{
return (boolean)array_search($name, $this->_levels);
}
/**
* Add a custom log level
*
* @param string $name Name of level.
* @param integer $level Numeric level.
*/
public function addLevel($name, $level)
{
// Log level names must be uppercase for predictability.
$name = Horde_String::upper($name);
if (isset($this->_levels[$level]) || $this->hasLevel($name)) {
throw new Horde_Log_Exception('Existing log levels cannot be overwritten');
}
$this->_levels[$level] = $name;
}
/**
* Add a filter that will be applied before all log handlers.
* Before a message will be received by any of the handlers, it
* must be accepted by all filters added with this method.
*
* @param Horde_Log_Filter $filter Filter to add.
*/
public function addFilter($filter)
{
$this->_filters[] = is_integer($filter)
? new Horde_Log_Filter_Level($filter)
: $filter;
}
/**
* Add a handler. A handler is responsible for taking a log
* message and writing it out to storage.
*
* @param Horde_Log_Handler_Base $handler Handler to add.
*/
public function addHandler($handler)
{
$this->_handlers[] = $handler;
}
}
Horde_Log-2.1.3/lib/Horde/Log.php 0000664 0001750 0001750 00000001740 12653742146 014540 0 ustar jan jan
* @author Chuck Hagenbuch
* @license http://www.horde.org/licenses/bsd BSD
*/
/**
* @category Horde
* @package Log
*/
class Horde_Log {
/** Emergency: system is unusable */
const EMERG = 0;
/** Alert: action must be taken immediately */
const ALERT = 1;
/** Critical: critical conditions */
const CRIT = 2;
/** Error: error conditions */
const ERR = 3;
/** Warning: warning conditions */
const WARN = 4;
/** Notice: normal but significant condition */
const NOTICE = 5;
/** Informational: informational messages */
const INFO = 6;
/** Debug: debug-level messages */
const DEBUG = 7;
}
Horde_Log-2.1.3/test/Horde/Log/Filter/ChainingTest.php 0000664 0001750 0001750 00000004353 12653742146 020561 0 ustar jan jan
* @author Chuck Hagenbuch
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Log
* @subpackage UnitTests
*/
/**
* @author Mike Naberezny
* @author Chuck Hagenbuch
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Log
* @subpackage UnitTests
*/
class Horde_Log_Filter_ChainingTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
date_default_timezone_set('America/New_York');
$this->log = fopen('php://memory', 'w');
$this->logger = new Horde_Log_Logger();
$this->logger->addHandler(new Horde_Log_Handler_Stream($this->log));
}
public function tearDown()
{
fclose($this->log);
}
public function testFilterAllHandlers()
{
// filter out anything above a WARNing for all handlers
$this->logger->addFilter(Horde_Log::WARN);
$this->logger->info($ignored = 'info-message-ignored');
$this->logger->warn($logged = 'warn-message-logged');
rewind($this->log);
$logdata = stream_get_contents($this->log);
$this->assertNotContains($ignored, $logdata);
$this->assertContains($logged, $logdata);
}
public function testFilterOnSpecificHandler()
{
$log2 = fopen('php://memory', 'w');
$handler2 = new Horde_Log_Handler_Stream($log2);
$handler2->addFilter(Horde_Log::ERR);
$this->logger->addHandler($handler2);
$this->logger->warn($warn = 'warn-message');
$this->logger->err($err = 'err-message');
rewind($this->log);
$logdata = stream_get_contents($this->log);
$this->assertContains($warn, $logdata);
$this->assertContains($err, $logdata);
rewind($log2);
$logdata = stream_get_contents($log2);
$this->assertContains($err, $logdata);
$this->assertNotContains($warn, $logdata);
}
}
Horde_Log-2.1.3/test/Horde/Log/Filter/ConstraintTest.php 0000664 0001750 0001750 00000007121 12653742146 021161 0 ustar jan jan
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Log
* @subpackage UnitTests
*/
/**
* @author James Pepin
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Log
* @subpackage UnitTests
*/
class Horde_Log_Filter_ConstraintTest extends Horde_Test_Case
{
public function testFilterDoesNotAcceptWhenRequiredFieldIsMissing()
{
$event = array(
'someotherfield' => 'other value',
);
$filterator = new Horde_Log_Filter_Constraint();
$filterator->addRequiredField('required_field');
$this->assertFalse($filterator->accept($event));
}
public function testFilterAcceptsWhenRequiredFieldisPresent()
{
$event = array(
'required_field' => 'somevalue',
'someotherfield' => 'other value',
);
$filterator = new Horde_Log_Filter_Constraint();
$filterator->addRequiredField('required_field');
$this->assertTrue($filterator->accept($event));
}
public function testFilterAcceptsWhenRegexMatchesField()
{
$event = array(
'regex_field' => 'somevalue',
'someotherfield' => 'other value',
);
$filterator = new Horde_Log_Filter_Constraint();
$filterator->addRegex('regex_field', '/somevalue/');
$this->assertTrue($filterator->accept($event));
}
public function testFilterAcceptsWhenRegex_DOESNOT_MatcheField()
{
$event = array(
'regex_field' => 'somevalue',
'someotherfield' => 'other value',
);
$filterator = new Horde_Log_Filter_Constraint();
$filterator->addRegex('regex_field', '/someothervalue/');
$this->assertFalse($filterator->accept($event));
}
private function getConstraintMock($returnVal)
{
$const = $this->getMock('Horde_Constraint', array('evaluate'));
$const->expects($this->once())
->method('evaluate')
->will($this->returnValue($returnVal));
return $const;
}
public function testFilterCallsEvalOnAllConstraintsWhenTheyAreAllTrue()
{
$filterator = new Horde_Log_Filter_Constraint();
$filterator->addConstraint('fieldname', $this->getConstraintMock(true));
$filterator->addConstraint('fieldname', $this->getConstraintMock(true));
$filterator->accept(array('fieldname' => 'foo'));
}
public function testFilterStopsWhenItFindsAFalseCondition()
{
$filterator = new Horde_Log_Filter_Constraint();
$filterator->addConstraint('fieldname', $this->getConstraintMock(true));
$filterator->addConstraint('fieldname', $this->getConstraintMock(true));
$filterator->addConstraint('fieldname', new Horde_Constraint_AlwaysFalse());
$const = $this->getMock('Horde_Constraint', array('evaluate'));
$const->expects($this->never())
->method('evaluate');
$filterator->addConstraint('fieldname', $const);
$filterator->accept(array('fieldname' => 'foo'));
}
public function testFilterAcceptCallsConstraintOnNullWhenFieldDoesnotExist()
{
$filterator = new Horde_Log_Filter_Constraint();
$const = $this->getMock('Horde_Constraint', array('evaluate'));
$const->expects($this->once())
->method('evaluate')
->with(null);
$filterator->addConstraint('fieldname', $const);
$filterator->accept(array('someotherfield' => 'foo'));
}
}
Horde_Log-2.1.3/test/Horde/Log/Filter/ExactLevelTest.php 0000664 0001750 0001750 00000003121 12653742146 021065 0 ustar jan jan
* @author Chuck Hagenbuch
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Log
* @subpackage UnitTests
*/
/**
* @author Mike Naberezny
* @author Chuck Hagenbuch
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Log
* @subpackage UnitTests
*/
class Horde_Log_Filter_ExactLevelTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
// accept at and only at level 2
$this->filter = new Horde_Log_Filter_ExactLevel(2);
}
public function testLevelFilterAccept()
{
$this->assertTrue($this->filter->accept(array('message' => '', 'level' => 2)));
}
public function testLevelFilterReject()
{
$this->assertFalse($this->filter->accept(array('message' => '', 'level' => 1)));
$this->assertFalse($this->filter->accept(array('message' => '', 'level' => 3)));
}
public function testConstructorThrowsOnInvalidLevel()
{
try {
new Horde_Log_Filter_Level('foo');
$this->fail();
} catch (Exception $e) {
$this->assertInstanceOf('InvalidArgumentException', $e);
$this->assertRegExp('/must be an integer/i', $e->getMessage());
}
}
}
Horde_Log-2.1.3/test/Horde/Log/Filter/LevelTest.php 0000664 0001750 0001750 00000003103 12653742146 020100 0 ustar jan jan
* @author Chuck Hagenbuch
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Log
* @subpackage UnitTests
*/
/**
* @author Mike Naberezny
* @author Chuck Hagenbuch
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Log
* @subpackage UnitTests
*/
class Horde_Log_Filter_LevelTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
// accept at or below level 2
$this->filter = new Horde_Log_Filter_Level(2);
}
public function testLevelFilterAccept()
{
$this->assertTrue($this->filter->accept(array('message' => '', 'level' => 2)));
$this->assertTrue($this->filter->accept(array('message' => '', 'level' => 1)));
}
public function testLevelFilterReject()
{
$this->assertFalse($this->filter->accept(array('message' => '', 'level' => 3)));
}
public function testConstructorThrowsOnInvalidLevel()
{
try {
new Horde_Log_Filter_Level('foo');
$this->fail();
} catch (Exception $e) {
$this->assertInstanceOf('InvalidArgumentException', $e);
$this->assertRegExp('/must be an integer/i', $e->getMessage());
}
}
}
Horde_Log-2.1.3/test/Horde/Log/Filter/MessageTest.php 0000664 0001750 0001750 00000002542 12653742146 020423 0 ustar jan jan
* @author Chuck Hagenbuch
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Log
* @subpackage UnitTests
*/
/**
* @author Mike Naberezny
* @author Chuck Hagenbuch
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Log
* @subpackage UnitTests
*/
class Horde_Log_Filter_MessageTest extends PHPUnit_Framework_TestCase
{
public function testMessageFilterRecognizesInvalidRegularExpression()
{
try {
$filter = new Horde_Log_Filter_Message('invalid regexp');
$this->fail();
} catch (InvalidArgumentException $e) {
$this->assertRegexp('/invalid reg/i', $e->getMessage());
}
}
public function testMessageFilter()
{
$filter = new Horde_Log_Filter_Message('/accept/');
$this->assertTrue($filter->accept(array('message' => 'foo accept bar', 'level' => 0)));
$this->assertFalse($filter->accept(array('message' => 'foo reject bar', 'level' => 0)));
}
}
Horde_Log-2.1.3/test/Horde/Log/Filter/SuppressTest.php 0000664 0001750 0001750 00000003312 12653742146 020657 0 ustar jan jan
* @author Chuck Hagenbuch
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Log
* @subpackage UnitTests
*/
/**
* @author Mike Naberezny
* @author Chuck Hagenbuch
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Log
* @subpackage UnitTests
*/
class Horde_Log_Filter_SuppressTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->filter = new Horde_Log_Filter_Suppress();
}
public function testSuppressIsInitiallyOff()
{
$this->assertTrue($this->filter->accept(array()));
}
public function testSuppressOn()
{
$this->filter->suppress(true);
$this->assertFalse($this->filter->accept(array()));
$this->assertFalse($this->filter->accept(array()));
}
public function testSuppressOff()
{
$this->filter->suppress(false);
$this->assertTrue($this->filter->accept(array()));
$this->assertTrue($this->filter->accept(array()));
}
public function testSuppressCanBeReset()
{
$this->filter->suppress(true);
$this->assertFalse($this->filter->accept(array()));
$this->filter->suppress(false);
$this->assertTrue($this->filter->accept(array()));
$this->filter->suppress(true);
$this->assertFalse($this->filter->accept(array()));
}
}
Horde_Log-2.1.3/test/Horde/Log/Formatter/SimpleTest.php 0000664 0001750 0001750 00000002741 12653742146 021007 0 ustar jan jan
* @author Chuck Hagenbuch
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Log
* @subpackage UnitTests
*/
/**
* @author Mike Naberezny
* @author Chuck Hagenbuch
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Log
* @subpackage UnitTests
*/
class Horde_Log_Formatter_SimpleTest extends PHPUnit_Framework_TestCase
{
public function testConstructorThrowsOnBadFormatString()
{
try {
new Horde_Log_Formatter_Simple(1);
$this->fail();
} catch (Exception $e) {
$this->assertInstanceOf('InvalidArgumentException', $e);
$this->assertRegExp('/must be a string/i', $e->getMessage());
}
}
public function testDefaultFormat()
{
$f = new Horde_Log_Formatter_Simple();
$line = $f->format(array('message' => $message = 'message',
'level' => $level = Horde_Log::ALERT,
'levelName' => $levelName = 'ALERT'));
$this->assertContains($message, $line);
$this->assertContains($levelName, $line);
}
}
Horde_Log-2.1.3/test/Horde/Log/Formatter/XmlTest.php 0000664 0001750 0001750 00000003162 12653742146 020314 0 ustar jan jan
* @author Chuck Hagenbuch
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Log
*/
/**
* @author Mike Naberezny
* @author Chuck Hagenbuch
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Log
*/
class Horde_Log_Formatter_XmlTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
date_default_timezone_set('America/New_York');
}
public function testDefaultFormat()
{
$f = new Horde_Log_Formatter_Xml();
$line = $f->format(array('message' => $message = 'message', 'level' => $level = 1));
$this->assertContains($message, $line);
$this->assertContains((string)$level, $line);
}
public function testXmlDeclarationIsStripped()
{
$f = new Horde_Log_Formatter_Xml();
$line = $f->format(array('message' => $message = 'message', 'level' => $level = 1));
$this->assertNotContains('<\?xml version=', $line);
}
public function testXmlValidates()
{
$f = new Horde_Log_Formatter_Xml();
$line = $f->format(array('message' => $message = 'message', 'level' => $level = 1));
$sxml = @simplexml_load_string($line);
$this->assertInstanceOf('SimpleXMLElement', $sxml, 'Formatted XML is invalid');
}
}
Horde_Log-2.1.3/test/Horde/Log/Handler/FirebugTest.php 0000664 0001750 0001750 00000003435 12653742146 020554 0 ustar jan jan
* @author Chuck Hagenbuch
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Log
* @subpackage UnitTests
*/
/**
* @author Mike Naberezny
* @author Chuck Hagenbuch
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Log
* @subpackage UnitTests
*/
class Horde_Log_Handler_FirebugTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
date_default_timezone_set('America/New_York');
}
public function testSettingBadOptionThrows()
{
try {
$handler = new Horde_Log_Handler_Stream('php://memory');
$handler->setOption('foo', 42);
$this->fail();
} catch (Exception $e) {
$this->assertInstanceOf('Horde_Log_Exception', $e);
$this->assertRegExp('/unknown option/i', $e->getMessage());
}
}
public function testWrite()
{
ob_start();
$handler = new Horde_Log_Handler_Firebug();
$handler->write(array('message' => $message = 'message-to-log',
'level' => $level = Horde_Log::ALERT,
'levelName' => $levelName = 'ALERT',
'timestamp' => date('c')));
$contents = ob_get_clean();
$date = '\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}-\d{2}:\d{2}';
$this->assertRegExp("/console.error\(\"$date $levelName: $message\"\);/", $contents);
}
}
Horde_Log-2.1.3/test/Horde/Log/Handler/NullTest.php 0000664 0001750 0001750 00000001633 12653742146 020101 0 ustar jan jan
* @author Chuck Hagenbuch
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Log
* @subpackage UnitTests
*/
/**
* @author Mike Naberezny
* @author Chuck Hagenbuch
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Log
* @subpackage UnitTests
*/
class Horde_Log_Handler_NullTest extends PHPUnit_Framework_TestCase
{
public function testWrite()
{
$handler = new Horde_Log_Handler_Null();
$this->assertTrue($handler->write(array('message' => 'foo', 'level' => 42)));
}
}
Horde_Log-2.1.3/test/Horde/Log/Handler/StreamTest.php 0000664 0001750 0001750 00000007415 12653742146 020426 0 ustar jan jan
* @author Chuck Hagenbuch
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Log
* @subpackage UnitTests
*/
/**
* @author Mike Naberezny
* @author Chuck Hagenbuch
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Log
* @subpackage UnitTests
*/
class Horde_Log_Handler_StreamTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
date_default_timezone_set('America/New_York');
}
public function testConstructorThrowsWhenResourceIsNotStream()
{
$resource = xml_parser_create();
try {
new Horde_Log_Handler_Stream($resource);
$this->fail();
} catch (Exception $e) {
$this->assertInstanceOf('Horde_Log_Exception', $e);
$this->assertRegExp('/not a stream/i', $e->getMessage());
}
xml_parser_free($resource);
}
public function testConstructorWithValidStream()
{
$stream = fopen('php://memory', 'a');
new Horde_Log_Handler_Stream($stream);
}
public function testConstructorWithValidUrl()
{
new Horde_Log_Handler_Stream('php://memory');
}
public function testConstructorThrowsWhenModeSpecifiedForExistingStream()
{
$stream = fopen('php://memory', 'a');
try {
new Horde_Log_Handler_Stream($stream, 'w');
$this->fail();
} catch (Exception $e) {
$this->assertInstanceOf('Horde_Log_Exception', $e);
$this->assertRegExp('/existing stream/i', $e->getMessage());
}
}
public function testConstructorThrowsWhenStreamCannotBeOpened()
{
try {
new Horde_Log_Handler_Stream('');
$this->fail();
} catch (Exception $e) {
$this->assertInstanceOf('Horde_Log_Exception', $e);
$this->assertRegExp('/cannot be opened/i', $e->getMessage());
}
}
public function testSettingBadOptionThrows()
{
try {
$handler = new Horde_Log_Handler_Stream('php://memory');
$handler->setOption('foo', 42);
$this->fail();
} catch (Exception $e) {
$this->assertInstanceOf('Horde_Log_Exception', $e);
$this->assertRegExp('/unknown option/i', $e->getMessage());
}
}
public function testWrite()
{
$stream = fopen('php://memory', 'a');
$handler = new Horde_Log_Handler_Stream($stream);
$handler->write(array('message' => $message = 'message-to-log',
'level' => $level = Horde_Log::ALERT,
'levelName' => $levelName = 'ALERT',
'timestamp' => date('c')));
rewind($stream);
$contents = stream_get_contents($stream);
fclose($stream);
$date = '\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}-\d{2}:\d{2}';
$this->assertRegExp("/$date $levelName: $message/", $contents);
}
public function testWriteThrowsWhenStreamWriteFails()
{
$stream = fopen('php://memory', 'a');
$handler = new Horde_Log_Handler_Stream($stream);
fclose($stream);
try {
$handler->write(array('message' => 'foo', 'level' => 1));
$this->fail();
} catch (Exception $e) {
$this->assertInstanceOf('Horde_Log_Exception', $e);
$this->assertRegExp('/unable to write/i', $e->getMessage());
}
}
}
Horde_Log-2.1.3/test/Horde/Log/AllTests.php 0000664 0001750 0001750 00000000132 12653742146 016476 0 ustar jan jan run();
Horde_Log-2.1.3/test/Horde/Log/bootstrap.php 0000664 0001750 0001750 00000000143 12653742146 016762 0 ustar jan jan
* @author Chuck Hagenbuch
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Log
* @subpackage UnitTests
*/
/**
* @author Mike Naberezny
* @author Chuck Hagenbuch
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Log
* @subpackage UnitTests
*/
class Horde_Log_LogTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
date_default_timezone_set('America/New_York');
$this->log = fopen('php://memory', 'a');
$this->handler = new Horde_Log_Handler_Stream($this->log);
}
// Handlers
public function testHandlerCanBeAddedWithConstructor()
{
$logger = new Horde_Log_Logger($this->handler);
$logger->log($message = 'message-to-long', Horde_Log::INFO);
rewind($this->log);
$this->assertContains($message, stream_get_contents($this->log));
}
public function testaddHandler()
{
$logger = new Horde_Log_Logger();
$logger->addHandler($this->handler);
$logger->log($message = 'message-to-log', Horde_Log::INFO);
rewind($this->log);
$this->assertContains($message, stream_get_contents($this->log));
}
public function testaddHandlerAddsMultipleHandlers()
{
$logger = new Horde_Log_Logger();
// create handlers for two separate streams of temporary memory
$log1 = fopen('php://memory', 'a');
$handler1 = new Horde_Log_Handler_Stream($log1);
$log2 = fopen('php://memory', 'a');
$handler2 = new Horde_Log_Handler_Stream($log2);
// add the handlers
$logger->addHandler($handler1);
$logger->addHandler($handler2);
// log to both handlers
$logger->log($message = 'message-sent-to-both-logs', Horde_Log::INFO);
// verify both handlers were called by the logger
rewind($log1);
$this->assertContains($message, stream_get_contents($log1));
rewind($log2);
$this->assertContains($message, stream_get_contents($log2));
// prove the two memory streams are different
// and both handlers were indeed called
fwrite($log1, 'foo');
$this->assertNotEquals(ftell($log1), ftell($log2));
}
public function testLoggerThrowsWhenNoHandlers()
{
$logger = new Horde_Log_Logger();
try {
$logger->log('message', Horde_Log::INFO);
$this->fail();
} catch (Horde_Log_Exception $e) {
$this->assertRegexp('/no handler/i', $e->getMessage());
}
}
// Levels
public function testLogThrowsOnBadLogLevel()
{
$logger = new Horde_Log_Logger($this->handler);
try {
$logger->log('foo', 42);
$this->fail();
} catch (Exception $e) {
$this->assertInstanceOf('Horde_Log_Exception', $e);
$this->assertRegExp('/bad log level/i', $e->getMessage());
}
}
public function testLogThrough__callThrowsOnBadLogLevel()
{
$logger = new Horde_Log_Logger($this->handler);
try {
$logger->nonexistantLevel('');
$this->fail();
} catch (Exception $e) {
$this->assertInstanceOf('Horde_Log_Exception', $e);
$this->assertRegExp('/bad log level/i', $e->getMessage());
}
}
public function testAddingLevelThrowsWhenOverridingBuiltinLogLevel()
{
try {
$logger = new Horde_Log_Logger($this->handler);
$logger->addLevel('BOB', 0);
$this->fail();
} catch (Exception $e) {
$this->assertInstanceOf('Horde_Log_Exception', $e);
$this->assertRegExp('/existing log level/i', $e->getMessage());
}
}
public function testAddLogLevel()
{
$logger = new Horde_Log_Logger($this->handler);
$logger->addLevel($levelName = 'EIGHT', $level = 8);
$logger->eight($message = 'eight message');
rewind($this->log);
$logdata = stream_get_contents($this->log);
$this->assertContains($levelName, $logdata);
$this->assertContains($message, $logdata);
}
}
Horde_Log-2.1.3/test/Horde/Log/phpunit.xml 0000664 0001750 0001750 00000000056 12653742146 016450 0 ustar jan jan