Skip to content

Commit

Permalink
Add basic MailhogContext
Browse files Browse the repository at this point in the history
  • Loading branch information
rpkamp committed Dec 12, 2017
1 parent b427e13 commit 18160f3
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 15 deletions.
3 changes: 3 additions & 0 deletions behat.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ default:
suites:
default:
path: '%paths.base%/features'
contexts:
- FeatureContext
- rpkamp\Behat\MailhogExtension\Context\MailhogContext
extensions:
rpkamp\Behat\MailhogExtension:
base_url: http://localhost:10025
29 changes: 15 additions & 14 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

use Behat\Behat\Context\Context;
use PHPUnit\Framework\TestCase;
use rpkamp\Behat\MailhogExtension\Context\MailhogAwareContext;
use rpkamp\Mailhog\MailhogClient;

Expand All @@ -25,7 +24,7 @@ public function setMailhog(MailhogClient $client)
public function iSendAnEmailWithSubjectAndBody(string $subject, string $body)
{
$message = (new Swift_Message())
->setFrom('[email protected]')
->setFrom('[email protected]', 'Myself')
->setTo('[email protected]')
->setBody($body)
->setSubject($subject);
Expand All @@ -36,21 +35,23 @@ public function iSendAnEmailWithSubjectAndBody(string $subject, string $body)
}

/**
* @Then /^I should receive an email with subject "([^"]*)" and body "([^"]*)"$/
* @Given /^I send an email with attachment "([^"]*)"$/
*/
public function iShouldReceiveAnEmailWithSubjectAndBody(string $subject, string $body)
public function iSendAnEmailWithAttachment(string $filename)
{
$message = $this->mailHog->getLastMessage();
$message = (new Swift_Message())
->setFrom('[email protected]', 'Myself')
->setTo('[email protected]')
->setBody('Please see attached')
->setSubject('Email with attachment')
->attach(new Swift_Attachment(
'Hello world!',
$filename,
'text/plain'
));

TestCase::assertEquals($subject, $message->subject);
TestCase::assertEquals($body, $message->body);
}
$mailer = new Swift_Mailer(new Swift_SmtpTransport('localhost', 3025));

/**
* @Then /^there should be (\d+) email in my inbox$/
*/
public function thereShouldBeEmailInMyInbox(int $numEmails)
{
TestCase::assertEquals($numEmails, $this->mailHog->getNumberOfMessages());
$mailer->send($message);
}
}
17 changes: 16 additions & 1 deletion features/tests.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,24 @@ Feature: As the developer of this context I want it to function correctly

Scenario: As a user I want to receive an email
Given I send an email with subject "Hello" and body "How are you?"
Then I should receive an email with subject "Hello" and body "How are you?"
Then I should see an email with subject "Hello"
And I should see an email with body "How are you?"
And I should see an email from "[email protected]"
And I should see an email from "Myself"
And I should see an email with subject "Hello" and body "How are you?"
And I should see an email with subject "Hello" and body "How are you?" from "[email protected]"
And I should see an email with subject "Hello" from "[email protected]"
And I should see "How" in email

@email
Scenario: As a developer I want the extension to purge the inbox for email tag
Given I send an email with subject "Hello" and body "How are you?"
Then there should be 1 email in my inbox

Scenario: As a developer I want the extension to purge the inbox when I say so
Given my inbox is empty
Then there should be 0 emails in my inbox

Scenario: As a developer I want the extension to see attachments in emails
Given I send an email with attachment "hello.txt"
Then I should receive an email with attachment "hello.txt"
104 changes: 104 additions & 0 deletions src/Context/MailhogContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
declare(strict_types=1);

namespace rpkamp\Behat\MailhogExtension\Context;

use Behat\Behat\Tester\Exception\PendingException;
use Exception;
use rpkamp\Mailhog\MailhogClient;

final class MailhogContext implements MailhogAwareContext
{
/**
* @var MailhogClient
*/
private $mailhogClient;

public function setMailhog(MailhogClient $client)
{
$this->mailhogClient = $client;
}

/**
* @Given /^my inbox is empty$/
*/
public function myInboxIsEmpty()
{
$this->mailhogClient->purgeMessages();
}

/**
* @Then /^I should see an email with subject "(?P<subject>[^"]*)"$/
* @Then /^I should see an email with body "(?P<body>[^"]*)"$/
* @Then /^I should see an email from "(?P<from>[^"]*)"$/
* @Then /^I should see an email with subject "(?P<subject>[^"]*)" and body "(?P<body>[^"]*)"$/
* @Then /^I should see an email with subject "(?P<subject>[^"]*)" and body "(?P<body>[^"]*)" from "(?P<from>[^"]*)"$/
* @Then /^I should see an email with subject "(?P<subject>[^"]*)" from "(?P<from>[^"]*)"$/
*/
public function iShouldReceiveAnEmailWithSubjectAndBodyFrom(string $subject = null, string $body = null, string $from = null)
{
$message = $this->mailhogClient->getLastMessage();

if (!empty($subject) && $subject !== $message->subject) {
throw new Exception(sprintf('Expected subject "%s", found "%s"', $subject, $message->subject));
}

if (!empty($body) && $body !== $message->body) {
throw new Exception('Unexpected body value');
}

if (!empty($from) && $from !== $message->sender->emailAddress && $from !== $message->sender->name) {
throw new Exception(sprintf('Could not find expected message from "%s"', $from));
}
}

/**
* @Given /^I should see "([^"]*)" in email$/
*/
public function iShouldSeeInEmail(string $text)
{
$message = $this->mailhogClient->getLastMessage();

if (false === strpos($message->body, $text)) {
throw new Exception(sprintf('Could not find "%s" in email', $text));
}
}

/**
* @Then /^there should be (\d+) email(?:s)? in my inbox$/
*/
public function thereShouldBeEmailInMyInbox(int $numEmails)
{
$numMailhogMessages = $this->mailhogClient->getNumberOfMessages();

if ($numMailhogMessages !== $numEmails) {
throw new Exception(
sprintf(
'Expected %d messages in inbox, but there were %d',
$numEmails,
$numMailhogMessages
)
);
}
}

/**
* @Then /^I should receive an email with attachment "([^"]*)"$/
*/
public function iShouldReceiveAnEmailWithAttachment(string $filename)
{
$message = $this->mailhogClient->getLastMessage();

$found = false;
foreach ($message->attachments as $attachment) {
if ($filename === $attachment->filename) {
$found = true;
break;
}
}

if (!$found) {
throw new Exception(sprintf('Messages does not contain a message with attachment "%s', $filename));
}
}
}

0 comments on commit 18160f3

Please sign in to comment.