Skip to content

Instantly share code, notes, and snippets.

@GesJeremie
Created November 23, 2015 02:58
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save GesJeremie/2ff4e1b3a9a75c191a07 to your computer and use it in GitHub Desktop.
Save GesJeremie/2ff4e1b3a9a75c191a07 to your computer and use it in GitHub Desktop.
Email test case for testing emails with Mailtrap.io in Phpunit
<?php
/*
|--------------------------------------------------------------------------
| EmailExampleTest
|--------------------------------------------------------------------------
|
| Email test case for testing emails with Mailtrap.io in Phpunit
|
| Requirements:
| - Laravel 5.1
| - Guzzle
| - Faker
| - Mailtrap
|
| Inspired by that awesome gist: https://gist.github.com/DavertMik/7969053
|
*/
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Faker\Factory as Faker;
use GuzzleHttp\Client;
class EmailExampleTest extends TestCase
{
public function setUp()
{
parent::setUp();
// Create connection mailtrap.io
$this->mailtrap = new Client([
'base_uri' => getenv('MAILTRAP_API_BASE_URI'),
'headers' => [
'Api-Token' => getenv('MAILTRAP_API_TOKEN')
]
]);
$this->mailtrap_inbox = getenv('MAILTRAP_API_INBOX');
// Clean messages of mailtrap between each tests
$this->cleanMessages();
// Faker
$this->faker = Faker::create();
}
public function test_it_send_contact_email()
{
$this->visit('/contact')
->type($this->faker->email, 'email')
->type($this->faker->paragraph(50), 'message')
->press('contact us')
->seePageIs('/contact')
->see('alert --success');
// Check if email sent (here it's where the magic happens)
$this->assertEmailIsSent('Email didn\'t send');
}
/*
|--------------------------------------------------------------------------
| Super simple mailtrap API used in new asserts created
|--------------------------------------------------------------------------
|
*/
/**
* Fetch messages of the mailtrap inbox
* @return json The messages of the inbox given
*/
private function getMessages()
{
$response = $this->mailtrap->request('GET', "inboxes/$this->mailtrap_inbox/messages");
return json_decode((string) $response->getBody());
}
/**
* Fetch the last message received in mailtrap inbox
* @return object Message
*/
private function getLastMessage()
{
$messages = $this->getMessages();
if (empty($messages))
{
$this->fail('Api Mailtrap: No messages found.');
}
return $messages[0];
}
/**
* Clean Messages of the mailtrap inbox
* @return void
*/
private function cleanMessages()
{
$response = $this->mailtrap->request('PATCH', "inboxes/$this->mailtrap_inbox/clean");
}
/*
|--------------------------------------------------------------------------
| Asserts for mails (mailtrap)
|--------------------------------------------------------------------------
|
*/
public function assertEmailIsSent($description = '')
{
$this->assertNotEmpty($this->getMessages(), $description);
}
/**
* Write here your awesome assert .....
*
* you can find inspiration assert here: https://gist.github.com/DavertMik/7969053
*/
}
@mkantautas
Copy link

One does not simply access the env values directly - you have to do this via config(); method .

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