You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NotificationFake.php 7.2KB

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