SupportTestingMailFakeTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. namespace Illuminate\Tests\Support;
  3. use Illuminate\Contracts\Mail\Mailable as MailableContract;
  4. use Illuminate\Contracts\Queue\ShouldQueue;
  5. use Illuminate\Contracts\Translation\HasLocalePreference;
  6. use Illuminate\Mail\Mailable;
  7. use Illuminate\Support\Testing\Fakes\MailFake;
  8. use PHPUnit\Framework\Constraint\ExceptionMessage;
  9. use PHPUnit\Framework\ExpectationFailedException;
  10. use PHPUnit\Framework\TestCase;
  11. class SupportTestingMailFakeTest extends TestCase
  12. {
  13. /**
  14. * @var \Illuminate\Support\Testing\Fakes\MailFake
  15. */
  16. private $fake;
  17. /**
  18. * @var \Illuminate\Tests\Support\MailableStub
  19. */
  20. private $mailable;
  21. protected function setUp(): void
  22. {
  23. parent::setUp();
  24. $this->fake = new MailFake;
  25. $this->mailable = new MailableStub;
  26. }
  27. public function testAssertSent()
  28. {
  29. try {
  30. $this->fake->assertSent(MailableStub::class);
  31. $this->fail();
  32. } catch (ExpectationFailedException $e) {
  33. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\MailableStub] mailable was not sent.'));
  34. }
  35. $this->fake->to('taylor@laravel.com')->send($this->mailable);
  36. $this->fake->assertSent(MailableStub::class);
  37. }
  38. public function testAssertSentWhenRecipientHasPreferredLocale()
  39. {
  40. $user = new LocalizedRecipientStub;
  41. $this->fake->to($user)->send($this->mailable);
  42. $this->fake->assertSent(MailableStub::class, function ($mail) use ($user) {
  43. return $mail->hasTo($user) && $mail->locale === 'au';
  44. });
  45. }
  46. public function testAssertNotSent()
  47. {
  48. $this->fake->assertNotSent(MailableStub::class);
  49. $this->fake->to('taylor@laravel.com')->send($this->mailable);
  50. try {
  51. $this->fake->assertNotSent(MailableStub::class);
  52. $this->fail();
  53. } catch (ExpectationFailedException $e) {
  54. $this->assertThat($e, new ExceptionMessage('The unexpected [Illuminate\Tests\Support\MailableStub] mailable was sent.'));
  55. }
  56. }
  57. public function testAssertNotSentWithClosure()
  58. {
  59. $callback = function (MailableStub $mail) {
  60. return $mail->hasTo('taylor@laravel.com');
  61. };
  62. $this->fake->assertNotSent($callback);
  63. $this->fake->to('taylor@laravel.com')->send($this->mailable);
  64. $this->expectException(ExpectationFailedException::class);
  65. $this->expectExceptionMessageMatches('/The unexpected \['.preg_quote(MailableStub::class, '/').'\] mailable was sent./m');
  66. $this->fake->assertNotSent($callback);
  67. }
  68. public function testAssertSentTimes()
  69. {
  70. $this->fake->to('taylor@laravel.com')->send($this->mailable);
  71. $this->fake->to('taylor@laravel.com')->send($this->mailable);
  72. try {
  73. $this->fake->assertSent(MailableStub::class, 1);
  74. $this->fail();
  75. } catch (ExpectationFailedException $e) {
  76. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\MailableStub] mailable was sent 2 times instead of 1 times.'));
  77. }
  78. $this->fake->assertSent(MailableStub::class, 2);
  79. }
  80. public function testAssertQueued()
  81. {
  82. try {
  83. $this->fake->assertQueued(MailableStub::class);
  84. $this->fail();
  85. } catch (ExpectationFailedException $e) {
  86. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\MailableStub] mailable was not queued.'));
  87. }
  88. $this->fake->to('taylor@laravel.com')->queue($this->mailable);
  89. $this->fake->assertQueued(MailableStub::class);
  90. }
  91. public function testAssertQueuedTimes()
  92. {
  93. $this->fake->to('taylor@laravel.com')->queue($this->mailable);
  94. $this->fake->to('taylor@laravel.com')->queue($this->mailable);
  95. try {
  96. $this->fake->assertQueued(MailableStub::class, 1);
  97. $this->fail();
  98. } catch (ExpectationFailedException $e) {
  99. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\MailableStub] mailable was queued 2 times instead of 1 times.'));
  100. }
  101. $this->fake->assertQueued(MailableStub::class, 2);
  102. }
  103. public function testSendQueuesAMailableThatShouldBeQueued()
  104. {
  105. $this->fake->to('taylor@laravel.com')->send(new QueueableMailableStub);
  106. $this->fake->assertQueued(QueueableMailableStub::class);
  107. try {
  108. $this->fake->assertSent(QueueableMailableStub::class);
  109. $this->fail();
  110. } catch (ExpectationFailedException $e) {
  111. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\QueueableMailableStub] mailable was not sent.'));
  112. }
  113. }
  114. public function testAssertNothingSent()
  115. {
  116. $this->fake->assertNothingSent();
  117. $this->fake->to('taylor@laravel.com')->send($this->mailable);
  118. try {
  119. $this->fake->assertNothingSent();
  120. $this->fail();
  121. } catch (ExpectationFailedException $e) {
  122. $this->assertThat($e, new ExceptionMessage('The following mailables were sent unexpectedly: Illuminate\Tests\Support\MailableStub'));
  123. }
  124. }
  125. public function testAssertNothingQueued()
  126. {
  127. $this->fake->assertNothingQueued();
  128. $this->fake->to('taylor@laravel.com')->queue($this->mailable);
  129. try {
  130. $this->fake->assertNothingQueued();
  131. $this->fail();
  132. } catch (ExpectationFailedException $e) {
  133. $this->assertThat($e, new ExceptionMessage('The following mailables were queued unexpectedly: Illuminate\Tests\Support\MailableStub'));
  134. }
  135. }
  136. public function testAssertQueuedWithClosure()
  137. {
  138. $this->fake->to($user = new LocalizedRecipientStub)->queue($this->mailable);
  139. $this->fake->assertQueued(function (MailableStub $mail) use ($user) {
  140. return $mail->hasTo($user);
  141. });
  142. }
  143. public function testAssertSentWithClosure()
  144. {
  145. $this->fake->to($user = new LocalizedRecipientStub)->send($this->mailable);
  146. $this->fake->assertSent(function (MailableStub $mail) use ($user) {
  147. return $mail->hasTo($user);
  148. });
  149. }
  150. }
  151. class MailableStub extends Mailable implements MailableContract
  152. {
  153. public $framework = 'Laravel';
  154. protected $version = '6.0';
  155. /**
  156. * Build the message.
  157. *
  158. * @return $this
  159. */
  160. public function build()
  161. {
  162. $this->with('first_name', 'Taylor')
  163. ->withLastName('Otwell');
  164. }
  165. }
  166. class QueueableMailableStub extends Mailable implements ShouldQueue
  167. {
  168. public $framework = 'Laravel';
  169. protected $version = '6.0';
  170. /**
  171. * Build the message.
  172. *
  173. * @return $this
  174. */
  175. public function build()
  176. {
  177. $this->with('first_name', 'Taylor')
  178. ->withLastName('Otwell');
  179. }
  180. }
  181. class LocalizedRecipientStub implements HasLocalePreference
  182. {
  183. public $email = 'taylor@laravel.com';
  184. public function preferredLocale()
  185. {
  186. return 'au';
  187. }
  188. }