SupportTestingNotificationFakeTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace Illuminate\Tests\Support;
  3. use Exception;
  4. use Illuminate\Contracts\Translation\HasLocalePreference;
  5. use Illuminate\Foundation\Auth\User;
  6. use Illuminate\Notifications\AnonymousNotifiable;
  7. use Illuminate\Notifications\Notification;
  8. use Illuminate\Support\Collection;
  9. use Illuminate\Support\Testing\Fakes\NotificationFake;
  10. use PHPUnit\Framework\Constraint\ExceptionMessage;
  11. use PHPUnit\Framework\ExpectationFailedException;
  12. use PHPUnit\Framework\TestCase;
  13. class SupportTestingNotificationFakeTest extends TestCase
  14. {
  15. /**
  16. * @var \Illuminate\Support\Testing\Fakes\NotificationFake
  17. */
  18. private $fake;
  19. /**
  20. * @var \Illuminate\Tests\Support\NotificationStub
  21. */
  22. private $notification;
  23. /**
  24. * @var \Illuminate\Tests\Support\UserStub
  25. */
  26. private $user;
  27. protected function setUp(): void
  28. {
  29. parent::setUp();
  30. $this->fake = new NotificationFake;
  31. $this->notification = new NotificationStub;
  32. $this->user = new UserStub;
  33. }
  34. public function testAssertSentTo()
  35. {
  36. try {
  37. $this->fake->assertSentTo($this->user, NotificationStub::class);
  38. $this->fail();
  39. } catch (ExpectationFailedException $e) {
  40. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\NotificationStub] notification was not sent.'));
  41. }
  42. $this->fake->send($this->user, new NotificationStub);
  43. $this->fake->assertSentTo($this->user, NotificationStub::class);
  44. }
  45. public function testAssertSentToClosure()
  46. {
  47. $this->fake->send($this->user, new NotificationStub);
  48. $this->fake->assertSentTo($this->user, function (NotificationStub $notification) {
  49. return true;
  50. });
  51. }
  52. public function testAssertSentOnDemand()
  53. {
  54. $this->fake->send(new AnonymousNotifiable, new NotificationStub);
  55. $this->fake->assertSentOnDemand(NotificationStub::class);
  56. }
  57. public function testAssertSentOnDemandClosure()
  58. {
  59. $this->fake->send(new AnonymousNotifiable, new NotificationStub);
  60. $this->fake->assertSentOnDemand(NotificationStub::class, function (NotificationStub $notification) {
  61. return true;
  62. });
  63. }
  64. public function testAssertNotSentTo()
  65. {
  66. $this->fake->assertNotSentTo($this->user, NotificationStub::class);
  67. $this->fake->send($this->user, new NotificationStub);
  68. try {
  69. $this->fake->assertNotSentTo($this->user, NotificationStub::class);
  70. $this->fail();
  71. } catch (ExpectationFailedException $e) {
  72. $this->assertThat($e, new ExceptionMessage('The unexpected [Illuminate\Tests\Support\NotificationStub] notification was sent.'));
  73. }
  74. }
  75. public function testAssertNotSentToClosure()
  76. {
  77. $this->fake->send($this->user, new NotificationStub);
  78. try {
  79. $this->fake->assertNotSentTo($this->user, function (NotificationStub $notification) {
  80. return true;
  81. });
  82. $this->fail();
  83. } catch (ExpectationFailedException $e) {
  84. $this->assertThat($e, new ExceptionMessage('The unexpected [Illuminate\Tests\Support\NotificationStub] notification was sent.'));
  85. }
  86. }
  87. public function testAssertSentToFailsForEmptyArray()
  88. {
  89. $this->expectException(Exception::class);
  90. $this->fake->assertSentTo([], NotificationStub::class);
  91. }
  92. public function testAssertSentToFailsForEmptyCollection()
  93. {
  94. $this->expectException(Exception::class);
  95. $this->fake->assertSentTo(new Collection, NotificationStub::class);
  96. }
  97. public function testResettingNotificationId()
  98. {
  99. $this->fake->send($this->user, $this->notification);
  100. $id = $this->notification->id;
  101. $this->fake->send($this->user, $this->notification);
  102. $this->assertSame($id, $this->notification->id);
  103. $this->notification->id = null;
  104. $this->fake->send($this->user, $this->notification);
  105. $this->assertNotNull($this->notification->id);
  106. $this->assertNotSame($id, $this->notification->id);
  107. }
  108. public function testAssertTimesSent()
  109. {
  110. $this->fake->assertTimesSent(0, NotificationStub::class);
  111. $this->fake->send($this->user, new NotificationStub);
  112. $this->fake->send($this->user, new NotificationStub);
  113. $this->fake->send(new UserStub, new NotificationStub);
  114. $this->fake->assertTimesSent(3, NotificationStub::class);
  115. }
  116. public function testAssertSentToTimes()
  117. {
  118. $this->fake->assertSentToTimes($this->user, NotificationStub::class, 0);
  119. $this->fake->send($this->user, new NotificationStub);
  120. $this->fake->send($this->user, new NotificationStub);
  121. $this->fake->send($this->user, new NotificationStub);
  122. $this->fake->assertSentToTimes($this->user, NotificationStub::class, 3);
  123. }
  124. public function testAssertSentOnDemandTimes()
  125. {
  126. $this->fake->assertSentOnDemandTimes(NotificationStub::class, 0);
  127. $this->fake->send(new AnonymousNotifiable, new NotificationStub);
  128. $this->fake->send(new AnonymousNotifiable, new NotificationStub);
  129. $this->fake->send(new AnonymousNotifiable, new NotificationStub);
  130. $this->fake->assertSentOnDemandTimes(NotificationStub::class, 3);
  131. }
  132. public function testAssertSentToWhenNotifiableHasPreferredLocale()
  133. {
  134. $user = new LocalizedUserStub;
  135. $this->fake->send($user, new NotificationStub);
  136. $this->fake->assertSentTo($user, NotificationStub::class, function ($notification, $channels, $notifiable, $locale) use ($user) {
  137. return $notifiable === $user && $locale === 'au';
  138. });
  139. }
  140. public function testAssertSentToWhenNotifiableHasFalsyShouldSend()
  141. {
  142. $user = new LocalizedUserStub;
  143. $this->fake->send($user, new NotificationWithFalsyShouldSendStub);
  144. $this->fake->assertNotSentTo($user, NotificationWithFalsyShouldSendStub::class);
  145. }
  146. }
  147. class NotificationStub extends Notification
  148. {
  149. public function via($notifiable)
  150. {
  151. return ['mail'];
  152. }
  153. }
  154. class NotificationWithFalsyShouldSendStub extends Notification
  155. {
  156. public function via($notifiable)
  157. {
  158. return ['mail'];
  159. }
  160. public function shouldSend($notifiable, $channel)
  161. {
  162. return false;
  163. }
  164. }
  165. class UserStub extends User
  166. {
  167. //
  168. }
  169. class LocalizedUserStub extends User implements HasLocalePreference
  170. {
  171. public function preferredLocale()
  172. {
  173. return 'au';
  174. }
  175. }