diff --git a/README.md b/README.md index b9364e3..e37b2e5 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,13 @@ Then I should see an email with from "sender@domain.example" 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 "sender@domain.example" Then I should see an email with subject "subject" from "sender@domain.example" +Then I should see an email to "recipient@domain.example" +Then I should see an email with subject "subject" to "recipient@domain.example" +Then I should see an email with body "body" to "recipient@domain.example" +Then I should see an email from "sender@domain.example" to "recipient@domain.example" +Then I should see an email with subject "subject" and body "body" to "recipient@domain.example" +Then I should see an email with subject "subject" and body "body" from "sender@domain.example" to "recipient@domain.example" +Then I should see an email with subject "subject" from "sender@domain.example" to "recipient@domain.example" 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" @@ -69,7 +76,14 @@ When I open the latest email to "recipient@domain.example" When I open the latest email with subject "Hello world" When I open the latest email from "sender@domain.example" with subject "Hello world" When I open the latest email to "recipient@domain.example" 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 "sender@domain.example" to "recipient@domain.example" +When I open the latest email from "sender@domain.example" with body "body" +When I open the latest email to "recipient@domain.example" with body "body" +When I open the latest email from "sender@domain.example" with subject "subject" and body "body" +When I open the latest email to "recipient@domain.example" with subject "subject" and body "body" +When I open the latest email from "sender@domain.example" to "recipient@domain.example" 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 ``` diff --git a/features/tests.feature b/features/tests.feature index 90df173..f6d3788 100644 --- a/features/tests.feature +++ b/features/tests.feature @@ -44,6 +44,12 @@ Feature: As the developer of this context I want it to function correctly When I open the latest email to "test@example.org" 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 "test@example.org" + Given I send an email with subject "Goodbye" and body "See you later" to "test@example.org" + 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 "me@myself.example" diff --git a/src/Context/MailhogContext.php b/src/Context/MailhogContext.php index 24c4b73..4b90f1e 100644 --- a/src/Context/MailhogContext.php +++ b/src/Context/MailhogContext.php @@ -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 { /** @@ -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; @@ -62,7 +96,6 @@ public function myInboxIsEmpty(): void * @Then /^I should see an email with subject "(?P[^"]*)" and body "(?P[^"]*)" to "(?P[^"]*)"$/ * @Then /^I should see an email with subject "(?P[^"]*)" and body "(?P[^"]*)" from "(?P[^"]*)" to "(?P[^"]*)"$/ * @Then /^I should see an email with subject "(?P[^"]*)" from "(?P[^"]*)" to "(?P[^"]*)"$/ - * @SuppressWarnings(PHPMD.NPathComplexity) */ public function iShouldSeeAnEmailWithSubjectAndBodyFromToRecipient( ?string $subject = null, @@ -70,23 +103,7 @@ public function iShouldSeeAnEmailWithSubjectAndBodyFromToRecipient( ?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)); @@ -109,25 +126,24 @@ public function iShouldSeeAnEmailWithSubjectAndBodyFromToRecipient( * @When /^I open the latest email from "(?P[^"]*)"$/ * @When /^I open the latest email to "(?P[^"]*)"$/ * @When /^I open the latest email with subject "(?P[^"]*)"$/ + * @When /^I open the latest email with body "(?P[^"]*)"$/ + * @When /^I open the latest email with subject "(?P[^"]*)" and body "(?P[^"]*)"$/ + * @When /^I open the latest email from "(?P[^"]*)" to "(?P[^"]*)"$/ * @When /^I open the latest email from "(?P[^"]*)" with subject "(?P[^"]*)"$/ * @When /^I open the latest email to "(?P[^"]*)" with subject "(?P[^"]*)"$/ - * @SuppressWarnings(PHPMD.NPathComplexity) + * @When /^I open the latest email from "(?P[^"]*)" with body "(?P[^"]*)"$/ + * @When /^I open the latest email to "(?P[^"]*)" with body "(?P[^"]*)"$/ + * @When /^I open the latest email from "(?P[^"]*)" with subject "(?P[^"]*)" and body "(?P[^"]*)"$/ + * @When /^I open the latest email to "(?P[^"]*)" with subject "(?P[^"]*)" and body "(?P[^"]*)"$/ + * @When /^I open the latest email from "(?P[^"]*)" to "(?P[^"]*)" with subject "(?P[^"]*)" and body "(?P[^"]*)"$/ */ - 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)); @@ -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)); } /**