NewRelicHandlerTest.php 5.6 KB

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