Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/metamta/adapter/PhabricatorMailTestAdapter.php
12256 views
1
<?php
2
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 {
9
10
const ADAPTERTYPE = 'test';
11
12
private $guts = array();
13
14
private $supportsMessageID;
15
private $failPermanently;
16
private $failTemporarily;
17
18
public function setSupportsMessageID($support) {
19
$this->supportsMessageID = $support;
20
return $this;
21
}
22
23
public function setFailPermanently($fail) {
24
$this->failPermanently = true;
25
return $this;
26
}
27
28
public function setFailTemporarily($fail) {
29
$this->failTemporarily = true;
30
return $this;
31
}
32
33
public function getSupportedMessageTypes() {
34
return array(
35
PhabricatorMailEmailMessage::MESSAGETYPE,
36
PhabricatorMailSMSMessage::MESSAGETYPE,
37
);
38
}
39
40
protected function validateOptions(array $options) {
41
PhutilTypeSpec::checkMap($options, array());
42
}
43
44
public function newDefaultOptions() {
45
return array();
46
}
47
48
public function supportsMessageIDHeader() {
49
return $this->supportsMessageID;
50
}
51
52
public function getGuts() {
53
return $this->guts;
54
}
55
56
public function sendMessage(PhabricatorMailExternalMessage $message) {
57
if ($this->failPermanently) {
58
throw new PhabricatorMetaMTAPermanentFailureException(
59
pht('Unit Test (Permanent)'));
60
}
61
62
if ($this->failTemporarily) {
63
throw new Exception(
64
pht('Unit Test (Temporary)'));
65
}
66
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;
74
}
75
76
$guts['did-send'] = true;
77
$this->guts = $guts;
78
}
79
80
public function getBody() {
81
return idx($this->guts, 'body');
82
}
83
84
public function getHTMLBody() {
85
return idx($this->guts, 'html-body');
86
}
87
88
private function newEmailGuts(PhabricatorMailExternalMessage $message) {
89
$guts = array();
90
91
$from = $message->getFromAddress();
92
$guts['from'] = (string)$from;
93
94
$reply_to = $message->getReplyToAddress();
95
if ($reply_to) {
96
$guts['reply-to'] = (string)$reply_to;
97
}
98
99
$to_addresses = $message->getToAddresses();
100
$to = array();
101
foreach ($to_addresses as $address) {
102
$to[] = (string)$address;
103
}
104
$guts['tos'] = $to;
105
106
$cc_addresses = $message->getCCAddresses();
107
$cc = array();
108
foreach ($cc_addresses as $address) {
109
$cc[] = (string)$address;
110
}
111
$guts['ccs'] = $cc;
112
113
$subject = $message->getSubject();
114
if (strlen($subject)) {
115
$guts['subject'] = $subject;
116
}
117
118
$headers = $message->getHeaders();
119
$header_list = array();
120
foreach ($headers as $header) {
121
$header_list[] = array(
122
$header->getName(),
123
$header->getValue(),
124
);
125
}
126
$guts['headers'] = $header_list;
127
128
$text_body = $message->getTextBody();
129
if (phutil_nonempty_string($text_body)) {
130
$guts['body'] = $text_body;
131
}
132
133
$html_body = $message->getHTMLBody();
134
if (phutil_nonempty_string($html_body)) {
135
$guts['html-body'] = $html_body;
136
}
137
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(),
145
);
146
}
147
$guts['attachments'] = $file_list;
148
149
return $guts;
150
}
151
152
private function newSMSGuts(PhabricatorMailExternalMessage $message) {
153
$guts = array();
154
155
$guts['to'] = $message->getToNumber();
156
$guts['body'] = $message->getTextBody();
157
158
return $guts;
159
}
160
161
}
162
163