Skip to content

Commit

Permalink
allow testing of html and plain text bodys right off mailables
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 7, 2020
1 parent 198eea4 commit afb858a
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions src/Illuminate/Mail/Mailable.php
Expand Up @@ -7,12 +7,14 @@
use Illuminate\Contracts\Mail\Factory as MailFactory;
use Illuminate\Contracts\Mail\Mailable as MailableContract;
use Illuminate\Contracts\Queue\Factory as Queue;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Support\Collection;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\ForwardsCalls;
use Illuminate\Support\Traits\Localizable;
use PHPUnit\Framework\Assert as PHPUnit;
use ReflectionClass;
use ReflectionProperty;

Expand Down Expand Up @@ -146,6 +148,13 @@ class Mailable implements MailableContract, Renderable
*/
public $mailer;

/**
* The rendered views for assertions.
*
* @var array
*/
protected $assertionableRender;

/**
* The callback that should be invoked while building the view data.
*
Expand Down Expand Up @@ -844,6 +853,102 @@ public function attachData($data, $name, array $options = [])
return $this;
}

/**
* Assert that the given text is present in the HTML email body.
*
* @param string $string
* @return void
*/
public function assertSeeInHtml($string)
{
[$html, $text] = $this->renderForAssertions();

PHPUnit::assertTrue(
Str::contains($html, $string),
"Did not see expected text [{$string}] within email body."
);
}

/**
* Assert that the given text is not present in the HTML email body.
*
* @param string $string
* @return void
*/
public function assertDontSeeInHtml($string)
{
[$html, $text] = $this->renderForAssertions();

PHPUnit::assertFalse(
Str::contains($html, $string),
"Saw unexpected text [{$string}] within email body."
);
}

/**
* Assert that the given text is present in the plain-text email body.
*
* @param string $string
* @return void
*/
public function assertSeeInText($string)
{
[$html, $text] = $this->renderForAssertions();

PHPUnit::assertTrue(
Str::contains($text, $string),
"Did not see expected text [{$string}] within text email body."
);
}

/**
* Assert that the given text is not present in the plain-text email body.
*
* @param string $string
* @return void
*/
public function assertDontSeeInText($string)
{
[$html, $text] = $this->renderForAssertions();

PHPUnit::assertFalse(
Str::contains($text, $string),
"Saw unexpected text [{$string}] within text email body."
);
}

/**
* Render the HTML and plain-text version of the mailable into views for assertions.
*
* @return array
*
* @throws \ReflectionException
*/
protected function renderForAssertions()
{
if ($this->assertionableRender) {
return $this->assertionableRender;
}

return $this->assertionableRender = $this->withLocale($this->locale, function () {
Container::getInstance()->call([$this, 'build']);

$html = Container::getInstance()->make('mailer')->render(
$view = $this->buildView(), $this->buildViewData()
);

$text = $view['text'] ?? '';

if (! empty($text) && ! $text instanceof Htmlable) {
$text = Container::getInstance()->make('mailer')->render(
$view['text'], $this->buildViewData()
);
}

return [(string) $html, (string) $text];
});
}

/**
* Set the name of the mailer that should send the message.
*
Expand Down

0 comments on commit afb858a

Please sign in to comment.