From a0517d768019509b8d25e328a0177f06abd85bcf Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Thu, 27 Feb 2014 23:02:43 +1300 Subject: [PATCH] Better email assertions --- .../BehatExtension/Context/EmailContext.php | 13 +++++++++- .../BehatExtension/Utility/TestMailer.php | 24 +++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/SilverStripe/BehatExtension/Context/EmailContext.php b/src/SilverStripe/BehatExtension/Context/EmailContext.php index 2cff882..f8c5cb7 100644 --- a/src/SilverStripe/BehatExtension/Context/EmailContext.php +++ b/src/SilverStripe/BehatExtension/Context/EmailContext.php @@ -87,10 +87,21 @@ class EmailContext extends BehatContext $to = ($direction == 'to') ? $email : null; $from = ($direction == 'from') ? $email : null; $match = $this->mailer->findEmail($to, $from, $subject); + $allMails = $this->mailer->findEmails($to, $from); + $allTitles = $allMails ? '"' . implode('","', array_map(function($email) {return $email['Subject'];}, $allMails)) . '"' : null; if(trim($negate)) { assertNull($match); } else { - assertNotNull($match); + $msg = sprintf( + 'Could not find email %s "%s" titled "%s".', + $direction, + $email, + $subject + ); + if($allTitles) { + $msg .= ' Existing emails: ' . $allTitles; + } + assertNotNull($match,$msg); } $this->lastMatchedEmail = $match; } diff --git a/src/SilverStripe/BehatExtension/Utility/TestMailer.php b/src/SilverStripe/BehatExtension/Utility/TestMailer.php index e6d60b1..1e5dfaa 100644 --- a/src/SilverStripe/BehatExtension/Utility/TestMailer.php +++ b/src/SilverStripe/BehatExtension/Utility/TestMailer.php @@ -68,6 +68,7 @@ class TestMailer extends \Mailer { /** * Search for an email that was sent. * All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression. + * * @param $to * @param $from * @param $subject @@ -76,8 +77,25 @@ class TestMailer extends \Mailer { * 'customHeaders', 'htmlContent', 'inlineImages' */ public function findEmail($to = null, $from = null, $subject = null, $content = null) { - $this->initTable(); + $matches = $this->findEmails($to, $from, $subject, $content); + return $matches ? $matches[0] : null; + } + /** + * Search for all emails. + * All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression. + * + * @param $to + * @param $from + * @param $subject + * @param $content + * @return array Contains the keys: 'type', 'to', 'from', 'subject', 'content', 'plainContent', 'attachedFiles', + * 'customHeaders', 'htmlContent', 'inlineImages' + */ + public function findEmails($to = null, $from = null, $subject = null, $content = null) { + $matches = array(); + + $this->initTable(); $args = func_get_args(); $db = $this->getDb(); $emails = $db->query(sprintf('SELECT * FROM "%s"', $this->table)); @@ -92,8 +110,10 @@ class TestMailer extends \Mailer { if(!$matched) break; } } - if($matched) return $email; + if($matched) $matches[] = $email; } + + return $matches; } protected function initTable() {