Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / metamta / adapter / PhabricatorMailTestAdapter.php
bloba6258a88746dc4c70b37486c341d0d764500f064
1 <?php
3 /**
4 * Mail adapter that doesn't actually send any email, for writing unit tests
5 * against.
6 */
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;
20 return $this;
23 public function setFailPermanently($fail) {
24 $this->failPermanently = true;
25 return $this;
28 public function setFailTemporarily($fail) {
29 $this->failTemporarily = true;
30 return $this;
33 public function getSupportedMessageTypes() {
34 return array(
35 PhabricatorMailEmailMessage::MESSAGETYPE,
36 PhabricatorMailSMSMessage::MESSAGETYPE,
40 protected function validateOptions(array $options) {
41 PhutilTypeSpec::checkMap($options, array());
44 public function newDefaultOptions() {
45 return array();
48 public function supportsMessageIDHeader() {
49 return $this->supportsMessageID;
52 public function getGuts() {
53 return $this->guts;
56 public function sendMessage(PhabricatorMailExternalMessage $message) {
57 if ($this->failPermanently) {
58 throw new PhabricatorMetaMTAPermanentFailureException(
59 pht('Unit Test (Permanent)'));
62 if ($this->failTemporarily) {
63 throw new Exception(
64 pht('Unit Test (Temporary)'));
67 switch ($message->getMessageType()) {
68 case PhabricatorMailEmailMessage::MESSAGETYPE:
69 $guts = $this->newEmailGuts($message);
70 break;
71 case PhabricatorMailSMSMessage::MESSAGETYPE:
72 $guts = $this->newSMSGuts($message);
73 break;
76 $guts['did-send'] = true;
77 $this->guts = $guts;
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) {
89 $guts = array();
91 $from = $message->getFromAddress();
92 $guts['from'] = (string)$from;
94 $reply_to = $message->getReplyToAddress();
95 if ($reply_to) {
96 $guts['reply-to'] = (string)$reply_to;
99 $to_addresses = $message->getToAddresses();
100 $to = array();
101 foreach ($to_addresses as $address) {
102 $to[] = (string)$address;
104 $guts['tos'] = $to;
106 $cc_addresses = $message->getCCAddresses();
107 $cc = array();
108 foreach ($cc_addresses as $address) {
109 $cc[] = (string)$address;
111 $guts['ccs'] = $cc;
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(
122 $header->getName(),
123 $header->getValue(),
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;
149 return $guts;
152 private function newSMSGuts(PhabricatorMailExternalMessage $message) {
153 $guts = array();
155 $guts['to'] = $message->getToNumber();
156 $guts['body'] = $message->getTextBody();
158 return $guts;