From 6fc50cae5c2794e576392eec8c0a3b34a80d9af3 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Thu, 12 Jan 2017 09:55:10 +1300 Subject: [PATCH] FIX: Refactor TestMailer to better be base class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This small refactoring makes TestMailer better suited as a base class for the behat-extension’s implementation, which means that we don’t need to coordinate cross-module commits in dhensby’ SwiftMailer work. See https://github.com/silverstripe/silverstripe-framework/pull/6466 --- src/Dev/TestMailer.php | 64 ++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/src/Dev/TestMailer.php b/src/Dev/TestMailer.php index 2d7565797..2b447795d 100644 --- a/src/Dev/TestMailer.php +++ b/src/Dev/TestMailer.php @@ -22,18 +22,18 @@ class TestMailer extends Mailer */ public function sendPlain($to, $from, $subject, $plainContent, $attachedFiles = false, $customHeaders = false) { - $this->emailsSent[] = array( - 'type' => 'plain', - 'to' => $to, - 'from' => $from, - 'subject' => $subject, + $this->saveEmail([ + 'Type' => 'plain', + 'To' => $to, + 'From' => $from, + 'Subject' => $subject, - 'content' => $plainContent, - 'plainContent' => $plainContent, + 'Content' => $plainContent, + 'PlainContent' => $plainContent, - 'attachedFiles' => $attachedFiles, - 'customHeaders' => $customHeaders, - ); + 'AttachedFiles' => $attachedFiles, + 'CustomHeaders' => $customHeaders, + ]); return true; } @@ -63,24 +63,33 @@ class TestMailer extends Mailer $inlineImages = false ) { - $this->emailsSent[] = array( - 'type' => 'html', - 'to' => $to, - 'from' => $from, - 'subject' => $subject, + $this->saveEmail([ + 'Type' => 'html', + 'To' => $to, + 'From' => $from, + 'Subject' => $subject, - 'content' => $htmlContent, - 'plainContent' => $plainContent, - 'htmlContent' => $htmlContent, + 'Content' => $htmlContent, + 'PlainContent' => $plainContent, + 'HtmlContent' => $htmlContent, - 'attachedFiles' => $attachedFiles, - 'customHeaders' => $customHeaders, - 'inlineImages' => $inlineImages, - ); + 'AttachedFiles' => $attachedFiles, + 'CustomHeaders' => $customHeaders, + 'InlineImages' => $inlineImages, + ]); return true; } + /** + * Save a single email to the log + * @param $data A map of information about the email + */ + protected function saveEmail($data) + { + $this->emailsSent[] = $data; + } + /** * Clear the log of emails sent */ @@ -102,11 +111,18 @@ class TestMailer extends Mailer */ public function findEmail($to, $from = null, $subject = null, $content = null) { + $compare = [ + 'To' => $to, + 'From' => $from, + 'Subject' => $subject, + 'Content' => $content, + ]; + foreach ($this->emailsSent as $email) { $matched = true; - foreach (array('to','from','subject','content') as $field) { - if ($value = $$field) { + foreach (array('To','From','Subject','Content') as $field) { + if ($value = $compare[$field]) { if ($value[0] == '/') { $matched = preg_match($value, $email[$field]); } else {