FIX: Refactor TestMailer to better be base class

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
This commit is contained in:
Sam Minnee 2017-01-12 09:55:10 +13:00
parent 747c0770e7
commit 6fc50cae5c

View File

@ -22,18 +22,18 @@ class TestMailer extends Mailer
*/ */
public function sendPlain($to, $from, $subject, $plainContent, $attachedFiles = false, $customHeaders = false) public function sendPlain($to, $from, $subject, $plainContent, $attachedFiles = false, $customHeaders = false)
{ {
$this->emailsSent[] = array( $this->saveEmail([
'type' => 'plain', 'Type' => 'plain',
'to' => $to, 'To' => $to,
'from' => $from, 'From' => $from,
'subject' => $subject, 'Subject' => $subject,
'content' => $plainContent, 'Content' => $plainContent,
'plainContent' => $plainContent, 'PlainContent' => $plainContent,
'attachedFiles' => $attachedFiles, 'AttachedFiles' => $attachedFiles,
'customHeaders' => $customHeaders, 'CustomHeaders' => $customHeaders,
); ]);
return true; return true;
} }
@ -63,24 +63,33 @@ class TestMailer extends Mailer
$inlineImages = false $inlineImages = false
) { ) {
$this->emailsSent[] = array( $this->saveEmail([
'type' => 'html', 'Type' => 'html',
'to' => $to, 'To' => $to,
'from' => $from, 'From' => $from,
'subject' => $subject, 'Subject' => $subject,
'content' => $htmlContent, 'Content' => $htmlContent,
'plainContent' => $plainContent, 'PlainContent' => $plainContent,
'htmlContent' => $htmlContent, 'HtmlContent' => $htmlContent,
'attachedFiles' => $attachedFiles, 'AttachedFiles' => $attachedFiles,
'customHeaders' => $customHeaders, 'CustomHeaders' => $customHeaders,
'inlineImages' => $inlineImages, 'InlineImages' => $inlineImages,
); ]);
return true; 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 * Clear the log of emails sent
*/ */
@ -102,11 +111,18 @@ class TestMailer extends Mailer
*/ */
public function findEmail($to, $from = null, $subject = null, $content = null) public function findEmail($to, $from = null, $subject = null, $content = null)
{ {
$compare = [
'To' => $to,
'From' => $from,
'Subject' => $subject,
'Content' => $content,
];
foreach ($this->emailsSent as $email) { foreach ($this->emailsSent as $email) {
$matched = true; $matched = true;
foreach (array('to','from','subject','content') as $field) { foreach (array('To','From','Subject','Content') as $field) {
if ($value = $$field) { if ($value = $compare[$field]) {
if ($value[0] == '/') { if ($value[0] == '/') {
$matched = preg_match($value, $email[$field]); $matched = preg_match($value, $email[$field]);
} else { } else {