Mailtrap test helper
Test helper library for testing email submissions through Mailtrap, based on PHPUnit., (*1)
Installation
This package can be installed via Composer:, (*2)
``` bash
composer require dmoen/mailtrap-test-helper --dev, (*3)
## Usage
Create an instance of MailTrapInbox with api key and id of the inbox:
```php
$inbox = new MailTrapInbox('api_key', 'inbox_id');
Example using a PHPUnit test case. Before each test the inbox should be cleaned:, (*4)
private $inbox;
public function setUp()
{
parent::setUp();
$this->inbox = new MailTrapInbox('api_key', 'inbox_id'));
$this->inbox->deleteAllMessages();
}
Simple inbox testing
Test if the inbox has any messages:, (*5)
$inbox->assertHasMails();
Test if the inbox has any messages from a specific address:, (*6)
$inbox->assertHasMailFrom("sender@example.com");
With name:, (*7)
$inbox->assertHasMailFrom("sender@example.com", "Sender Sendersson");
Test if the inbox has any messages to a specific address:, (*8)
$inbox->assertHasMailFor("reciever@example.com");
With name:, (*9)
$inbox->assertHasMailFor("reciever@example.com", "Receiver Receiversson");
Test if inbox has a message with a subject:, (*10)
$inbox->assertHasMailWithSubject('Lorem subject');
Test if inbox has a message with a specific body:, (*11)
$inbox->assertHasMailWithHtmlContent('<b>Lorem ipsum sit amet</b>');
$inbox->assertHasMailWithTextContent('Lorem ipsum sit amet');
A bit more advanced testing for specific emails
To retrieve a specific message in the inbox:, (*12)
$inbox->getLastMessage()
$inbox->getFirstMessage()
Or if you know the specific index in the inbox:, (*13)
$message = $inbox->getMessage(2);
You can also search the inbox for a specific unique message using a condition:, (*14)
$message = $inbox->findUnique(function($message){
return $message->to_email == "receiver@example.com";
});
Or retrieve an array of all messages or by condition:, (*15)
$messages = $inbox->fetchAllMessages();
$messages = $inbox->findMessages(function($message){
return $message->to_email == "receiver@example.com";
});
The message instance passed to the closure has all the properties
retrieved from the Mailtrap API: http://docs.mailtrap.apiary.io/#reference/message/apiv1inboxesinboxidmessagesid/get, (*16)
The message(s) can then be tested with a combination of tests:, (*17)
$message->assertIsFrom("me@railsware.com", "Private Person")
->assertIsFor("test@railsware.com", "A Test User")
->assertHasSubject("SMTP e-mail test")
->assertHasTextContent("This is a test e-mail message")
->assertHasHtmlContent("<b>Lorem ipsum sit amet.</b>");