mirror of
https://github.com/silverstripe/silverstripe-behat-extension
synced 2024-10-22 17:05:32 +02:00
More flexible EmailContext
This commit is contained in:
parent
d79acc457a
commit
58a9d59e7f
@ -26,6 +26,11 @@ class EmailContext extends BehatContext
|
|||||||
|
|
||||||
protected $mailer;
|
protected $mailer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stored to simplify later assertions
|
||||||
|
*/
|
||||||
|
protected $lastMatchedEmail;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes context.
|
* Initializes context.
|
||||||
* Every scenario gets it's own context object.
|
* Every scenario gets it's own context object.
|
||||||
@ -67,6 +72,7 @@ class EmailContext extends BehatContext
|
|||||||
$from = ($direction == 'from') ? $email : null;
|
$from = ($direction == 'from') ? $email : null;
|
||||||
$match = $this->mailer->findEmail($to, $from);
|
$match = $this->mailer->findEmail($to, $from);
|
||||||
assertNotNull($match);
|
assertNotNull($match);
|
||||||
|
$this->lastMatchedEmail = $match;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -78,6 +84,28 @@ class EmailContext extends BehatContext
|
|||||||
$from = ($direction == 'from') ? $email : null;
|
$from = ($direction == 'from') ? $email : null;
|
||||||
$match = $this->mailer->findEmail($to, $from, $subject);
|
$match = $this->mailer->findEmail($to, $from, $subject);
|
||||||
assertNotNull($match);
|
assertNotNull($match);
|
||||||
|
$this->lastMatchedEmail = $match;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Example: Given the email contains "Thank you for registering!".
|
||||||
|
* Assumes an email has been identified by a previous step,
|
||||||
|
* e.g. through 'Given there should be an email to "test@test.com"'.
|
||||||
|
*
|
||||||
|
* @Given /^the email should contain "([^"]*)"$/
|
||||||
|
*/
|
||||||
|
public function thereTheEmailContains($content)
|
||||||
|
{
|
||||||
|
if(!$this->lastMatchedEmail) {
|
||||||
|
throw new \LogicException('No matched email found from previous step');
|
||||||
|
}
|
||||||
|
|
||||||
|
$email = $this->lastMatchedEmail;
|
||||||
|
if($email['Content']) {
|
||||||
|
assertContains($content, $email['Content']);
|
||||||
|
} else {
|
||||||
|
assertContains($content, $email['PlainContent']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -99,11 +127,34 @@ class EmailContext extends BehatContext
|
|||||||
return new Step\When(sprintf('I go to "%s"', $link));
|
return new Step\When(sprintf('I go to "%s"', $link));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assumes an email has been identified by a previous step,
|
||||||
|
* e.g. through 'Given there should be an email to "test@test.com"'.
|
||||||
|
*
|
||||||
|
* @When /^I click on the "([^"]*)" link in the email"$/
|
||||||
|
*/
|
||||||
|
public function iGoToInTheEmail($linkSelector)
|
||||||
|
{
|
||||||
|
if(!$this->lastMatchedEmail) {
|
||||||
|
throw new \LogicException('No matched email found from previous step');
|
||||||
|
}
|
||||||
|
|
||||||
|
$match = $this->lastMatchedEmail;
|
||||||
|
$crawler = new Crawler($match['Content']);
|
||||||
|
$linkEl = $crawler->selectLink($linkSelector);
|
||||||
|
assertNotNull($linkEl);
|
||||||
|
$link = $linkEl->attr('href');
|
||||||
|
assertNotNull($link);
|
||||||
|
|
||||||
|
return new Step\When(sprintf('I go to "%s"', $link));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Given /^I clear all emails$/
|
* @Given /^I clear all emails$/
|
||||||
*/
|
*/
|
||||||
public function iClearAllEmails()
|
public function iClearAllEmails()
|
||||||
{
|
{
|
||||||
|
$this->lastMatchedEmail = null;
|
||||||
return $this->mailer->clearEmails();
|
return $this->mailer->clearEmails();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user