Changeset View
Changeset View
Standalone View
Standalone View
src/applications/metamta/adapter/PhabricatorMailTestAdapter.php
<?php | <?php | ||||
/** | /** | ||||
* Mail adapter that doesn't actually send any email, for writing unit tests | * Mail adapter that doesn't actually send any email, for writing unit tests | ||||
* against. | * against. | ||||
*/ | */ | ||||
final class PhabricatorMailTestAdapter | final class PhabricatorMailTestAdapter | ||||
extends PhabricatorMailAdapter { | extends PhabricatorMailAdapter { | ||||
const ADAPTERTYPE = 'test'; | const ADAPTERTYPE = 'test'; | ||||
private $guts = array(); | private $guts = array(); | ||||
private $config = array(); | |||||
protected function validateOptions(array $options) { | private $supportsMessageID; | ||||
PhutilTypeSpec::checkMap( | private $failPermanently; | ||||
$options, | private $failTemporarily; | ||||
array()); | |||||
} | |||||
public function newDefaultOptions() { | public function setSupportsMessageID($support) { | ||||
return array(); | $this->supportsMessageID = $support; | ||||
return $this; | |||||
} | } | ||||
public function prepareForSend(array $config = array()) { | public function setFailPermanently($fail) { | ||||
$this->config = $config; | $this->failPermanently = true; | ||||
return $this; | |||||
} | } | ||||
public function setFrom($email, $name = '') { | public function setFailTemporarily($fail) { | ||||
$this->guts['from'] = $email; | $this->failTemporarily = true; | ||||
$this->guts['from-name'] = $name; | |||||
return $this; | return $this; | ||||
} | } | ||||
public function addReplyTo($email, $name = '') { | public function getSupportedMessageTypes() { | ||||
if (empty($this->guts['reply-to'])) { | return array( | ||||
$this->guts['reply-to'] = array(); | PhabricatorMailEmailMessage::MESSAGETYPE, | ||||
} | |||||
$this->guts['reply-to'][] = array( | |||||
'email' => $email, | |||||
'name' => $name, | |||||
); | ); | ||||
return $this; | |||||
} | } | ||||
public function addTos(array $emails) { | protected function validateOptions(array $options) { | ||||
foreach ($emails as $email) { | PhutilTypeSpec::checkMap($options, array()); | ||||
$this->guts['tos'][] = $email; | |||||
} | |||||
return $this; | |||||
} | } | ||||
public function addCCs(array $emails) { | public function newDefaultOptions() { | ||||
foreach ($emails as $email) { | return array(); | ||||
$this->guts['ccs'][] = $email; | |||||
} | } | ||||
return $this; | |||||
public function supportsMessageIDHeader() { | |||||
return $this->supportsMessageID; | |||||
} | } | ||||
public function addAttachment($data, $filename, $mimetype) { | public function getGuts() { | ||||
$this->guts['attachments'][] = array( | return $this->guts; | ||||
'data' => $data, | |||||
'filename' => $filename, | |||||
'mimetype' => $mimetype, | |||||
); | |||||
return $this; | |||||
} | } | ||||
public function addHeader($header_name, $header_value) { | public function sendMessage(PhabricatorMailExternalMessage $message) { | ||||
$this->guts['headers'][] = array($header_name, $header_value); | if ($this->failPermanently) { | ||||
return $this; | throw new PhabricatorMetaMTAPermanentFailureException( | ||||
pht('Unit Test (Permanent)')); | |||||
} | } | ||||
public function setBody($body) { | if ($this->failTemporarily) { | ||||
$this->guts['body'] = $body; | throw new Exception( | ||||
return $this; | pht('Unit Test (Temporary)')); | ||||
} | } | ||||
public function setHTMLBody($html_body) { | $guts = array(); | ||||
$this->guts['html-body'] = $html_body; | |||||
return $this; | $from = $message->getFromAddress(); | ||||
$guts['from'] = (string)$from; | |||||
$reply_to = $message->getReplyToAddress(); | |||||
if ($reply_to) { | |||||
$guts['reply-to'] = (string)$reply_to; | |||||
} | } | ||||
public function setSubject($subject) { | $to_addresses = $message->getToAddresses(); | ||||
$this->guts['subject'] = $subject; | $to = array(); | ||||
return $this; | foreach ($to_addresses as $address) { | ||||
$to[] = (string)$address; | |||||
} | } | ||||
$guts['tos'] = $to; | |||||
public function supportsMessageIDHeader() { | $cc_addresses = $message->getCCAddresses(); | ||||
return idx($this->config, 'supportsMessageIDHeader', true); | $cc = array(); | ||||
foreach ($cc_addresses as $address) { | |||||
$cc[] = (string)$address; | |||||
} | } | ||||
$guts['ccs'] = $cc; | |||||
public function send() { | $subject = $message->getSubject(); | ||||
if (!empty($this->guts['fail-permanently'])) { | if (strlen($subject)) { | ||||
throw new PhabricatorMetaMTAPermanentFailureException( | $guts['subject'] = $subject; | ||||
pht('Unit Test (Permanent)')); | |||||
} | } | ||||
if (!empty($this->guts['fail-temporarily'])) { | $headers = $message->getHeaders(); | ||||
throw new Exception( | $header_list = array(); | ||||
pht('Unit Test (Temporary)')); | foreach ($headers as $header) { | ||||
$header_list[] = array( | |||||
$header->getName(), | |||||
$header->getValue(), | |||||
); | |||||
} | } | ||||
$guts['headers'] = $header_list; | |||||
$this->guts['did-send'] = true; | $text_body = $message->getTextBody(); | ||||
return true; | if (strlen($text_body)) { | ||||
$guts['body'] = $text_body; | |||||
} | } | ||||
public function getGuts() { | $html_body = $message->getHTMLBody(); | ||||
return $this->guts; | if (strlen($html_body)) { | ||||
$guts['html-body'] = $html_body; | |||||
} | } | ||||
public function setFailPermanently($fail) { | $attachments = $message->getAttachments(); | ||||
$this->guts['fail-permanently'] = $fail; | $file_list = array(); | ||||
return $this; | foreach ($attachments as $attachment) { | ||||
$file_list[] = array( | |||||
'data' => $attachment->getData(), | |||||
'filename' => $attachment->getFilename(), | |||||
'mimetype' => $attachment->getMimeType(), | |||||
); | |||||
} | } | ||||
$guts['attachments'] = $file_list; | |||||
public function setFailTemporarily($fail) { | $guts['did-send'] = true; | ||||
$this->guts['fail-temporarily'] = $fail; | |||||
return $this; | $this->guts = $guts; | ||||
} | } | ||||
public function getBody() { | public function getBody() { | ||||
return idx($this->guts, 'body'); | return idx($this->guts, 'body'); | ||||
} | } | ||||
public function getHTMLBody() { | public function getHTMLBody() { | ||||
return idx($this->guts, 'html-body'); | return idx($this->guts, 'html-body'); | ||||
} | } | ||||
} | } |