jtproductions / mailtrap-assertions
Assertions for PHPUnit to use with Mailtrap mailbox
Requires
- php: ^7.3
- ext-json: *
- guzzlehttp/guzzle: ^7.2
README
The mailtrap assertiosn is to TDD your Laravel Application to check if mails has been send to mailtrap.
Installation
You can install the package using
composer require --dev jtproductions/mailtrap-assertions
.env file
You need to add a few variables into your .env
file. You need to firstly login to your mailtrap.io account.
MAILTRAP_API_KEY
=>
In the left navigation bar you click on API. There you see your API Token which you copy and paste in your .env file
MAILTRAP_INBOX_ID
=> In the left navigation bar you click on inboxes and then click on the name of your inbox. Now you
see in the address bar something like https://mailtrap.io/inboxes/123456789/messages. The number between inboxes and
messages is your inbox ID. In this case it is 123456789
.
tests/TestCase.php
In your tests directory you open up TestCase.php and add make sure you have the following code
protected function setUpTraits() { $uses = parent::setUpTraits(); // TODO: Change the autogenerated stub if (isset($uses[RefreshMailtrap::class])) { $this->refreshMailtrap(); } if (isset($uses[MailtrapAssertions::class])) { $this->mailtrapSetup(); } }
phpunit.xml
Open up your phpunit.xml
file. Change MAIL_MAILER
from array
to smtp
Usage
Adding traits
The trait RefreshMailtrap will empty you mailbox on each
class ExampleTest extends TestCase { use RefreshMailtrap; }
All the assertions are in the MailtrapAssertions Trait
class ExampleTest extends TestCase { use MailtrapAssertions; }
Assert empty mailbox
To check if the mailbox is empty
$this->assertMailboxEmpty();
Assert mailbox has $count messages
This asserts that there are 3 messages in the mailbox
$this->assertMailboxCount(3);
Manual clean mailbox
If you don't want to use the RefreshMailtrap trait or during a test clean your mailbox
$this->emptyMailbox();