4 * Mail adapter that doesn't actually send any email, for writing unit tests
7 final class PhabricatorMailTestAdapter
8 extends PhabricatorMailAdapter
{
10 const ADAPTERTYPE
= 'test';
12 private $guts = array();
14 private $supportsMessageID;
15 private $failPermanently;
16 private $failTemporarily;
18 public function setSupportsMessageID($support) {
19 $this->supportsMessageID
= $support;
23 public function setFailPermanently($fail) {
24 $this->failPermanently
= true;
28 public function setFailTemporarily($fail) {
29 $this->failTemporarily
= true;
33 public function getSupportedMessageTypes() {
35 PhabricatorMailEmailMessage
::MESSAGETYPE
,
36 PhabricatorMailSMSMessage
::MESSAGETYPE
,
40 protected function validateOptions(array $options) {
41 PhutilTypeSpec
::checkMap($options, array());
44 public function newDefaultOptions() {
48 public function supportsMessageIDHeader() {
49 return $this->supportsMessageID
;
52 public function getGuts() {
56 public function sendMessage(PhabricatorMailExternalMessage
$message) {
57 if ($this->failPermanently
) {
58 throw new PhabricatorMetaMTAPermanentFailureException(
59 pht('Unit Test (Permanent)'));
62 if ($this->failTemporarily
) {
64 pht('Unit Test (Temporary)'));
67 switch ($message->getMessageType()) {
68 case PhabricatorMailEmailMessage
::MESSAGETYPE
:
69 $guts = $this->newEmailGuts($message);
71 case PhabricatorMailSMSMessage
::MESSAGETYPE
:
72 $guts = $this->newSMSGuts($message);
76 $guts['did-send'] = true;
80 public function getBody() {
81 return idx($this->guts
, 'body');
84 public function getHTMLBody() {
85 return idx($this->guts
, 'html-body');
88 private function newEmailGuts(PhabricatorMailExternalMessage
$message) {
91 $from = $message->getFromAddress();
92 $guts['from'] = (string)$from;
94 $reply_to = $message->getReplyToAddress();
96 $guts['reply-to'] = (string)$reply_to;
99 $to_addresses = $message->getToAddresses();
101 foreach ($to_addresses as $address) {
102 $to[] = (string)$address;
106 $cc_addresses = $message->getCCAddresses();
108 foreach ($cc_addresses as $address) {
109 $cc[] = (string)$address;
113 $subject = $message->getSubject();
114 if (strlen($subject)) {
115 $guts['subject'] = $subject;
118 $headers = $message->getHeaders();
119 $header_list = array();
120 foreach ($headers as $header) {
121 $header_list[] = array(
126 $guts['headers'] = $header_list;
128 $text_body = $message->getTextBody();
129 if (strlen($text_body)) {
130 $guts['body'] = $text_body;
133 $html_body = $message->getHTMLBody();
134 if (strlen($html_body)) {
135 $guts['html-body'] = $html_body;
138 $attachments = $message->getAttachments();
139 $file_list = array();
140 foreach ($attachments as $attachment) {
141 $file_list[] = array(
142 'data' => $attachment->getData(),
143 'filename' => $attachment->getFilename(),
144 'mimetype' => $attachment->getMimeType(),
147 $guts['attachments'] = $file_list;
152 private function newSMSGuts(PhabricatorMailExternalMessage
$message) {
155 $guts['to'] = $message->getToNumber();
156 $guts['body'] = $message->getTextBody();