Skip to content

Instantly share code, notes, and snippets.

@macghriogair
Created May 14, 2019 10:03
Show Gist options
  • Save macghriogair/67568633c662bbe5537f1a520ae48fe2 to your computer and use it in GitHub Desktop.
Save macghriogair/67568633c662bbe5537f1a520ae48fe2 to your computer and use it in GitHub Desktop.
[mailcatcher integration test helper] #mailcatcher #mailhog #testing
<?php
namespace Tests;
use GuzzleHttp\Client as Client;
trait InteractsWithMail
{
/**
* @var \GuzzleHttp\Client
*/
private $mailcatcher;
private static $initialized = false;
private static $serviceStart = "mailcatcher --http-ip 0.0.0.0;";
private static $getProcessId = "ps aux | grep '[m]ailcatcher' | awk '{print $2}'";
public static function ensureServiceIsRunning()
{
if (self::$initialized) {
return;
}
$out = [];
exec(self::$getProcessId, $out);
if (isset($out[0])) {
exec('kill ' . intval($out[0]));
}
exec(self::$serviceStart);
self::$initialized = true;
}
public function initMailcatcher()
{
self::ensureServiceIsRunning();
$this->mailcatcher = new Client(['base_uri' => 'http://127.0.0.1:1080']);
// clean emails between tests
$this->cleanMessages();
return $this;
}
// api calls
public function cleanMessages()
{
$this->mailcatcher->delete('/messages');
}
public function getLastMessage() : \stdClass
{
$messages = $this->getMessages();
if (empty($messages)) {
$this->fail("No messages received");
}
// messages are in descending order
return reset($messages);
}
public function getMessages() : array
{
$jsonResponse = $this->mailcatcher->get('/messages');
return json_decode($jsonResponse->getBody());
}
public function getMessageBody($message) : string
{
$response = $this->mailcatcher->get("/messages/{$message->id}.json");
return json_decode($response->getBody())->source;
}
// 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");
$this->assertContains($needle, (string)$response->getBody(), $description);
}
public function assertEmailTextContains($needle, $email, $description = '')
{
$response = $this->mailcatcher->get("/messages/{$email->id}.plain");
$this->assertContains($needle, (string)$response->getBody(), $description);
}
public function assertEmailSenderEquals($expected, $email, $description = '')
{
$response = $this->mailcatcher->get("/messages/{$email->id}.json");
$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");
$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