1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-07-31 18:30:15 +02:00

Refactored ErrorLogHandler to use constantes for types

- Create tests for ErrorLogHandler;
- Remove types 1, 2 and 3 in favor of specific handlers.
This commit is contained in:
Henrique Moody
2013-08-01 13:07:06 -03:00
parent b33357d16b
commit fd8b621a93
2 changed files with 63 additions and 3 deletions

View File

@@ -20,6 +20,9 @@ use Monolog\Logger;
*/ */
class ErrorLogHandler extends AbstractProcessingHandler class ErrorLogHandler extends AbstractProcessingHandler
{ {
const OPERATING_SYSTEM = 0;
const SAPI = 4;
protected $messageType; protected $messageType;
/** /**
@@ -27,15 +30,29 @@ class ErrorLogHandler extends AbstractProcessingHandler
* @param integer $level The minimum logging level at which this handler will be triggered * @param integer $level The minimum logging level at which this handler will be triggered
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
*/ */
public function __construct($messageType = 0, $level = Logger::DEBUG, $bubble = true) public function __construct($messageType = self::OPERATING_SYSTEM, $level = Logger::DEBUG, $bubble = true)
{ {
parent::__construct($level, $bubble); parent::__construct($level, $bubble);
if (!in_array($messageType, array(0, 4))) {
throw new \InvalidArgumentException('Only message types 0 and 4 are supported'); if (false === in_array($messageType, self::getAvailableTypes())) {
$message = sprintf('The given message type "%s" is not supported', print_r($messageType, true));
throw new \InvalidArgumentException($message);
} }
$this->messageType = $messageType; $this->messageType = $messageType;
} }
/**
* @return array With all available types
*/
public static function getAvailableTypes()
{
return array(
self::OPERATING_SYSTEM,
self::SAPI,
);
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@@ -0,0 +1,43 @@
<?php
namespace Monolog\Handler;
use Monolog\TestCase;
use Monolog\Logger;
function error_log()
{
$GLOBALS['error_log'] = func_get_args();
}
class ErrorLogHandlerTest extends TestCase
{
protected function setUp()
{
$GLOBALS['error_log'] = array();
}
/**
* @covers Monolog\Handler\ErrorLogHandler::__construct
* @expectedException InvalidArgumentException
* @expectedExceptionMessage The given message type "42" is not supported
*/
public function testShouldNotAcceptAnInvalidTypeOnContructor()
{
new ErrorLogHandler(42);
}
/**
* @covers Monolog\Handler\ErrorLogHandler::write
*/
public function testShouldLogMessagesUsingErrorLogFuncion()
{
$type = ErrorLogHandler::OPERATING_SYSTEM;
$handler = new ErrorLogHandler($type);
$handler->handle($this->getRecord(Logger::ERROR));
$this->assertStringMatchesFormat('[%s] test.ERROR: test [] []', $GLOBALS['error_log'][0]);
$this->assertSame($GLOBALS['error_log'][1], $type);
}
}