1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jacobemerick\Archangel; |
4
|
|
|
|
5
|
|
|
use PHPUnit_Framework_TestCase; |
6
|
|
|
use ReflectionClass; |
7
|
|
|
|
8
|
|
|
class ArchangelTest extends PHPUnit_Framework_TestCase |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
public function testIsInstanceOfArchangel() |
12
|
|
|
{ |
13
|
|
|
$archangel = new Archangel(); |
14
|
|
|
|
15
|
|
|
$this->assertInstanceOf('Jacobemerick\Archangel\Archangel', $archangel); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function testIsLoggerAwareInterface() |
19
|
|
|
{ |
20
|
|
|
$archangel = new Archangel(); |
21
|
|
|
|
22
|
|
|
$this->assertInstanceOf('Psr\Log\LoggerAwareInterface', $archangel); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testConstructSetsDefaultMailer() |
26
|
|
|
{ |
27
|
|
|
$archangel = new Archangel(); |
28
|
|
|
$mailer = sprintf('PHP/%s', phpversion()); |
29
|
|
|
$headers = array('X-Mailer' => $mailer); |
30
|
|
|
|
31
|
|
|
$this->assertAttributeEquals($headers, 'headers', $archangel); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testConstructOverridesMailer() |
35
|
|
|
{ |
36
|
|
|
$archangel = new Archangel('AwesomeMailer'); |
37
|
|
|
$headers = array('X-Mailer' => 'AwesomeMailer'); |
38
|
|
|
|
39
|
|
|
$this->assertAttributeEquals($headers, 'headers', $archangel); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testConstructSetsNullLogger() |
43
|
|
|
{ |
44
|
|
|
$archangel = new Archangel(); |
45
|
|
|
|
46
|
|
|
$this->assertAttributeInstanceOf('Psr\Log\NullLogger', 'logger', $archangel); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testConstructSetsBoundaries() |
50
|
|
|
{ |
51
|
|
|
$archangel = new Archangel(); |
52
|
|
|
$expectedBoundaryMixed = sprintf('PHP-mixed-%s', uniqid()); |
53
|
|
|
$expectedBoundaryAlternative = sprintf('PHP-alternative-%s', uniqid()); |
54
|
|
|
|
55
|
|
|
$this->assertAttributeEquals($expectedBoundaryMixed, 'boundaryMixed', $archangel); |
56
|
|
|
$this->assertAttributeEquals($expectedBoundaryAlternative, 'boundaryAlternative', $archangel); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testSetLogger() |
60
|
|
|
{ |
61
|
|
|
$logger = $this->getMock('Psr\Log\LoggerInterface'); |
62
|
|
|
$archangel = new Archangel(); |
63
|
|
|
$archangel->setLogger($logger); |
64
|
|
|
|
65
|
|
|
$this->assertAttributeSame($logger, 'logger', $archangel); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testAddTo() |
69
|
|
|
{ |
70
|
|
|
$archangel = new Archangel(); |
71
|
|
|
$archangel->addTo('[email protected]'); |
72
|
|
|
|
73
|
|
|
$this->assertAttributeContains('[email protected]', 'toAddresses', $archangel); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testAddToMultiple() |
77
|
|
|
{ |
78
|
|
|
$archangel = new Archangel(); |
79
|
|
|
$archangel->addTo('[email protected]'); |
80
|
|
|
$archangel->addTo('[email protected]'); |
81
|
|
|
|
82
|
|
|
$this->assertAttributeContains('[email protected]', 'toAddresses', $archangel); |
83
|
|
|
$this->assertAttributeContains('[email protected]', 'toAddresses', $archangel); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testAddToWithTitle() |
87
|
|
|
{ |
88
|
|
|
$archangel = new Archangel(); |
89
|
|
|
$archangel->addTo('[email protected]', 'Mr. Test Alot'); |
90
|
|
|
|
91
|
|
|
$this->assertAttributeContains('"Mr. Test Alot" <[email protected]>', 'toAddresses', $archangel); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testAddCC() |
95
|
|
|
{ |
96
|
|
|
$archangel = new Archangel(); |
97
|
|
|
$archangel->addCC('[email protected]'); |
98
|
|
|
$headersProperty = $this->getProtectedProperty('headers'); |
99
|
|
|
$headers = $headersProperty->getValue($archangel); |
100
|
|
|
|
101
|
|
|
$this->assertArraySubset( |
102
|
|
|
array('CC' => array('[email protected]')), |
103
|
|
|
$headers |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
View Code Duplication |
public function testAddCCMultiple() |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
$archangel = new Archangel(); |
110
|
|
|
$archangel->addCC('[email protected]'); |
111
|
|
|
$archangel->addCC('[email protected]'); |
112
|
|
|
$headersProperty = $this->getProtectedProperty('headers'); |
113
|
|
|
$headers = $headersProperty->getValue($archangel); |
114
|
|
|
|
115
|
|
|
$this->assertArraySubset( |
116
|
|
|
array('CC' => array('[email protected]', '[email protected]')), |
117
|
|
|
$headers |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function testAddCCWithTitle() |
122
|
|
|
{ |
123
|
|
|
$archangel = new Archangel(); |
124
|
|
|
$archangel->addCC('[email protected]', 'Mr. Test Alot'); |
125
|
|
|
$headersProperty = $this->getProtectedProperty('headers'); |
126
|
|
|
$headers = $headersProperty->getValue($archangel); |
127
|
|
|
|
128
|
|
|
$this->assertArraySubset( |
129
|
|
|
array('CC' => array('"Mr. Test Alot" <[email protected]>')), |
130
|
|
|
$headers |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function testAddBCC() |
135
|
|
|
{ |
136
|
|
|
$archangel = new Archangel(); |
137
|
|
|
$archangel->addBCC('[email protected]'); |
138
|
|
|
$headersProperty = $this->getProtectedProperty('headers'); |
139
|
|
|
$headers = $headersProperty->getValue($archangel); |
140
|
|
|
|
141
|
|
|
$this->assertArraySubset( |
142
|
|
|
array('BCC' => array('[email protected]')), |
143
|
|
|
$headers |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
View Code Duplication |
public function testAddBCCMultiple() |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
$archangel = new Archangel(); |
150
|
|
|
$archangel->addBCC('[email protected]'); |
151
|
|
|
$archangel->addBCC('[email protected]'); |
152
|
|
|
$headersProperty = $this->getProtectedProperty('headers'); |
153
|
|
|
$headers = $headersProperty->getValue($archangel); |
154
|
|
|
|
155
|
|
|
$this->assertArraySubset( |
156
|
|
|
array('BCC' => array('[email protected]', '[email protected]')), |
157
|
|
|
$headers |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function testAddBCCWithTitle() |
162
|
|
|
{ |
163
|
|
|
$archangel = new Archangel(); |
164
|
|
|
$archangel->addBCC('[email protected]', 'Mr. Test Alot'); |
165
|
|
|
$headersProperty = $this->getProtectedProperty('headers'); |
166
|
|
|
$headers = $headersProperty->getValue($archangel); |
167
|
|
|
|
168
|
|
|
$this->assertArraySubset( |
169
|
|
|
array('BCC' => array('"Mr. Test Alot" <[email protected]>')), |
170
|
|
|
$headers |
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function testSetFrom() |
175
|
|
|
{ |
176
|
|
|
$archangel = new Archangel(); |
177
|
|
|
$archangel->setFrom('[email protected]'); |
178
|
|
|
$headersProperty = $this->getProtectedProperty('headers'); |
179
|
|
|
$headers = $headersProperty->getValue($archangel); |
180
|
|
|
|
181
|
|
|
$this->assertArraySubset(array('From' => '[email protected]'), $headers); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
View Code Duplication |
public function testSetFromMultiple() |
|
|
|
|
185
|
|
|
{ |
186
|
|
|
$archangel = new Archangel(); |
187
|
|
|
$archangel->setFrom('[email protected]'); |
188
|
|
|
$archangel->setFrom('[email protected]'); |
189
|
|
|
$headersProperty = $this->getProtectedProperty('headers'); |
190
|
|
|
$headers = $headersProperty->getValue($archangel); |
191
|
|
|
|
192
|
|
|
$this->assertArraySubset(array('From' => '[email protected]'), $headers); |
193
|
|
|
$this->assertNotContains('[email protected]', $headers); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function testSetFromWithTitle() |
197
|
|
|
{ |
198
|
|
|
$archangel = new Archangel(); |
199
|
|
|
$archangel->setFrom('[email protected]', 'Mr. Test Alot'); |
200
|
|
|
$headersProperty = $this->getProtectedProperty('headers'); |
201
|
|
|
$headers = $headersProperty->getValue($archangel); |
202
|
|
|
|
203
|
|
|
$this->assertArraySubset(array('From' => '"Mr. Test Alot" <[email protected]>'), $headers); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function testSetReplyTo() |
207
|
|
|
{ |
208
|
|
|
$archangel = new Archangel(); |
209
|
|
|
$archangel->setReplyTo('[email protected]'); |
210
|
|
|
$headersProperty = $this->getProtectedProperty('headers'); |
211
|
|
|
$headers = $headersProperty->getValue($archangel); |
212
|
|
|
|
213
|
|
|
$this->assertArraySubset(array('Reply-To' => '[email protected]'), $headers); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
View Code Duplication |
public function testSetReplyToMultiple() |
|
|
|
|
217
|
|
|
{ |
218
|
|
|
$archangel = new Archangel(); |
219
|
|
|
$archangel->setReplyTo('[email protected]'); |
220
|
|
|
$archangel->setReplyTo('[email protected]'); |
221
|
|
|
$headersProperty = $this->getProtectedProperty('headers'); |
222
|
|
|
$headers = $headersProperty->getValue($archangel); |
223
|
|
|
|
224
|
|
|
$this->assertArraySubset(array('Reply-To' => '[email protected]'), $headers); |
225
|
|
|
$this->assertNotContains('[email protected]', $headers); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function testSetReplyToWithTitle() |
229
|
|
|
{ |
230
|
|
|
$archangel = new Archangel(); |
231
|
|
|
$archangel->setReplyTo('[email protected]', 'Mr. Test Alot'); |
232
|
|
|
$headersProperty = $this->getProtectedProperty('headers'); |
233
|
|
|
$headers = $headersProperty->getValue($archangel); |
234
|
|
|
|
235
|
|
|
$this->assertArraySubset(array('Reply-To' => '"Mr. Test Alot" <[email protected]>'), $headers); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
View Code Duplication |
public function testFormatEmailAddress() |
|
|
|
|
239
|
|
|
{ |
240
|
|
|
$archangel = new Archangel(); |
241
|
|
|
$formatMethod = $this->getProtectedMethod('formatEmailAddress'); |
242
|
|
|
$formattedEmail = $formatMethod->invokeArgs($archangel, array('[email protected]', '')); |
243
|
|
|
|
244
|
|
|
$this->assertEquals('[email protected]', $formattedEmail); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
View Code Duplication |
public function testFormatEmailAddressWithTitle() |
|
|
|
|
248
|
|
|
{ |
249
|
|
|
$archangel = new Archangel(); |
250
|
|
|
$formatMethod = $this->getProtectedMethod('formatEmailAddress'); |
251
|
|
|
$formattedEmail = $formatMethod->invokeArgs($archangel, array('[email protected]', 'Mr. Test Alot')); |
252
|
|
|
|
253
|
|
|
$this->assertEquals('"Mr. Test Alot" <[email protected]>', $formattedEmail); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
public function testSetSubject() |
257
|
|
|
{ |
258
|
|
|
$archangel = new Archangel(); |
259
|
|
|
$archangel->setSubject('Test Subject'); |
260
|
|
|
|
261
|
|
|
$this->assertAttributeEquals('Test Subject', 'subject', $archangel); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function testSetPlainMessage() |
265
|
|
|
{ |
266
|
|
|
$archangel = new Archangel(); |
267
|
|
|
$archangel->setPlainMessage('Plain text message'); |
268
|
|
|
|
269
|
|
|
$this->assertAttributeEquals('Plain text message', 'plainMessage', $archangel); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
public function testSetHTMLMessage() |
273
|
|
|
{ |
274
|
|
|
$archangel = new Archangel(); |
275
|
|
|
$archangel->setHTMLMessage('<p>An HTML message.</p>'); |
276
|
|
|
|
277
|
|
|
$this->assertAttributeEquals('<p>An HTML message.</p>', 'htmlMessage', $archangel); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @dataProvider dataBuildHeaders |
282
|
|
|
*/ |
283
|
|
|
public function testBuildHeaders( |
284
|
|
|
$expectedHeaders, |
285
|
|
|
$headers, |
286
|
|
|
$attachments, |
287
|
|
|
$plainMessage, |
288
|
|
|
$htmlMessage |
289
|
|
|
) { |
290
|
|
|
$archangel = new Archangel(); |
291
|
|
|
$headersProperty = $this->getProtectedProperty('headers'); |
292
|
|
|
$headersProperty->setValue($archangel, $headers); |
293
|
|
|
|
294
|
|
|
if (!empty($attachments)) { |
295
|
|
|
$attachmentsProperty = $this->getProtectedProperty('attachments'); |
296
|
|
|
$attachmentsProperty->setValue($archangel, $attachments); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
if (!empty($plainMessage)) { |
300
|
|
|
$plainMessageProperty = $this->getProtectedProperty('plainMessage'); |
301
|
|
|
$plainMessageProperty->setValue($archangel, $plainMessage); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
if (!empty($htmlMessage)) { |
305
|
|
|
$htmlMessageProperty = $this->getProtectedProperty('htmlMessage'); |
306
|
|
|
$htmlMessageProperty->setValue($archangel, $htmlMessage); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
$buildHeadersMethod = $this->getProtectedMethod('buildHeaders'); |
310
|
|
|
$builtHeaders = $buildHeadersMethod->invoke($archangel); |
311
|
|
|
|
312
|
|
|
$this->assertEquals($expectedHeaders, $builtHeaders); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
public function dataBuildHeaders() |
316
|
|
|
{ |
317
|
|
|
return array( |
318
|
|
|
array( |
319
|
|
|
'expectedHeaders' => |
320
|
|
|
"From: [email protected]\r\n" . |
321
|
|
|
"X-Mailer: PHP/6.0.0", |
322
|
|
|
'headers' => array( |
323
|
|
|
'From' => '[email protected]', |
324
|
|
|
'X-Mailer' => sprintf('PHP/%s', phpversion()) |
325
|
|
|
), |
326
|
|
|
'attachments' => null, |
327
|
|
|
'plainMessage' => true, |
328
|
|
|
'htmlMessage' => null, |
329
|
|
|
), |
330
|
|
|
array( |
331
|
|
|
'expectedHeaders' => |
332
|
|
|
"CC: [email protected], [email protected]\r\n" . |
333
|
|
|
"From: [email protected]\r\n" . |
334
|
|
|
"X-Mailer: PHP/6.0.0", |
335
|
|
|
'headers' => array( |
336
|
|
|
'CC' => array('[email protected]', '[email protected]'), |
337
|
|
|
'From' => '[email protected]', |
338
|
|
|
'X-Mailer' => sprintf('PHP/%s', phpversion()) |
339
|
|
|
), |
340
|
|
|
'attachments' => null, |
341
|
|
|
'plainMessage' => true, |
342
|
|
|
'htmlMessage' => null, |
343
|
|
|
), |
344
|
|
|
array( |
345
|
|
|
'expectedHeaders' => |
346
|
|
|
"BCC: [email protected], [email protected]\r\n" . |
347
|
|
|
"From: [email protected]\r\n" . |
348
|
|
|
"X-Mailer: PHP/6.0.0", |
349
|
|
|
'headers' => array( |
350
|
|
|
'BCC' => array('[email protected]', '[email protected]'), |
351
|
|
|
'From' => '[email protected]', |
352
|
|
|
'X-Mailer' => sprintf('PHP/%s', phpversion()) |
353
|
|
|
), |
354
|
|
|
'attachments' => null, |
355
|
|
|
'plainMessage' => true, |
356
|
|
|
'htmlMessage' => null, |
357
|
|
|
), |
358
|
|
|
array( |
359
|
|
|
'expectedHeaders' => |
360
|
|
|
"From: [email protected]\r\n" . |
361
|
|
|
"X-Mailer: PHP/6.0.0\r\n" . |
362
|
|
|
"Content-Type: multipart/mixed; boundary=\"PHP-mixed-1234567890123\"", |
363
|
|
|
'headers' => array( |
364
|
|
|
'From' => '[email protected]', |
365
|
|
|
'X-Mailer' => sprintf('PHP/%s', phpversion()) |
366
|
|
|
), |
367
|
|
|
'attachments' => true, |
368
|
|
|
'plainMessage' => true, |
369
|
|
|
'htmlMessage' => null, |
370
|
|
|
), |
371
|
|
|
array( |
372
|
|
|
'expectedHeaders' => |
373
|
|
|
"From: [email protected]\r\n" . |
374
|
|
|
"X-Mailer: PHP/6.0.0\r\n" . |
375
|
|
|
"Content-Type: multipart/alternative; boundary=\"PHP-alternative-1234567890123\"", |
376
|
|
|
'headers' => array( |
377
|
|
|
'From' => '[email protected]', |
378
|
|
|
'X-Mailer' => sprintf('PHP/%s', phpversion()) |
379
|
|
|
), |
380
|
|
|
'attachments' => null, |
381
|
|
|
'plainMessage' => true, |
382
|
|
|
'htmlMessage' => true, |
383
|
|
|
), |
384
|
|
|
array( |
385
|
|
|
'expectedHeaders' => |
386
|
|
|
"From: [email protected]\r\n" . |
387
|
|
|
"X-Mailer: PHP/6.0.0\r\n" . |
388
|
|
|
"Content-type: text/html; charset=\"iso-8859-1\"", |
389
|
|
|
'headers' => array( |
390
|
|
|
'From' => '[email protected]', |
391
|
|
|
'X-Mailer' => sprintf('PHP/%s', phpversion()) |
392
|
|
|
), |
393
|
|
|
'attachments' => null, |
394
|
|
|
'plainMessage' => null, |
395
|
|
|
'htmlMessage' => true, |
396
|
|
|
), |
397
|
|
|
); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
View Code Duplication |
public function testAddAttachment() |
|
|
|
|
401
|
|
|
{ |
402
|
|
|
$archangel = new Archangel(); |
403
|
|
|
$archangel->addAttachment('path', 'type'); |
404
|
|
|
|
405
|
|
|
$this->assertAttributeContains( |
406
|
|
|
array('path' => 'path', 'type' => 'type', 'title' => ''), |
407
|
|
|
'attachments', |
408
|
|
|
$archangel |
409
|
|
|
); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
public function testAddAttachmentMultiple() |
413
|
|
|
{ |
414
|
|
|
$archangel = new Archangel(); |
415
|
|
|
$archangel->addAttachment('pathOne', 'typeOne'); |
416
|
|
|
$archangel->addAttachment('pathTwo', 'typeTwo'); |
417
|
|
|
|
418
|
|
|
$this->assertAttributeContains( |
419
|
|
|
array('path' => 'pathOne', 'type' => 'typeOne', 'title' => ''), |
420
|
|
|
'attachments', |
421
|
|
|
$archangel |
422
|
|
|
); |
423
|
|
|
$this->assertAttributeContains( |
424
|
|
|
array('path' => 'pathTwo', 'type' => 'typeTwo', 'title' => ''), |
425
|
|
|
'attachments', |
426
|
|
|
$archangel |
427
|
|
|
); |
428
|
|
|
} |
429
|
|
|
|
430
|
|
View Code Duplication |
public function testAddAttachmentWithTitle() |
|
|
|
|
431
|
|
|
{ |
432
|
|
|
$archangel = new Archangel(); |
433
|
|
|
$archangel->addAttachment('path', 'type', 'title'); |
434
|
|
|
|
435
|
|
|
$this->assertAttributeContains( |
436
|
|
|
array('path' => 'path', 'type' => 'type', 'title' => 'title'), |
437
|
|
|
'attachments', |
438
|
|
|
$archangel |
439
|
|
|
); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
public function testSend() |
443
|
|
|
{ |
444
|
|
|
$archangel = new Archangel(); |
445
|
|
|
$archangel->addTo('[email protected]'); |
446
|
|
|
$archangel->setSubject('Test Subject'); |
447
|
|
|
$archangel->setPlainMessage('Plain text message'); |
448
|
|
|
$response = $archangel->send(); |
449
|
|
|
|
450
|
|
|
$expectedResponse = array( |
451
|
|
|
'to' => '[email protected]', |
452
|
|
|
'subject' => 'Test Subject', |
453
|
|
|
'message' => 'Plain text message' . Archangel::LINE_BREAK, |
454
|
|
|
'headers' => 'X-Mailer: PHP/6.0.0', |
455
|
|
|
); |
456
|
|
|
|
457
|
|
|
$this->assertEquals($expectedResponse, $response); |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
/** |
461
|
|
|
* @dataProvider dataCheckRequiredFields |
462
|
|
|
*/ |
463
|
|
|
public function testCheckRequiredFields( |
464
|
|
|
$expectedResult, |
465
|
|
|
$toAddresses, |
466
|
|
|
$subject, |
467
|
|
|
$plainMessage, |
468
|
|
|
$htmlMessage, |
469
|
|
|
$attachments |
470
|
|
|
) { |
471
|
|
|
$archangel = new Archangel(); |
472
|
|
|
|
473
|
|
|
if (!empty($toAddresses)) { |
474
|
|
|
$toAddressesProperty = $this->getProtectedProperty('toAddresses'); |
475
|
|
|
$toAddressesProperty->setValue($archangel, $toAddresses); |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
if (!empty($subject)) { |
479
|
|
|
$subjectProperty = $this->getProtectedProperty('subject'); |
480
|
|
|
$subjectProperty->setValue($archangel, $subject); |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
if (!empty($plainMessage)) { |
484
|
|
|
$plainMessageProperty = $this->getProtectedProperty('plainMessage'); |
485
|
|
|
$plainMessageProperty->setValue($archangel, $plainMessage); |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
if (!empty($htmlMessage)) { |
489
|
|
|
$htmlMessageProperty = $this->getProtectedProperty('htmlMessage'); |
490
|
|
|
$htmlMessageProperty->setValue($archangel, $htmlMessage); |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
if (!empty($attachments)) { |
494
|
|
|
$attachmentsProperty = $this->getProtectedProperty('attachments'); |
495
|
|
|
$attachmentsProperty->setValue($archangel, $attachments); |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
$checkMethod = $this->getProtectedMethod('checkRequiredFields'); |
499
|
|
|
$isValid = $checkMethod->invoke($archangel); |
500
|
|
|
|
501
|
|
|
if ($expectedResult == true) { |
502
|
|
|
$this->assertTrue($isValid); |
503
|
|
|
return; |
504
|
|
|
} |
505
|
|
|
$this->assertNotTrue($isValid); |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
public function dataCheckRequiredFields() |
509
|
|
|
{ |
510
|
|
|
return array( |
511
|
|
|
array( |
512
|
|
|
'expectedResult' => false, |
513
|
|
|
'toAddresses' => array(), |
514
|
|
|
'subject' => '', |
515
|
|
|
'plainMessage' => '', |
516
|
|
|
'htmlMessage' => '', |
517
|
|
|
'attachments' => array(), |
518
|
|
|
), |
519
|
|
|
array( |
520
|
|
|
'expectedResult' => false, |
521
|
|
|
'toAddresses' => array('[email protected]'), |
522
|
|
|
'subject' => '', |
523
|
|
|
'plainMessage' => '', |
524
|
|
|
'htmlMessage' => '', |
525
|
|
|
'attachments' => array(), |
526
|
|
|
), |
527
|
|
|
array( |
528
|
|
|
'expectedResult' => false, |
529
|
|
|
'toAddresses' => array('[email protected]'), |
530
|
|
|
'subject' => 'Test Subject', |
531
|
|
|
'plainMessage' => '', |
532
|
|
|
'htmlMessage' => '', |
533
|
|
|
'attachments' => array(), |
534
|
|
|
), |
535
|
|
|
array( |
536
|
|
|
'expectedResult' => false, |
537
|
|
|
'toAddresses' => array(), |
538
|
|
|
'subject' => 'Test Subject', |
539
|
|
|
'plainMessage' => '', |
540
|
|
|
'htmlMessage' => '', |
541
|
|
|
'attachments' => array(), |
542
|
|
|
), |
543
|
|
|
array( |
544
|
|
|
'expectedResult' => false, |
545
|
|
|
'toAddresses' => array(), |
546
|
|
|
'subject' => 'Test Subject', |
547
|
|
|
'plainMessage' => 'Plain text message', |
548
|
|
|
'htmlMessage' => '', |
549
|
|
|
'attachments' => array(), |
550
|
|
|
), |
551
|
|
|
array( |
552
|
|
|
'expectedResult' => true, |
553
|
|
|
'toAddresses' => array('[email protected]'), |
554
|
|
|
'subject' => 'Test Subject', |
555
|
|
|
'plainMessage' => 'Plain text message', |
556
|
|
|
'htmlMessage' => '', |
557
|
|
|
'attachments' => array(), |
558
|
|
|
), |
559
|
|
|
array( |
560
|
|
|
'expectedResult' => true, |
561
|
|
|
'toAddresses' => array('[email protected]'), |
562
|
|
|
'subject' => 'Test Subject', |
563
|
|
|
'plainMessage' => '', |
564
|
|
|
'htmlMessage' => '<p>An HTML message.</p>', |
565
|
|
|
'attachments' => array(), |
566
|
|
|
), |
567
|
|
|
array( |
568
|
|
|
'expectedResult' => true, |
569
|
|
|
'toAddresses' => array('[email protected]'), |
570
|
|
|
'subject' => 'Test Subject', |
571
|
|
|
'plainMessage' => '', |
572
|
|
|
'htmlMessage' => '', |
573
|
|
|
'attachments' => array( |
574
|
|
|
array('path' => 'path', 'type' => 'type'), |
575
|
|
|
), |
576
|
|
|
), |
577
|
|
|
array( |
578
|
|
|
'expectedResult' => true, |
579
|
|
|
'toAddresses' => array('[email protected]'), |
580
|
|
|
'subject' => 'Test Subject', |
581
|
|
|
'plainMessage' => 'Plain text message', |
582
|
|
|
'htmlMessage' => '<p>An HTML message.</p>', |
583
|
|
|
'attachments' => array( |
584
|
|
|
array('path' => 'path', 'type' => 'type'), |
585
|
|
|
), |
586
|
|
|
), |
587
|
|
|
); |
588
|
|
|
} |
589
|
|
|
|
590
|
|
View Code Duplication |
public function testBuildTo() |
|
|
|
|
591
|
|
|
{ |
592
|
|
|
$archangel = new Archangel(); |
593
|
|
|
$addressesProperty = $this->getProtectedProperty('toAddresses'); |
594
|
|
|
$addressesProperty->setValue($archangel, array('[email protected]')); |
595
|
|
|
$buildMethod = $this->getProtectedMethod('buildTo'); |
596
|
|
|
$toAddresses = $buildMethod->invoke($archangel); |
597
|
|
|
|
598
|
|
|
$this->assertEquals('[email protected]', $toAddresses); |
599
|
|
|
} |
600
|
|
|
|
601
|
|
View Code Duplication |
public function testBuildToMultiple() |
|
|
|
|
602
|
|
|
{ |
603
|
|
|
$archangel = new Archangel(); |
604
|
|
|
$addressesProperty = $this->getProtectedProperty('toAddresses'); |
605
|
|
|
$addressesProperty->setValue($archangel, array('[email protected]', '[email protected]')); |
606
|
|
|
$buildMethod = $this->getProtectedMethod('buildTo'); |
607
|
|
|
$toAddresses = $buildMethod->invoke($archangel); |
608
|
|
|
|
609
|
|
|
$this->assertEquals('[email protected], [email protected]', $toAddresses); |
610
|
|
|
} |
611
|
|
|
|
612
|
|
|
protected function getProtectedProperty($property) |
613
|
|
|
{ |
614
|
|
|
$reflectedArchangel = new ReflectionClass('Jacobemerick\Archangel\Archangel'); |
615
|
|
|
$reflectedProperty = $reflectedArchangel->getProperty($property); |
616
|
|
|
$reflectedProperty->setAccessible(true); |
617
|
|
|
|
618
|
|
|
return $reflectedProperty; |
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
protected function getProtectedMethod($method) |
622
|
|
|
{ |
623
|
|
|
$reflectedArchangel = new ReflectionClass('Jacobemerick\Archangel\Archangel'); |
624
|
|
|
$reflectedMethod = $reflectedArchangel->getMethod($method); |
625
|
|
|
$reflectedMethod->setAccessible(true); |
626
|
|
|
|
627
|
|
|
return $reflectedMethod; |
628
|
|
|
} |
629
|
|
|
} |
630
|
|
|
|
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.