MailHandlerTest.php 2.2KB

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\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 = array(
  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. $record = $this->getRecord();
  54. $records = array($record);
  55. $records[0]['formatted'] = '['.$record['datetime']->format('Y-m-d H:i:s').'] test.WARNING: test [] []'."\n";
  56. $handler->expects($this->once())
  57. ->method('send')
  58. ->with($records[0]['formatted'], $records);
  59. $handler->handle($record);
  60. }
  61. }