| 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 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public $manager; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
protected $currentMailer; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
protected $mailables = []; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
protected $queuedMailables = []; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public function __construct(MailManager $manager) |
| 57 |
{ |
| 58 |
$this->manager = $manager; |
| 59 |
$this->currentMailer = $manager->getDefaultDriver(); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 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 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 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 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public function assertNotOutgoing($mailable, $callback = null) |
| 127 |
{ |
| 128 |
$this->assertNotSent($mailable, $callback); |
| 129 |
$this->assertNotQueued($mailable, $callback); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 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 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public function assertNothingOutgoing() |
| 168 |
{ |
| 169 |
$this->assertNothingSent(); |
| 170 |
$this->assertNothingQueued(); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 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 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 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 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 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 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 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 |
|
| 274 |
|
| 275 |
|
| 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 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 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 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 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 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 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 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 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 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
public function hasSent($mailable) |
| 363 |
{ |
| 364 |
return $this->mailablesOf($mailable)->count() > 0; |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 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 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
public function hasQueued($mailable) |
| 394 |
{ |
| 395 |
return $this->queuedMailablesOf($mailable)->count() > 0; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
protected function mailablesOf($type) |
| 405 |
{ |
| 406 |
return (new Collection($this->mailables))->filter(fn ($mailable) => $mailable instanceof $type); |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
protected function queuedMailablesOf($type) |
| 416 |
{ |
| 417 |
return (new Collection($this->queuedMailables))->filter(fn ($mailable) => $mailable instanceof $type); |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
public function mailer($name = null) |
| 427 |
{ |
| 428 |
$this->currentMailer = $name; |
| 429 |
|
| 430 |
return $this; |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
public function to($users) |
| 440 |
{ |
| 441 |
return (new PendingMailFake($this))->to($users); |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
public function cc($users) |
| 451 |
{ |
| 452 |
return (new PendingMailFake($this))->cc($users); |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
public function bcc($users) |
| 462 |
{ |
| 463 |
return (new PendingMailFake($this))->bcc($users); |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
public function raw($text, $callback) |
| 474 |
{ |
| 475 |
|
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
public function send($view, array $data = [], $callback = null) |
| 487 |
{ |
| 488 |
return $this->sendMail($view, $view instanceof ShouldQueue); |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
public function sendNow($mailable, array $data = [], $callback = null) |
| 500 |
{ |
| 501 |
$this->sendMail($mailable, shouldQueue: false); |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 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 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 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 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
public function later($delay, $view, $queue = null) |
| 557 |
{ |
| 558 |
$this->queue($view, $queue); |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 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 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
public function forgetMailers() |
| 583 |
{ |
| 584 |
$this->currentMailer = null; |
| 585 |
|
| 586 |
return $this; |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
public function __call($method, $parameters) |
| 597 |
{ |
| 598 |
return $this->forwardCallTo($this->manager, $method, $parameters); |
| 599 |
} |
| 600 |
} |