|
|
@@ -0,0 +1,130 @@ |
|
|
<?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 |
|
|
*/ |
|
|
|
|
|
} |
|
|
|
|
|
|