Skip to content

Instantly share code, notes, and snippets.

@JeffreyWay
Last active February 10, 2024 10:56
Show Gist options
  • Star 51 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save JeffreyWay/b501c53d958b07b8a332 to your computer and use it in GitHub Desktop.
Save JeffreyWay/b501c53d958b07b8a332 to your computer and use it in GitHub Desktop.
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{
use MailTracking;
/**
* A basic functional test example.
*
* @return void
*/
public function testBasicExample()
{
$this->visit('/route-that-sends-an-email')
->seeEmailWasSent()
->seeEmailSubject('Hello World')
->seeEmailTo('foo@bar.com')
->seeEmailEquals('Click here to buy this jewelry.')
->seeEmailContains('Click here');
}
}
<?php
trait MailTracking
{
/**
* Delivered emails.
*/
protected $emails = [];
/**
* Register a listener for new emails.
*
* @before
*/
public function setUpMailTracking()
{
Mail::getSwiftMailer()
->registerPlugin(new TestingMailEventListener($this));
}
/**
* Assert that at least one email was sent.
*/
protected function seeEmailWasSent()
{
$this->assertNotEmpty(
$this->emails, 'No emails have been sent.'
);
return $this;
}
/**
* Assert that no emails were sent.
*/
protected function seeEmailWasNotSent()
{
$this->assertEmpty(
$this->emails, 'Did not expect any emails to have been sent.'
);
return $this;
}
/**
* Assert that the given number of emails were sent.
*
* @param integer $count
*/
protected function seeEmailsSent($count)
{
$emailsSent = count($this->emails);
$this->assertCount(
$count, $this->emails,
"Expected $count emails to have been sent, but $emailsSent were."
);
return $this;
}
/**
* Assert that the last email's body equals the given text.
*
* @param string $body
* @param Swift_Message $message
*/
protected function seeEmailEquals($body, Swift_Message $message = null)
{
$this->assertEquals(
$body, $this->getEmail($message)->getBody(),
"No email with the provided body was sent."
);
return $this;
}
/**
* Assert that the last email's body contains the given text.
*
* @param string $excerpt
* @param Swift_Message $message
*/
protected function seeEmailContains($excerpt, Swift_Message $message = null)
{
$this->assertContains(
$excerpt, $this->getEmail($message)->getBody(),
"No email containing the provided body was found."
);
return $this;
}
/**
* Assert that the last email's subject matches the given string.
*
* @param string $subject
* @param Swift_Message $message
*/
protected function seeEmailSubject($subject, Swift_Message $message = null)
{
$this->assertEquals(
$subject, $this->getEmail($message)->getSubject(),
"No email with a subject of $subject was found."
);
return $this;
}
/**
* Assert that the last email was sent to the given recipient.
*
* @param string $recipient
* @param Swift_Message $message
*/
protected function seeEmailTo($recipient, Swift_Message $message = null)
{
$this->assertArrayHasKey(
$recipient, (array) $this->getEmail($message)->getTo(),
"No email was sent to $recipient."
);
return $this;
}
/**
* Assert that the last email was delivered by the given address.
*
* @param string $sender
* @param Swift_Message $message
*/
protected function seeEmailFrom($sender, Swift_Message $message = null)
{
$this->assertArrayHasKey(
$sender, (array) $this->getEmail($message)->getFrom(),
"No email was sent from $sender."
);
return $this;
}
/**
* Store a new swift message.
*
* @param Swift_Message $email
*/
public function addEmail(Swift_Message $email)
{
$this->emails[] = $email;
}
/**
* Retrieve the appropriate swift message.
*
* @param Swift_Message $message
*/
protected function getEmail(Swift_Message $message = null)
{
$this->seeEmailWasSent();
return $message ?: $this->lastEmail();
}
/**
* Retrieve the mostly recently sent swift message.
*/
protected function lastEmail()
{
return end($this->emails);
}
}
class TestingMailEventListener implements Swift_Events_EventListener
{
protected $test;
public function __construct($test)
{
$this->test = $test;
}
public function beforeSendPerformed($event)
{
$this->test->addEmail($event->getMessage());
}
}
@jstierney
Copy link

jstierney commented Aug 6, 2022

I made an attempt to adapt it to work with Laravel 9, which works for all my existing tests.

Since LogTransport no longer supports registerPlugin(), this needs ArrayTransport to work instead, so either set the default mailer to array in your .env file, i.e. MAIL_MAILER=array, or set it in runtime in your test (or setUp() method) before running the test: config()->set('mail.default', 'array');

Hope it helps.

<?php

namespace Tests\TestHelpers;

trait MailTracking
{
    /**
     * Delivered emails.
     */
    protected $emails = [];

    /**
     * Read messages from mailer and assign to emails array.
     */
    public function getMessages($overwrite = false)
    {
        if (! $overwrite && ! empty($this->emails)) {
            return $this;
        }
        $transport = app()->make('mailer')->getSymfonyTransport();
        if (method_exists($transport, 'messages')) {
            $this->emails = $transport->messages();
        }
        return $this;
    }

    /**
     * Assert that at least one email was sent.
     */
    protected function seeEmailWasSent()
    {
        $this->getMessages()->assertNotEmpty(
            $this->emails, 'No emails have been sent.'
        );
        return $this;
    }

    /**
     * Assert that no emails were sent.
     */
    protected function seeEmailWasNotSent()
    {
        $this->getMessages()->assertEmpty(
            $this->emails, 'Did not expect any emails to have been sent.'
        );
        return $this;
    }

    /**
     * Assert that the given number of emails were sent.
     */
    protected function seeEmailsSent($count)
    {
        $emailsSent = count($this->emails);
        $this->getMessages()->assertCount(
            $count, $this->emails,
            "Expected $count emails to have been sent, but $emailsSent were."
        );
        return $this;
    }

    /**
     * Assert that the last email's body equals the given text.
     */
    protected function seeEmailEquals($body)
    {
        $this->getMessages()->assertEquals(
            $body,
            $this->getEmailBody(),
            'No email with the provided body was sent.'
        );
        return $this;
    }

    /**
     * Assert that the last email's body contains the given text.
     */
    protected function seeEmailContains($excerpt)
    {
        $this->getMessages()->assertStringContainsString(
            $excerpt,
            $this->getEmailBody(),
            'No email containing the provided body was found.'
        );
        return $this;
    }

    /**
     * Assert that the last email's subject matches the given string.
     */
    protected function seeEmailSubject($subject)
    {
        $this->getMessages()->assertEquals(
            $subject, $this->lastEmail()->getSubject(),
            "No email with a subject of $subject was found."
        );
        return $this;
    }

    /**
     * Assert that the last email was sent to the given recipient.
     */
    protected function seeEmailTo($recipient)
    {
        $this->getMessages();
        $to = collect($this->lastEmail()->getTo())->map(fn ($item) => $item->getAddress())->toArray();
        $this->assertContains(
            $recipient,
            $to,
            "No email was sent to $recipient."
        );
        return $this;
    }

    /**
     * Assert that the last email was delivered by the given address.
     */
    protected function seeEmailFrom($sender)
    {
        $this->getMessages();
        $from = collect($this->lastEmail()->getFrom())->map(fn ($item) => $item->getAddress())->toArray();
        $this->assertContains(
            $sender,
            $from,
            "No email was sent from $sender."
        );
        return $this;
    }

    /**
     * Retrieve the mostly recently sent message.
     */
    protected function lastEmail()
    {
        $email = $this->emails->last();
        if ($email) {
            return $email->getOriginalMessage();
        }
    }

    /**
     * Retrieve the body of the most recent message.
     */
    protected function getEmailBody()
    {
        $email = $this->lastEmail();
        if ($email) {
            return $email->getTextBody() ?? $email->getHtmlBody();
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment