NotificationFake.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace Illuminate\Support\Testing\Fakes;
  3. use Ramsey\Uuid\Uuid;
  4. use Illuminate\Support\Collection;
  5. use PHPUnit\Framework\Assert as PHPUnit;
  6. use Illuminate\Contracts\Notifications\Factory as NotificationFactory;
  7. use Illuminate\Contracts\Notifications\Dispatcher as NotificationDispatcher;
  8. class NotificationFake implements NotificationFactory, NotificationDispatcher
  9. {
  10. /**
  11. * All of the notifications that have been sent.
  12. *
  13. * @var array
  14. */
  15. protected $notifications = [];
  16. /**
  17. * Assert if a notification was sent based on a truth-test callback.
  18. *
  19. * @param mixed $notifiable
  20. * @param string $notification
  21. * @param callable|null $callback
  22. * @return void
  23. */
  24. public function assertSentTo($notifiable, $notification, $callback = null)
  25. {
  26. if (is_array($notifiable) || $notifiable instanceof Collection) {
  27. foreach ($notifiable as $singleNotifiable) {
  28. $this->assertSentTo($singleNotifiable, $notification, $callback);
  29. }
  30. return;
  31. }
  32. if (is_numeric($callback)) {
  33. return $this->assertSentToTimes($notifiable, $notification, $callback);
  34. }
  35. PHPUnit::assertTrue(
  36. $this->sent($notifiable, $notification, $callback)->count() > 0,
  37. "The expected [{$notification}] notification was not sent."
  38. );
  39. }
  40. /**
  41. * Assert if a notification was sent a number of times.
  42. *
  43. * @param mixed $notifiable
  44. * @param string $notification
  45. * @param int $times
  46. * @return void
  47. */
  48. public function assertSentToTimes($notifiable, $notification, $times = 1)
  49. {
  50. PHPUnit::assertTrue(
  51. ($count = $this->sent($notifiable, $notification)->count()) === $times,
  52. "The expected [{$notification}] notification was sent {$count} times instead of {$times} times."
  53. );
  54. }
  55. /**
  56. * Determine if a notification was sent based on a truth-test callback.
  57. *
  58. * @param mixed $notifiable
  59. * @param string $notification
  60. * @param callable|null $callback
  61. * @return void
  62. */
  63. public function assertNotSentTo($notifiable, $notification, $callback = null)
  64. {
  65. if (is_array($notifiable) || $notifiable instanceof Collection) {
  66. foreach ($notifiable as $singleNotifiable) {
  67. $this->assertNotSentTo($singleNotifiable, $notification, $callback);
  68. }
  69. return;
  70. }
  71. PHPUnit::assertTrue(
  72. $this->sent($notifiable, $notification, $callback)->count() === 0,
  73. "The unexpected [{$notification}] notification was sent."
  74. );
  75. }
  76. /**
  77. * Assert that no notifications were sent.
  78. *
  79. * @return void
  80. */
  81. public function assertNothingSent()
  82. {
  83. PHPUnit::assertEmpty($this->notifications, 'Notifications were sent unexpectedly.');
  84. }
  85. /**
  86. * Get all of the notifications matching a truth-test callback.
  87. *
  88. * @param mixed $notifiable
  89. * @param string $notification
  90. * @param callable|null $callback
  91. * @return \Illuminate\Support\Collection
  92. */
  93. public function sent($notifiable, $notification, $callback = null)
  94. {
  95. if (! $this->hasSent($notifiable, $notification)) {
  96. return collect();
  97. }
  98. $callback = $callback ?: function () {
  99. return true;
  100. };
  101. $notifications = collect($this->notificationsFor($notifiable, $notification));
  102. return $notifications->filter(function ($arguments) use ($callback) {
  103. return $callback(...array_values($arguments));
  104. })->pluck('notification');
  105. }
  106. /**
  107. * Determine if there are more notifications left to inspect.
  108. *
  109. * @param mixed $notifiable
  110. * @param string $notification
  111. * @return bool
  112. */
  113. public function hasSent($notifiable, $notification)
  114. {
  115. return ! empty($this->notificationsFor($notifiable, $notification));
  116. }
  117. /**
  118. * Get all of the notifications for a notifiable entity by type.
  119. *
  120. * @param mixed $notifiable
  121. * @param string $notification
  122. * @return array
  123. */
  124. protected function notificationsFor($notifiable, $notification)
  125. {
  126. if (isset($this->notifications[get_class($notifiable)][$notifiable->getKey()][$notification])) {
  127. return $this->notifications[get_class($notifiable)][$notifiable->getKey()][$notification];
  128. }
  129. return [];
  130. }
  131. /**
  132. * Send the given notification to the given notifiable entities.
  133. *
  134. * @param \Illuminate\Support\Collection|array|mixed $notifiables
  135. * @param mixed $notification
  136. * @return void
  137. */
  138. public function send($notifiables, $notification)
  139. {
  140. return $this->sendNow($notifiables, $notification);
  141. }
  142. /**
  143. * Send the given notification immediately.
  144. *
  145. * @param \Illuminate\Support\Collection|array|mixed $notifiables
  146. * @param mixed $notification
  147. * @return void
  148. */
  149. public function sendNow($notifiables, $notification)
  150. {
  151. if (! $notifiables instanceof Collection && ! is_array($notifiables)) {
  152. $notifiables = [$notifiables];
  153. }
  154. foreach ($notifiables as $notifiable) {
  155. if (! $notification->id) {
  156. $notification->id = Uuid::uuid4()->toString();
  157. }
  158. $this->notifications[get_class($notifiable)][$notifiable->getKey()][get_class($notification)][] = [
  159. 'notification' => $notification,
  160. 'channels' => $notification->via($notifiable),
  161. 'notifiable' => $notifiable,
  162. ];
  163. }
  164. }
  165. /**
  166. * Get a channel instance by name.
  167. *
  168. * @param string|null $name
  169. * @return mixed
  170. */
  171. public function channel($name = null)
  172. {
  173. //
  174. }
  175. }