From b1d8f813d13960096493f3adc3bc32ace66ba2e6 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sat, 24 Dec 2016 13:57:51 -0600 Subject: [PATCH] Refactor the mail fake to not be really stupid. Now it works more similarly to the other fakes and you can just use $mailable->hasTo ->hasCc, etc. to make sure it has a given recipient within your passed callable. The callable simply receives the mailable instance. Properties on mailable are public for easy inspection. --- .../Support/Testing/Fakes/MailFake.php | 116 +++--------------- .../Support/Testing/Fakes/PendingMailFake.php | 24 +--- 2 files changed, 20 insertions(+), 120 deletions(-) diff --git a/src/Illuminate/Support/Testing/Fakes/MailFake.php b/src/Illuminate/Support/Testing/Fakes/MailFake.php index 781940bc2ab3..5fc815e6ee4e 100644 --- a/src/Illuminate/Support/Testing/Fakes/MailFake.php +++ b/src/Illuminate/Support/Testing/Fakes/MailFake.php @@ -32,68 +32,6 @@ public function assertSent($mailable, $callback = null) ); } - /** - * Assert if a mailable was sent based on a truth-test callback. - * - * @param mixed $users - * @param string $mailable - * @param callable|null $callback - * @return void - */ - public function assertSentTo($users, $mailable, $callback = null) - { - $users = $this->formatRecipients($users); - - return $this->assertSent($mailable, function ($mailable, $to) use ($users, $callback) { - if (! $this->recipientsMatch($users, $this->formatRecipients($to))) { - return false; - } - - if (! is_null($callback)) { - return $callback(...func_get_args()); - } - - return true; - }); - } - - /** - * Format the recipients into a collection. - * - * @param mixed $recipients - * @return \Illuminate\Support\Collection - */ - protected function formatRecipients($recipients) - { - if ($recipients instanceof Collection) { - return $recipients; - } - - return collect(is_array($recipients) ? $recipients : [$recipients]); - } - - /** - * Determine if two given recipient lists match. - * - * @param \Illuminate\Support\Collection $expected - * @param \Illuminate\Support\Collection $recipients - * @return bool - */ - protected function recipientsMatch($expected, $recipients) - { - $expected = $expected->map(function ($expected) { - return is_object($expected) ? $expected->email : $expected; - }); - - return $recipients->map(function ($recipient) { - if (is_array($recipient)) { - return $recipient['email']; - } - - return is_object($recipient) ? $recipient->email : $recipient; - })->diff($expected)->count() === 0; - } - /** * Determine if a mailable was sent based on a truth-test callback. * @@ -127,7 +65,7 @@ public function sent($mailable, $callback = null) }; return $this->mailablesOf($mailable)->filter(function ($mailable) use ($callback) { - return $callback($mailable->mailable, ...array_values($mailable->getRecipients())); + return $callback($mailable); }); } @@ -150,8 +88,8 @@ public function hasSent($mailable) */ protected function mailablesOf($type) { - return collect($this->mailables)->filter(function ($m) use ($type) { - return $m->mailable instanceof $type; + return collect($this->mailables)->filter(function ($mailable) use ($type) { + return $mailable instanceof $type; }); } @@ -163,9 +101,7 @@ protected function mailablesOf($type) */ public function to($users) { - $this->mailables[] = $mailable = (new PendingMailFake)->to($users); - - return $mailable; + return (new PendingMailFake($this))->to($users); } /** @@ -176,9 +112,7 @@ public function to($users) */ public function bcc($users) { - $this->mailables[] = $mailable = (new PendingMailFake)->bcc($users); - - return $mailable; + return (new PendingMailFake($this))->bcc($users); } /** @@ -207,35 +141,7 @@ public function send($view, array $data = [], $callback = null) return; } - Container::getInstance()->call([$view, 'build']); - - $mailable = new PendingMailFake; - - $mailable->mailable = $view; - - if ($recipients = $view->to) { - $mailable->to($recipients); - } - - if ($recipients = $view->bcc) { - $mailable->bcc($recipients); - } - - if ($recipients = $view->cc) { - $mailable->cc($recipients); - } - - $this->mailables[] = $mailable; - } - - /** - * Get the array of failed recipients. - * - * @return array - */ - public function failures() - { - // + $this->mailables[] = $view; } /** @@ -251,4 +157,14 @@ public function queue($view, array $data = [], $callback = null, $queue = null) { $this->send($view); } + + /** + * Get the array of failed recipients. + * + * @return array + */ + public function failures() + { + // + } } diff --git a/src/Illuminate/Support/Testing/Fakes/PendingMailFake.php b/src/Illuminate/Support/Testing/Fakes/PendingMailFake.php index 4be58a4fdcfa..76899d890731 100644 --- a/src/Illuminate/Support/Testing/Fakes/PendingMailFake.php +++ b/src/Illuminate/Support/Testing/Fakes/PendingMailFake.php @@ -7,21 +7,15 @@ class PendingMailFake extends PendingMail { - /** - * The mailable instance. - * - * @var mixed - */ - public $mailable; - /** * Create a new instance. * + * @param \Illuminate\Support\Testing\Fakes\MailFake * @return void */ - public function __construct() + public function __construct($mailer) { - // + $this->mailer = $mailer; } /** @@ -43,7 +37,7 @@ public function send(Mailable $mailable) */ public function sendNow(Mailable $mailable) { - $this->mailable = $mailable; + $this->mailer->send($this->fill($mailable)); } /** @@ -56,14 +50,4 @@ public function queue(Mailable $mailable) { return $this->sendNow($mailable); } - - /** - * Get the recipient information for the mailable. - * - * @return array - */ - public function getRecipients() - { - return ['to' => $this->to, 'cc' => $this->cc, 'bcc' => $this->bcc]; - } }