Skip to content

Commit

Permalink
Refactor the mail fake to not be really stupid.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
taylorotwell committed Dec 24, 2016
1 parent fb29b38 commit b1d8f81
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 120 deletions.
116 changes: 16 additions & 100 deletions src/Illuminate/Support/Testing/Fakes/MailFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
});
}

Expand All @@ -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;
});
}

Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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()
{
//
}
}
24 changes: 4 additions & 20 deletions src/Illuminate/Support/Testing/Fakes/PendingMailFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -43,7 +37,7 @@ public function send(Mailable $mailable)
*/
public function sendNow(Mailable $mailable)
{
$this->mailable = $mailable;
$this->mailer->send($this->fill($mailable));
}

/**
Expand All @@ -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];
}
}

0 comments on commit b1d8f81

Please sign in to comment.