MailHandlerTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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\Logger;
  12. use Monolog\Test\TestCase;
  13. class MailHandlerTest extends TestCase
  14. {
  15. /**
  16. * @covers Monolog\Handler\MailHandler::handleBatch
  17. */
  18. public function testHandleBatch()
  19. {
  20. $formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
  21. $formatter->expects($this->once())
  22. ->method('formatBatch'); // Each record is formatted
  23. $handler = $this->getMockForAbstractClass('Monolog\\Handler\\MailHandler');
  24. $handler->expects($this->once())
  25. ->method('send');
  26. $handler->expects($this->never())
  27. ->method('write'); // write is for individual records
  28. $handler->setFormatter($formatter);
  29. $handler->handleBatch($this->getMultipleRecords());
  30. }
  31. /**
  32. * @covers Monolog\Handler\MailHandler::handleBatch
  33. */
  34. public function testHandleBatchNotSendsMailIfMessagesAreBelowLevel()
  35. {
  36. $records = [
  37. $this->getRecord(Logger::DEBUG, 'debug message 1'),
  38. $this->getRecord(Logger::DEBUG, 'debug message 2'),
  39. $this->getRecord(Logger::INFO, 'information'),
  40. ];
  41. $handler = $this->getMockForAbstractClass('Monolog\\Handler\\MailHandler');
  42. $handler->expects($this->never())
  43. ->method('send');
  44. $handler->setLevel(Logger::ERROR);
  45. $handler->handleBatch($records);
  46. }
  47. /**
  48. * @covers Monolog\Handler\MailHandler::write
  49. */
  50. public function testHandle()
  51. {
  52. $handler = $this->getMockForAbstractClass('Monolog\\Handler\\MailHandler');
  53. $handler->setFormatter(new \Monolog\Formatter\LineFormatter);
  54. $record = $this->getRecord();
  55. $records = [$record];
  56. $records[0]['formatted'] = '['.$record['datetime'].'] test.WARNING: test [] []'."\n";
  57. $handler->expects($this->once())
  58. ->method('send')
  59. ->with($records[0]['formatted'], $records);
  60. $handler->handle($record);
  61. }
  62. }