NewRelicHandlerTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. class NewRelicHandlerTest extends TestCase
  14. {
  15. public static $appname;
  16. public static $customParameters;
  17. public function setUp()
  18. {
  19. self::$appname = null;
  20. self::$customParameters = array();
  21. }
  22. /**
  23. * @expectedException Monolog\Handler\MissingExtensionException
  24. */
  25. public function testThehandlerThrowsAnExceptionIfTheNRExtensionIsNotLoaded()
  26. {
  27. $handler = new StubNewRelicHandlerWithoutExtension();
  28. $handler->handle($this->getRecord(Logger::ERROR));
  29. }
  30. public function testThehandlerCanHandleTheRecord()
  31. {
  32. $handler = new StubNewRelicHandler();
  33. $handler->handle($this->getRecord(Logger::ERROR));
  34. }
  35. public function testThehandlerCanAddContextParamsToTheNewRelicTrace()
  36. {
  37. $handler = new StubNewRelicHandler();
  38. $handler->handle($this->getRecord(Logger::ERROR, 'log message', array('a' => 'b')));
  39. $this->assertEquals(array('context_a' => 'b'), self::$customParameters);
  40. }
  41. public function testThehandlerCanAddExplodedContextParamsToTheNewRelicTrace()
  42. {
  43. $handler = new StubNewRelicHandler(Logger::ERROR, true, self::$appname, true);
  44. $handler->handle($this->getRecord(
  45. Logger::ERROR,
  46. 'log message',
  47. array('a' => array('key1' => 'value1', 'key2' => 'value2'))
  48. ));
  49. $this->assertEquals(
  50. array('context_a_key1' => 'value1', 'context_a_key2' => 'value2'),
  51. self::$customParameters
  52. );
  53. }
  54. public function testThehandlerCanAddExtraParamsToTheNewRelicTrace()
  55. {
  56. $record = $this->getRecord(Logger::ERROR, 'log message');
  57. $record['extra'] = array('c' => 'd');
  58. $handler = new StubNewRelicHandler();
  59. $handler->handle($record);
  60. $this->assertEquals(array('extra_c' => 'd'), self::$customParameters);
  61. }
  62. public function testThehandlerCanAddExplodedExtraParamsToTheNewRelicTrace()
  63. {
  64. $record = $this->getRecord(Logger::ERROR, 'log message');
  65. $record['extra'] = array('c' => array('key1' => 'value1', 'key2' => 'value2'));
  66. $handler = new StubNewRelicHandler(Logger::ERROR, true, self::$appname, true);
  67. $handler->handle($record);
  68. $this->assertEquals(
  69. array('extra_c_key1' => 'value1', 'extra_c_key2' => 'value2'),
  70. self::$customParameters
  71. );
  72. }
  73. public function testThehandlerCanAddExtraContextAndParamsToTheNewRelicTrace()
  74. {
  75. $record = $this->getRecord(Logger::ERROR, 'log message', array('a' => 'b'));
  76. $record['extra'] = array('c' => 'd');
  77. $handler = new StubNewRelicHandler();
  78. $handler->handle($record);
  79. $expected = array(
  80. 'context_a' => 'b',
  81. 'extra_c' => 'd',
  82. );
  83. $this->assertEquals($expected, self::$customParameters);
  84. }
  85. public function testTheAppNameIsNullByDefault()
  86. {
  87. $handler = new StubNewRelicHandler();
  88. $handler->handle($this->getRecord(Logger::ERROR, 'log message'));
  89. $this->assertEquals(null, self::$appname);
  90. }
  91. public function testTheAppNameCanBeInjectedFromtheConstructor()
  92. {
  93. $handler = new StubNewRelicHandler(Logger::DEBUG, false, 'myAppName');
  94. $handler->handle($this->getRecord(Logger::ERROR, 'log message'));
  95. $this->assertEquals('myAppName', self::$appname);
  96. }
  97. public function testTheAppNameCanBeOverriddenFromEachLog()
  98. {
  99. $handler = new StubNewRelicHandler(Logger::DEBUG, false, 'myAppName');
  100. $handler->handle($this->getRecord(Logger::ERROR, 'log message', array('appname' => 'logAppName')));
  101. $this->assertEquals('logAppName', self::$appname);
  102. }
  103. }
  104. class StubNewRelicHandlerWithoutExtension extends NewRelicHandler
  105. {
  106. protected function isNewRelicEnabled()
  107. {
  108. return false;
  109. }
  110. }
  111. class StubNewRelicHandler extends NewRelicHandler
  112. {
  113. protected function isNewRelicEnabled()
  114. {
  115. return true;
  116. }
  117. }
  118. function newrelic_notice_error()
  119. {
  120. return true;
  121. }
  122. function newrelic_set_appname($appname)
  123. {
  124. return NewRelicHandlerTest::$appname = $appname;
  125. }
  126. function newrelic_add_custom_parameter($key, $value)
  127. {
  128. NewRelicHandlerTest::$customParameters[$key] = $value;
  129. return true;
  130. }