Skip to content

Instantly share code, notes, and snippets.

@sebastiaanluca
Created March 28, 2017 11:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebastiaanluca/514c72c95ae85ee188d698449a08c60c to your computer and use it in GitHub Desktop.
Save sebastiaanluca/514c72c95ae85ee188d698449a08c60c to your computer and use it in GitHub Desktop.
<?php
namespace App\Mail;
use Illuminate\Container\Container;
use Illuminate\Mail\Mailable as BaseMailable;
use PHPUnit\Framework\Assert;
class Mailable extends BaseMailable
{
/**
* @return array
*/
public function getRenderedBody() : array
{
Container::getInstance()->call([$this, 'build']);
return $this->buildView();
}
/**
* @param string $string
*
* @return $this
*/
public function assertHtmlBodyContains(string $string)
{
Assert::assertTrue(
str_contains($this->getRenderedBody()['html'], $string),
"The rendered HTML body does not contain \"$string\"."
);
return $this;
}
/**
* @param string $string
*
* @return $this
*/
public function assertTextBodyContains(string $string)
{
Assert::assertTrue(
str_contains($this->getRenderedBody()['text'], $string),
"The rendered text body does not contain \"$string\"."
);
return $this;
}
}
<?php
public function testItCanMail()
{
Mail::assertSent(MyMail::class, function (MyMail $mail) use ($url) {
return $mail->assertHtmlBodyContains($url)
->assertTextBodyContains($url)
->hasTo(config('app.mail_to'));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment