From fd8b621a931d1c3231d28ad95b9326129ad45597 Mon Sep 17 00:00:00 2001 From: Henrique Moody Date: Thu, 1 Aug 2013 13:07:06 -0300 Subject: [PATCH] Refactored ErrorLogHandler to use constantes for types - Create tests for ErrorLogHandler; - Remove types 1, 2 and 3 in favor of specific handlers. --- src/Monolog/Handler/ErrorLogHandler.php | 23 ++++++++-- tests/Monolog/Handler/ErrorLogHandlerTest.php | 43 +++++++++++++++++++ 2 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 tests/Monolog/Handler/ErrorLogHandlerTest.php diff --git a/src/Monolog/Handler/ErrorLogHandler.php b/src/Monolog/Handler/ErrorLogHandler.php index 477c5654..9e11c3b7 100644 --- a/src/Monolog/Handler/ErrorLogHandler.php +++ b/src/Monolog/Handler/ErrorLogHandler.php @@ -20,6 +20,9 @@ use Monolog\Logger; */ class ErrorLogHandler extends AbstractProcessingHandler { + const OPERATING_SYSTEM = 0; + const SAPI = 4; + 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 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); - 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; } + /** + * @return array With all available types + */ + public static function getAvailableTypes() + { + return array( + self::OPERATING_SYSTEM, + self::SAPI, + ); + } + /** * {@inheritdoc} */ diff --git a/tests/Monolog/Handler/ErrorLogHandlerTest.php b/tests/Monolog/Handler/ErrorLogHandlerTest.php new file mode 100644 index 00000000..19bfdb5e --- /dev/null +++ b/tests/Monolog/Handler/ErrorLogHandlerTest.php @@ -0,0 +1,43 @@ +handle($this->getRecord(Logger::ERROR)); + + $this->assertStringMatchesFormat('[%s] test.ERROR: test [] []', $GLOBALS['error_log'][0]); + $this->assertSame($GLOBALS['error_log'][1], $type); + } +}