SendingMailNotificationsTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <?php
  2. namespace Illuminate\Tests\Integration\Notifications;
  3. use Illuminate\Contracts\Mail\Factory as MailFactory;
  4. use Illuminate\Contracts\Mail\Mailable;
  5. use Illuminate\Contracts\Mail\Mailer;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Illuminate\Database\Schema\Blueprint;
  8. use Illuminate\Mail\Markdown;
  9. use Illuminate\Mail\Message;
  10. use Illuminate\Notifications\Channels\MailChannel;
  11. use Illuminate\Notifications\Messages\MailMessage;
  12. use Illuminate\Notifications\Notifiable;
  13. use Illuminate\Notifications\Notification;
  14. use Illuminate\Support\Facades\Schema;
  15. use Illuminate\Support\Facades\View;
  16. use Illuminate\Support\Str;
  17. use Mockery as m;
  18. use Orchestra\Testbench\TestCase;
  19. class SendingMailNotificationsTest extends TestCase
  20. {
  21. public $mailer;
  22. public $markdown;
  23. protected function tearDown(): void
  24. {
  25. parent::tearDown();
  26. m::close();
  27. }
  28. protected function getEnvironmentSetUp($app)
  29. {
  30. $this->mailFactory = m::mock(MailFactory::class);
  31. $this->mailer = m::mock(Mailer::class);
  32. $this->mailFactory->shouldReceive('mailer')->andReturn($this->mailer);
  33. $this->markdown = m::mock(Markdown::class);
  34. $app->extend(Markdown::class, function () {
  35. return $this->markdown;
  36. });
  37. $app->extend(Mailer::class, function () {
  38. return $this->mailer;
  39. });
  40. $app->extend(MailFactory::class, function () {
  41. return $this->mailFactory;
  42. });
  43. View::addLocation(__DIR__.'/Fixtures');
  44. }
  45. protected function setUp(): void
  46. {
  47. parent::setUp();
  48. Schema::create('users', function (Blueprint $table) {
  49. $table->increments('id');
  50. $table->string('email');
  51. $table->string('name')->nullable();
  52. });
  53. }
  54. public function testMailIsSent()
  55. {
  56. $notification = new TestMailNotification;
  57. $notification->id = Str::uuid()->toString();
  58. $user = NotifiableUser::forceCreate([
  59. 'email' => 'taylor@laravel.com',
  60. ]);
  61. $this->markdown->shouldReceive('render')->once()->andReturn('htmlContent');
  62. $this->markdown->shouldReceive('renderText')->once()->andReturn('textContent');
  63. $this->mailer->shouldReceive('send')->once()->with(
  64. ['html' => 'htmlContent', 'text' => 'textContent'],
  65. array_merge($notification->toMail($user)->toArray(), [
  66. '__laravel_notification_id' => $notification->id,
  67. '__laravel_notification' => get_class($notification),
  68. '__laravel_notification_queued' => false,
  69. ]),
  70. m::on(function ($closure) {
  71. $message = m::mock(Message::class);
  72. $message->shouldReceive('to')->once()->with(['taylor@laravel.com']);
  73. $message->shouldReceive('cc')->once()->with('cc@deepblue.com', 'cc');
  74. $message->shouldReceive('bcc')->once()->with('bcc@deepblue.com', 'bcc');
  75. $message->shouldReceive('from')->once()->with('jack@deepblue.com', 'Jacques Mayol');
  76. $message->shouldReceive('replyTo')->once()->with('jack@deepblue.com', 'Jacques Mayol');
  77. $message->shouldReceive('subject')->once()->with('Test Mail Notification');
  78. $message->shouldReceive('setPriority')->once()->with(1);
  79. $closure($message);
  80. return true;
  81. })
  82. );
  83. $user->notify($notification);
  84. }
  85. public function testMailIsSentToNamedAddress()
  86. {
  87. $notification = new TestMailNotification;
  88. $notification->id = Str::uuid()->toString();
  89. $user = NotifiableUserWithNamedAddress::forceCreate([
  90. 'email' => 'taylor@laravel.com',
  91. 'name' => 'Taylor Otwell',
  92. ]);
  93. $this->markdown->shouldReceive('render')->once()->andReturn('htmlContent');
  94. $this->markdown->shouldReceive('renderText')->once()->andReturn('textContent');
  95. $this->mailer->shouldReceive('send')->once()->with(
  96. ['html' => 'htmlContent', 'text' => 'textContent'],
  97. array_merge($notification->toMail($user)->toArray(), [
  98. '__laravel_notification_id' => $notification->id,
  99. '__laravel_notification' => get_class($notification),
  100. '__laravel_notification_queued' => false,
  101. ]),
  102. m::on(function ($closure) {
  103. $message = m::mock(Message::class);
  104. $message->shouldReceive('to')->once()->with(['taylor@laravel.com' => 'Taylor Otwell', 'foo_taylor@laravel.com']);
  105. $message->shouldReceive('cc')->once()->with('cc@deepblue.com', 'cc');
  106. $message->shouldReceive('bcc')->once()->with('bcc@deepblue.com', 'bcc');
  107. $message->shouldReceive('from')->once()->with('jack@deepblue.com', 'Jacques Mayol');
  108. $message->shouldReceive('replyTo')->once()->with('jack@deepblue.com', 'Jacques Mayol');
  109. $message->shouldReceive('subject')->once()->with('Test Mail Notification');
  110. $message->shouldReceive('setPriority')->once()->with(1);
  111. $closure($message);
  112. return true;
  113. })
  114. );
  115. $user->notify($notification);
  116. }
  117. public function testMailIsSentWithSubject()
  118. {
  119. $notification = new TestMailNotificationWithSubject;
  120. $notification->id = Str::uuid()->toString();
  121. $user = NotifiableUser::forceCreate([
  122. 'email' => 'taylor@laravel.com',
  123. ]);
  124. $this->markdown->shouldReceive('render')->once()->andReturn('htmlContent');
  125. $this->markdown->shouldReceive('renderText')->once()->andReturn('textContent');
  126. $this->mailer->shouldReceive('send')->once()->with(
  127. ['html' => 'htmlContent', 'text' => 'textContent'],
  128. array_merge($notification->toMail($user)->toArray(), [
  129. '__laravel_notification_id' => $notification->id,
  130. '__laravel_notification' => get_class($notification),
  131. '__laravel_notification_queued' => false,
  132. ]),
  133. m::on(function ($closure) {
  134. $message = m::mock(Message::class);
  135. $message->shouldReceive('to')->once()->with(['taylor@laravel.com']);
  136. $message->shouldReceive('subject')->once()->with('mail custom subject');
  137. $closure($message);
  138. return true;
  139. })
  140. );
  141. $user->notify($notification);
  142. }
  143. public function testMailIsSentToMultipleAddresses()
  144. {
  145. $notification = new TestMailNotificationWithSubject;
  146. $notification->id = Str::uuid()->toString();
  147. $user = NotifiableUserWithMultipleAddresses::forceCreate([
  148. 'email' => 'taylor@laravel.com',
  149. ]);
  150. $this->markdown->shouldReceive('render')->once()->andReturn('htmlContent');
  151. $this->markdown->shouldReceive('renderText')->once()->andReturn('textContent');
  152. $this->mailer->shouldReceive('send')->once()->with(
  153. ['html' => 'htmlContent', 'text' => 'textContent'],
  154. array_merge($notification->toMail($user)->toArray(), [
  155. '__laravel_notification_id' => $notification->id,
  156. '__laravel_notification' => get_class($notification),
  157. '__laravel_notification_queued' => false,
  158. ]),
  159. m::on(function ($closure) {
  160. $message = m::mock(Message::class);
  161. $message->shouldReceive('to')->once()->with(['foo_taylor@laravel.com', 'bar_taylor@laravel.com']);
  162. $message->shouldReceive('subject')->once()->with('mail custom subject');
  163. $closure($message);
  164. return true;
  165. })
  166. );
  167. $user->notify($notification);
  168. }
  169. public function testMailIsSentUsingMailable()
  170. {
  171. $notification = new TestMailNotificationWithMailable;
  172. $user = NotifiableUser::forceCreate([
  173. 'email' => 'taylor@laravel.com',
  174. ]);
  175. $user->notify($notification);
  176. }
  177. public function testMailIsSentUsingMailMessageWithHtmlAndPlain()
  178. {
  179. $notification = new TestMailNotificationWithHtmlAndPlain;
  180. $notification->id = Str::uuid()->toString();
  181. $user = NotifiableUser::forceCreate([
  182. 'email' => 'taylor@laravel.com',
  183. ]);
  184. $this->mailer->shouldReceive('send')->once()->with(
  185. ['html', 'plain'],
  186. array_merge($notification->toMail($user)->toArray(), [
  187. '__laravel_notification_id' => $notification->id,
  188. '__laravel_notification' => get_class($notification),
  189. '__laravel_notification_queued' => false,
  190. ]),
  191. m::on(function ($closure) {
  192. $message = m::mock(Message::class);
  193. $message->shouldReceive('to')->once()->with(['taylor@laravel.com']);
  194. $message->shouldReceive('subject')->once()->with('Test Mail Notification With Html And Plain');
  195. $closure($message);
  196. return true;
  197. })
  198. );
  199. $user->notify($notification);
  200. }
  201. public function testMailIsSentUsingMailMessageWithHtmlOnly()
  202. {
  203. $notification = new TestMailNotificationWithHtmlOnly;
  204. $notification->id = Str::uuid()->toString();
  205. $user = NotifiableUser::forceCreate([
  206. 'email' => 'taylor@laravel.com',
  207. ]);
  208. $this->mailer->shouldReceive('send')->once()->with(
  209. 'html',
  210. array_merge($notification->toMail($user)->toArray(), [
  211. '__laravel_notification_id' => $notification->id,
  212. '__laravel_notification' => get_class($notification),
  213. '__laravel_notification_queued' => false,
  214. ]),
  215. m::on(function ($closure) {
  216. $message = m::mock(Message::class);
  217. $message->shouldReceive('to')->once()->with(['taylor@laravel.com']);
  218. $message->shouldReceive('subject')->once()->with('Test Mail Notification With Html Only');
  219. $closure($message);
  220. return true;
  221. })
  222. );
  223. $user->notify($notification);
  224. }
  225. public function testMailIsSentUsingMailMessageWithPlainOnly()
  226. {
  227. $notification = new TestMailNotificationWithPlainOnly;
  228. $notification->id = Str::uuid()->toString();
  229. $user = NotifiableUser::forceCreate([
  230. 'email' => 'taylor@laravel.com',
  231. ]);
  232. $this->mailer->shouldReceive('send')->once()->with(
  233. [null, 'plain'],
  234. array_merge($notification->toMail($user)->toArray(), [
  235. '__laravel_notification_id' => $notification->id,
  236. '__laravel_notification' => get_class($notification),
  237. '__laravel_notification_queued' => false,
  238. ]),
  239. m::on(function ($closure) {
  240. $message = m::mock(Message::class);
  241. $message->shouldReceive('to')->once()->with(['taylor@laravel.com']);
  242. $message->shouldReceive('subject')->once()->with('Test Mail Notification With Plain Only');
  243. $closure($message);
  244. return true;
  245. })
  246. );
  247. $user->notify($notification);
  248. }
  249. }
  250. class NotifiableUser extends Model
  251. {
  252. use Notifiable;
  253. public $table = 'users';
  254. public $timestamps = false;
  255. }
  256. class NotifiableUserWithNamedAddress extends NotifiableUser
  257. {
  258. public function routeNotificationForMail($notification)
  259. {
  260. return [
  261. $this->email => $this->name,
  262. 'foo_'.$this->email,
  263. ];
  264. }
  265. }
  266. class NotifiableUserWithMultipleAddresses extends NotifiableUser
  267. {
  268. public function routeNotificationForMail($notification)
  269. {
  270. return [
  271. 'foo_'.$this->email,
  272. 'bar_'.$this->email,
  273. ];
  274. }
  275. }
  276. class TestMailNotification extends Notification
  277. {
  278. public function via($notifiable)
  279. {
  280. return [MailChannel::class];
  281. }
  282. public function toMail($notifiable)
  283. {
  284. return (new MailMessage)
  285. ->priority(1)
  286. ->cc('cc@deepblue.com', 'cc')
  287. ->bcc('bcc@deepblue.com', 'bcc')
  288. ->from('jack@deepblue.com', 'Jacques Mayol')
  289. ->replyTo('jack@deepblue.com', 'Jacques Mayol')
  290. ->line('The introduction to the notification.')
  291. ->mailer('foo');
  292. }
  293. }
  294. class TestMailNotificationWithSubject extends Notification
  295. {
  296. public function via($notifiable)
  297. {
  298. return [MailChannel::class];
  299. }
  300. public function toMail($notifiable)
  301. {
  302. return (new MailMessage)
  303. ->subject('mail custom subject')
  304. ->line('The introduction to the notification.');
  305. }
  306. }
  307. class TestMailNotificationWithMailable extends Notification
  308. {
  309. public function via($notifiable)
  310. {
  311. return [MailChannel::class];
  312. }
  313. public function toMail($notifiable)
  314. {
  315. $mailable = m::mock(Mailable::class);
  316. $mailable->shouldReceive('send')->once();
  317. return $mailable;
  318. }
  319. }
  320. class TestMailNotificationWithHtmlAndPlain extends Notification
  321. {
  322. public function via($notifiable)
  323. {
  324. return [MailChannel::class];
  325. }
  326. public function toMail($notifiable)
  327. {
  328. return (new MailMessage)
  329. ->view(['html', 'plain']);
  330. }
  331. }
  332. class TestMailNotificationWithHtmlOnly extends Notification
  333. {
  334. public function via($notifiable)
  335. {
  336. return [MailChannel::class];
  337. }
  338. public function toMail($notifiable)
  339. {
  340. return (new MailMessage)
  341. ->view('html');
  342. }
  343. }
  344. class TestMailNotificationWithPlainOnly extends Notification
  345. {
  346. public function via($notifiable)
  347. {
  348. return [MailChannel::class];
  349. }
  350. public function toMail($notifiable)
  351. {
  352. return (new MailMessage)
  353. ->view([null, 'plain']);
  354. }
  355. }