RavenHandlerTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 testExtraParameters()
  69. {
  70. $ravenClient = $this->getRavenClient();
  71. $handler = $this->getHandler($ravenClient);
  72. $checksum = '098f6bcd4621d373cade4e832627b4f6';
  73. $release = '05a671c66aefea124cc08b76ea6d30bb';
  74. $eventId = '31423';
  75. $record = $this->getRecord(Logger::INFO, 'test', array('checksum' => $checksum, 'release' => $release, 'event_id' => $eventId));
  76. $handler->handle($record);
  77. $this->assertEquals($checksum, $ravenClient->lastData['checksum']);
  78. $this->assertEquals($release, $ravenClient->lastData['release']);
  79. $this->assertEquals($eventId, $ravenClient->lastData['event_id']);
  80. }
  81. public function testFingerprint()
  82. {
  83. $ravenClient = $this->getRavenClient();
  84. $handler = $this->getHandler($ravenClient);
  85. $fingerprint = array('{{ default }}', 'other value');
  86. $record = $this->getRecord(Logger::INFO, 'test', array('fingerprint' => $fingerprint));
  87. $handler->handle($record);
  88. $this->assertEquals($fingerprint, $ravenClient->lastData['fingerprint']);
  89. }
  90. public function testUserContext()
  91. {
  92. $ravenClient = $this->getRavenClient();
  93. $handler = $this->getHandler($ravenClient);
  94. $recordWithNoContext = $this->getRecord(Logger::INFO, 'test with default user context');
  95. // set user context 'externally'
  96. $user = array(
  97. 'id' => '123',
  98. 'email' => 'test@test.com',
  99. );
  100. $recordWithContext = $this->getRecord(Logger::INFO, 'test', array('user' => $user));
  101. $ravenClient->user_context(array('id' => 'test_user_id'));
  102. // handle context
  103. $handler->handle($recordWithContext);
  104. $this->assertEquals($user, $ravenClient->lastData['user']);
  105. // check to see if its reset
  106. $handler->handle($recordWithNoContext);
  107. $this->assertInternalType('array', $ravenClient->context->user);
  108. $this->assertSame('test_user_id', $ravenClient->context->user['id']);
  109. // handle with null context
  110. $ravenClient->user_context(null);
  111. $handler->handle($recordWithContext);
  112. $this->assertEquals($user, $ravenClient->lastData['user']);
  113. // check to see if its reset
  114. $handler->handle($recordWithNoContext);
  115. $this->assertNull($ravenClient->context->user);
  116. }
  117. public function testException()
  118. {
  119. $ravenClient = $this->getRavenClient();
  120. $handler = $this->getHandler($ravenClient);
  121. try {
  122. $this->methodThatThrowsAnException();
  123. } catch (\Exception $e) {
  124. $record = $this->getRecord(Logger::ERROR, $e->getMessage(), array('exception' => $e));
  125. $handler->handle($record);
  126. }
  127. $this->assertEquals($record['message'], $ravenClient->lastData['message']);
  128. }
  129. public function testHandleBatch()
  130. {
  131. $records = $this->getMultipleRecords();
  132. $records[] = $this->getRecord(Logger::WARNING, 'warning');
  133. $records[] = $this->getRecord(Logger::WARNING, 'warning');
  134. $logFormatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
  135. $logFormatter->expects($this->once())->method('formatBatch');
  136. $formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
  137. $formatter->expects($this->once())->method('format')->with($this->callback(function ($record) {
  138. return $record['level'] == 400;
  139. }));
  140. $handler = $this->getHandler($this->getRavenClient());
  141. $handler->setBatchFormatter($logFormatter);
  142. $handler->setFormatter($formatter);
  143. $handler->handleBatch($records);
  144. }
  145. public function testHandleBatchDoNothingIfRecordsAreBelowLevel()
  146. {
  147. $records = array(
  148. $this->getRecord(Logger::DEBUG, 'debug message 1'),
  149. $this->getRecord(Logger::DEBUG, 'debug message 2'),
  150. $this->getRecord(Logger::INFO, 'information'),
  151. );
  152. $handler = $this->getMock('Monolog\Handler\RavenHandler', null, array($this->getRavenClient()));
  153. $handler->expects($this->never())->method('handle');
  154. $handler->setLevel(Logger::ERROR);
  155. $handler->handleBatch($records);
  156. }
  157. public function testHandleBatchPicksProperMessage()
  158. {
  159. $records = array(
  160. $this->getRecord(Logger::DEBUG, 'debug message 1'),
  161. $this->getRecord(Logger::DEBUG, 'debug message 2'),
  162. $this->getRecord(Logger::INFO, 'information 1'),
  163. $this->getRecord(Logger::ERROR, 'error 1'),
  164. $this->getRecord(Logger::WARNING, 'warning'),
  165. $this->getRecord(Logger::ERROR, 'error 2'),
  166. $this->getRecord(Logger::INFO, 'information 2'),
  167. );
  168. $logFormatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
  169. $logFormatter->expects($this->once())->method('formatBatch');
  170. $formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
  171. $formatter->expects($this->once())->method('format')->with($this->callback(function ($record) use ($records) {
  172. return $record['message'] == 'error 1';
  173. }));
  174. $handler = $this->getHandler($this->getRavenClient());
  175. $handler->setBatchFormatter($logFormatter);
  176. $handler->setFormatter($formatter);
  177. $handler->handleBatch($records);
  178. }
  179. public function testGetSetBatchFormatter()
  180. {
  181. $ravenClient = $this->getRavenClient();
  182. $handler = $this->getHandler($ravenClient);
  183. $handler->setBatchFormatter($formatter = new LineFormatter());
  184. $this->assertSame($formatter, $handler->getBatchFormatter());
  185. }
  186. public function testRelease()
  187. {
  188. $ravenClient = $this->getRavenClient();
  189. $handler = $this->getHandler($ravenClient);
  190. $release = 'v42.42.42';
  191. $handler->setRelease($release);
  192. $record = $this->getRecord(Logger::INFO, 'test');
  193. $handler->handle($record);
  194. $this->assertEquals($release, $ravenClient->lastData['release']);
  195. $localRelease = 'v41.41.41';
  196. $record = $this->getRecord(Logger::INFO, 'test', array('release' => $localRelease));
  197. $handler->handle($record);
  198. $this->assertEquals($localRelease, $ravenClient->lastData['release']);
  199. }
  200. private function methodThatThrowsAnException()
  201. {
  202. throw new \Exception('This is an exception');
  203. }
  204. }