MailHandlerTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php declare(strict_types=1);
  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\Level;
  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->createMock('Monolog\Formatter\FormatterInterface');
  21. $formatter->expects($this->once())
  22. ->method('formatBatch'); // Each record is formatted
  23. $handler = $this->createPartialMock('Monolog\Handler\MailHandler', ['send', 'write']);
  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(Level::Debug, 'debug message 1'),
  38. $this->getRecord(Level::Debug, 'debug message 2'),
  39. $this->getRecord(Level::Info, 'information'),
  40. ];
  41. $handler = $this->createPartialMock('Monolog\Handler\MailHandler', ['send']);
  42. $handler->expects($this->never())
  43. ->method('send');
  44. $handler->setLevel(Level::Error);
  45. $handler->handleBatch($records);
  46. }
  47. /**
  48. * @covers Monolog\Handler\MailHandler::write
  49. */
  50. public function testHandle()
  51. {
  52. $handler = $this->createPartialMock('Monolog\Handler\MailHandler', ['send']);
  53. $handler->setFormatter(new \Monolog\Formatter\LineFormatter);
  54. $record = $this->getRecord(message: 'test handle');
  55. $record->formatted = '['.$record->datetime.'] test.WARNING: test handle [] []'."\n";
  56. $handler->expects($this->once())
  57. ->method('send')
  58. ->with($record->formatted, [$record]);
  59. $handler->handle($record);
  60. }
  61. }