Created
June 14, 2014 22:35
-
-
Save laracasts/f90aa8dc4d71207378c4 to your computer and use it in GitHub Desktop.
Use Mailcatcher to test email.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use GuzzleHttp\Message\Response; | |
class MailTestCase extends TestCase { | |
protected $mailcatcher; | |
function __construct() | |
{ | |
$this->mailcatcher = new \GuzzleHttp\Client(['base_url' => 'http://localhost:1080']); | |
} | |
public function deleteAllEmails() | |
{ | |
return $this->mailcatcher->delete('/messages'); | |
} | |
public function getAllEmails() | |
{ | |
$emails = $this->mailcatcher->get('/messages')->json(); | |
if (empty($emails)) | |
{ | |
$this->fail('No messages returned.'); | |
} | |
return $emails; | |
} | |
public function getLastEmail() | |
{ | |
$emailId = $this->getAllEmails()[0]['id']; | |
return $this->mailcatcher->get("/messages/{$emailId}.json"); | |
} | |
public function assertEmailBodyContains($body, Response $email) | |
{ | |
$this->assertContains($body, (string) $email->getBody()); | |
} | |
public function assertNotEmailBodyContains($body, Response $email) | |
{ | |
$this->assertNotContains($body, (string) $email->getBody()); | |
} | |
public function assertEmailWasSentTo($recipient, Response $email) | |
{ | |
$this->assertContains("<{$recipient}>", $email->json()['recipients']); | |
} | |
public function assertNotEmailWasSentTo($recipient, Response $email) | |
{ | |
$this->assertNotContains("<{$recipient}>", $email->json()['recipients']); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment