Skip to content

Commit

Permalink
Improved mail step syntaxes
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanjfshaw committed Feb 12, 2018
1 parent b35b0a0 commit dc386dc
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 52 deletions.
43 changes: 34 additions & 9 deletions features/mail.feature
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,26 @@ Feature: MailContext
| jane | test | body 2 |
When Drupal sends a mail:
| to | jack@example.com |
| subject | test |
| subject | for jack |
| body | test body with many words |
Then new email is sent:
| to | subject | body | body |
| jack | test | test | many words |
| to | body | body |
| jack | test | many words |
And a mail has been sent to "jane@example.com"
And a mail has been sent to "jane@example.com":
| subject |
| test |
And an email has been sent with the subject "test"
And emails have been sent with the subject "test":
| to |
| fred |
| jane |
And a mail has been sent to "fred" with the subject "test"
And emails have been sent to "fred" with the subject "test":
| body |
| test body |

Scenario: Mail is sent to
Scenario: New mail is sent to someone
When Drupal sends a mail:
| to | fred@example.com |
| subject | test 1 |
Expand All @@ -35,22 +48,34 @@ Feature: MailContext
Then new mail is sent to fred:
| subject |
| test 1 |
And mail has been sent to "jane@example.com":
| subject |
| test 2 |


Scenario: No mail is sent
Then no mail has been sent

Scenario: Count sent mail
When Drupal sends an email:
| to | fred@example.com |
| subject | test |
And Drupal sends a mail:
| to | jane@example.com |
| subject | test |
And Drupal sends a mail:
| to | jane@example.com |
| subject | something else |
Then 2 new emails are sent with the subject "test"
And 1 mail has been sent to "jane" with the subject "something else"
And no new emails are sent
And no mail has been sent to "hans"

Scenario: I follow link in mail
When Drupal sends a mail:
| to | fred@example.com |
| subject | test link |
| body | A link to Google: http://www.Google.com |
And I follow the link to "google" from the mail with the subject "test link"
Then I should see "Search"



Scenario: We try to be order insensitive
When Drupal sends an email:
| to | fred@example.com |
Expand Down
69 changes: 35 additions & 34 deletions src/Drupal/DrupalExtension/Context/MailContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,63 +80,61 @@ public function DrupalSendsMail(TableNode $fields) {
/**
* Check all mail sent during the scenario.
*
* @Then (e)mail(s) has/have been sent:
* @Then (e)mail(s) has/have been sent to :to:
* @Then (a )(an )(e)mail(s) has/have been sent:
* @Then (a )(an )(e)mail(s) has/have been sent to :to:
* @Then (a )(an )(e)mail(s) has/have been sent with the subject :subject:
* @Then (a )(an )(e)mail(s) has/have been sent to :to with the subject :subject:
*/
public function mailHasBeenSent(TableNode $expectedMailTable, $to = NULL) {
public function mailHasBeenSent(TableNode $expectedMailTable, $to = '', $subject = '') {
$expectedMail = $expectedMailTable->getHash();
$matches = [];
if (!is_null($to)) {
$matches = ['to' => $to];
}
$actualMail = $this->getMail($matches);
$actualMail = $this->getMail(['to' => $to, 'subject' => $subject], FALSE);
$this->compareMail($actualMail, $expectedMail);
}

/**
* Check mail sent since the last step that checked mail.
*
* @Then new (e)mail(s) is/are sent:
* @Then new (e)mail(s) is/are sent to :to:
* @Then (a )(an )new (e)mail(s) is/are sent:
* @Then (a )(an )new (e)mail(s) is/are sent to :to:
* @Then (a )(an )new (e)mail(s) is/are sent with the subject :subject:
* @Then (a )(an )new (e)mail(s) is/are sent to :to with the subject :subject:
*/
public function newMailIsSent(TableNode $expectedMailTable, $to = NULL) {
public function newMailIsSent(TableNode $expectedMailTable, $to = '', $subject = '') {
$expectedMail = $expectedMailTable->getHash();
$matches = [];
if (!is_null($to)) {
$matches = ['to' => $to];
}
$actualMail = $this->getMail($matches, TRUE);
$actualMail = $this->getMail(['to' => $to, 'subject' => $subject], TRUE);
$this->compareMail($actualMail, $expectedMail);
}

/**
* Check all mail sent during the scenario.
*
* @Then no (e)mail(s) has/have been sent
* @Then no (e)mail(s) has/have been sent to :to
* @Then :count (e)mail(s) has/have been sent
* @Then :count (e)mail(s) has/have been sent to :to
* @Then :count (e)mail(s) has/have been sent with the subject :subject
* @Then :count (e)mail(s) has/have been sent to :to with the subject :subject
*/
public function noMailHasBeenSent($to = NULL) {
$matches = [];
if (!is_null($to)) {
$matches = ['to' => $to];
}
$actualMail = $this->getMail($matches);
$this->compareMail($actualMail, []);
public function noMailHasBeenSent($count, $to = '', $subject = '') {
$actualMail = $this->getMail(['to' => $to, 'subject' => $subject], FALSE);
$count = $count === 'no' ? 0 : $count;
$count = $count === 'a' ? NULL : $count;
$count = $count === 'an' ? NULL : $count;
$this->assertMailCount($actualMail, $count);
}

/**
* Check mail sent since the last step that checked mail.
*
* @Then no new (e)mail(s) is/are sent
* @Then no new (e)mail(s) is/are sent to :to
* @Then :count new (e)mail(s) is/are sent
* @Then :count new (e)mail(s) is/are sent to :to
* @Then :count new (e)mail(s) is/are sent with the subject :subject
* @Then :count new (e)mail(s) is/are sent to :to with the subject :subject
*/
public function noNewMailIsSent($to = NULL) {
$matches = [];
if (!is_null($to)) {
$matches = ['to' => $to];
}
$actualMail = $this->getMail($matches, TRUE);
$this->compareMail($actualMail, []);
public function noNewMailIsSent($count, $to = '', $subject = '') {
$actualMail = $this->getMail(['to' => $to, 'subject' => $subject], TRUE);
$count = $count === 'no' ? 0 : $count;
$count = $count === 'a' ? 1 : $count;
$count = $count === 'an' ? 1 : $count;
$this->assertMailCount($actualMail, $count);
}

/**
Expand All @@ -149,6 +147,9 @@ public function followLinkInMail($urlFragment, $to = '', $subject = '') {
// Get the mail
$matches = ['to' => $to, 'subject' => $subject];
$mail = $this->getMail($matches, FALSE, -1);
if (count($mail) == 0) {
throw new \Exception('No such mail found.');
}
$body = $mail['body'];

// Find web URLs in the mail
Expand Down
43 changes: 34 additions & 9 deletions src/Drupal/DrupalExtension/Context/RawMailContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,9 @@ protected function matchesMail($mail = [], $matches = []) {
* An array of expected mail.
*/
protected function compareMail($actualMail, $expectedMail) {
// Make sure there is the same number of actual and expected
$actualCount = count($actualMail);
// Make sure there is the same number of actual and expected.
$expectedCount = count($expectedMail);
if ($expectedCount !== $actualCount) {
$prettyActualMail = [];
foreach ($actualMail as $singleActualMail) {
$prettyActualMail[] = ['to' => $singleActualMail['to'],'subject' => $singleActualMail['subject'],];
}
throw new \Exception(sprintf("%s mail expected, but %s found:\n\n%s", $expectedCount, $actualCount, print_r($prettyActualMail, TRUE)));
}
$this->assertMailCount($actualMail, $expectedCount);

// For each row of expected mail, check the corresponding actual mail.
// Make the comparison insensitive to the order mails were sent.
Expand All @@ -155,6 +148,38 @@ protected function compareMail($actualMail, $expectedMail) {
}
}

/**
* Assert there is the expected number of mails, or that there are some mails
* if the exact number expected is not specified.
*
* @param array $actualMail
* An array of actual mail.
* @param int $expectedCount
* Optional. The number of mails expected.
*/
protected function assertMailCount($actualMail, $expectedCount = NULL) {
$actualCount = count($actualMail);
if (is_null($expectedCount)) {
// If number to expect is not specified, expect more than zero.
if ($actualCount === 0) {
throw new \Exception("Expected some mail, but none found.");
}
}
else {
if ($expectedCount != $actualCount) {
// Prepare a simple list of actual mail.
$prettyActualMail = [];
foreach ($actualMail as $singleActualMail) {
$prettyActualMail[] = [
'to' => $singleActualMail['to'],
'subject' => $singleActualMail['subject'],
];
}
throw new \Exception(sprintf("Expected %s mail, but %s found:\n\n%s", $expectedCount, $actualCount, print_r($prettyActualMail, TRUE)));
}
}
}

/**
* Sort mail by to, subject and body.
*
Expand Down

0 comments on commit dc386dc

Please sign in to comment.