Skip to content

Commit

Permalink
Merge pull request #37 from 4c0n/feature/allowing-constraint-on-body-…
Browse files Browse the repository at this point in the history
…when-opening-email

Allowing specification on body when opening email
  • Loading branch information
rpkamp authored Jan 31, 2020
2 parents 9ac1208 + 755d78b commit 8fe78ba
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 39 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ Then I should see an email with from "[email protected]"
Then I should see an email with subject "subject" and body "body"
Then I should see an email with subject "subject" and body "body" from "[email protected]"
Then I should see an email with subject "subject" from "[email protected]"
Then I should see an email to "[email protected]"
Then I should see an email with subject "subject" to "[email protected]"
Then I should see an email with body "body" to "[email protected]"
Then I should see an email from "[email protected]" to "[email protected]"
Then I should see an email with subject "subject" and body "body" to "[email protected]"
Then I should see an email with subject "subject" and body "body" from "[email protected]" to "[email protected]"
Then I should see an email with subject "subject" from "[email protected]" to "[email protected]"
Then I should see "some text" in email
Then there should be 2 emails in my inbox
Then I should see an email with attachment "lorem-ipsum.pdf"
Expand All @@ -69,7 +76,14 @@ When I open the latest email to "[email protected]"
When I open the latest email with subject "Hello world"
When I open the latest email from "[email protected]" with subject "Hello world"
When I open the latest email to "[email protected]" with subject "Hello world"
When I open the latest email with body "body"
When I open the latest email with subject "subject" and body "body"
When I open the latest email from "[email protected]" to "[email protected]"
When I open the latest email from "[email protected]" with body "body"
When I open the latest email to "[email protected]" with body "body"
When I open the latest email from "[email protected]" with subject "subject" and body "body"
When I open the latest email to "[email protected]" with subject "subject" and body "body"
When I open the latest email from "[email protected]" to "[email protected]" with subject "subject" and body "body"
Then I should see "Hello world" in the opened email
Then I should see an attachment with filename "lorem-ipsum.pdf" in the opened email
```
Expand Down
6 changes: 6 additions & 0 deletions features/tests.feature
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ Feature: As the developer of this context I want it to function correctly
When I open the latest email to "[email protected]"
Then I should see "See you later" in the opened email

Scenario: As as user I want to open an email based on body
Given I send an email with subject "Hello" and body "How are you?" to "[email protected]"
Given I send an email with subject "Goodbye" and body "See you later" to "[email protected]"
When I open the latest email with body "How are you?"
Then I should see "How are you?" in the opened email

Scenario: As a user I want to check for an attachment in an opened email
Given I send an email with attachment "hello.txt"
When I open the latest email from "[email protected]"
Expand Down
90 changes: 52 additions & 38 deletions src/Context/MailhogContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@
use rpkamp\Mailhog\Specification\BodySpecification;
use rpkamp\Mailhog\Specification\RecipientSpecification;
use rpkamp\Mailhog\Specification\SenderSpecification;
use rpkamp\Mailhog\Specification\Specification;
use rpkamp\Mailhog\Specification\SubjectSpecification;
use RuntimeException;
use function array_keys;
use function array_shift;
use function count;
use function sprintf;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
final class MailhogContext implements MailhogAwareContext, OpenedEmailStorageAwareContext
{
/**
Expand All @@ -30,6 +34,36 @@ final class MailhogContext implements MailhogAwareContext, OpenedEmailStorageAwa
*/
private $openedEmailStorage;

/**
* @return Specification[]
*/
private function getSpecifications(
?string $subject = null,
?string $body = null,
?string $from = null,
?string $recipient = null
): array {
$specifications = [];

if (!empty($subject)) {
$specifications[] = new SubjectSpecification($subject);
}

if (!empty($body)) {
$specifications[] = new BodySpecification($body);
}

if (!empty($from)) {
$specifications[] = new SenderSpecification(Contact::fromString($from));
}

if (!empty($recipient)) {
$specifications[] = new RecipientSpecification(Contact::fromString($recipient));
}

return $specifications;
}

public function setMailhog(MailhogClient $client): void
{
$this->mailhogClient = $client;
Expand Down Expand Up @@ -62,31 +96,14 @@ public function myInboxIsEmpty(): void
* @Then /^I should see an email with subject "(?P<subject>[^"]*)" and body "(?P<body>[^"]*)" to "(?P<recipient>[^"]*)"$/
* @Then /^I should see an email with subject "(?P<subject>[^"]*)" and body "(?P<body>[^"]*)" from "(?P<from>[^"]*)" to "(?P<recipient>[^"]*)"$/
* @Then /^I should see an email with subject "(?P<subject>[^"]*)" from "(?P<from>[^"]*)" to "(?P<recipient>[^"]*)"$/
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function iShouldSeeAnEmailWithSubjectAndBodyFromToRecipient(
?string $subject = null,
?string $body = null,
?string $from = null,
?string $recipient = null
): void {
$specifications = [];

if (!empty($subject)) {
$specifications[] = new SubjectSpecification($subject);
}

if (!empty($body)) {
$specifications[] = new BodySpecification($body);
}

if (!empty($from)) {
$specifications[] = new SenderSpecification(Contact::fromString($from));
}

if (!empty($recipient)) {
$specifications[] = new RecipientSpecification(Contact::fromString($recipient));
}
$specifications = $this->getSpecifications($subject, $body, $from, $recipient);

$messages = $this->mailhogClient->findMessagesSatisfying(AndSpecification::all(...$specifications));

Expand All @@ -109,25 +126,24 @@ public function iShouldSeeAnEmailWithSubjectAndBodyFromToRecipient(
* @When /^I open the latest email from "(?P<from>[^"]*)"$/
* @When /^I open the latest email to "(?P<recipient>[^"]*)"$/
* @When /^I open the latest email with subject "(?P<subject>[^"]*)"$/
* @When /^I open the latest email with body "(?P<body>[^"]*)"$/
* @When /^I open the latest email with subject "(?P<subject>[^"]*)" and body "(?P<body>[^"]*)"$/
* @When /^I open the latest email from "(?P<from>[^"]*)" to "(?P<recipient>[^"]*)"$/
* @When /^I open the latest email from "(?P<from>[^"]*)" with subject "(?P<subject>[^"]*)"$/
* @When /^I open the latest email to "(?P<recipient>[^"]*)" with subject "(?P<subject>[^"]*)"$/
* @SuppressWarnings(PHPMD.NPathComplexity)
* @When /^I open the latest email from "(?P<from>[^"]*)" with body "(?P<body>[^"]*)"$/
* @When /^I open the latest email to "(?P<recipient>[^"]*)" with body "(?P<body>[^"]*)"$/
* @When /^I open the latest email from "(?P<from>[^"]*)" with subject "(?P<subject>[^"]*)" and body "(?P<body>[^"]*)"$/
* @When /^I open the latest email to "(?P<recipient>[^"]*)" with subject "(?P<subject>[^"]*)" and body "(?P<body>[^"]*)"$/
* @When /^I open the latest email from "(?P<from>[^"]*)" to "(?P<recipient>[^"]*)" with subject "(?P<subject>[^"]*)" and body "(?P<body>[^"]*)"$/
*/
public function iOpenTheEmail(?string $from = null, ?string $recipient = null, ?string $subject = null): void
{
$specifications = [];

if (!empty($from)) {
$specifications[] = new SenderSpecification(Contact::fromString($from));
}

if (!empty($recipient)) {
$specifications[] = new RecipientSpecification(Contact::fromString($recipient));
}

if (!empty($subject)) {
$specifications[] = new SubjectSpecification($subject);
}
public function iOpenTheEmail(
?string $from = null,
?string $recipient = null,
?string $subject = null,
?string $body = null
): void {
$specifications = $this->getSpecifications($subject, $body, $from, $recipient);

$messages = $this->mailhogClient->findMessagesSatisfying(AndSpecification::all(...$specifications));

Expand All @@ -142,9 +158,7 @@ public function iOpenTheEmail(?string $from = null, ?string $recipient = null, ?
);
}

$messageKeys = array_keys($messages);

$this->openedEmailStorage->setOpenedEmail($messages[$messageKeys[0]]);
$this->openedEmailStorage->setOpenedEmail(array_shift($messages));
}

/**
Expand Down

0 comments on commit 8fe78ba

Please sign in to comment.