-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
138 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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); | ||
|
@@ -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); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
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" |
This file contains 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
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)); | ||
} | ||
} | ||
} |