MailSesTransportTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace Illuminate\Tests\Mail;
  3. use Aws\Ses\SesClient;
  4. use Illuminate\Config\Repository;
  5. use Illuminate\Container\Container;
  6. use Illuminate\Mail\MailManager;
  7. use Illuminate\Mail\Transport\SesTransport;
  8. use Illuminate\Support\Str;
  9. use Illuminate\View\Factory;
  10. use PHPUnit\Framework\TestCase;
  11. use Swift_Message;
  12. class MailSesTransportTest extends TestCase
  13. {
  14. public function testGetTransport()
  15. {
  16. $container = new Container;
  17. $container->singleton('config', function () {
  18. return new Repository([
  19. 'services.ses' => [
  20. 'key' => 'foo',
  21. 'secret' => 'bar',
  22. 'region' => 'us-east-1',
  23. ],
  24. ]);
  25. });
  26. $manager = new MailManager($container);
  27. /** @var \Illuminate\Mail\Transport\SesTransport $transport */
  28. $transport = $manager->createTransport(['transport' => 'ses']);
  29. $ses = $transport->ses();
  30. $this->assertSame('us-east-1', $ses->getRegion());
  31. }
  32. public function testSend()
  33. {
  34. $message = new Swift_Message('Foo subject', 'Bar body');
  35. $message->setSender('myself@example.com');
  36. $message->setTo('me@example.com');
  37. $message->setBcc('you@example.com');
  38. $client = $this->getMockBuilder(SesClient::class)
  39. ->addMethods(['sendRawEmail'])
  40. ->disableOriginalConstructor()
  41. ->getMock();
  42. $transport = new SesTransport($client);
  43. // Generate a messageId for our mock to return to ensure that the post-sent message
  44. // has X-Message-ID in its headers
  45. $messageId = Str::random(32);
  46. $sendRawEmailMock = new SendRawEmailMock($messageId);
  47. $client->expects($this->once())
  48. ->method('sendRawEmail')
  49. ->with($this->equalTo([
  50. 'Source' => 'myself@example.com',
  51. 'RawMessage' => ['Data' => (string) $message],
  52. ]))
  53. ->willReturn($sendRawEmailMock);
  54. $transport->send($message);
  55. $this->assertEquals($messageId, $message->getHeaders()->get('X-Message-ID')->getFieldBody());
  56. $this->assertEquals($messageId, $message->getHeaders()->get('X-SES-Message-ID')->getFieldBody());
  57. }
  58. public function testSesLocalConfiguration()
  59. {
  60. $container = new Container;
  61. $container->singleton('config', function () {
  62. return new Repository([
  63. 'mail' => [
  64. 'mailers' => [
  65. 'ses' => [
  66. 'transport' => 'ses',
  67. 'region' => 'eu-west-1',
  68. 'options' => [
  69. 'ConfigurationSetName' => 'Laravel',
  70. 'Tags' => [
  71. ['Name' => 'Laravel', 'Value' => 'Framework'],
  72. ],
  73. ],
  74. ],
  75. ],
  76. ],
  77. 'services' => [
  78. 'ses' => [
  79. 'region' => 'us-east-1',
  80. ],
  81. ],
  82. ]);
  83. });
  84. $container->instance('view', $this->createMock(Factory::class));
  85. $container->bind('events', function () {
  86. return null;
  87. });
  88. $manager = new MailManager($container);
  89. /** @var \Illuminate\Mail\Mailer $mailer */
  90. $mailer = $manager->mailer('ses');
  91. /** @var \Illuminate\Mail\Transport\SesTransport $transport */
  92. $transport = $mailer->getSwiftMailer()->getTransport();
  93. $this->assertSame('eu-west-1', $transport->ses()->getRegion());
  94. $this->assertSame([
  95. 'ConfigurationSetName' => 'Laravel',
  96. 'Tags' => [
  97. ['Name' => 'Laravel', 'Value' => 'Framework'],
  98. ],
  99. ], $transport->getOptions());
  100. }
  101. }
  102. class SendRawEmailMock
  103. {
  104. protected $getResponse;
  105. public function __construct($responseValue)
  106. {
  107. $this->getResponse = $responseValue;
  108. }
  109. public function get($key)
  110. {
  111. return $this->getResponse;
  112. }
  113. }