RollbarHandlerTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 Exception;
  12. use Monolog\TestCase;
  13. use Monolog\Logger;
  14. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  15. /**
  16. * @author Erik Johansson <erik.pm.johansson@gmail.com>
  17. * @see https://rollbar.com/docs/notifier/rollbar-php/
  18. *
  19. * @coversDefaultClass Monolog\Handler\RollbarHandler
  20. */
  21. class RollbarHandlerTest extends TestCase
  22. {
  23. /**
  24. * @var MockObject
  25. */
  26. private $rollbarNotifier;
  27. /**
  28. * @var array
  29. */
  30. public $reportedExceptionArguments = null;
  31. protected function setUp()
  32. {
  33. parent::setUp();
  34. $this->setupRollbarNotifierMock();
  35. }
  36. /**
  37. * When reporting exceptions to Rollbar the
  38. * level has to be set in the payload data
  39. */
  40. public function testExceptionLogLevel()
  41. {
  42. $handler = $this->createHandler();
  43. $handler->handle($this->createExceptionRecord(Logger::DEBUG));
  44. $this->assertEquals('debug', $this->reportedExceptionArguments['payload']['level']);
  45. }
  46. private function setupRollbarNotifierMock()
  47. {
  48. $this->rollbarNotifier = $this->getMockBuilder('RollbarNotifier')
  49. ->setMethods(array('report_message', 'report_exception', 'flush'))
  50. ->getMock();
  51. $that = $this;
  52. $this->rollbarNotifier
  53. ->expects($this->any())
  54. ->method('report_exception')
  55. ->willReturnCallback(function ($exception, $context, $payload) use ($that) {
  56. $that->reportedExceptionArguments = compact('exception', 'context', 'payload');
  57. });
  58. }
  59. private function createHandler()
  60. {
  61. return new RollbarHandler($this->rollbarNotifier, Logger::DEBUG);
  62. }
  63. private function createExceptionRecord($level = Logger::DEBUG, $message = 'test', $exception = null)
  64. {
  65. return $this->getRecord($level, $message, array(
  66. 'exception' => $exception ?: new Exception()
  67. ));
  68. }
  69. }