1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-02-24 06:52:34 +01:00
php-monolog/tests/Monolog/Handler/NewRelicHandlerTest.php

135 lines
3.5 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog\Handler;
use Monolog\TestCase;
use Monolog\Logger;
use Psr\Log\LogLevel;
class NewRelicHandlerTest extends TestCase
2013-07-28 23:19:20 +02:00
{
public static $appname;
public static $customParameters;
public function setUp()
{
self::$appname = null;
self::$customParameters = array();
}
/**
* @expectedException Monolog\Handler\MissingExtensionException
*/
public function testThehandlerThrowsAnExceptionIfTheNRExtensionIsNotLoaded()
{
$handler = new StubNewRelicHandlerWithoutExtension();
2013-07-28 23:19:20 +02:00
$handler->handle($this->getRecord(Logger::ERROR));
}
2013-07-28 23:19:20 +02:00
public function testThehandlerCanHandleTheRecord()
{
$handler = new StubNewRelicHandler();
2013-07-28 23:19:20 +02:00
$handler->handle($this->getRecord(Logger::ERROR));
}
2013-07-28 23:19:20 +02:00
public function testThehandlerCanAddContextParamsToTheNewRelicTrace()
{
$handler = new StubNewRelicHandler();
2013-07-28 23:19:20 +02:00
$handler->handle($this->getRecord(Logger::ERROR, 'log message', array('a' => 'b')));
$this->assertEquals(array('a' => 'b'), self::$customParameters);
}
public function testThehandlerCanAddExtraParamsToTheNewRelicTrace()
{
$record = $this->getRecord(Logger::ERROR, 'log message');
$record['extra'] = array('c' => 'd');
$handler = new StubNewRelicHandler();
$handler->handle($record);
$this->assertEquals(array('c' => 'd'), self::$customParameters);
}
public function testThehandlerCanAddExtraContextAndParamsToTheNewRelicTrace()
{
$record = $this->getRecord(Logger::ERROR, 'log message', array('a' => 'b'));
$record['extra'] = array('c' => 'd');
$handler = new StubNewRelicHandler();
$handler->handle($record);
$expected = array(
'a' => 'b',
'c' => 'd',
);
$this->assertEquals($expected, self::$customParameters);
}
2013-08-26 12:04:22 +04:00
public function testTheAppNameIsNullByDefault()
{
$handler = new StubNewRelicHandler();
$handler->handle($this->getRecord(Logger::ERROR, 'log message'));
$this->assertEquals(null, self::$appname);
}
public function testTheAppNameCanBeInjectedFromtheConstructor()
{
$handler = new StubNewRelicHandler(LogLevel::ALERT, false, 'myAppName');
$handler->handle($this->getRecord(Logger::ERROR, 'log message'));
$this->assertEquals('myAppName', self::$appname);
}
public function testTheAppNameCanBeOverriddenFromEachLog()
{
$handler = new StubNewRelicHandler(LogLevel::ALERT, false, 'myAppName');
$handler->handle($this->getRecord(Logger::ERROR, 'log message', array('appname' => 'logAppName')));
$this->assertEquals('logAppName', self::$appname);
}
}
class StubNewRelicHandlerWithoutExtension extends NewRelicHandler
{
protected function isNewRelicEnabled()
{
return false;
}
}
class StubNewRelicHandler extends NewRelicHandler
{
protected function isNewRelicEnabled()
{
return true;
}
}
2013-07-29 00:08:51 +02:00
function newrelic_notice_error()
{
return true;
}
function newrelic_set_appname($appname)
2013-08-26 12:04:22 +04:00
{
return NewRelicHandlerTest::$appname = $appname;
2013-08-26 12:04:22 +04:00
}
function newrelic_add_custom_parameter($key, $value)
2013-07-29 00:08:51 +02:00
{
NewRelicHandlerTest::$customParameters[$key] = $value;
2014-06-04 18:30:04 +02:00
return true;
}