RavenHandlerTest.php 4.6 KB

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