Skip to content

Instantly share code, notes, and snippets.

@chanakasan
Forked from DavertMik/gist:7969053
Created January 14, 2014 03:03
Show Gist options
  • Save chanakasan/8412325 to your computer and use it in GitHub Desktop.
Save chanakasan/8412325 to your computer and use it in GitHub Desktop.
<?php
class EmailTestCase extends PHPUnit_Framework_TestCase {
/**
* @var \Guzzle\Http\Client
*/
private $mailcatcher;
public function setUp()
{
$this->mailcatcher = new \Guzzle\Http\Client('http://127.0.0.1:1080');
$this->cleanMessages();
}
// api calls
public function cleanMessages()
{
$this->mailcatcher->delete('/messages')->send();
}
public function getLastMessage()
{
$messages = $this->getMessages();
if (empty($messages)) {
$this->fail("No messages received");
}
// messages are in descending order
return reset($messages);
}
public function getMessages()
{
$jsonResponse = $this->mailcatcher->get('/messages')->send();
return json_decode($jsonResponse->getBody());
}
// assertions
public function assertEmailIsSent($description = '')
{
$this->assertNotEmpty($this->getMessages(), $description);
}
public function assertEmailSubjectContains($needle, $email, $description = '')
{
$this->assertContains($needle, $email->subject, $description);
}
public function assertEmailSubjectEquals($expected, $email, $description = '')
{
$this->assertContains($expected, $email->subject, $description);
}
public function assertEmailHtmlContains($needle, $email, $description = '')
{
$response = $this->mailcatcher->get("/messages/{$email->id}.html")->send();
$this->assertContains($needle, (string)$response->getBody(), $description);
}
public function assertEmailTextContains($needle, $email, $description = '')
{
$response = $this->mailcatcher->get("/messages/{$email->id}.plain")->send();
$this->assertContains($needle, (string)$response->getBody(), $description);
}
public function assertEmailSenderEquals($expected, $email, $description = '')
{
$response = $this->mailcatcher->get("/messages/{$email->id}.json")->send();
$email = json_decode($response->getBody());
$this->assertEquals($expected, $email->sender, $description);
}
public function assertEmailRecipientsContain($needle, $email, $description = '')
{
$response = $this->mailcatcher->get("/messages/{$email->id}.json")->send();
$email = json_decode($response->getBody());
$this->assertContains($needle, $email->recipients, $description);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment