Skip to content

Instantly share code, notes, and snippets.

@anis-marrouchi
Forked from GesJeremie/EmailExampleTest.php
Created November 20, 2017 13:08
Show Gist options
  • Save anis-marrouchi/5252a6c8d10a054e19832876037319dc to your computer and use it in GitHub Desktop.
Save anis-marrouchi/5252a6c8d10a054e19832876037319dc to your computer and use it in GitHub Desktop.

Revisions

  1. Ges Jérémie created this gist Nov 23, 2015.
    130 changes: 130 additions & 0 deletions EmailExampleTest.php
    Original file line number Diff line number Diff line change
    @@ -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
    */

    }