RollbarHandlerTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php declare(strict_types=1);
  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\Test\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. private $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. $this->rollbarNotifier
  52. ->expects($this->any())
  53. ->method('report_exception')
  54. ->willReturnCallback(function ($exception, $context, $payload) {
  55. $this->reportedExceptionArguments = compact('exception', 'context', 'payload');
  56. });
  57. }
  58. private function createHandler(): RollbarHandler
  59. {
  60. return new RollbarHandler($this->rollbarNotifier, Logger::DEBUG);
  61. }
  62. private function createExceptionRecord($level = Logger::DEBUG, $message = 'test', $exception = null): array
  63. {
  64. return $this->getRecord($level, $message, [
  65. 'exception' => $exception ?: new Exception(),
  66. ]);
  67. }
  68. }