MailMailerTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. namespace Illuminate\Tests\Mail;
  3. use Illuminate\Contracts\Events\Dispatcher;
  4. use Illuminate\Contracts\View\Factory;
  5. use Illuminate\Mail\Events\MessageSending;
  6. use Illuminate\Mail\Events\MessageSent;
  7. use Illuminate\Mail\Mailer;
  8. use Illuminate\Support\HtmlString;
  9. use Mockery as m;
  10. use PHPUnit\Framework\TestCase;
  11. use stdClass;
  12. use Swift_Mailer;
  13. use Swift_Message;
  14. use Swift_Mime_SimpleMessage;
  15. use Swift_Transport;
  16. class MailMailerTest extends TestCase
  17. {
  18. protected function tearDown(): void
  19. {
  20. m::close();
  21. }
  22. public function testMailerSendSendsMessageWithProperViewContent()
  23. {
  24. unset($_SERVER['__mailer.test']);
  25. $mailer = $this->getMockBuilder(Mailer::class)->onlyMethods(['createMessage'])->setConstructorArgs($this->getMocks())->getMock();
  26. $message = m::mock(Swift_Mime_SimpleMessage::class);
  27. $mailer->expects($this->once())->method('createMessage')->willReturn($message);
  28. $view = m::mock(stdClass::class);
  29. $mailer->getViewFactory()->shouldReceive('make')->once()->with('foo', ['data', 'message' => $message])->andReturn($view);
  30. $view->shouldReceive('render')->once()->andReturn('rendered.view');
  31. $message->shouldReceive('setBody')->once()->with('rendered.view', 'text/html');
  32. $message->shouldReceive('setFrom')->never();
  33. $this->setSwiftMailer($mailer);
  34. $message->shouldReceive('getSwiftMessage')->once()->andReturn($message);
  35. $mailer->getSwiftMailer()->shouldReceive('send')->once()->with($message, []);
  36. $mailer->send('foo', ['data'], function ($m) {
  37. $_SERVER['__mailer.test'] = $m;
  38. });
  39. unset($_SERVER['__mailer.test']);
  40. }
  41. public function testMailerSendSendsMessageWithProperViewContentUsingHtmlStrings()
  42. {
  43. unset($_SERVER['__mailer.test']);
  44. $mailer = $this->getMockBuilder(Mailer::class)->onlyMethods(['createMessage'])->setConstructorArgs($this->getMocks())->getMock();
  45. $message = m::mock(Swift_Mime_SimpleMessage::class);
  46. $mailer->expects($this->once())->method('createMessage')->willReturn($message);
  47. $view = m::mock(stdClass::class);
  48. $mailer->getViewFactory()->shouldReceive('make')->never();
  49. $view->shouldReceive('render')->never();
  50. $message->shouldReceive('setBody')->once()->with('rendered.view', 'text/html');
  51. $message->shouldReceive('addPart')->once()->with('rendered.text', 'text/plain');
  52. $message->shouldReceive('setFrom')->never();
  53. $this->setSwiftMailer($mailer);
  54. $message->shouldReceive('getSwiftMessage')->once()->andReturn($message);
  55. $mailer->getSwiftMailer()->shouldReceive('send')->once()->with($message, []);
  56. $mailer->send(['html' => new HtmlString('rendered.view'), 'text' => new HtmlString('rendered.text')], ['data'], function ($m) {
  57. $_SERVER['__mailer.test'] = $m;
  58. });
  59. unset($_SERVER['__mailer.test']);
  60. }
  61. public function testMailerSendSendsMessageWithProperViewContentUsingHtmlMethod()
  62. {
  63. unset($_SERVER['__mailer.test']);
  64. $mailer = $this->getMockBuilder(Mailer::class)->onlyMethods(['createMessage'])->setConstructorArgs($this->getMocks())->getMock();
  65. $message = m::mock(Swift_Mime_SimpleMessage::class);
  66. $mailer->expects($this->once())->method('createMessage')->willReturn($message);
  67. $view = m::mock(stdClass::class);
  68. $mailer->getViewFactory()->shouldReceive('make')->never();
  69. $view->shouldReceive('render')->never();
  70. $message->shouldReceive('setBody')->once()->with('rendered.view', 'text/html');
  71. $message->shouldReceive('setFrom')->never();
  72. $this->setSwiftMailer($mailer);
  73. $message->shouldReceive('getSwiftMessage')->once()->andReturn($message);
  74. $mailer->getSwiftMailer()->shouldReceive('send')->once()->with($message, []);
  75. $mailer->html('rendered.view', function ($m) {
  76. $_SERVER['__mailer.test'] = $m;
  77. });
  78. unset($_SERVER['__mailer.test']);
  79. }
  80. public function testMailerSendSendsMessageWithProperPlainViewContent()
  81. {
  82. unset($_SERVER['__mailer.test']);
  83. $mailer = $this->getMockBuilder(Mailer::class)->onlyMethods(['createMessage'])->setConstructorArgs($this->getMocks())->getMock();
  84. $message = m::mock(Swift_Mime_SimpleMessage::class);
  85. $mailer->expects($this->once())->method('createMessage')->willReturn($message);
  86. $view = m::mock(stdClass::class);
  87. $mailer->getViewFactory()->shouldReceive('make')->once()->with('foo', ['data', 'message' => $message])->andReturn($view);
  88. $mailer->getViewFactory()->shouldReceive('make')->once()->with('bar', ['data', 'message' => $message])->andReturn($view);
  89. $view->shouldReceive('render')->twice()->andReturn('rendered.view');
  90. $message->shouldReceive('setBody')->once()->with('rendered.view', 'text/html');
  91. $message->shouldReceive('addPart')->once()->with('rendered.view', 'text/plain');
  92. $message->shouldReceive('setFrom')->never();
  93. $this->setSwiftMailer($mailer);
  94. $message->shouldReceive('getSwiftMessage')->once()->andReturn($message);
  95. $mailer->getSwiftMailer()->shouldReceive('send')->once()->with($message, []);
  96. $mailer->send(['foo', 'bar'], ['data'], function ($m) {
  97. $_SERVER['__mailer.test'] = $m;
  98. });
  99. unset($_SERVER['__mailer.test']);
  100. }
  101. public function testMailerSendSendsMessageWithProperPlainViewContentWhenExplicit()
  102. {
  103. unset($_SERVER['__mailer.test']);
  104. $mailer = $this->getMockBuilder(Mailer::class)->onlyMethods(['createMessage'])->setConstructorArgs($this->getMocks())->getMock();
  105. $message = m::mock(Swift_Mime_SimpleMessage::class);
  106. $mailer->expects($this->once())->method('createMessage')->willReturn($message);
  107. $view = m::mock(stdClass::class);
  108. $mailer->getViewFactory()->shouldReceive('make')->once()->with('foo', ['data', 'message' => $message])->andReturn($view);
  109. $mailer->getViewFactory()->shouldReceive('make')->once()->with('bar', ['data', 'message' => $message])->andReturn($view);
  110. $view->shouldReceive('render')->twice()->andReturn('rendered.view');
  111. $message->shouldReceive('setBody')->once()->with('rendered.view', 'text/html');
  112. $message->shouldReceive('addPart')->once()->with('rendered.view', 'text/plain');
  113. $message->shouldReceive('setFrom')->never();
  114. $this->setSwiftMailer($mailer);
  115. $message->shouldReceive('getSwiftMessage')->once()->andReturn($message);
  116. $mailer->getSwiftMailer()->shouldReceive('send')->once()->with($message, []);
  117. $mailer->send(['html' => 'foo', 'text' => 'bar'], ['data'], function ($m) {
  118. $_SERVER['__mailer.test'] = $m;
  119. });
  120. unset($_SERVER['__mailer.test']);
  121. }
  122. public function testGlobalFromIsRespectedOnAllMessages()
  123. {
  124. unset($_SERVER['__mailer.test']);
  125. $mailer = $this->getMailer();
  126. $view = m::mock(stdClass::class);
  127. $mailer->getViewFactory()->shouldReceive('make')->once()->andReturn($view);
  128. $view->shouldReceive('render')->once()->andReturn('rendered.view');
  129. $this->setSwiftMailer($mailer);
  130. $mailer->alwaysFrom('taylorotwell@gmail.com', 'Taylor Otwell');
  131. $mailer->getSwiftMailer()->shouldReceive('send')->once()->with(m::type(Swift_Message::class), [])->andReturnUsing(function ($message) {
  132. $this->assertEquals(['taylorotwell@gmail.com' => 'Taylor Otwell'], $message->getFrom());
  133. });
  134. $mailer->send('foo', ['data'], function ($m) {
  135. //
  136. });
  137. }
  138. public function testGlobalReplyToIsRespectedOnAllMessages()
  139. {
  140. unset($_SERVER['__mailer.test']);
  141. $mailer = $this->getMailer();
  142. $view = m::mock(stdClass::class);
  143. $mailer->getViewFactory()->shouldReceive('make')->once()->andReturn($view);
  144. $view->shouldReceive('render')->once()->andReturn('rendered.view');
  145. $this->setSwiftMailer($mailer);
  146. $mailer->alwaysReplyTo('taylorotwell@gmail.com', 'Taylor Otwell');
  147. $mailer->getSwiftMailer()->shouldReceive('send')->once()->with(m::type(Swift_Message::class), [])->andReturnUsing(function ($message) {
  148. $this->assertEquals(['taylorotwell@gmail.com' => 'Taylor Otwell'], $message->getReplyTo());
  149. });
  150. $mailer->send('foo', ['data'], function ($m) {
  151. //
  152. });
  153. }
  154. public function testGlobalToIsRespectedOnAllMessages()
  155. {
  156. unset($_SERVER['__mailer.test']);
  157. $mailer = $this->getMailer();
  158. $view = m::mock(stdClass::class);
  159. $mailer->getViewFactory()->shouldReceive('make')->once()->andReturn($view);
  160. $view->shouldReceive('render')->once()->andReturn('rendered.view');
  161. $this->setSwiftMailer($mailer);
  162. $mailer->alwaysTo('taylorotwell@gmail.com', 'Taylor Otwell');
  163. $mailer->getSwiftMailer()->shouldReceive('send')->once()->with(m::type(Swift_Message::class), [])->andReturnUsing(function ($message) {
  164. $this->assertEquals(['taylorotwell@gmail.com' => 'Taylor Otwell'], $message->getTo());
  165. });
  166. $mailer->send('foo', ['data'], function ($m) {
  167. //
  168. });
  169. }
  170. public function testGlobalReturnPathIsRespectedOnAllMessages()
  171. {
  172. unset($_SERVER['__mailer.test']);
  173. $mailer = $this->getMailer();
  174. $view = m::mock(stdClass::class);
  175. $mailer->getViewFactory()->shouldReceive('make')->once()->andReturn($view);
  176. $view->shouldReceive('render')->once()->andReturn('rendered.view');
  177. $this->setSwiftMailer($mailer);
  178. $mailer->alwaysReturnPath('taylorotwell@gmail.com');
  179. $mailer->getSwiftMailer()->shouldReceive('send')->once()->with(m::type(Swift_Message::class), [])->andReturnUsing(function ($message) {
  180. $this->assertSame('taylorotwell@gmail.com', $message->getReturnPath());
  181. });
  182. $mailer->send('foo', ['data'], function ($m) {
  183. //
  184. });
  185. }
  186. public function testFailedRecipientsAreAppendedAndCanBeRetrieved()
  187. {
  188. unset($_SERVER['__mailer.test']);
  189. $mailer = $this->getMailer();
  190. $mailer->getSwiftMailer()->shouldReceive('getTransport')->andReturn($transport = m::mock(Swift_Transport::class));
  191. $transport->shouldReceive('stop');
  192. $view = m::mock(stdClass::class);
  193. $mailer->getViewFactory()->shouldReceive('make')->once()->andReturn($view);
  194. $view->shouldReceive('render')->once()->andReturn('rendered.view');
  195. $swift = new FailingSwiftMailerStub;
  196. $mailer->setSwiftMailer($swift);
  197. $mailer->send('foo', ['data'], function ($m) {
  198. //
  199. });
  200. $this->assertEquals(['taylorotwell@gmail.com'], $mailer->failures());
  201. }
  202. public function testEventsAreDispatched()
  203. {
  204. unset($_SERVER['__mailer.test']);
  205. $events = m::mock(Dispatcher::class);
  206. $events->shouldReceive('until')->once()->with(m::type(MessageSending::class));
  207. $events->shouldReceive('dispatch')->once()->with(m::type(MessageSent::class));
  208. $mailer = $this->getMailer($events);
  209. $view = m::mock(stdClass::class);
  210. $mailer->getViewFactory()->shouldReceive('make')->once()->andReturn($view);
  211. $view->shouldReceive('render')->once()->andReturn('rendered.view');
  212. $this->setSwiftMailer($mailer);
  213. $mailer->getSwiftMailer()->shouldReceive('send')->once()->with(m::type(Swift_Message::class), []);
  214. $mailer->send('foo', ['data'], function ($m) {
  215. //
  216. });
  217. }
  218. public function testMacroable()
  219. {
  220. Mailer::macro('foo', function () {
  221. return 'bar';
  222. });
  223. $mailer = $this->getMailer();
  224. $this->assertSame(
  225. 'bar', $mailer->foo()
  226. );
  227. }
  228. protected function getMailer($events = null)
  229. {
  230. return new Mailer('smtp', m::mock(Factory::class), m::mock(Swift_Mailer::class), $events);
  231. }
  232. public function setSwiftMailer($mailer)
  233. {
  234. $swift = m::mock(Swift_Mailer::class);
  235. $swift->shouldReceive('createMessage')->andReturn(new Swift_Message);
  236. $swift->shouldReceive('getTransport')->andReturn($transport = m::mock(Swift_Transport::class));
  237. $transport->shouldReceive('stop');
  238. $mailer->setSwiftMailer($swift);
  239. return $mailer;
  240. }
  241. protected function getMocks()
  242. {
  243. return ['smtp', m::mock(Factory::class), m::mock(Swift_Mailer::class)];
  244. }
  245. }
  246. class FailingSwiftMailerStub
  247. {
  248. public function send($message, &$failed)
  249. {
  250. $failed[] = 'taylorotwell@gmail.com';
  251. }
  252. public function getTransport()
  253. {
  254. $transport = m::mock(Swift_Transport::class);
  255. $transport->shouldReceive('stop');
  256. return $transport;
  257. }
  258. public function createMessage()
  259. {
  260. return new Swift_Message;
  261. }
  262. }