MailHandlerTest.php 2.2 KB

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