1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\BehatExtension\Utility; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Same principle as core TestMailer class, |
7
|
|
|
* but saves emails in {@link TestSessionEnvironment} |
8
|
|
|
* to share the state between PHP calls (CLI vs. browser). |
9
|
|
|
*/ |
10
|
|
|
class TestMailer extends \Mailer |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var TestSessionEnvironment |
15
|
|
|
*/ |
16
|
|
|
protected $testSessionEnvironment; |
17
|
|
|
|
18
|
|
|
public function __construct() |
19
|
|
|
{ |
20
|
|
|
$this->testSessionEnvironment = \Injector::inst()->get('TestSessionEnvironment'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Send a plain-text email. |
25
|
|
|
* TestMailer will merely record that the email was asked to be sent, without sending anything. |
26
|
|
|
*/ |
27
|
|
View Code Duplication |
public function sendPlain($to, $from, $subject, $plainContent, $attachedFiles = false, $customHeaders = false) |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$this->saveEmail(array( |
30
|
|
|
'Type' => 'plain', |
31
|
|
|
'To' => $to, |
32
|
|
|
'From' => $from, |
33
|
|
|
'Subject' => $subject, |
34
|
|
|
'Content' => $plainContent, |
35
|
|
|
'PlainContent' => $plainContent, |
36
|
|
|
'AttachedFiles' => $attachedFiles, |
37
|
|
|
'CustomHeaders' => $customHeaders, |
38
|
|
|
)); |
39
|
|
|
|
40
|
|
|
return true; |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Send a multi-part HTML email |
45
|
|
|
* TestMailer will merely record that the email was asked to be sent, without sending anything. |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
public function sendHTML( |
|
|
|
|
48
|
|
|
$to, |
49
|
|
|
$from, |
50
|
|
|
$subject, |
51
|
|
|
$htmlContent, |
52
|
|
|
$attachedFiles = false, |
53
|
|
|
$customHeaders = false, |
54
|
|
|
$plainContent = false, |
55
|
|
|
$inlineImages = false |
|
|
|
|
56
|
|
|
) { |
57
|
|
|
|
58
|
|
|
$this->saveEmail(array( |
59
|
|
|
'Type' => 'html', |
60
|
|
|
'To' => $to, |
61
|
|
|
'From' => $from, |
62
|
|
|
'Subject' => $subject, |
63
|
|
|
'Content' => $htmlContent, |
64
|
|
|
'PlainContent' => $plainContent, |
65
|
|
|
'AttachedFiles' => $attachedFiles, |
66
|
|
|
'CustomHeaders' => $customHeaders, |
67
|
|
|
)); |
68
|
|
|
|
69
|
|
|
return true; |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Clear the log of emails sent |
74
|
|
|
*/ |
75
|
|
|
public function clearEmails() |
76
|
|
|
{ |
77
|
|
|
$state = $this->testSessionEnvironment->getState(); |
78
|
|
|
if (isset($state->emails)) { |
79
|
|
|
unset($state->emails); |
80
|
|
|
} |
81
|
|
|
$this->testSessionEnvironment->applyState($state); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Search for an email that was sent. |
86
|
|
|
* All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression. |
87
|
|
|
* |
88
|
|
|
* @param $to |
89
|
|
|
* @param $from |
90
|
|
|
* @param $subject |
91
|
|
|
* @param $content |
92
|
|
|
* @return array Contains the keys: 'type', 'to', 'from', 'subject', 'content', 'plainContent', 'attachedFiles', |
93
|
|
|
* 'customHeaders', 'htmlContent', 'inlineImages' |
94
|
|
|
*/ |
95
|
|
|
public function findEmail($to = null, $from = null, $subject = null, $content = null) |
96
|
|
|
{ |
97
|
|
|
$matches = $this->findEmails($to, $from, $subject, $content); |
98
|
|
|
//got the count of matches emails |
99
|
|
|
$emailCount = count($matches); |
100
|
|
|
//get the last(latest) one |
101
|
|
|
return $matches ? $matches[$emailCount-1] : null; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Search for all emails. |
106
|
|
|
* All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression. |
107
|
|
|
* |
108
|
|
|
* @param $to |
109
|
|
|
* @param $from |
110
|
|
|
* @param $subject |
111
|
|
|
* @param $content |
112
|
|
|
* @return array Contains the keys: 'type', 'to', 'from', 'subject', 'content', 'plainContent', 'attachedFiles', |
113
|
|
|
* 'customHeaders', 'htmlContent', 'inlineImages' |
114
|
|
|
*/ |
115
|
|
|
public function findEmails($to = null, $from = null, $subject = null, $content = null) |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
$matches = array(); |
118
|
|
|
$args = func_get_args(); |
119
|
|
|
$state = $this->testSessionEnvironment->getState(); |
120
|
|
|
$emails = isset($state->emails) ? $state->emails : array(); |
121
|
|
|
foreach ($emails as $email) { |
122
|
|
|
$matched = true; |
123
|
|
|
|
124
|
|
|
foreach (array('To', 'From', 'Subject', 'Content') as $i => $field) { |
125
|
|
|
if (!isset($email->$field)) { |
126
|
|
|
continue; |
127
|
|
|
} |
128
|
|
|
$value = (isset($args[$i])) ? $args[$i] : null; |
129
|
|
|
if ($value) { |
130
|
|
|
if ($value[0] == '/') { |
131
|
|
|
$matched = preg_match($value, $email->$field); |
132
|
|
|
} else { |
133
|
|
|
$matched = ($value == $email->$field); |
134
|
|
|
} |
135
|
|
|
if (!$matched) { |
136
|
|
|
break; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
if ($matched) { |
141
|
|
|
$matches[] = $email; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $matches; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
protected function saveEmail($data) |
149
|
|
|
{ |
150
|
|
|
$state = $this->testSessionEnvironment->getState(); |
151
|
|
|
if (!isset($state->emails)) { |
152
|
|
|
$state->emails = array(); |
153
|
|
|
} |
154
|
|
|
$state->emails[] = array_filter($data); |
155
|
|
|
$this->testSessionEnvironment->applyState($state); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.