/laradock/vendor/illuminate/support/Testing/Fakes/NotificationFake.php

https://gitlab.com/hoangduys4k5/laravelproject · PHP · 355 lines · 178 code · 44 blank · 133 comment · 19 complexity · 210e684120d509725bdb6ad348c241cc MD5 · raw file

  1. <?php
  2. namespace Illuminate\Support\Testing\Fakes;
  3. use Closure;
  4. use Exception;
  5. use Illuminate\Contracts\Notifications\Dispatcher as NotificationDispatcher;
  6. use Illuminate\Contracts\Notifications\Factory as NotificationFactory;
  7. use Illuminate\Contracts\Translation\HasLocalePreference;
  8. use Illuminate\Notifications\AnonymousNotifiable;
  9. use Illuminate\Support\Collection;
  10. use Illuminate\Support\Str;
  11. use Illuminate\Support\Traits\Macroable;
  12. use Illuminate\Support\Traits\ReflectsClosures;
  13. use PHPUnit\Framework\Assert as PHPUnit;
  14. class NotificationFake implements NotificationDispatcher, NotificationFactory
  15. {
  16. use Macroable, ReflectsClosures;
  17. /**
  18. * All of the notifications that have been sent.
  19. *
  20. * @var array
  21. */
  22. protected $notifications = [];
  23. /**
  24. * Locale used when sending notifications.
  25. *
  26. * @var string|null
  27. */
  28. public $locale;
  29. /**
  30. * Assert if a notification was sent on-demand based on a truth-test callback.
  31. *
  32. * @param string|\Closure $notification
  33. * @param callable|null $callback
  34. * @return void
  35. *
  36. * @throws \Exception
  37. */
  38. public function assertSentOnDemand($notification, $callback = null)
  39. {
  40. $this->assertSentTo(new AnonymousNotifiable, $notification, $callback);
  41. }
  42. /**
  43. * Assert if a notification was sent based on a truth-test callback.
  44. *
  45. * @param mixed $notifiable
  46. * @param string|\Closure $notification
  47. * @param callable|null $callback
  48. * @return void
  49. *
  50. * @throws \Exception
  51. */
  52. public function assertSentTo($notifiable, $notification, $callback = null)
  53. {
  54. if (is_array($notifiable) || $notifiable instanceof Collection) {
  55. if (count($notifiable) === 0) {
  56. throw new Exception('No notifiable given.');
  57. }
  58. foreach ($notifiable as $singleNotifiable) {
  59. $this->assertSentTo($singleNotifiable, $notification, $callback);
  60. }
  61. return;
  62. }
  63. if ($notification instanceof Closure) {
  64. [$notification, $callback] = [$this->firstClosureParameterType($notification), $notification];
  65. }
  66. if (is_numeric($callback)) {
  67. return $this->assertSentToTimes($notifiable, $notification, $callback);
  68. }
  69. PHPUnit::assertTrue(
  70. $this->sent($notifiable, $notification, $callback)->count() > 0,
  71. "The expected [{$notification}] notification was not sent."
  72. );
  73. }
  74. /**
  75. * Assert if a notification was sent on-demand a number of times.
  76. *
  77. * @param string $notification
  78. * @param int $times
  79. * @return void
  80. */
  81. public function assertSentOnDemandTimes($notification, $times = 1)
  82. {
  83. return $this->assertSentToTimes(new AnonymousNotifiable, $notification, $times);
  84. }
  85. /**
  86. * Assert if a notification was sent a number of times.
  87. *
  88. * @param mixed $notifiable
  89. * @param string $notification
  90. * @param int $times
  91. * @return void
  92. */
  93. public function assertSentToTimes($notifiable, $notification, $times = 1)
  94. {
  95. $count = $this->sent($notifiable, $notification)->count();
  96. PHPUnit::assertSame(
  97. $times, $count,
  98. "Expected [{$notification}] to be sent {$times} times, but was sent {$count} times."
  99. );
  100. }
  101. /**
  102. * Determine if a notification was sent based on a truth-test callback.
  103. *
  104. * @param mixed $notifiable
  105. * @param string|\Closure $notification
  106. * @param callable|null $callback
  107. * @return void
  108. *
  109. * @throws \Exception
  110. */
  111. public function assertNotSentTo($notifiable, $notification, $callback = null)
  112. {
  113. if (is_array($notifiable) || $notifiable instanceof Collection) {
  114. if (count($notifiable) === 0) {
  115. throw new Exception('No notifiable given.');
  116. }
  117. foreach ($notifiable as $singleNotifiable) {
  118. $this->assertNotSentTo($singleNotifiable, $notification, $callback);
  119. }
  120. return;
  121. }
  122. if ($notification instanceof Closure) {
  123. [$notification, $callback] = [$this->firstClosureParameterType($notification), $notification];
  124. }
  125. PHPUnit::assertCount(
  126. 0, $this->sent($notifiable, $notification, $callback),
  127. "The unexpected [{$notification}] notification was sent."
  128. );
  129. }
  130. /**
  131. * Assert that no notifications were sent.
  132. *
  133. * @return void
  134. */
  135. public function assertNothingSent()
  136. {
  137. PHPUnit::assertEmpty($this->notifications, 'Notifications were sent unexpectedly.');
  138. }
  139. /**
  140. * Assert that no notifications were sent to the given notifiable.
  141. *
  142. * @param mixed $notifiable
  143. * @return void
  144. *
  145. * @throws \Exception
  146. */
  147. public function assertNothingSentTo($notifiable)
  148. {
  149. if (is_array($notifiable) || $notifiable instanceof Collection) {
  150. if (count($notifiable) === 0) {
  151. throw new Exception('No notifiable given.');
  152. }
  153. foreach ($notifiable as $singleNotifiable) {
  154. $this->assertNothingSentTo($singleNotifiable);
  155. }
  156. return;
  157. }
  158. PHPUnit::assertEmpty(
  159. $this->notifications[get_class($notifiable)][$notifiable->getKey()] ?? [],
  160. 'Notifications were sent unexpectedly.',
  161. );
  162. }
  163. /**
  164. * Assert the total amount of times a notification was sent.
  165. *
  166. * @param string $notification
  167. * @param int $expectedCount
  168. * @return void
  169. */
  170. public function assertSentTimes($notification, $expectedCount)
  171. {
  172. $actualCount = collect($this->notifications)
  173. ->flatten(1)
  174. ->reduce(function ($count, $sent) use ($notification) {
  175. return $count + count($sent[$notification] ?? []);
  176. }, 0);
  177. PHPUnit::assertSame(
  178. $expectedCount, $actualCount,
  179. "Expected [{$notification}] to be sent {$expectedCount} times, but was sent {$actualCount} times."
  180. );
  181. }
  182. /**
  183. * Assert the total amount of times a notification was sent.
  184. *
  185. * @param int $expectedCount
  186. * @param string $notification
  187. * @return void
  188. *
  189. * @deprecated Use the assertSentTimes method instead
  190. */
  191. public function assertTimesSent($expectedCount, $notification)
  192. {
  193. $this->assertSentTimes($notification, $expectedCount);
  194. }
  195. /**
  196. * Get all of the notifications matching a truth-test callback.
  197. *
  198. * @param mixed $notifiable
  199. * @param string $notification
  200. * @param callable|null $callback
  201. * @return \Illuminate\Support\Collection
  202. */
  203. public function sent($notifiable, $notification, $callback = null)
  204. {
  205. if (! $this->hasSent($notifiable, $notification)) {
  206. return collect();
  207. }
  208. $callback = $callback ?: function () {
  209. return true;
  210. };
  211. $notifications = collect($this->notificationsFor($notifiable, $notification));
  212. return $notifications->filter(function ($arguments) use ($callback) {
  213. return $callback(...array_values($arguments));
  214. })->pluck('notification');
  215. }
  216. /**
  217. * Determine if there are more notifications left to inspect.
  218. *
  219. * @param mixed $notifiable
  220. * @param string $notification
  221. * @return bool
  222. */
  223. public function hasSent($notifiable, $notification)
  224. {
  225. return ! empty($this->notificationsFor($notifiable, $notification));
  226. }
  227. /**
  228. * Get all of the notifications for a notifiable entity by type.
  229. *
  230. * @param mixed $notifiable
  231. * @param string $notification
  232. * @return array
  233. */
  234. protected function notificationsFor($notifiable, $notification)
  235. {
  236. return $this->notifications[get_class($notifiable)][$notifiable->getKey()][$notification] ?? [];
  237. }
  238. /**
  239. * Send the given notification to the given notifiable entities.
  240. *
  241. * @param \Illuminate\Support\Collection|array|mixed $notifiables
  242. * @param mixed $notification
  243. * @return void
  244. */
  245. public function send($notifiables, $notification)
  246. {
  247. $this->sendNow($notifiables, $notification);
  248. }
  249. /**
  250. * Send the given notification immediately.
  251. *
  252. * @param \Illuminate\Support\Collection|array|mixed $notifiables
  253. * @param mixed $notification
  254. * @param array|null $channels
  255. * @return void
  256. */
  257. public function sendNow($notifiables, $notification, array $channels = null)
  258. {
  259. if (! $notifiables instanceof Collection && ! is_array($notifiables)) {
  260. $notifiables = [$notifiables];
  261. }
  262. foreach ($notifiables as $notifiable) {
  263. if (! $notification->id) {
  264. $notification->id = Str::uuid()->toString();
  265. }
  266. $notifiableChannels = $channels ?: $notification->via($notifiable);
  267. if (method_exists($notification, 'shouldSend')) {
  268. $notifiableChannels = array_filter(
  269. $notifiableChannels,
  270. function ($channel) use ($notification, $notifiable) {
  271. return $notification->shouldSend($notifiable, $channel) !== false;
  272. }
  273. );
  274. if (empty($notifiableChannels)) {
  275. continue;
  276. }
  277. }
  278. $this->notifications[get_class($notifiable)][$notifiable->getKey()][get_class($notification)][] = [
  279. 'notification' => $notification,
  280. 'channels' => $notifiableChannels,
  281. 'notifiable' => $notifiable,
  282. 'locale' => $notification->locale ?? $this->locale ?? value(function () use ($notifiable) {
  283. if ($notifiable instanceof HasLocalePreference) {
  284. return $notifiable->preferredLocale();
  285. }
  286. }),
  287. ];
  288. }
  289. }
  290. /**
  291. * Get a channel instance by name.
  292. *
  293. * @param string|null $name
  294. * @return mixed
  295. */
  296. public function channel($name = null)
  297. {
  298. //
  299. }
  300. /**
  301. * Set the locale of notifications.
  302. *
  303. * @param string $locale
  304. * @return $this
  305. */
  306. public function locale($locale)
  307. {
  308. $this->locale = $locale;
  309. return $this;
  310. }
  311. }