Skip to content

Instantly share code, notes, and snippets.

@alxy
Created November 24, 2019 21:26
Show Gist options
  • Save alxy/6e74d306be7bd0c9d1f134f34d547a44 to your computer and use it in GitHub Desktop.
Save alxy/6e74d306be7bd0c9d1f134f34d547a44 to your computer and use it in GitHub Desktop.
<?php namespace October\Rain\Support\Testing\Fakes;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Testing\Fakes\MailFake as MailFakeBase;
use October\Rain\Mail\Mailable;
class MailFake extends MailFakeBase
{
/**
* Send a new message using a view.
*
* @param string|array $view
* @param array $data
* @param \Closure|string $callback
* @return void
*/
public function send($view, array $data = [], $callback = null)
{
if (! $view instanceof Mailable) {
$view = $this->buildMailable($view, $data, $callback);
}
$this->mailables[] = $view;
}
/**
* Get all of the mailables matching a truth-test callback.
*
* @param string $mailable
* @param callable|null $callback
* @return \Illuminate\Support\Collection
*/
public function sent($view, $callback = null)
{
// var_dump($view);
if (! $this->hasSent($view)) {
return collect();
}
$callback = $callback ?: function () {
return true;
};
return $this->mailablesOf($view)->filter(function ($mailable) use ($callback) {
return $callback($mailable);
});
}
/**
* Determine if the given mailable has been sent.
*
* @param string $mailable
* @return bool
*/
public function hasSent($mailable)
{
return $this->mailablesOf($mailable)->count() > 0;
}
/**
* Get all of the queued mailables matching a truth-test callback.
*
* @param string $mailable
* @param callable|null $callback
* @return \Illuminate\Support\Collection
*/
public function queued($mailable, $callback = null)
{
if (! $this->hasQueued($mailable)) {
return collect();
}
$callback = $callback ?: function () {
return true;
};
return $this->queuedMailablesOf($mailable)->filter(function ($mailable) use ($callback) {
return $callback($mailable);
});
}
/**
* Determine if the given mailable has been queued.
*
* @param string $mailable
* @return bool
*/
public function hasQueued($mailable)
{
return $this->queuedMailablesOf($mailable)->count() > 0;
}
/**
* Get all of the mailed mailables for a given type.
*
* @param string $type
* @return \Illuminate\Support\Collection
*/
protected function mailablesOf($type)
{
return collect($this->mailables)->filter(function ($mailable) use ($type) {
return $mailable->view === $type;
});
}
/**
* Get all of the mailed mailables for a given type.
*
* @param string $type
* @return \Illuminate\Support\Collection
*/
protected function queuedMailablesOf($type)
{
return collect($this->queuedMailables)->filter(function ($mailable) use ($type) {
return $mailable instanceof $type;
});
}
/**
* Build the mailable for a queued e-mail job.
*
* @param mixed $callback
* @return mixed
*/
protected function buildMailable($view, $data, $callback, $queued = false)
{
$mailable = new Mailable;
if($queued) {
$mailable->view($view)->withSerializedData($data);
} else {
$mailable->view($view, $data);
}
if ($callback !== null) {
call_user_func($callback, $mailable);
}
return $mailable;
}
/**
* Queue a new e-mail message for sending.
*
* @param string|array $view
* @param array $data
* @param \Closure|string $callback
* @param string|null $queue
* @return mixed
*/
public function queue($view, $data = null, $callback = null, $queue = null)
{
if (!$view instanceof Mailable) {
$mailable = $this->buildMailable($view, $data, $callback, true);
}
else {
$mailable = $view;
}
$this->queuedMailables[] = $mailable;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment