<?php
require_once 'EasyLogger.php';
require_once 'EasyLogger/Handler/TestHandler.php';
class EasyLoggerTest extends PHPUnit_Framework_TestCase
{
protected function _assertHandlers($logger, array $handlers)
{
$this->assertAttributeEquals(
$handlers,
'_handlers',
$logger
);
}
public function testAddingSingleHandlerOnInstantiation()
{
$handler = new EasyLogger_Handler_TestHandler();
$logger = new EasyLogger($handler);
$this->_assertHandlers($logger, array($handler));
}
public function testAddingMultipleHandlersOnInstantiation()
{
$handlers = array(
new EasyLogger_Handler_TestHandler(),
new EasyLogger_Handler_TestHandler()
);
$logger = new EasyLogger($handlers);
$this->_assertHandlers($logger, $handlers);
}
public function testManuallyAddingHandlers()
{
$logger = new EasyLogger();
$handler1 = new EasyLogger_Handler_TestHandler();
$handler2 = new EasyLogger_Handler_TestHandler();
$handler3 = new EasyLogger_Handler_TestHandler();
$handlers = array($handler1, $handler2, $handler3);
$logger->addHandler($handler1);
$this->_assertHandlers($logger, array($handler1));
$logger->addHandlers(array($handler2, $handler3));
$this->_assertHandlers($logger, $handlers);
}
/**
* @expectedException LogicException
*/
public function testLoggingExceptionWhenHandlersStackIsEmpty()
{
$logger = new EasyLogger();
$logger->log(EasyLogger::LEVEL_INFO, 'test');
}
/**
* @expectedException InvalidArgumentException
*/
public function testLoggingExceptionOnUnsupportedLevel()
{
$logger = new EasyLogger(new EasyLogger_Handler_TestHandler());
$logger->log(3, 'test');
}
public function testLoggingHandling()
{
$logger = new EasyLogger();
$handler1 = new EasyLogger_Handler_TestHandler();
$handler2 = new EasyLogger_Handler_TestHandler();
$logger->addHandlers(array($handler1, $handler2));
$level = EasyLogger::LEVEL_DEBUG;
$message = 'test';
$logger->log($level, $message);
$records1 = $handler1->getRecords();
$records2 = $handler2->getRecords();
$this->assertEquals($records1, $records2);
$record = current($records1);
$this->assertTrue(is_int($record['timestamp']));
$this->assertEquals($message, $record['message']);
$this->assertEquals($level, $record['level']);
$this->assertEquals($logger->getLevelName($level), $record['levelName']);
}
/**
* @dataProvider loggingMethodsProvider
*/
public function testShortcutLoggingMethods($method, $expectedLevel)
{
$handler = new EasyLogger_Handler_TestHandler();
$logger = new EasyLogger($handler);
$logger->$method('test');
$record = current($handler->getRecords());
$this->assertEquals($expectedLevel, $record['level']);
}
public function loggingMethodsProvider()
{
return array(
array('debug', EasyLogger::LEVEL_DEBUG),
array('info', EasyLogger::LEVEL_INFO),
array('notice', EasyLogger::LEVEL_NOTICE),
array('warning', EasyLogger::LEVEL_WARNING),
array('error', EasyLogger::LEVEL_ERROR),
array('critical', EasyLogger::LEVEL_CRITICAL),
array('alert', EasyLogger::LEVEL_ALERT),
array('emergency', EasyLogger::LEVEL_EMERGENCY)
);
}
/**
* @expectedException LogicException
*/
public function testShortcutLoggingMethodExceptionWhenMessageIsNotSupplied()
{
$logger = new EasyLogger(new EasyLogger_Handler_TestHandler());
$logger->alert();
}
}
|