|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kinglozzer\SilverStripeMailgunner\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Kinglozzer\SilverStripeMailgunner\Mailer; |
|
6
|
|
|
use Config; |
|
7
|
|
|
use Email; |
|
8
|
|
|
use Injector; |
|
9
|
|
|
|
|
10
|
|
|
class MailerTest extends \SapphireTest |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @param object &$object |
|
14
|
|
|
* @param string $methodName |
|
15
|
|
|
* @param array $parameters |
|
16
|
|
|
* @return mixed |
|
17
|
|
|
*/ |
|
18
|
|
|
protected function invokeMethod(&$object, $methodName, array $parameters = []) |
|
19
|
|
|
{ |
|
20
|
|
|
$reflection = new \ReflectionClass(get_class($object)); |
|
21
|
|
|
$method = $reflection->getMethod($methodName); |
|
22
|
|
|
$method->setAccessible(true); |
|
23
|
|
|
|
|
24
|
|
|
return $method->invokeArgs($object, $parameters); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Simple test to check that registering the mailer as an Injector service |
|
29
|
|
|
* will make it the default mailer used by Email |
|
30
|
|
|
*/ |
|
31
|
|
|
public function testMailerRegistrationWithEmail() |
|
32
|
|
|
{ |
|
33
|
|
|
Injector::nest(); |
|
34
|
|
|
Injector::inst()->registerService(new Mailer, 'Mailer'); |
|
|
|
|
|
|
35
|
|
|
$this->assertInstanceOf('Kinglozzer\SilverStripeMailgunner\Mailer', Email::mailer()); |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
Injector::unnest(); |
|
38
|
|
|
$this->assertNotInstanceOf('Kinglozzer\SilverStripeMailgunner\Mailer', Email::mailer()); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function testSetGetMailgunClient() |
|
42
|
|
|
{ |
|
43
|
|
|
$mailer = new Mailer; |
|
44
|
|
|
$mockClient = $this->getMock('Mailgun\Mailgun'); |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
$this->assertInstanceOf('Mailgun\Mailgun', $mailer->getMailgunClient()); |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
$mailer->setMailgunClient($mockClient); |
|
49
|
|
|
$this->assertSame($mockClient, $mailer->getMailgunClient()); |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function getMockEmail() |
|
56
|
|
|
{ |
|
57
|
|
|
return [ |
|
58
|
|
|
'[email protected]', // to |
|
59
|
|
|
'[email protected]', // from |
|
60
|
|
|
'Important question', // subject |
|
61
|
|
|
'<p>How much foo could a foo bar baz if a baz bam could bar foo?</p>', // html content |
|
62
|
|
|
'How much foo could a foo bar baz if a baz bam could bar foo?', // plain text content |
|
63
|
|
|
['filename.jpg'], // attachments |
|
64
|
|
|
[ |
|
65
|
|
|
'X-Custom-Header' => 'foo', |
|
66
|
|
|
'Cc' => '[email protected]', |
|
67
|
|
|
'Bcc' => '[email protected]' |
|
68
|
|
|
] // headers |
|
69
|
|
|
]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
View Code Duplication |
public function testSendPlain() |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
list($to, $from, $subject, $content, $plainContent, $attachments, $headers) = $this->getMockEmail(); |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
$mailer = $this->getMock('Kinglozzer\SilverStripeMailgunner\Mailer', ['sendMessage']); |
|
|
|
|
|
|
77
|
|
|
$mailer->expects($this->once()) |
|
|
|
|
|
|
78
|
|
|
->method('sendMessage') |
|
79
|
|
|
->with( |
|
80
|
|
|
$this->equalTo($to), |
|
|
|
|
|
|
81
|
|
|
$this->equalTo($from), |
|
|
|
|
|
|
82
|
|
|
$this->equalTo($subject), |
|
|
|
|
|
|
83
|
|
|
$this->equalTo(''), |
|
|
|
|
|
|
84
|
|
|
$this->equalTo($plainContent), |
|
|
|
|
|
|
85
|
|
|
$this->equalTo($attachments), |
|
|
|
|
|
|
86
|
|
|
$this->equalTo($headers) |
|
|
|
|
|
|
87
|
|
|
); |
|
88
|
|
|
|
|
89
|
|
|
$mailer->sendPlain($to, $from, $subject, $plainContent, $attachments, $headers); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
View Code Duplication |
public function testSendHTML() |
|
|
|
|
|
|
93
|
|
|
{ |
|
94
|
|
|
list($to, $from, $subject, $content, $plainContent, $attachments, $headers) = $this->getMockEmail(); |
|
95
|
|
|
|
|
96
|
|
|
$mailer = $this->getMock('Kinglozzer\SilverStripeMailgunner\Mailer', ['sendMessage']); |
|
|
|
|
|
|
97
|
|
|
$mailer->expects($this->once()) |
|
|
|
|
|
|
98
|
|
|
->method('sendMessage') |
|
99
|
|
|
->with( |
|
100
|
|
|
$this->equalTo($to), |
|
|
|
|
|
|
101
|
|
|
$this->equalTo($from), |
|
|
|
|
|
|
102
|
|
|
$this->equalTo($subject), |
|
|
|
|
|
|
103
|
|
|
$this->equalTo($content), |
|
|
|
|
|
|
104
|
|
|
$this->equalTo($plainContent), |
|
|
|
|
|
|
105
|
|
|
$this->equalTo($attachments), |
|
|
|
|
|
|
106
|
|
|
$this->equalTo($headers) |
|
|
|
|
|
|
107
|
|
|
); |
|
108
|
|
|
|
|
109
|
|
|
$mailer->sendHTML($to, $from, $subject, $content, $attachments, $headers, $plainContent); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function testSendMessage() |
|
113
|
|
|
{ |
|
114
|
|
|
$domain = 'http://testdomain.com'; |
|
115
|
|
|
Config::inst()->update('Kinglozzer\SilverStripeMailgunner\Mailer', 'api_domain', $domain); |
|
116
|
|
|
|
|
117
|
|
|
list($to, $from, $subject, $content, $plainContent, $attachments, $headers) = $this->getMockEmail(); |
|
118
|
|
|
|
|
119
|
|
|
$messageBuilder = $this->getMock('Mailgun\Messages\MessageBuilder', ['getMessage']); |
|
|
|
|
|
|
120
|
|
|
// We expect that sendMessage() will fetch the full message text from the builder |
|
121
|
|
|
$messageBuilder->expects($this->once()) |
|
|
|
|
|
|
122
|
|
|
->method('getMessage') |
|
123
|
|
|
->will($this->returnValue('test message')); |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
$client = $this->getMock('Mailgun\Mailgun', ['MessageBuilder', 'sendMessage']); |
|
|
|
|
|
|
126
|
|
|
// We expect that sendMessage() will fetch the message builder from the Mailgun client, and |
|
127
|
|
|
// we use this point to inject our mock message builder |
|
128
|
|
|
$client->expects($this->once()) |
|
|
|
|
|
|
129
|
|
|
->method('MessageBuilder') |
|
130
|
|
|
->will($this->returnValue($messageBuilder)); |
|
|
|
|
|
|
131
|
|
|
// We expect that Mailer::sendMessage() will trigger Mailgun::sendMessage() with the |
|
132
|
|
|
// domain set in config, and the prepared message and attachments |
|
133
|
|
|
$client->expects($this->once()) |
|
|
|
|
|
|
134
|
|
|
->method('sendMessage') |
|
135
|
|
|
->with( |
|
136
|
|
|
$this->equalTo($domain), |
|
|
|
|
|
|
137
|
|
|
$this->equalTo('test message'), |
|
|
|
|
|
|
138
|
|
|
$this->equalTo(['preparedattachments']) |
|
|
|
|
|
|
139
|
|
|
); |
|
140
|
|
|
|
|
141
|
|
|
$mailer = $this->getMock( |
|
|
|
|
|
|
142
|
|
|
'Kinglozzer\SilverStripeMailgunner\Mailer', |
|
143
|
|
|
['getMailgunClient', 'buildMessage', 'prepareAttachments', 'closeTempFileHandles'] |
|
144
|
|
|
); |
|
145
|
|
|
// We inject our mock Mailgun client while asserting that sendMessage() does request it |
|
146
|
|
|
$mailer->expects($this->once()) |
|
|
|
|
|
|
147
|
|
|
->method('getMailgunClient') |
|
148
|
|
|
->will($this->returnValue($client)); |
|
|
|
|
|
|
149
|
|
|
// We expect that sendMessage() will pass everything off to the buildMessage() method |
|
150
|
|
|
$mailer->expects($this->once()) |
|
|
|
|
|
|
151
|
|
|
->method('buildMessage') |
|
152
|
|
|
->with( |
|
153
|
|
|
$this->equalTo($messageBuilder), |
|
|
|
|
|
|
154
|
|
|
$this->equalTo($to), |
|
|
|
|
|
|
155
|
|
|
$this->equalTo($from), |
|
|
|
|
|
|
156
|
|
|
$this->equalTo($subject), |
|
|
|
|
|
|
157
|
|
|
$this->equalTo($content), |
|
|
|
|
|
|
158
|
|
|
$this->equalTo($plainContent), |
|
|
|
|
|
|
159
|
|
|
$this->equalTo($headers) |
|
|
|
|
|
|
160
|
|
|
); |
|
161
|
|
|
// We've got attachments, so we assert that sendMessage() passes them off to |
|
162
|
|
|
// prepareAttachments() and specify a mock "prepared" return value |
|
163
|
|
|
$mailer->expects($this->once()) |
|
|
|
|
|
|
164
|
|
|
->method('prepareAttachments') |
|
165
|
|
|
->with( $this->equalTo($attachments)) |
|
|
|
|
|
|
166
|
|
|
->will($this->returnValue(['preparedattachments'])); |
|
|
|
|
|
|
167
|
|
|
// Assert that the mailer attempts to close any remaining open file handles |
|
168
|
|
|
$mailer->expects($this->once()) |
|
|
|
|
|
|
169
|
|
|
->method('closeTempFileHandles'); |
|
170
|
|
|
|
|
171
|
|
|
// Let's go! |
|
172
|
|
|
$this->invokeMethod( |
|
173
|
|
|
$mailer, |
|
174
|
|
|
'sendMessage', |
|
175
|
|
|
[$to, $from, $subject, $content, $plainContent, $attachments, $headers] |
|
176
|
|
|
); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
public function testBuildMessage() |
|
180
|
|
|
{ |
|
181
|
|
|
list($to, $from, $subject, $content, $plainContent, $attachments, $headers) = $this->getMockEmail(); |
|
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
$messageBuilder = $this->getMock('Mailgun\Messages\MessageBuilder'); |
|
|
|
|
|
|
184
|
|
|
$messageBuilder->expects($this->once()) |
|
|
|
|
|
|
185
|
|
|
->method('addToRecipient') |
|
186
|
|
|
->with($this->equalTo($to)); |
|
|
|
|
|
|
187
|
|
|
|
|
188
|
|
|
$messageBuilder->expects($this->once()) |
|
|
|
|
|
|
189
|
|
|
->method('setFromAddress') |
|
190
|
|
|
->with($this->equalTo($from)); |
|
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
$messageBuilder->expects($this->once()) |
|
|
|
|
|
|
193
|
|
|
->method('setSubject') |
|
194
|
|
|
->with($this->equalTo($subject)); |
|
|
|
|
|
|
195
|
|
|
|
|
196
|
|
|
$messageBuilder->expects($this->once()) |
|
|
|
|
|
|
197
|
|
|
->method('setHtmlBody') |
|
198
|
|
|
->with($this->equalTo($content)); |
|
|
|
|
|
|
199
|
|
|
|
|
200
|
|
|
$messageBuilder->expects($this->once()) |
|
|
|
|
|
|
201
|
|
|
->method('setTextBody') |
|
202
|
|
|
->with($this->equalTo($plainContent)); |
|
|
|
|
|
|
203
|
|
|
|
|
204
|
|
|
$messageBuilder->expects($this->once()) |
|
|
|
|
|
|
205
|
|
|
->method('addCcRecipient') |
|
206
|
|
|
->with($this->equalTo($headers['Cc'])); |
|
|
|
|
|
|
207
|
|
|
|
|
208
|
|
|
$messageBuilder->expects($this->once()) |
|
|
|
|
|
|
209
|
|
|
->method('addBccRecipient') |
|
210
|
|
|
->with($this->equalTo($headers['Bcc'])); |
|
|
|
|
|
|
211
|
|
|
|
|
212
|
|
|
$messageBuilder->expects($this->once()) |
|
|
|
|
|
|
213
|
|
|
->method('addCustomHeader') |
|
214
|
|
|
->with( |
|
215
|
|
|
$this->equalTo('X-Custom-Header'), |
|
|
|
|
|
|
216
|
|
|
$this->equalTo('foo') |
|
|
|
|
|
|
217
|
|
|
); |
|
218
|
|
|
|
|
219
|
|
|
$this->invokeMethod( |
|
220
|
|
|
new Mailer, |
|
221
|
|
|
'buildMessage', |
|
222
|
|
|
[$messageBuilder, $to, $from, $subject, $content, $plainContent, $headers] |
|
223
|
|
|
); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
public function testPrepareAttachments() |
|
227
|
|
|
{ |
|
228
|
|
|
$attachments = [ |
|
229
|
|
|
['filename' => 'test1.jpg', 'contents' => 'abcdefg'], |
|
230
|
|
|
['filename' => 'test2.jpg', 'contents' => 'hijklmn'] |
|
231
|
|
|
]; |
|
232
|
|
|
|
|
233
|
|
|
$expected = [ |
|
234
|
|
|
'attachment' => [ |
|
235
|
|
|
['filePath' => 'tmp/test1.jpg', 'remoteName' => 'test1.jpg'], |
|
236
|
|
|
['filePath' => 'tmp/test2.jpg', 'remoteName' => 'test2.jpg'] |
|
237
|
|
|
] |
|
238
|
|
|
]; |
|
239
|
|
|
|
|
240
|
|
|
$mailer = $this->getMock('Kinglozzer\SilverStripeMailgunner\Mailer', ['writeToTempFile']); |
|
|
|
|
|
|
241
|
|
|
$mailer->expects($this->at(0)) |
|
|
|
|
|
|
242
|
|
|
->method('writeToTempFile') |
|
243
|
|
|
->with($this->equalTo('abcdefg')) |
|
|
|
|
|
|
244
|
|
|
->will($this->returnValue('tmp/test1.jpg')); |
|
|
|
|
|
|
245
|
|
|
$mailer->expects($this->at(1)) |
|
|
|
|
|
|
246
|
|
|
->method('writeToTempFile') |
|
247
|
|
|
->with($this->equalTo('hijklmn')) |
|
|
|
|
|
|
248
|
|
|
->will($this->returnValue('tmp/test2.jpg')); |
|
|
|
|
|
|
249
|
|
|
|
|
250
|
|
|
$prepared = $this->invokeMethod($mailer, 'prepareAttachments', [$attachments]); |
|
251
|
|
|
$this->assertEquals($expected, $prepared); |
|
|
|
|
|
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
public function testWriteToTempFile() |
|
255
|
|
|
{ |
|
256
|
|
|
$contents = 'test file contents'; |
|
257
|
|
|
$mailer = new Mailer; |
|
258
|
|
|
$tempFile = $this->invokeMethod($mailer, 'writeToTempFile', [$contents]); |
|
259
|
|
|
|
|
260
|
|
|
$this->assertEquals($contents, file_get_contents($tempFile)); |
|
|
|
|
|
|
261
|
|
|
|
|
262
|
|
|
// Assert that the stream and temp file path are stored |
|
263
|
|
|
$reflection = new \ReflectionClass(get_class($mailer)); |
|
264
|
|
|
$property = $reflection->getProperty('tempFileHandles'); |
|
265
|
|
|
$property->setAccessible(true); |
|
266
|
|
|
$fileHandles = $property->getValue($mailer); |
|
267
|
|
|
|
|
268
|
|
|
$this->assertNotEmpty($fileHandles); |
|
|
|
|
|
|
269
|
|
|
|
|
270
|
|
|
// Test the contents of the stream |
|
271
|
|
|
$handle = $fileHandles[0]['handle']; |
|
272
|
|
|
rewind($handle); |
|
273
|
|
|
$this->assertEquals($contents, fread($handle, filesize($fileHandles[0]['path']))); |
|
|
|
|
|
|
274
|
|
|
$this->assertEquals($tempFile, $fileHandles[0]['path']); |
|
|
|
|
|
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
public function testCloseTempFileHandles() |
|
278
|
|
|
{ |
|
279
|
|
|
$mailer = new Mailer; |
|
280
|
|
|
$tempFile = tempnam(sys_get_temp_dir(), 'SS_MG_TESTS_TMP'); |
|
281
|
|
|
$fileHandle = fopen($tempFile, 'w'); |
|
282
|
|
|
fwrite($fileHandle, 'test data'); |
|
283
|
|
|
$handleData = ['handle' => $fileHandle, 'path' => $tempFile]; |
|
284
|
|
|
|
|
285
|
|
|
$reflection = new \ReflectionClass(get_class($mailer)); |
|
286
|
|
|
$property = $reflection->getProperty('tempFileHandles'); |
|
287
|
|
|
$property->setAccessible(true); |
|
288
|
|
|
$fileHandles = $property->setValue($mailer, [$handleData]); |
|
|
|
|
|
|
289
|
|
|
|
|
290
|
|
|
$this->invokeMethod($mailer, 'closeTempFileHandles'); |
|
291
|
|
|
|
|
292
|
|
|
$this->assertEmpty($property->getValue($mailer)); |
|
|
|
|
|
|
293
|
|
|
$this->assertFalse(file_exists($tempFile)); |
|
|
|
|
|
|
294
|
|
|
$this->assertEquals('Unknown', get_resource_type($fileHandle)); |
|
|
|
|
|
|
295
|
|
|
} |
|
296
|
|
|
} |
|
297
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: