MailerAssertionsTrait.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  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 Symfony\Bundle\FrameworkBundle\Test;
  11. use PHPUnit\Framework\Constraint\LogicalNot;
  12. use Symfony\Component\Mailer\Event\MessageEvent;
  13. use Symfony\Component\Mailer\Event\MessageEvents;
  14. use Symfony\Component\Mailer\Test\Constraint as MailerConstraint;
  15. use Symfony\Component\Mime\RawMessage;
  16. use Symfony\Component\Mime\Test\Constraint as MimeConstraint;
  17. trait MailerAssertionsTrait
  18. {
  19. public static function assertEmailCount(int $count, string $transport = null, string $message = ''): void
  20. {
  21. self::assertThat(self::getMessageMailerEvents(), new MailerConstraint\EmailCount($count, $transport), $message);
  22. }
  23. public static function assertQueuedEmailCount(int $count, string $transport = null, string $message = ''): void
  24. {
  25. self::assertThat(self::getMessageMailerEvents(), new MailerConstraint\EmailCount($count, $transport, true), $message);
  26. }
  27. public static function assertEmailIsQueued(MessageEvent $event, string $message = ''): void
  28. {
  29. self::assertThat($event, new MailerConstraint\EmailIsQueued(), $message);
  30. }
  31. public static function assertEmailIsNotQueued(MessageEvent $event, string $message = ''): void
  32. {
  33. self::assertThat($event, new LogicalNot(new MailerConstraint\EmailIsQueued()), $message);
  34. }
  35. public static function assertEmailAttachmentCount(RawMessage $email, int $count, string $message = ''): void
  36. {
  37. self::assertThat($email, new MimeConstraint\EmailAttachmentCount($count), $message);
  38. }
  39. public static function assertEmailTextBodyContains(RawMessage $email, string $text, string $message = ''): void
  40. {
  41. self::assertThat($email, new MimeConstraint\EmailTextBodyContains($text), $message);
  42. }
  43. public static function assertEmailTextBodyNotContains(RawMessage $email, string $text, string $message = ''): void
  44. {
  45. self::assertThat($email, new LogicalNot(new MimeConstraint\EmailTextBodyContains($text)), $message);
  46. }
  47. public static function assertEmailHtmlBodyContains(RawMessage $email, string $text, string $message = ''): void
  48. {
  49. self::assertThat($email, new MimeConstraint\EmailHtmlBodyContains($text), $message);
  50. }
  51. public static function assertEmailHtmlBodyNotContains(RawMessage $email, string $text, string $message = ''): void
  52. {
  53. self::assertThat($email, new LogicalNot(new MimeConstraint\EmailHtmlBodyContains($text)), $message);
  54. }
  55. public static function assertEmailHasHeader(RawMessage $email, string $headerName, string $message = ''): void
  56. {
  57. self::assertThat($email, new MimeConstraint\EmailHasHeader($headerName), $message);
  58. }
  59. public static function assertEmailNotHasHeader(RawMessage $email, string $headerName, string $message = ''): void
  60. {
  61. self::assertThat($email, new LogicalNot(new MimeConstraint\EmailHasHeader($headerName)), $message);
  62. }
  63. public static function assertEmailHeaderSame(RawMessage $email, string $headerName, string $expectedValue, string $message = ''): void
  64. {
  65. self::assertThat($email, new MimeConstraint\EmailHeaderSame($headerName, $expectedValue), $message);
  66. }
  67. public static function assertEmailHeaderNotSame(RawMessage $email, string $headerName, string $expectedValue, string $message = ''): void
  68. {
  69. self::assertThat($email, new LogicalNot(new MimeConstraint\EmailHeaderSame($headerName, $expectedValue)), $message);
  70. }
  71. public static function assertEmailAddressContains(RawMessage $email, string $headerName, string $expectedValue, string $message = ''): void
  72. {
  73. self::assertThat($email, new MimeConstraint\EmailAddressContains($headerName, $expectedValue), $message);
  74. }
  75. /**
  76. * @return MessageEvents[]
  77. */
  78. public static function getMailerEvents(string $transport = null): array
  79. {
  80. return self::getMessageMailerEvents()->getEvents($transport);
  81. }
  82. public static function getMailerEvent(int $index = 0, string $transport = null): ?MessageEvent
  83. {
  84. return self::getMailerEvents($transport)[$index] ?? null;
  85. }
  86. /**
  87. * @return RawMessage[]
  88. */
  89. public static function getMailerMessages(string $transport = null): array
  90. {
  91. return self::getMessageMailerEvents()->getMessages($transport);
  92. }
  93. public static function getMailerMessage(int $index = 0, string $transport = null): ?RawMessage
  94. {
  95. return self::getMailerMessages($transport)[$index] ?? null;
  96. }
  97. private static function getMessageMailerEvents(): MessageEvents
  98. {
  99. if (self::$container->has('mailer.message_logger_listener')) {
  100. return self::$container->get('mailer.message_logger_listener')->getEvents();
  101. }
  102. if (self::$container->has('mailer.logger_message_listener')) {
  103. return self::$container->get('mailer.logger_message_listener')->getEvents();
  104. }
  105. static::fail('A client must have Mailer enabled to make email assertions. Did you forget to require symfony/mailer?');
  106. }
  107. }