NewRelicHandlerTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Handler;
  11. use Monolog\TestCase;
  12. use Monolog\Logger;
  13. use Psr\Log\LogLevel;
  14. class NewRelicHandlerTest extends TestCase
  15. {
  16. public static $appname;
  17. public static $customParameters;
  18. public function setUp()
  19. {
  20. self::$appname = null;
  21. self::$customParameters = array();
  22. }
  23. /**
  24. * @expectedException Monolog\Handler\MissingExtensionException
  25. */
  26. public function testThehandlerThrowsAnExceptionIfTheNRExtensionIsNotLoaded()
  27. {
  28. $handler = new StubNewRelicHandlerWithoutExtension();
  29. $handler->handle($this->getRecord(Logger::ERROR));
  30. }
  31. public function testThehandlerCanHandleTheRecord()
  32. {
  33. $handler = new StubNewRelicHandler();
  34. $handler->handle($this->getRecord(Logger::ERROR));
  35. }
  36. public function testThehandlerCanAddContextParamsToTheNewRelicTrace()
  37. {
  38. $handler = new StubNewRelicHandler();
  39. $handler->handle($this->getRecord(Logger::ERROR, 'log message', array('a' => 'b')));
  40. $this->assertEquals(array('a' => 'b'), self::$customParameters);
  41. }
  42. public function testThehandlerCanAddExtraParamsToTheNewRelicTrace()
  43. {
  44. $record = $this->getRecord(Logger::ERROR, 'log message');
  45. $record['extra'] = array('c' => 'd');
  46. $handler = new StubNewRelicHandler();
  47. $handler->handle($record);
  48. $this->assertEquals(array('c' => 'd'), self::$customParameters);
  49. }
  50. public function testThehandlerCanAddExtraContextAndParamsToTheNewRelicTrace()
  51. {
  52. $record = $this->getRecord(Logger::ERROR, 'log message', array('a' => 'b'));
  53. $record['extra'] = array('c' => 'd');
  54. $handler = new StubNewRelicHandler();
  55. $handler->handle($record);
  56. $expected = array(
  57. 'a' => 'b',
  58. 'c' => 'd',
  59. );
  60. $this->assertEquals($expected, self::$customParameters);
  61. }
  62. public function testTheAppNameIsNullByDefault()
  63. {
  64. $handler = new StubNewRelicHandler();
  65. $handler->handle($this->getRecord(Logger::ERROR, 'log message'));
  66. $this->assertEquals(null, self::$appname);
  67. }
  68. public function testTheAppNameCanBeInjectedFromtheConstructor()
  69. {
  70. $handler = new StubNewRelicHandler(LogLevel::ALERT, false, 'myAppName');
  71. $handler->handle($this->getRecord(Logger::ERROR, 'log message'));
  72. $this->assertEquals('myAppName', self::$appname);
  73. }
  74. public function testTheAppNameCanBeOverriddenFromEachLog()
  75. {
  76. $handler = new StubNewRelicHandler(LogLevel::ALERT, false, 'myAppName');
  77. $handler->handle($this->getRecord(Logger::ERROR, 'log message', array('appname' => 'logAppName')));
  78. $this->assertEquals('logAppName', self::$appname);
  79. }
  80. }
  81. class StubNewRelicHandlerWithoutExtension extends NewRelicHandler
  82. {
  83. protected function isNewRelicEnabled()
  84. {
  85. return false;
  86. }
  87. }
  88. class StubNewRelicHandler extends NewRelicHandler
  89. {
  90. protected function isNewRelicEnabled()
  91. {
  92. return true;
  93. }
  94. }
  95. function newrelic_notice_error()
  96. {
  97. return true;
  98. }
  99. function newrelic_set_appname($appname)
  100. {
  101. return NewRelicHandlerTest::$appname = $appname;
  102. }
  103. function newrelic_add_custom_parameter($key, $value)
  104. {
  105. NewRelicHandlerTest::$customParameters[$key] = $value;
  106. return true;
  107. }