Debian Codegraph @

lib/3rdParty/composer/illuminate/support/Testing/Fakes/NotificationFake.php

raw symbols SCIP
1 <?php
2
3 namespace Illuminate\Support\Testing\Fakes;
4
5 use Closure;
6 use Exception;
7 use Illuminate\Contracts\Notifications\Dispatcher as NotificationDispatcher;
8 use Illuminate\Contracts\Notifications\Factory as NotificationFactory;
9 use Illuminate\Contracts\Queue\ShouldQueue;
10 use Illuminate\Contracts\Translation\HasLocalePreference;
11 use Illuminate\Notifications\AnonymousNotifiable;
12 use Illuminate\Support\Collection;
13 use Illuminate\Support\Str;
14 use Illuminate\Support\Traits\Macroable;
15 use Illuminate\Support\Traits\ReflectsClosures;
16 use PHPUnit\Framework\Assert as PHPUnit;
17
18 class NotificationFake implements Fake, NotificationDispatcher, NotificationFactory
19 {
20 use Macroable, ReflectsClosures;
21
22 /**
23 * All of the notifications that have been sent.
24 *
25 * @var array
26 */
27 protected $notifications = [];
28
29 /**
30 * Locale used when sending notifications.
31 *
32 * @var string|null
33 */
34 public $locale;
35
36 /**
37 * Indicates if notifications should be serialized and restored when pushed to the queue.
38 *
39 * @var bool
40 */
41 protected $serializeAndRestore = false;
42
43 /**
44 * Assert if a notification was sent on-demand based on a truth-test callback.
45 *
46 * @param string|\Closure $notification
47 * @param callable|null $callback
48 * @return void
49 *
50 * @throws \Exception
51 */
52 public function assertSentOnDemand($notification, $callback = null)
53 {
54 $this->assertSentTo(new AnonymousNotifiable, $notification, $callback);
55 }
56
57 /**
58 * Assert if a notification was sent based on a truth-test callback.
59 *
60 * @param mixed $notifiable
61 * @param string|\Closure $notification
62 * @param callable|null $callback
63 * @return void
64 *
65 * @throws \Exception
66 */
67 public function assertSentTo($notifiable, $notification, $callback = null)
68 {
69 if (is_array($notifiable) || $notifiable instanceof Collection) {
70 if (count($notifiable) === 0) {
71 throw new Exception('No notifiable given.');
72 }
73
74 foreach ($notifiable as $singleNotifiable) {
75 $this->assertSentTo($singleNotifiable, $notification, $callback);
76 }
77
78 return;
79 }
80
81 if ($notification instanceof Closure) {
82 [$notification, $callback] = [$this->firstClosureParameterType($notification), $notification];
83 }
84
85 if (is_numeric($callback)) {
86 return $this->assertSentToTimes($notifiable, $notification, $callback);
87 }
88
89 PHPUnit::assertTrue(
90 $this->sent($notifiable, $notification, $callback)->count() > 0,
91 "The expected [{$notification}] notification was not sent."
92 );
93 }
94
95 /**
96 * Assert if a notification was sent on-demand a number of times.
97 *
98 * @param string $notification
99 * @param int $times
100 * @return void
101 */
102 public function assertSentOnDemandTimes($notification, $times = 1)
103 {
104 $this->assertSentToTimes(new AnonymousNotifiable, $notification, $times);
105 }
106
107 /**
108 * Assert if a notification was sent a number of times.
109 *
110 * @param mixed $notifiable
111 * @param string $notification
112 * @param int $times
113 * @return void
114 */
115 public function assertSentToTimes($notifiable, $notification, $times = 1)
116 {
117 $count = $this->sent($notifiable, $notification)->count();
118
119 PHPUnit::assertSame(
120 $times, $count,
121 "Expected [{$notification}] to be sent {$times} times, but was sent {$count} times."
122 );
123 }
124
125 /**
126 * Determine if a notification was sent based on a truth-test callback.
127 *
128 * @param mixed $notifiable
129 * @param string|\Closure $notification
130 * @param callable|null $callback
131 * @return void
132 *
133 * @throws \Exception
134 */
135 public function assertNotSentTo($notifiable, $notification, $callback = null)
136 {
137 if (is_array($notifiable) || $notifiable instanceof Collection) {
138 if (count($notifiable) === 0) {
139 throw new Exception('No notifiable given.');
140 }
141
142 foreach ($notifiable as $singleNotifiable) {
143 $this->assertNotSentTo($singleNotifiable, $notification, $callback);
144 }
145
146 return;
147 }
148
149 if ($notification instanceof Closure) {
150 [$notification, $callback] = [$this->firstClosureParameterType($notification), $notification];
151 }
152
153 PHPUnit::assertCount(
154 0, $this->sent($notifiable, $notification, $callback),
155 "The unexpected [{$notification}] notification was sent."
156 );
157 }
158
159 /**
160 * Assert that no notifications were sent.
161 *
162 * @return void
163 */
164 public function assertNothingSent()
165 {
166 $notificationNames = (new Collection($this->notifications))
167 ->map(fn ($notifiableModels) => (new Collection($notifiableModels))
168 ->map(fn ($notifiables) => (new Collection($notifiables))->keys())
169 )
170 ->flatten()->join("\n- ");
171
172 PHPUnit::assertEmpty($this->notifications, "The following notifications were sent unexpectedly:\n\n- $notificationNames\n");
173 }
174
175 /**
176 * Assert that no notifications were sent to the given notifiable.
177 *
178 * @param mixed $notifiable
179 * @return void
180 *
181 * @throws \Exception
182 */
183 public function assertNothingSentTo($notifiable)
184 {
185 if (is_array($notifiable) || $notifiable instanceof Collection) {
186 if (count($notifiable) === 0) {
187 throw new Exception('No notifiable given.');
188 }
189
190 foreach ($notifiable as $singleNotifiable) {
191 $this->assertNothingSentTo($singleNotifiable);
192 }
193
194 return;
195 }
196
197 PHPUnit::assertEmpty(
198 $this->notifications[get_class($notifiable)][$notifiable->getKey() ?? ''] ?? [],
199 'Notifications were sent unexpectedly.',
200 );
201 }
202
203 /**
204 * Assert the total amount of times a notification was sent.
205 *
206 * @param string $notification
207 * @param int $expectedCount
208 * @return void
209 */
210 public function assertSentTimes($notification, $expectedCount)
211 {
212 $actualCount = (new Collection($this->notifications))
213 ->flatten(1)
214 ->reduce(fn ($count, $sent) => $count + count($sent[$notification] ?? []), 0);
215
216 PHPUnit::assertSame(
217 $expectedCount, $actualCount,
218 sprintf(
219 "Expected [{$notification}] to be sent {$expectedCount} %s, but was sent {$actualCount} %s.",
220 Str::plural('time', $expectedCount),
221 Str::plural('time', $actualCount)
222 )
223 );
224 }
225
226 /**
227 * Assert the total count of notification that were sent.
228 *
229 * @param int $expectedCount
230 * @return void
231 */
232 public function assertCount($expectedCount)
233 {
234 $actualCount = (new Collection($this->notifications))->flatten(3)->count();
235
236 PHPUnit::assertSame(
237 $expectedCount, $actualCount,
238 "Expected {$expectedCount} notifications to be sent, but {$actualCount} were sent."
239 );
240 }
241
242 /**
243 * Get all of the notifications matching a truth-test callback.
244 *
245 * @param mixed $notifiable
246 * @param string $notification
247 * @param callable|null $callback
248 * @return \Illuminate\Support\Collection
249 */
250 public function sent($notifiable, $notification, $callback = null)
251 {
252 if (! $this->hasSent($notifiable, $notification)) {
253 return new Collection;
254 }
255
256 $callback = $callback ?: fn () => true;
257
258 $notifications = new Collection($this->notificationsFor($notifiable, $notification));
259
260 return $notifications->filter(
261 fn ($arguments) => $callback(...array_values($arguments))
262 )->pluck('notification');
263 }
264
265 /**
266 * Determine if there are more notifications left to inspect.
267 *
268 * @param mixed $notifiable
269 * @param string $notification
270 * @return bool
271 */
272 public function hasSent($notifiable, $notification)
273 {
274 return ! empty($this->notificationsFor($notifiable, $notification));
275 }
276
277 /**
278 * Get all of the notifications for a notifiable entity by type.
279 *
280 * @param mixed $notifiable
281 * @param string $notification
282 * @return array
283 */
284 protected function notificationsFor($notifiable, $notification)
285 {
286 return $this->notifications[get_class($notifiable)][(string) $notifiable->getKey()][$notification] ?? [];
287 }
288
289 /**
290 * Send the given notification to the given notifiable entities.
291 *
292 * @param \Illuminate\Support\Collection|mixed $notifiables
293 * @param mixed $notification
294 * @return void
295 */
296 public function send($notifiables, $notification)
297 {
298 $this->sendNow($notifiables, $notification);
299 }
300
301 /**
302 * Send the given notification immediately.
303 *
304 * @param \Illuminate\Support\Collection|mixed $notifiables
305 * @param mixed $notification
306 * @param array|null $channels
307 * @return void
308 */
309 public function sendNow($notifiables, $notification, ?array $channels = null)
310 {
311 if (! $notifiables instanceof Collection && ! is_array($notifiables)) {
312 $notifiables = [$notifiables];
313 }
314
315 foreach ($notifiables as $notifiable) {
316 if (! $notification->id) {
317 $notification->id = (string) Str::uuid();
318 }
319
320 $notifiableChannels = $channels ?: $notification->via($notifiable);
321
322 if (method_exists($notification, 'shouldSend')) {
323 $notifiableChannels = array_filter(
324 $notifiableChannels,
325 fn ($channel) => $notification->shouldSend($notifiable, $channel) !== false
326 );
327 }
328
329 if (empty($notifiableChannels)) {
330 continue;
331 }
332
333 $this->notifications[get_class($notifiable)][(string) $notifiable->getKey()][get_class($notification)][] = [
334 'notification' => $this->serializeAndRestore && $notification instanceof ShouldQueue
335 ? $this->serializeAndRestoreNotification($notification)
336 : $notification,
337 'channels' => $notifiableChannels,
338 'notifiable' => $notifiable,
339 'locale' => $notification->locale ?? $this->locale ?? value(function () use ($notifiable) {
340 if ($notifiable instanceof HasLocalePreference) {
341 return $notifiable->preferredLocale();
342 }
343 }),
344 ];
345 }
346 }
347
348 /**
349 * Get a channel instance by name.
350 *
351 * @param string|null $name
352 * @return mixed
353 */
354 public function channel($name = null)
355 {
356 //
357 }
358
359 /**
360 * Set the locale of notifications.
361 *
362 * @param string $locale
363 * @return $this
364 */
365 public function locale($locale)
366 {
367 $this->locale = $locale;
368
369 return $this;
370 }
371
372 /**
373 * Specify if notification should be serialized and restored when being "pushed" to the queue.
374 *
375 * @param bool $serializeAndRestore
376 * @return $this
377 */
378 public function serializeAndRestore(bool $serializeAndRestore = true)
379 {
380 $this->serializeAndRestore = $serializeAndRestore;
381
382 return $this;
383 }
384
385 /**
386 * Serialize and unserialize the notification to simulate the queueing process.
387 *
388 * @param mixed $notification
389 * @return mixed
390 */
391 protected function serializeAndRestoreNotification($notification)
392 {
393 return unserialize(serialize($notification));
394 }
395
396 /**
397 * Get the notifications that have been sent.
398 *
399 * @return array
400 */
401 public function sentNotifications()
402 {
403 return $this->notifications;
404 }
405 }