RavenHandlerTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 Monolog\Formatter\LineFormatter;
  14. use Monolog\Handler\RavenHandler;
  15. class RavenHandlerTest extends TestCase
  16. {
  17. public function setUp()
  18. {
  19. if (!class_exists("Raven_Client")) {
  20. $this->markTestSkipped("raven/raven not installed");
  21. }
  22. require_once __DIR__ . '/MockRavenClient.php';
  23. }
  24. /**
  25. * @covers Monolog\Handler\RavenHandler::__construct
  26. */
  27. public function testConstruct()
  28. {
  29. $handler = new RavenHandler($this->getRavenClient());
  30. $this->assertInstanceOf('Monolog\Handler\RavenHandler', $handler);
  31. }
  32. protected function getHandler($ravenClient)
  33. {
  34. $handler = new RavenHandler($ravenClient);
  35. return $handler;
  36. }
  37. protected function getRavenClient()
  38. {
  39. $dsn = 'http://43f6017361224d098402974103bfc53d:a6a0538fc2934ba2bed32e08741b2cd3@marca.python.live.cheggnet.com:9000/1';
  40. return new MockRavenClient($dsn);
  41. }
  42. public function testDebug()
  43. {
  44. $ravenClient = $this->getRavenClient();
  45. $handler = $this->getHandler($ravenClient);
  46. $record = $this->getRecord(Logger::DEBUG, "A test debug message");
  47. $handler->handle($record);
  48. $this->assertEquals($ravenClient::DEBUG, $ravenClient->lastData['level']);
  49. $this->assertContains($record['message'], $ravenClient->lastData['message']);
  50. }
  51. public function testWarning()
  52. {
  53. $ravenClient = $this->getRavenClient();
  54. $handler = $this->getHandler($ravenClient);
  55. $record = $this->getRecord(Logger::WARNING, "A test warning message");
  56. $handler->handle($record);
  57. $this->assertEquals($ravenClient::WARNING, $ravenClient->lastData['level']);
  58. $this->assertContains($record['message'], $ravenClient->lastData['message']);
  59. }
  60. public function testException()
  61. {
  62. $ravenClient = $this->getRavenClient();
  63. $handler = $this->getHandler($ravenClient);
  64. try {
  65. $this->methodThatThrowsAnException();
  66. } catch (\Exception $e) {
  67. $record = $this->getRecord(Logger::ERROR, $e->getMessage(), array('exception' => $e));
  68. $handler->handle($record);
  69. }
  70. $this->assertEquals($record['message'], $ravenClient->lastData['message']);
  71. }
  72. public function testHandleBatch()
  73. {
  74. $records = $this->getMultipleRecords();
  75. $logFormatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
  76. $logFormatter->expects($this->once())->method('formatBatch');
  77. $formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
  78. $formatter->expects($this->once())->method('format');
  79. $handler = $this->getHandler($this->getRavenClient());
  80. $handler->setBatchFormatter($logFormatter);
  81. $handler->setFormatter($formatter);
  82. $handler->handleBatch($records);
  83. }
  84. public function testHandleBatchDoNothingIfRecordsAreBelowLevel()
  85. {
  86. $records = array(
  87. $this->getRecord(Logger::DEBUG, 'debug message 1'),
  88. $this->getRecord(Logger::DEBUG, 'debug message 2'),
  89. $this->getRecord(Logger::INFO, 'information'),
  90. );
  91. $handler = $this->getMock('Monolog\Handler\RavenHandler', null, array($this->getRavenClient()));
  92. $handler->expects($this->never())->method('handle');
  93. $handler->setLevel(Logger::ERROR);
  94. $handler->handleBatch($records);
  95. }
  96. public function testGetSetBatchFormatter()
  97. {
  98. $ravenClient = $this->getRavenClient();
  99. $handler = $this->getHandler($ravenClient);
  100. $handler->setBatchFormatter($formatter = new LineFormatter());
  101. $this->assertSame($formatter, $handler->getBatchFormatter());
  102. }
  103. private function methodThatThrowsAnException()
  104. {
  105. throw new \Exception('This is an exception');
  106. }
  107. }