RavenHandlerTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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(1, 2, 'foo');
  64. $record = $this->getRecord(Logger::INFO, "test", array('tags' => $tags));
  65. $handler->handle($record);
  66. $this->assertEquals($tags, $ravenClient->lastData['tags']);
  67. }
  68. public function testUserContext()
  69. {
  70. $ravenClient = $this->getRavenClient();
  71. $handler = $this->getHandler($ravenClient);
  72. $user = array(
  73. 'id' => '123',
  74. 'email' => 'test@test.com'
  75. );
  76. $record = $this->getRecord(Logger::INFO, "test", array('user' => $user));
  77. $handler->handle($record);
  78. $this->assertEquals($user, $ravenClient->context->user);
  79. $secondRecord = $this->getRecord(Logger::INFO, "test without user");
  80. $handler->handle($secondRecord);
  81. $this->assertNull($ravenClient->context->user);
  82. }
  83. public function testException()
  84. {
  85. $ravenClient = $this->getRavenClient();
  86. $handler = $this->getHandler($ravenClient);
  87. try {
  88. $this->methodThatThrowsAnException();
  89. } catch (\Exception $e) {
  90. $record = $this->getRecord(Logger::ERROR, $e->getMessage(), array('exception' => $e));
  91. $handler->handle($record);
  92. }
  93. $this->assertEquals($record['message'], $ravenClient->lastData['message']);
  94. }
  95. public function testHandleBatch()
  96. {
  97. $records = $this->getMultipleRecords();
  98. $records[] = $this->getRecord(Logger::WARNING, 'warning');
  99. $records[] = $this->getRecord(Logger::WARNING, 'warning');
  100. $logFormatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
  101. $logFormatter->expects($this->once())->method('formatBatch');
  102. $formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
  103. $formatter->expects($this->once())->method('format')->with($this->callback(function ($record) {
  104. return $record['level'] == 400;
  105. }));
  106. $handler = $this->getHandler($this->getRavenClient());
  107. $handler->setBatchFormatter($logFormatter);
  108. $handler->setFormatter($formatter);
  109. $handler->handleBatch($records);
  110. }
  111. public function testHandleBatchDoNothingIfRecordsAreBelowLevel()
  112. {
  113. $records = array(
  114. $this->getRecord(Logger::DEBUG, 'debug message 1'),
  115. $this->getRecord(Logger::DEBUG, 'debug message 2'),
  116. $this->getRecord(Logger::INFO, 'information'),
  117. );
  118. $handler = $this->getMock('Monolog\Handler\RavenHandler', null, array($this->getRavenClient()));
  119. $handler->expects($this->never())->method('handle');
  120. $handler->setLevel(Logger::ERROR);
  121. $handler->handleBatch($records);
  122. }
  123. public function testGetSetBatchFormatter()
  124. {
  125. $ravenClient = $this->getRavenClient();
  126. $handler = $this->getHandler($ravenClient);
  127. $handler->setBatchFormatter($formatter = new LineFormatter());
  128. $this->assertSame($formatter, $handler->getBatchFormatter());
  129. }
  130. private function methodThatThrowsAnException()
  131. {
  132. throw new \Exception('This is an exception');
  133. }
  134. }