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)
{
$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 {