NewRelicHandlerTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 function setUp()
  18. {
  19. $this::$appname = null;
  20. }
  21. /**
  22. * @expectedException Monolog\Handler\MissingExtensionException
  23. */
  24. public function testThehandlerThrowsAnExceptionIfTheNRExtensionIsNotLoaded()
  25. {
  26. $handler = new StubNewRelicHandlerWithoutExtension();
  27. $handler->handle($this->getRecord(Logger::ERROR));
  28. }
  29. public function testThehandlerCanHandleTheRecord()
  30. {
  31. $handler = new StubNewRelicHandler();
  32. $handler->handle($this->getRecord(Logger::ERROR));
  33. }
  34. public function testThehandlerCanAddParamsToTheNewRelicTrace()
  35. {
  36. $handler = new StubNewRelicHandler();
  37. $handler->handle($this->getRecord(Logger::ERROR, 'log message', array('a' => 'b')));
  38. }
  39. public function testTheAppNameIsNullByDefault()
  40. {
  41. $handler = new StubNewRelicHandler();
  42. $handler->handle($this->getRecord(Logger::ERROR, 'log message'));
  43. $this->assertEquals(null, $this::$appname);
  44. }
  45. public function testTheAppNameCanBeInjectedFromtheConstructor()
  46. {
  47. $handler = new StubNewRelicHandler(LogLevel::ALERT, false, 'myAppName');
  48. $handler->handle($this->getRecord(Logger::ERROR, 'log message'));
  49. $this->assertEquals('myAppName', $this::$appname);
  50. }
  51. public function testTheAppNameCanBeOverriddenFromEachLog()
  52. {
  53. $handler = new StubNewRelicHandler(LogLevel::ALERT, false, 'myAppName');
  54. $handler->handle($this->getRecord(Logger::ERROR, 'log message', array('appname' => 'logAppName')));
  55. $this->assertEquals('logAppName', $this::$appname);
  56. }
  57. }
  58. class StubNewRelicHandlerWithoutExtension extends NewRelicHandler
  59. {
  60. protected function isNewRelicEnabled()
  61. {
  62. return false;
  63. }
  64. }
  65. class StubNewRelicHandler extends NewRelicHandler
  66. {
  67. protected function isNewRelicEnabled()
  68. {
  69. return true;
  70. }
  71. }
  72. function newrelic_notice_error()
  73. {
  74. return true;
  75. }
  76. function newrelic_set_appname($appname)
  77. {
  78. return NewRelicHandlerTest::$appname = $appname;
  79. }
  80. function newrelic_add_custom_parameter()
  81. {
  82. return true;
  83. }