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
|
|
|
[ |
64
|
|
|
[ |
65
|
|
|
'filename' => 'filename.jpg', |
66
|
|
|
'contents' => 'abcdefg' |
67
|
|
|
] |
68
|
|
|
], // attachments |
69
|
|
|
[ |
70
|
|
|
'X-Custom-Header' => 'foo', |
71
|
|
|
'Cc' => '[email protected]', |
72
|
|
|
'Bcc' => '[email protected]' |
73
|
|
|
] // headers |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
View Code Duplication |
public function testSendPlain() |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
list($to, $from, $subject, $content, $plainContent, $attachments, $headers) = $this->getMockEmail(); |
|
|
|
|
80
|
|
|
|
81
|
|
|
$mailer = $this->getMock('Kinglozzer\SilverStripeMailgunner\Mailer', ['sendMessage']); |
|
|
|
|
82
|
|
|
$mailer->expects($this->once()) |
|
|
|
|
83
|
|
|
->method('sendMessage') |
84
|
|
|
->with( |
85
|
|
|
$this->equalTo($to), |
|
|
|
|
86
|
|
|
$this->equalTo($from), |
|
|
|
|
87
|
|
|
$this->equalTo($subject), |
|
|
|
|
88
|
|
|
$this->equalTo(''), |
|
|
|
|
89
|
|
|
$this->equalTo($plainContent), |
|
|
|
|
90
|
|
|
$this->equalTo($attachments), |
|
|
|
|
91
|
|
|
$this->equalTo($headers) |
|
|
|
|
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
$mailer->sendPlain($to, $from, $subject, $plainContent, $attachments, $headers); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
View Code Duplication |
public function testSendHTML() |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
list($to, $from, $subject, $content, $plainContent, $attachments, $headers) = $this->getMockEmail(); |
100
|
|
|
|
101
|
|
|
$mailer = $this->getMock('Kinglozzer\SilverStripeMailgunner\Mailer', ['sendMessage']); |
|
|
|
|
102
|
|
|
$mailer->expects($this->once()) |
|
|
|
|
103
|
|
|
->method('sendMessage') |
104
|
|
|
->with( |
105
|
|
|
$this->equalTo($to), |
|
|
|
|
106
|
|
|
$this->equalTo($from), |
|
|
|
|
107
|
|
|
$this->equalTo($subject), |
|
|
|
|
108
|
|
|
$this->equalTo($content), |
|
|
|
|
109
|
|
|
$this->equalTo($plainContent), |
|
|
|
|
110
|
|
|
$this->equalTo($attachments), |
|
|
|
|
111
|
|
|
$this->equalTo($headers) |
|
|
|
|
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
$mailer->sendHTML($to, $from, $subject, $content, $attachments, $headers, $plainContent); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testSendMessage() |
118
|
|
|
{ |
119
|
|
|
$domain = 'http://testdomain.com'; |
120
|
|
|
Config::inst()->update('Kinglozzer\SilverStripeMailgunner\Mailer', 'api_domain', $domain); |
121
|
|
|
|
122
|
|
|
list($to, $from, $subject, $content, $plainContent, $attachments, $headers) = $this->getMockEmail(); |
123
|
|
|
|
124
|
|
|
$messageBuilder = $this->getMock('Mailgun\Messages\MessageBuilder', ['getMessage']); |
|
|
|
|
125
|
|
|
// We expect that sendMessage() will fetch the full message text from the builder |
126
|
|
|
$messageBuilder->expects($this->once()) |
|
|
|
|
127
|
|
|
->method('getMessage') |
128
|
|
|
->will($this->returnValue('test message')); |
|
|
|
|
129
|
|
|
|
130
|
|
|
$client = $this->getMock('Mailgun\Mailgun', ['MessageBuilder', 'sendMessage']); |
|
|
|
|
131
|
|
|
// We expect that sendMessage() will fetch the message builder from the Mailgun client, and |
132
|
|
|
// we use this point to inject our mock message builder |
133
|
|
|
$client->expects($this->once()) |
|
|
|
|
134
|
|
|
->method('MessageBuilder') |
135
|
|
|
->will($this->returnValue($messageBuilder)); |
|
|
|
|
136
|
|
|
// We expect that Mailer::sendMessage() will trigger Mailgun::sendMessage() with the |
137
|
|
|
// domain set in config, and the prepared message and attachments |
138
|
|
|
$client->expects($this->once()) |
|
|
|
|
139
|
|
|
->method('sendMessage') |
140
|
|
|
->with( |
141
|
|
|
$this->equalTo($domain), |
|
|
|
|
142
|
|
|
$this->equalTo('test message'), |
|
|
|
|
143
|
|
|
$this->equalTo(['preparedattachments']) |
|
|
|
|
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
$mailer = $this->getMock( |
|
|
|
|
147
|
|
|
'Kinglozzer\SilverStripeMailgunner\Mailer', |
148
|
|
|
['getMailgunClient', 'buildMessage', 'prepareAttachments', 'closeTempFileHandles'] |
149
|
|
|
); |
150
|
|
|
// We inject our mock Mailgun client while asserting that sendMessage() does request it |
151
|
|
|
$mailer->expects($this->once()) |
|
|
|
|
152
|
|
|
->method('getMailgunClient') |
153
|
|
|
->will($this->returnValue($client)); |
|
|
|
|
154
|
|
|
// We expect that sendMessage() will pass everything off to the buildMessage() method |
155
|
|
|
$mailer->expects($this->once()) |
|
|
|
|
156
|
|
|
->method('buildMessage') |
157
|
|
|
->with( |
158
|
|
|
$this->equalTo($messageBuilder), |
|
|
|
|
159
|
|
|
$this->equalTo($to), |
|
|
|
|
160
|
|
|
$this->equalTo($from), |
|
|
|
|
161
|
|
|
$this->equalTo($subject), |
|
|
|
|
162
|
|
|
$this->equalTo($content), |
|
|
|
|
163
|
|
|
$this->equalTo($plainContent), |
|
|
|
|
164
|
|
|
$this->equalTo($headers) |
|
|
|
|
165
|
|
|
); |
166
|
|
|
// We've got attachments, so we assert that sendMessage() passes them off to |
167
|
|
|
// prepareAttachments() and specify a mock "prepared" return value |
168
|
|
|
$mailer->expects($this->once()) |
|
|
|
|
169
|
|
|
->method('prepareAttachments') |
170
|
|
|
->with($this->equalTo($attachments)) |
|
|
|
|
171
|
|
|
->will($this->returnValue(['preparedattachments'])); |
|
|
|
|
172
|
|
|
// Assert that the mailer attempts to close any remaining open file handles |
173
|
|
|
$mailer->expects($this->once()) |
|
|
|
|
174
|
|
|
->method('closeTempFileHandles'); |
175
|
|
|
|
176
|
|
|
// Let's go! |
177
|
|
|
$this->invokeMethod( |
178
|
|
|
$mailer, |
179
|
|
|
'sendMessage', |
180
|
|
|
[$to, $from, $subject, $content, $plainContent, $attachments, $headers] |
181
|
|
|
); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @expectedException Exception |
186
|
|
|
*/ |
187
|
|
|
public function testSendMessageExceptionClosesHandles() |
188
|
|
|
{ |
189
|
|
|
list($to, $from, $subject, $content, $plainContent, $attachments, $headers) = $this->getMockEmail(); |
190
|
|
|
|
191
|
|
|
$client = $this->getMock('Mailgun\Mailgun', ['sendMessage']); |
|
|
|
|
192
|
|
|
// Make our mock client trigger an exception |
193
|
|
|
$client->expects($this->once()) |
|
|
|
|
194
|
|
|
->method('sendMessage') |
195
|
|
|
->will($this->throwException(new \Exception)); |
|
|
|
|
196
|
|
|
|
197
|
|
|
$mailer = $this->getMock( |
|
|
|
|
198
|
|
|
'Kinglozzer\SilverStripeMailgunner\Mailer', |
199
|
|
|
['getMailgunClient', 'closeTempFileHandles'] |
200
|
|
|
); |
201
|
|
|
// Inject our mock Mailgun client |
202
|
|
|
$mailer->expects($this->once()) |
|
|
|
|
203
|
|
|
->method('getMailgunClient') |
204
|
|
|
->will($this->returnValue($client)); |
|
|
|
|
205
|
|
|
// Assert that the exception that the client throws triggers closing open file handles |
206
|
|
|
$mailer->expects($this->once()) |
|
|
|
|
207
|
|
|
->method('closeTempFileHandles'); |
208
|
|
|
|
209
|
|
|
$this->invokeMethod( |
210
|
|
|
$mailer, |
211
|
|
|
'sendMessage', |
212
|
|
|
[$to, $from, $subject, $content, $plainContent, $attachments, $headers] |
213
|
|
|
); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function testBuildMessage() |
217
|
|
|
{ |
218
|
|
|
list($to, $from, $subject, $content, $plainContent, $attachments, $headers) = $this->getMockEmail(); |
|
|
|
|
219
|
|
|
|
220
|
|
|
$messageBuilder = $this->getMock('Mailgun\Messages\MessageBuilder'); |
|
|
|
|
221
|
|
|
$messageBuilder->expects($this->once()) |
|
|
|
|
222
|
|
|
->method('addToRecipient') |
223
|
|
|
->with($this->equalTo($to)); |
|
|
|
|
224
|
|
|
|
225
|
|
|
$messageBuilder->expects($this->once()) |
|
|
|
|
226
|
|
|
->method('setFromAddress') |
227
|
|
|
->with($this->equalTo($from)); |
|
|
|
|
228
|
|
|
|
229
|
|
|
$messageBuilder->expects($this->once()) |
|
|
|
|
230
|
|
|
->method('setSubject') |
231
|
|
|
->with($this->equalTo($subject)); |
|
|
|
|
232
|
|
|
|
233
|
|
|
$messageBuilder->expects($this->once()) |
|
|
|
|
234
|
|
|
->method('setHtmlBody') |
235
|
|
|
->with($this->equalTo($content)); |
|
|
|
|
236
|
|
|
|
237
|
|
|
$messageBuilder->expects($this->once()) |
|
|
|
|
238
|
|
|
->method('setTextBody') |
239
|
|
|
->with($this->equalTo($plainContent)); |
|
|
|
|
240
|
|
|
|
241
|
|
|
$messageBuilder->expects($this->once()) |
|
|
|
|
242
|
|
|
->method('addCcRecipient') |
243
|
|
|
->with($this->equalTo($headers['Cc'])); |
|
|
|
|
244
|
|
|
|
245
|
|
|
$messageBuilder->expects($this->once()) |
|
|
|
|
246
|
|
|
->method('addBccRecipient') |
247
|
|
|
->with($this->equalTo($headers['Bcc'])); |
|
|
|
|
248
|
|
|
|
249
|
|
|
$messageBuilder->expects($this->once()) |
|
|
|
|
250
|
|
|
->method('addCustomHeader') |
251
|
|
|
->with( |
252
|
|
|
$this->equalTo('X-Custom-Header'), |
|
|
|
|
253
|
|
|
$this->equalTo('foo') |
|
|
|
|
254
|
|
|
); |
255
|
|
|
|
256
|
|
|
$this->invokeMethod( |
257
|
|
|
new Mailer, |
258
|
|
|
'buildMessage', |
259
|
|
|
[$messageBuilder, $to, $from, $subject, $content, $plainContent, $headers] |
260
|
|
|
); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
public function testPrepareAttachments() |
264
|
|
|
{ |
265
|
|
|
$attachments = [ |
266
|
|
|
['filename' => 'test1.jpg', 'contents' => 'abcdefg'], |
267
|
|
|
['filename' => 'test2.jpg', 'contents' => 'hijklmn'] |
268
|
|
|
]; |
269
|
|
|
|
270
|
|
|
$expected = [ |
271
|
|
|
'attachment' => [ |
272
|
|
|
['filePath' => 'tmp/test1.jpg', 'remoteName' => 'test1.jpg'], |
273
|
|
|
['filePath' => 'tmp/test2.jpg', 'remoteName' => 'test2.jpg'] |
274
|
|
|
] |
275
|
|
|
]; |
276
|
|
|
|
277
|
|
|
$mailer = $this->getMock('Kinglozzer\SilverStripeMailgunner\Mailer', ['writeToTempFile']); |
|
|
|
|
278
|
|
|
$mailer->expects($this->at(0)) |
|
|
|
|
279
|
|
|
->method('writeToTempFile') |
280
|
|
|
->with($this->equalTo('abcdefg')) |
|
|
|
|
281
|
|
|
->will($this->returnValue('tmp/test1.jpg')); |
|
|
|
|
282
|
|
|
$mailer->expects($this->at(1)) |
|
|
|
|
283
|
|
|
->method('writeToTempFile') |
284
|
|
|
->with($this->equalTo('hijklmn')) |
|
|
|
|
285
|
|
|
->will($this->returnValue('tmp/test2.jpg')); |
|
|
|
|
286
|
|
|
|
287
|
|
|
$prepared = $this->invokeMethod($mailer, 'prepareAttachments', [$attachments]); |
288
|
|
|
$this->assertEquals($expected, $prepared); |
|
|
|
|
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
public function testWriteToTempFile() |
292
|
|
|
{ |
293
|
|
|
$contents = 'test file contents'; |
294
|
|
|
$mailer = new Mailer; |
295
|
|
|
$tempFile = $this->invokeMethod($mailer, 'writeToTempFile', [$contents]); |
296
|
|
|
|
297
|
|
|
$this->assertEquals($contents, file_get_contents($tempFile)); |
|
|
|
|
298
|
|
|
|
299
|
|
|
// Assert that the stream and temp file path are stored |
300
|
|
|
$reflection = new \ReflectionClass(get_class($mailer)); |
301
|
|
|
$property = $reflection->getProperty('tempFileHandles'); |
302
|
|
|
$property->setAccessible(true); |
303
|
|
|
$fileHandles = $property->getValue($mailer); |
304
|
|
|
|
305
|
|
|
$this->assertNotEmpty($fileHandles); |
|
|
|
|
306
|
|
|
|
307
|
|
|
// Test the contents of the stream |
308
|
|
|
$handle = $fileHandles[0]['handle']; |
309
|
|
|
rewind($handle); |
310
|
|
|
$this->assertEquals($contents, fread($handle, filesize($fileHandles[0]['path']))); |
|
|
|
|
311
|
|
|
$this->assertEquals($tempFile, $fileHandles[0]['path']); |
|
|
|
|
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
public function testCloseTempFileHandles() |
315
|
|
|
{ |
316
|
|
|
$mailer = new Mailer; |
317
|
|
|
$tempFile = tempnam(sys_get_temp_dir(), 'SS_MG_TESTS_TMP'); |
318
|
|
|
$fileHandle = fopen($tempFile, 'w'); |
319
|
|
|
fwrite($fileHandle, 'test data'); |
320
|
|
|
$handleData = ['handle' => $fileHandle, 'path' => $tempFile]; |
321
|
|
|
|
322
|
|
|
$reflection = new \ReflectionClass(get_class($mailer)); |
323
|
|
|
$property = $reflection->getProperty('tempFileHandles'); |
324
|
|
|
$property->setAccessible(true); |
325
|
|
|
$fileHandles = $property->setValue($mailer, [$handleData]); |
|
|
|
|
326
|
|
|
|
327
|
|
|
$this->invokeMethod($mailer, 'closeTempFileHandles'); |
328
|
|
|
|
329
|
|
|
$this->assertEmpty($property->getValue($mailer)); |
|
|
|
|
330
|
|
|
$this->assertFalse(file_exists($tempFile)); |
|
|
|
|
331
|
|
|
$this->assertEquals('Unknown', get_resource_type($fileHandle)); |
|
|
|
|
332
|
|
|
} |
333
|
|
|
} |
334
|
|
|
|
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: