Skip to content

Instantly share code, notes, and snippets.

@danilopinotti
Created August 18, 2021 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danilopinotti/7f787a5cfe8bb222dbc9e7693b3c359b to your computer and use it in GitHub Desktop.
Save danilopinotti/7f787a5cfe8bb222dbc9e7693b3c359b to your computer and use it in GitHub Desktop.
Test sent emails in Laravel 8. Applied to test emails sent by Mail or Notification facades.
<?php
namespace Tests;
use App\User;
use Tests\TestCase;
use Illuminate\Notifications\Notification;
class MyNotification extends Notification
{
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new MailMessage())
->subject('Test mail')
->line('Test mail');
}
}
class SentMailsTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
$this->user = (new User())
->fill(['email' => 'test@example.com']);
$this->sentMails = app('mailer')
->getSwiftMailer()
->getTransport()
->messages();
}
public function test_should_notification_sends_email()
{
$this->user->notify(new MyNotification());
$this->assertCount(1, $this->sentMails, 'Email should be sent');
$mail = $this->sentMails[0];
$this->assertArrayHasKey('test@example.com', $mail->getTo());
$this->assertStringContainsString('Test mail', $mail->getBody());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment