Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 namespace Illuminate\Support\Testing\Fakes;
4
5 use Closure;
6 use Illuminate\Contracts\Mail\Factory;
7 use Illuminate\Contracts\Mail\Mailable;
8 use Illuminate\Contracts\Mail\Mailer;
9 use Illuminate\Contracts\Mail\MailQueue;
10 use Illuminate\Contracts\Queue\ShouldQueue;
11 use Illuminate\Mail\MailManager;
12 use Illuminate\Support\Arr;
13 use Illuminate\Support\Collection;
14 use Illuminate\Support\Str;
15 use Illuminate\Support\Traits\ForwardsCalls;
16 use Illuminate\Support\Traits\ReflectsClosures;
17 use PHPUnit\Framework\Assert as PHPUnit;
18
19 class MailFake implements Factory, Fake, Mailer, MailQueue
20 {
21 use ForwardsCalls, ReflectsClosures;
22
23 /**
24 * The mailer instance.
25 *
26 * @var MailManager
27 */
28 public $manager;
29
30 /**
31 * The mailer currently being used to send a message.
32 *
33 * @var string
34 */
35 protected $currentMailer;
36
37 /**
38 * All of the mailables that have been sent.
39 *
40 * @var array
41 */
42 protected $mailables = [];
43
44 /**
45 * All of the mailables that have been queued.
46 *
47 * @var array
48 */
49 protected $queuedMailables = [];
50
51 /**
52 * Create a new mail fake.
53 *
54 * @param MailManager $manager
55 */
56 public function __construct(MailManager $manager)
57 {
58 $this->manager = $manager;
59 $this->currentMailer = $manager->getDefaultDriver();
60 }
61
62 /**
63 * Assert if a mailable was sent based on a truth-test callback.
64 *
65 * @param string|\Closure $mailable
66 * @param callable|array|string|int|null $callback
67 * @return void
68 */
69 public function assertSent($mailable, $callback = null)
70 {
71 [$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback);
72
73 if (is_numeric($callback)) {
74 return $this->assertSentTimes($mailable, $callback);
75 }
76
77 $suggestion = count($this->queuedMailables) ? ' Did you mean to use assertQueued() instead?' : '';
78
79 if (is_array($callback) || is_string($callback)) {
80 foreach (Arr::wrap($callback) as $address) {
81 $callback = fn ($mail) => $mail->hasTo($address);
82
83 PHPUnit::assertTrue(
84 $this->sent($mailable, $callback)->count() > 0,
85 "The expected [{$mailable}] mailable was not sent to address [{$address}].".$suggestion
86 );
87 }
88
89 return;
90 }
91
92 PHPUnit::assertTrue(
93 $this->sent($mailable, $callback)->count() > 0,
94 "The expected [{$mailable}] mailable was not sent.".$suggestion
95 );
96 }
97
98 /**
99 * Assert if a mailable was sent a number of times.
100 *
101 * @param string $mailable
102 * @param int $times
103 * @return void
104 */
105 public function assertSentTimes($mailable, $times = 1)
106 {
107 $count = $this->sent($mailable)->count();
108
109 PHPUnit::assertSame(
110 $times, $count,
111 sprintf(
112 "The expected [{$mailable}] mailable was sent {$count} %s instead of {$times} %s.",
113 Str::plural('time', $count),
114 Str::plural('time', $times)
115 )
116 );
117 }
118
119 /**
120 * Determine if a mailable was not sent or queued to be sent based on a truth-test callback.
121 *
122 * @param string|\Closure $mailable
123 * @param callable|null $callback
124 * @return void
125 */
126 public function assertNotOutgoing($mailable, $callback = null)
127 {
128 $this->assertNotSent($mailable, $callback);
129 $this->assertNotQueued($mailable, $callback);
130 }
131
132 /**
133 * Determine if a mailable was not sent based on a truth-test callback.
134 *
135 * @param string|\Closure $mailable
136 * @param callable|array|string|null $callback
137 * @return void
138 */
139 public function assertNotSent($mailable, $callback = null)
140 {
141 if (is_string($callback) || is_array($callback)) {
142 foreach (Arr::wrap($callback) as $address) {
143 $callback = fn ($mail) => $mail->hasTo($address);
144
145 PHPUnit::assertCount(
146 0, $this->sent($mailable, $callback),
147 "The unexpected [{$mailable}] mailable was sent to address [{$address}]."
148 );
149 }
150
151 return;
152 }
153
154 [$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback);
155
156 PHPUnit::assertCount(
157 0, $this->sent($mailable, $callback),
158 "The unexpected [{$mailable}] mailable was sent."
159 );
160 }
161
162 /**
163 * Assert that no mailables were sent or queued to be sent.
164 *
165 * @return void
166 */
167 public function assertNothingOutgoing()
168 {
169 $this->assertNothingSent();
170 $this->assertNothingQueued();
171 }
172
173 /**
174 * Assert that no mailables were sent.
175 *
176 * @return void
177 */
178 public function assertNothingSent()
179 {
180 $mailableNames = (new Collection($this->mailables))->map(
181 fn ($mailable) => get_class($mailable)
182 )->join("\n- ");
183
184 PHPUnit::assertEmpty($this->mailables, "The following mailables were sent unexpectedly:\n\n- $mailableNames\n");
185 }
186
187 /**
188 * Assert if a mailable was queued based on a truth-test callback.
189 *
190 * @param string|\Closure $mailable
191 * @param callable|array|string|int|null $callback
192 * @return void
193 */
194 public function assertQueued($mailable, $callback = null)
195 {
196 [$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback);
197
198 if (is_numeric($callback)) {
199 return $this->assertQueuedTimes($mailable, $callback);
200 }
201
202 if (is_string($callback) || is_array($callback)) {
203 foreach (Arr::wrap($callback) as $address) {
204 $callback = fn ($mail) => $mail->hasTo($address);
205
206 PHPUnit::assertTrue(
207 $this->queued($mailable, $callback)->count() > 0,
208 "The expected [{$mailable}] mailable was not queued to address [{$address}]."
209 );
210 }
211
212 return;
213 }
214
215 PHPUnit::assertTrue(
216 $this->queued($mailable, $callback)->count() > 0,
217 "The expected [{$mailable}] mailable was not queued."
218 );
219 }
220
221 /**
222 * Assert if a mailable was queued a number of times.
223 *
224 * @param string $mailable
225 * @param int $times
226 * @return void
227 */
228 protected function assertQueuedTimes($mailable, $times = 1)
229 {
230 $count = $this->queued($mailable)->count();
231
232 PHPUnit::assertSame(
233 $times, $count,
234 sprintf(
235 "The expected [{$mailable}] mailable was queued {$count} %s instead of {$times} %s.",
236 Str::plural('time', $count),
237 Str::plural('time', $times)
238 )
239 );
240 }
241
242 /**
243 * Determine if a mailable was not queued based on a truth-test callback.
244 *
245 * @param string|\Closure $mailable
246 * @param callable|array|string|null $callback
247 * @return void
248 */
249 public function assertNotQueued($mailable, $callback = null)
250 {
251 if (is_string($callback) || is_array($callback)) {
252 foreach (Arr::wrap($callback) as $address) {
253 $callback = fn ($mail) => $mail->hasTo($address);
254
255 PHPUnit::assertCount(
256 0, $this->queued($mailable, $callback),
257 "The unexpected [{$mailable}] mailable was queued to address [{$address}]."
258 );
259 }
260
261 return;
262 }
263
264 [$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback);
265
266 PHPUnit::assertCount(
267 0, $this->queued($mailable, $callback),
268 "The unexpected [{$mailable}] mailable was queued."
269 );
270 }
271
272 /**
273 * Assert that no mailables were queued.
274 *
275 * @return void
276 */
277 public function assertNothingQueued()
278 {
279 $mailableNames = (new Collection($this->queuedMailables))->map(
280 fn ($mailable) => get_class($mailable)
281 )->join("\n- ");
282
283 PHPUnit::assertEmpty($this->queuedMailables, "The following mailables were queued unexpectedly:\n\n- $mailableNames\n");
284 }
285
286 /**
287 * Assert the total number of mailables that were sent.
288 *
289 * @param int $count
290 * @return void
291 */
292 public function assertSentCount($count)
293 {
294 $total = (new Collection($this->mailables))->count();
295
296 PHPUnit::assertSame(
297 $count, $total,
298 "The total number of mailables sent was {$total} instead of {$count}."
299 );
300 }
301
302 /**
303 * Assert the total number of mailables that were queued.
304 *
305 * @param int $count
306 * @return void
307 */
308 public function assertQueuedCount($count)
309 {
310 $total = (new Collection($this->queuedMailables))->count();
311
312 PHPUnit::assertSame(
313 $count, $total,
314 "The total number of mailables queued was {$total} instead of {$count}."
315 );
316 }
317
318 /**
319 * Assert the total number of mailables that were sent or queued.
320 *
321 * @param int $count
322 * @return void
323 */
324 public function assertOutgoingCount($count)
325 {
326 $total = (new Collection($this->mailables))
327 ->concat($this->queuedMailables)
328 ->count();
329
330 PHPUnit::assertSame(
331 $count, $total,
332 "The total number of outgoing mailables was {$total} instead of {$count}."
333 );
334 }
335
336 /**
337 * Get all of the mailables matching a truth-test callback.
338 *
339 * @param string|\Closure $mailable
340 * @param callable|null $callback
341 * @return \Illuminate\Support\Collection
342 */
343 public function sent($mailable, $callback = null)
344 {
345 [$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback);
346
347 if (! $this->hasSent($mailable)) {
348 return new Collection;
349 }
350
351 $callback = $callback ?: fn () => true;
352
353 return $this->mailablesOf($mailable)->filter(fn ($mailable) => $callback($mailable));
354 }
355
356 /**
357 * Determine if the given mailable has been sent.
358 *
359 * @param string $mailable
360 * @return bool
361 */
362 public function hasSent($mailable)
363 {
364 return $this->mailablesOf($mailable)->count() > 0;
365 }
366
367 /**
368 * Get all of the queued mailables matching a truth-test callback.
369 *
370 * @param string|\Closure $mailable
371 * @param callable|null $callback
372 * @return \Illuminate\Support\Collection
373 */
374 public function queued($mailable, $callback = null)
375 {
376 [$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback);
377
378 if (! $this->hasQueued($mailable)) {
379 return new Collection;
380 }
381
382 $callback = $callback ?: fn () => true;
383
384 return $this->queuedMailablesOf($mailable)->filter(fn ($mailable) => $callback($mailable));
385 }
386
387 /**
388 * Determine if the given mailable has been queued.
389 *
390 * @param string $mailable
391 * @return bool
392 */
393 public function hasQueued($mailable)
394 {
395 return $this->queuedMailablesOf($mailable)->count() > 0;
396 }
397
398 /**
399 * Get all of the mailed mailables for a given type.
400 *
401 * @param string $type
402 * @return \Illuminate\Support\Collection
403 */
404 protected function mailablesOf($type)
405 {
406 return (new Collection($this->mailables))->filter(fn ($mailable) => $mailable instanceof $type);
407 }
408
409 /**
410 * Get all of the mailed mailables for a given type.
411 *
412 * @param string $type
413 * @return \Illuminate\Support\Collection
414 */
415 protected function queuedMailablesOf($type)
416 {
417 return (new Collection($this->queuedMailables))->filter(fn ($mailable) => $mailable instanceof $type);
418 }
419
420 /**
421 * Get a mailer instance by name.
422 *
423 * @param string|null $name
424 * @return \Illuminate\Contracts\Mail\Mailer
425 */
426 public function mailer($name = null)
427 {
428 $this->currentMailer = $name;
429
430 return $this;
431 }
432
433 /**
434 * Begin the process of mailing a mailable class instance.
435 *
436 * @param mixed $users
437 * @return \Illuminate\Mail\PendingMail
438 */
439 public function to($users)
440 {
441 return (new PendingMailFake($this))->to($users);
442 }
443
444 /**
445 * Begin the process of mailing a mailable class instance.
446 *
447 * @param mixed $users
448 * @return \Illuminate\Mail\PendingMail
449 */
450 public function cc($users)
451 {
452 return (new PendingMailFake($this))->cc($users);
453 }
454
455 /**
456 * Begin the process of mailing a mailable class instance.
457 *
458 * @param mixed $users
459 * @return \Illuminate\Mail\PendingMail
460 */
461 public function bcc($users)
462 {
463 return (new PendingMailFake($this))->bcc($users);
464 }
465
466 /**
467 * Send a new message with only a raw text part.
468 *
469 * @param string $text
470 * @param \Closure|string $callback
471 * @return void
472 */
473 public function raw($text, $callback)
474 {
475 //
476 }
477
478 /**
479 * Send a new message using a view.
480 *
481 * @param \Illuminate\Contracts\Mail\Mailable|string|array $view
482 * @param array $data
483 * @param \Closure|string|null $callback
484 * @return mixed|void
485 */
486 public function send($view, array $data = [], $callback = null)
487 {
488 return $this->sendMail($view, $view instanceof ShouldQueue);
489 }
490
491 /**
492 * Send a new message synchronously using a view.
493 *
494 * @param \Illuminate\Contracts\Mail\Mailable|string|array $mailable
495 * @param array $data
496 * @param \Closure|string|null $callback
497 * @return void
498 */
499 public function sendNow($mailable, array $data = [], $callback = null)
500 {
501 $this->sendMail($mailable, shouldQueue: false);
502 }
503
504 /**
505 * Send a new message using a view.
506 *
507 * @param \Illuminate\Contracts\Mail\Mailable|string|array $view
508 * @param bool $shouldQueue
509 * @return mixed|void
510 */
511 protected function sendMail($view, $shouldQueue = false)
512 {
513 if (! $view instanceof Mailable) {
514 return;
515 }
516
517 $view->mailer($this->currentMailer);
518
519 if ($shouldQueue) {
520 return $this->queue($view);
521 }
522
523 $this->currentMailer = null;
524
525 $this->mailables[] = $view;
526 }
527
528 /**
529 * Queue a new message for sending.
530 *
531 * @param \Illuminate\Contracts\Mail\Mailable|string|array $view
532 * @param string|null $queue
533 * @return mixed
534 */
535 public function queue($view, $queue = null)
536 {
537 if (! $view instanceof Mailable) {
538 return;
539 }
540
541 $view->mailer($this->currentMailer);
542
543 $this->currentMailer = null;
544
545 $this->queuedMailables[] = $view;
546 }
547
548 /**
549 * Queue a new e-mail message for sending after (n) seconds.
550 *
551 * @param \DateTimeInterface|\DateInterval|int $delay
552 * @param \Illuminate\Contracts\Mail\Mailable|string|array $view
553 * @param string|null $queue
554 * @return mixed
555 */
556 public function later($delay, $view, $queue = null)
557 {
558 $this->queue($view, $queue);
559 }
560
561 /**
562 * Infer mailable class using reflection if a typehinted closure is passed to assertion.
563 *
564 * @param string|\Closure $mailable
565 * @param callable|null $callback
566 * @return array
567 */
568 protected function prepareMailableAndCallback($mailable, $callback)
569 {
570 if ($mailable instanceof Closure) {
571 return [$this->firstClosureParameterType($mailable), $mailable];
572 }
573
574 return [$mailable, $callback];
575 }
576
577 /**
578 * Forget all of the resolved mailer instances.
579 *
580 * @return $this
581 */
582 public function forgetMailers()
583 {
584 $this->currentMailer = null;
585
586 return $this;
587 }
588
589 /**
590 * Handle dynamic method calls to the mailer.
591 *
592 * @param string $method
593 * @param array $parameters
594 * @return mixed
595 */
596 public function __call($method, $parameters)
597 {
598 return $this->forwardCallTo($this->manager, $method, $parameters);
599 }
600 }