Skip to content

Commit

Permalink
Merge pull request #5912 from NeverResponse/email/check-registration
Browse files Browse the repository at this point in the history
[Behat] Checked whether a customer receives the welcoming email
  • Loading branch information
michalmarcinkowski committed Aug 31, 2016
2 parents 5a50cac + 9a9e3b4 commit 7c7881b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 12 deletions.
6 changes: 6 additions & 0 deletions features/account/registration.feature
Expand Up @@ -47,3 +47,9 @@ Feature: Account registration
Then I should be notified that new account has been successfully created
And I should be logged in
And my email should be "goodman@gmail.com"

@ui @email
Scenario: Receiving a welcoming email after registration
When I register with email "ghastly@bespoke.com" and password "suitsarelife"
Then I should be notified that new account has been successfully created
And a welcoming email should have been sent to "ghastly@bespoke.com"
50 changes: 38 additions & 12 deletions src/Sylius/Behat/Context/Ui/Shop/RegistrationContext.php
Expand Up @@ -374,25 +374,19 @@ public function iShouldBeNotifiedThatTheVerificationEmailHasBeenSent()
}

/**
* @Then it should be sent to :email
* @Then it should be sent to :recipient
*/
public function verificationEmailShouldBeSentTo($email)
public function anEmailShouldBeSentTo($recipient)
{
Assert::true(
$this->emailChecker->hasRecipient($email),
'The verification email should have been sent.'
);
$this->assertEmailHasRecipient($recipient);
}

/**
* @Then :count email(s) should be sent to :email
* @Then :count email(s) should be sent to :recipient
*/
public function numberOfEmailsShouldBeSentTo($count, $email)
public function numberOfEmailsShouldBeSentTo($count, $recipient)
{
Assert::true(
$this->emailChecker->hasRecipient($email),
sprintf('At least 1 email should have been sent to %s.', $email)
);
$this->assertEmailHasRecipient($recipient);

Assert::eq(
$this->emailChecker->getMessagesCount(),
Expand All @@ -405,6 +399,38 @@ public function numberOfEmailsShouldBeSentTo($count, $email)
);
}

/**
* @Then a welcoming email should have been sent to :recipient
*/
public function aWelcomingEmailShouldHaveBeenSentTo($recipient)
{
$this->assertEmailHasRecipient($recipient);
$this->assertEmailContainsMessage('Welcome to our store');

}

/**
* @param string $recipient
*/
private function assertEmailHasRecipient($recipient)
{
Assert::true(
$this->emailChecker->hasRecipient($recipient),
'An email should have been sent to %s.'
);
}

/**
* @param string $message
*/
private function assertEmailContainsMessage($message)
{
Assert::true(
$this->emailChecker->hasMessage($message),
sprintf('Message "%s" was not found in any email.', $message)
);
}

/**
* @param string $element
* @param string $expectedMessage
Expand Down
15 changes: 15 additions & 0 deletions src/Sylius/Component/Core/Test/Services/EmailChecker.php
Expand Up @@ -49,6 +49,21 @@ public function hasRecipient($recipient)
return false;
}

/**
* {@inheritdoc}
*/
public function hasMessage($message)
{
$messages = $this->getMessages($this->spoolDirectory);
foreach ($messages as $sentMessage) {
if (false !== strpos($sentMessage->getBody(), $message)) {
return true;
}
}

return false;
}

/**
* {@inheritdoc}
*/
Expand Down
Expand Up @@ -24,6 +24,13 @@ interface EmailCheckerInterface
*/
public function hasRecipient($recipient);

/**
* @param string $message
*
* @return bool
*/
public function hasMessage($message);

/**
* @return int
*/
Expand Down

0 comments on commit 7c7881b

Please sign in to comment.