2
0

SymfonyMailerHandlerTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\Logger;
  12. use Monolog\Test\TestCase;
  13. use PHPUnit\Framework\MockObject\MockObject;
  14. use Symfony\Component\Mailer\MailerInterface;
  15. use Symfony\Component\Mime\Email;
  16. class SymfonyMailerHandlerTest extends TestCase
  17. {
  18. /** @var MailerInterface&MockObject */
  19. private $mailer;
  20. public function setUp(): void
  21. {
  22. $this->mailer = $this
  23. ->getMockBuilder(MailerInterface::class)
  24. ->disableOriginalConstructor()
  25. ->getMock();
  26. }
  27. public function tearDown(): void
  28. {
  29. parent::tearDown();
  30. unset($this->mailer);
  31. }
  32. public function testMessageCreationIsLazyWhenUsingCallback()
  33. {
  34. $this->mailer->expects($this->never())
  35. ->method('send');
  36. $callback = function () {
  37. throw new \RuntimeException('Email creation callback should not have been called in this test');
  38. };
  39. $handler = new SymfonyMailerHandler($this->mailer, $callback);
  40. $records = [
  41. $this->getRecord(Logger::DEBUG),
  42. $this->getRecord(Logger::INFO),
  43. ];
  44. $handler->handleBatch($records);
  45. }
  46. public function testMessageCanBeCustomizedGivenLoggedData()
  47. {
  48. // Wire Mailer to expect a specific Email with a customized Subject
  49. $expectedMessage = new Email();
  50. $this->mailer->expects($this->once())
  51. ->method('send')
  52. ->with($this->callback(function ($value) use ($expectedMessage) {
  53. return $value instanceof Email
  54. && $value->getSubject() === 'Emergency'
  55. && $value === $expectedMessage;
  56. }));
  57. // Callback dynamically changes subject based on number of logged records
  58. $callback = function ($content, array $records) use ($expectedMessage) {
  59. $subject = count($records) > 0 ? 'Emergency' : 'Normal';
  60. return $expectedMessage->subject($subject);
  61. };
  62. $handler = new SymfonyMailerHandler($this->mailer, $callback);
  63. // Logging 1 record makes this an Emergency
  64. $records = [
  65. $this->getRecord(Logger::EMERGENCY),
  66. ];
  67. $handler->handleBatch($records);
  68. }
  69. public function testMessageSubjectFormatting()
  70. {
  71. // Wire Mailer to expect a specific Email with a customized Subject
  72. $messageTemplate = new Email();
  73. $messageTemplate->subject('Alert: %level_name% %message%');
  74. $receivedMessage = null;
  75. $this->mailer->expects($this->once())
  76. ->method('send')
  77. ->with($this->callback(function ($value) use (&$receivedMessage) {
  78. $receivedMessage = $value;
  79. return true;
  80. }));
  81. $handler = new SymfonyMailerHandler($this->mailer, $messageTemplate);
  82. $records = [
  83. $this->getRecord(Logger::EMERGENCY),
  84. ];
  85. $handler->handleBatch($records);
  86. $this->assertEquals('Alert: EMERGENCY test', $receivedMessage->getSubject());
  87. }
  88. }