RavenHandlerTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 testException()
  60. {
  61. $ravenClient = $this->getRavenClient();
  62. $handler = $this->getHandler($ravenClient);
  63. try {
  64. $this->methodThatThrowsAnException();
  65. } catch (\Exception $e) {
  66. $record = $this->getRecord(Logger::ERROR, $e->getMessage(), array('exception' => $e));
  67. $handler->handle($record);
  68. }
  69. $this->assertEquals($record['message'], $ravenClient->lastData['message']);
  70. }
  71. public function testHandleBatch()
  72. {
  73. $records = $this->getMultipleRecords();
  74. $records[] = $this->getRecord(Logger::WARNING, 'warning');
  75. $records[] = $this->getRecord(Logger::WARNING, 'warning');
  76. $logFormatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
  77. $logFormatter->expects($this->once())->method('formatBatch');
  78. $formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
  79. $formatter->expects($this->once())->method('format')->with($this->callback(function ($record) {
  80. return $record['level'] == 400;
  81. }));
  82. $handler = $this->getHandler($this->getRavenClient());
  83. $handler->setBatchFormatter($logFormatter);
  84. $handler->setFormatter($formatter);
  85. $handler->handleBatch($records);
  86. }
  87. public function testHandleBatchDoNothingIfRecordsAreBelowLevel()
  88. {
  89. $records = array(
  90. $this->getRecord(Logger::DEBUG, 'debug message 1'),
  91. $this->getRecord(Logger::DEBUG, 'debug message 2'),
  92. $this->getRecord(Logger::INFO, 'information'),
  93. );
  94. $handler = $this->getMock('Monolog\Handler\RavenHandler', null, array($this->getRavenClient()));
  95. $handler->expects($this->never())->method('handle');
  96. $handler->setLevel(Logger::ERROR);
  97. $handler->handleBatch($records);
  98. }
  99. public function testGetSetBatchFormatter()
  100. {
  101. $ravenClient = $this->getRavenClient();
  102. $handler = $this->getHandler($ravenClient);
  103. $handler->setBatchFormatter($formatter = new LineFormatter());
  104. $this->assertSame($formatter, $handler->getBatchFormatter());
  105. }
  106. private function methodThatThrowsAnException()
  107. {
  108. throw new \Exception('This is an exception');
  109. }
  110. }