Completed
Push — master ( 7a6178...4e9949 )
by Jacob
02:31
created
tests/function-overrides.php 1 patch
Indentation   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -3,13 +3,13 @@
 block discarded – undo
3 3
 namespace Jacobemerick\Archangel;
4 4
 
5 5
 function mail($to, $subject, $message, $headers) {
6
-    return compact('to', 'subject', 'message', 'headers');
6
+	return compact('to', 'subject', 'message', 'headers');
7 7
 }
8 8
 
9 9
 function phpversion() {
10
-    return '6.0.0';
10
+	return '6.0.0';
11 11
 }
12 12
 
13 13
 function uniqid() {
14
-    return '1234567890123';
14
+	return '1234567890123';
15 15
 }
Please login to merge, or discard this patch.
tests/ArchangelTest.php 1 patch
Indentation   +958 added lines, -958 removed lines patch added patch discarded remove patch
@@ -10,963 +10,963 @@
 block discarded – undo
10 10
 class ArchangelTest extends PHPUnit_Framework_TestCase
11 11
 {
12 12
 
13
-    public function testIsInstanceOfArchangel()
14
-    {
15
-        $archangel = new Archangel();
16
-
17
-        $this->assertInstanceOf('Jacobemerick\Archangel\Archangel', $archangel);
18
-    }
19
-
20
-    public function testIsLoggerAwareInterface()
21
-    {
22
-        $archangel = new Archangel();
23
-
24
-        $this->assertInstanceOf('Psr\Log\LoggerAwareInterface', $archangel);
25
-    }
26
-
27
-    public function testConstructSetsDefaultMailer()
28
-    {
29
-        $archangel = new Archangel();
30
-        $mailer = sprintf('PHP/%s', phpversion());
31
-        $headers = array('X-Mailer' => $mailer);
32
-
33
-        $this->assertAttributeEquals($headers, 'headers', $archangel);
34
-    }
35
-
36
-    public function testConstructOverridesMailer()
37
-    {
38
-        $archangel = new Archangel('AwesomeMailer');
39
-        $headers = array('X-Mailer' => 'AwesomeMailer');
40
-
41
-        $this->assertAttributeEquals($headers, 'headers', $archangel);
42
-    }
43
-
44
-    public function testConstructSetsNullLogger()
45
-    {
46
-        $archangel = new Archangel();
47
-
48
-        $this->assertAttributeInstanceOf('Psr\Log\NullLogger', 'logger', $archangel);
49
-    }
50
-
51
-    public function testConstructSetsBoundaries()
52
-    {
53
-        $archangel = new Archangel();
54
-        $expectedBoundaryMixed = sprintf('PHP-mixed-%s', uniqid());
55
-        $expectedBoundaryAlternative = sprintf('PHP-alternative-%s', uniqid());
56
-
57
-        $this->assertAttributeEquals($expectedBoundaryMixed, 'boundaryMixed', $archangel);
58
-        $this->assertAttributeEquals($expectedBoundaryAlternative, 'boundaryAlternative', $archangel);
59
-    }
60
-
61
-    public function testSetLogger()
62
-    {
63
-        $logger = $this->getMock('Psr\Log\LoggerInterface');
64
-        $archangel = new Archangel();
65
-        $archangel->setLogger($logger);
66
-
67
-        $this->assertAttributeSame($logger, 'logger', $archangel);
68
-    }
69
-
70
-    public function testAddTo()
71
-    {
72
-        $archangel = new Archangel();
73
-        $archangel->addTo('[email protected]');
74
-
75
-        $this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
76
-    }
77
-
78
-    public function testAddToMultiple()
79
-    {
80
-        $archangel = new Archangel();
81
-        $archangel->addTo('[email protected]');
82
-        $archangel->addTo('[email protected]');
83
-
84
-        $this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
85
-        $this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
86
-    }
87
-
88
-    public function testAddToWithTitle()
89
-    {
90
-        $archangel = new Archangel();
91
-        $archangel->addTo('[email protected]', 'Mr. Test Alot');
92
-
93
-        $this->assertAttributeContains('"Mr. Test Alot" <[email protected]>', 'toAddresses', $archangel);
94
-    }
95
-
96
-    public function testAddCC()
97
-    {
98
-        $archangel = new Archangel();
99
-        $archangel->addCC('[email protected]');
100
-        $headersProperty = $this->getProtectedProperty('headers');
101
-        $headers = $headersProperty->getValue($archangel);
102
-
103
-        $this->assertArraySubset(
104
-            array('CC' => array('[email protected]')),
105
-            $headers
106
-        );
107
-    }
108
-
109
-    public function testAddCCMultiple()
110
-    {
111
-        $archangel = new Archangel();
112
-        $archangel->addCC('[email protected]');
113
-        $archangel->addCC('[email protected]');
114
-        $headersProperty = $this->getProtectedProperty('headers');
115
-        $headers = $headersProperty->getValue($archangel);
116
-
117
-        $this->assertArraySubset(
118
-            array('CC' => array('[email protected]', '[email protected]')),
119
-            $headers
120
-        );
121
-    }
122
-
123
-    public function testAddCCWithTitle()
124
-    {
125
-        $archangel = new Archangel();
126
-        $archangel->addCC('[email protected]', 'Mr. Test Alot');
127
-        $headersProperty = $this->getProtectedProperty('headers');
128
-        $headers = $headersProperty->getValue($archangel);
129
-
130
-        $this->assertArraySubset(
131
-            array('CC' => array('"Mr. Test Alot" <[email protected]>')),
132
-            $headers
133
-        );
134
-    }
135
-
136
-    public function testAddBCC()
137
-    {
138
-        $archangel = new Archangel();
139
-        $archangel->addBCC('[email protected]');
140
-        $headersProperty = $this->getProtectedProperty('headers');
141
-        $headers = $headersProperty->getValue($archangel);
142
-
143
-        $this->assertArraySubset(
144
-            array('BCC' => array('[email protected]')),
145
-            $headers
146
-        );
147
-    }
148
-
149
-    public function testAddBCCMultiple()
150
-    {
151
-        $archangel = new Archangel();
152
-        $archangel->addBCC('[email protected]');
153
-        $archangel->addBCC('[email protected]');
154
-        $headersProperty = $this->getProtectedProperty('headers');
155
-        $headers = $headersProperty->getValue($archangel);
156
-
157
-        $this->assertArraySubset(
158
-            array('BCC' => array('[email protected]', '[email protected]')),
159
-            $headers
160
-        );
161
-    }
162
-
163
-    public function testAddBCCWithTitle()
164
-    {
165
-        $archangel = new Archangel();
166
-        $archangel->addBCC('[email protected]', 'Mr. Test Alot');
167
-        $headersProperty = $this->getProtectedProperty('headers');
168
-        $headers = $headersProperty->getValue($archangel);
169
-
170
-        $this->assertArraySubset(
171
-            array('BCC' => array('"Mr. Test Alot" <[email protected]>')),
172
-            $headers
173
-        );
174
-    }
175
-
176
-    public function testSetFrom()
177
-    {
178
-        $archangel = new Archangel();
179
-        $archangel->setFrom('[email protected]');
180
-        $headersProperty = $this->getProtectedProperty('headers');
181
-        $headers = $headersProperty->getValue($archangel);
182
-
183
-        $this->assertArraySubset(array('From' => '[email protected]'), $headers);
184
-    }
185
-
186
-    public function testSetFromMultiple()
187
-    {
188
-        $archangel = new Archangel();
189
-        $archangel->setFrom('[email protected]');
190
-        $archangel->setFrom('[email protected]');
191
-        $headersProperty = $this->getProtectedProperty('headers');
192
-        $headers = $headersProperty->getValue($archangel);
193
-
194
-        $this->assertArraySubset(array('From' => '[email protected]'), $headers);
195
-        $this->assertNotContains('[email protected]', $headers);
196
-    }
197
-
198
-    public function testSetFromWithTitle()
199
-    {
200
-        $archangel = new Archangel();
201
-        $archangel->setFrom('[email protected]', 'Mr. Test Alot');
202
-        $headersProperty = $this->getProtectedProperty('headers');
203
-        $headers = $headersProperty->getValue($archangel);
204
-
205
-        $this->assertArraySubset(array('From' => '"Mr. Test Alot" <[email protected]>'), $headers);
206
-    }
207
-
208
-    public function testSetReplyTo()
209
-    {
210
-        $archangel = new Archangel();
211
-        $archangel->setReplyTo('[email protected]');
212
-        $headersProperty = $this->getProtectedProperty('headers');
213
-        $headers = $headersProperty->getValue($archangel);
214
-
215
-        $this->assertArraySubset(array('Reply-To' => '[email protected]'), $headers);
216
-    }
217
-
218
-    public function testSetReplyToMultiple()
219
-    {
220
-        $archangel = new Archangel();
221
-        $archangel->setReplyTo('[email protected]');
222
-        $archangel->setReplyTo('[email protected]');
223
-        $headersProperty = $this->getProtectedProperty('headers');
224
-        $headers = $headersProperty->getValue($archangel);
225
-
226
-        $this->assertArraySubset(array('Reply-To' => '[email protected]'), $headers);
227
-        $this->assertNotContains('[email protected]', $headers);
228
-    }
229
-
230
-    public function testSetReplyToWithTitle()
231
-    {
232
-        $archangel = new Archangel();
233
-        $archangel->setReplyTo('[email protected]', 'Mr. Test Alot');
234
-        $headersProperty = $this->getProtectedProperty('headers');
235
-        $headers = $headersProperty->getValue($archangel);
236
-
237
-        $this->assertArraySubset(array('Reply-To' => '"Mr. Test Alot" <[email protected]>'), $headers);
238
-    }
239
-
240
-    public function testFormatEmailAddress()
241
-    {
242
-        $archangel = new Archangel();
243
-        $formatMethod = $this->getProtectedMethod('formatEmailAddress');
244
-        $formattedEmail = $formatMethod->invokeArgs($archangel, array('[email protected]', ''));
245
-
246
-        $this->assertEquals('[email protected]', $formattedEmail);
247
-    }
248
-
249
-    public function testFormatEmailAddressWithTitle()
250
-    {
251
-        $archangel = new Archangel();
252
-        $formatMethod = $this->getProtectedMethod('formatEmailAddress');
253
-        $formattedEmail = $formatMethod->invokeArgs($archangel, array('[email protected]', 'Mr. Test Alot'));
254
-
255
-        $this->assertEquals('"Mr. Test Alot" <[email protected]>', $formattedEmail);
256
-    }
257
-
258
-    public function testSetSubject()
259
-    {
260
-        $archangel = new Archangel();
261
-        $archangel->setSubject('Test Subject');
262
-
263
-        $this->assertAttributeEquals('Test Subject', 'subject', $archangel);
264
-    }
265
-
266
-    public function testSetPlainMessage()
267
-    {
268
-        $archangel = new Archangel();
269
-        $archangel->setPlainMessage('Plain text message');
270
-
271
-        $this->assertAttributeEquals('Plain text message', 'plainMessage', $archangel);
272
-    }
273
-
274
-    public function testSetHTMLMessage()
275
-    {
276
-        $archangel = new Archangel();
277
-        $archangel->setHTMLMessage('<p>An HTML message.</p>');
278
-
279
-        $this->assertAttributeEquals('<p>An HTML message.</p>', 'htmlMessage', $archangel);
280
-    }
281
-
282
-    public function testAddAttachment()
283
-    {
284
-        $archangel = new Archangel();
285
-        $archangel->addAttachment('path', 'type');
286
-
287
-        $this->assertAttributeContains(
288
-            array('path' => 'path', 'type' => 'type', 'title' => ''),
289
-            'attachments',
290
-            $archangel
291
-        );
292
-    }
293
-
294
-    public function testAddAttachmentMultiple()
295
-    {
296
-        $archangel = new Archangel();
297
-        $archangel->addAttachment('pathOne', 'typeOne');
298
-        $archangel->addAttachment('pathTwo', 'typeTwo');
299
-
300
-        $this->assertAttributeContains(
301
-            array('path' => 'pathOne', 'type' => 'typeOne', 'title' => ''),
302
-            'attachments',
303
-            $archangel
304
-        );
305
-        $this->assertAttributeContains(
306
-            array('path' => 'pathTwo', 'type' => 'typeTwo', 'title' => ''),
307
-            'attachments',
308
-            $archangel
309
-        );
310
-    }
311
-
312
-    public function testAddAttachmentWithTitle()
313
-    {
314
-        $archangel = new Archangel();
315
-        $archangel->addAttachment('path', 'type', 'title');
316
-
317
-        $this->assertAttributeContains(
318
-            array('path' => 'path', 'type' => 'type', 'title' => 'title'),
319
-            'attachments',
320
-            $archangel
321
-        );
322
-    }
323
-
324
-    public function testSend()
325
-    {
326
-        $archangel = new Archangel();
327
-        $archangel->addTo('[email protected]');
328
-        $archangel->setSubject('Test Subject');
329
-        $archangel->setPlainMessage('Plain text message');
330
-        $response = $archangel->send();
331
-
332
-        $expectedResponse = array(
333
-            'to' => '[email protected]',
334
-            'subject' => 'Test Subject',
335
-            'message' => 'Plain text message',
336
-            'headers' => 'X-Mailer: PHP/6.0.0',
337
-        );
338
-
339
-        $this->assertEquals($expectedResponse, $response);
340
-    }
341
-
342
-    public function testSendFailure()
343
-    {
344
-        $archangel = new Archangel();
345
-        $response = $archangel->send();
346
-
347
-        $this->assertFalse($response);
348
-    }
349
-
350
-    /**
351
-     * @dataProvider dataCheckRequiredFields
352
-     */
353
-    public function testCheckRequiredFields(
354
-        $expectedResult,
355
-        $toAddresses,
356
-        $subject,
357
-        $plainMessage,
358
-        $htmlMessage,
359
-        $attachments
360
-    ) {
361
-        $archangel = new Archangel();
362
-
363
-        if (!empty($toAddresses)) {
364
-            $toAddressesProperty = $this->getProtectedProperty('toAddresses');
365
-            $toAddressesProperty->setValue($archangel, $toAddresses);
366
-        }
367
-
368
-        if (!empty($subject)) {
369
-            $subjectProperty = $this->getProtectedProperty('subject');
370
-            $subjectProperty->setValue($archangel, $subject);
371
-        }
372
-
373
-        if (!empty($plainMessage)) {
374
-            $plainMessageProperty = $this->getProtectedProperty('plainMessage');
375
-            $plainMessageProperty->setValue($archangel, $plainMessage);
376
-        }
377
-
378
-        if (!empty($htmlMessage)) {
379
-            $htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
380
-            $htmlMessageProperty->setValue($archangel, $htmlMessage);
381
-        }
382
-
383
-        if (!empty($attachments)) {
384
-            $attachmentsProperty = $this->getProtectedProperty('attachments');
385
-            $attachmentsProperty->setValue($archangel, $attachments);
386
-        }
387
-
388
-        $checkMethod = $this->getProtectedMethod('checkRequiredFields');
389
-        $isValid = $checkMethod->invoke($archangel);
390
-
391
-        if ($expectedResult == true) {
392
-            $this->assertTrue($isValid);
393
-            return;
394
-        }
395
-        $this->assertNotTrue($isValid);
396
-    }
397
-
398
-    public function dataCheckRequiredFields()
399
-    {
400
-        return array(
401
-            array(
402
-                'expectedResult' => false,
403
-                'toAddresses' => array(),
404
-                'subject' => '',
405
-                'plainMessage' => '',
406
-                'htmlMessage' => '',
407
-                'attachments' => array(),
408
-            ),
409
-            array(
410
-                'expectedResult' => false,
411
-                'toAddresses' => array('[email protected]'),
412
-                'subject' => '',
413
-                'plainMessage' => '',
414
-                'htmlMessage' => '',
415
-                'attachments' => array(),
416
-            ),
417
-            array(
418
-                'expectedResult' => false,
419
-                'toAddresses' => array('[email protected]'),
420
-                'subject' => 'Test Subject',
421
-                'plainMessage' => '',
422
-                'htmlMessage' => '',
423
-                'attachments' => array(),
424
-            ),
425
-            array(
426
-                'expectedResult' => false,
427
-                'toAddresses' => array(),
428
-                'subject' => 'Test Subject',
429
-                'plainMessage' => '',
430
-                'htmlMessage' => '',
431
-                'attachments' => array(),
432
-            ),
433
-            array(
434
-                'expectedResult' => false,
435
-                'toAddresses' => array(),
436
-                'subject' => 'Test Subject',
437
-                'plainMessage' => 'Plain text message',
438
-                'htmlMessage' => '',
439
-                'attachments' => array(),
440
-            ),
441
-            array(
442
-                'expectedResult' => true,
443
-                'toAddresses' => array('[email protected]'),
444
-                'subject' => 'Test Subject',
445
-                'plainMessage' => 'Plain text message',
446
-                'htmlMessage' => '',
447
-                'attachments' => array(),
448
-            ),
449
-            array(
450
-                'expectedResult' => true,
451
-                'toAddresses' => array('[email protected]'),
452
-                'subject' => 'Test Subject',
453
-                'plainMessage' => '',
454
-                'htmlMessage' => '<p>An HTML message.</p>',
455
-                'attachments' => array(),
456
-            ),
457
-            array(
458
-                'expectedResult' => true,
459
-                'toAddresses' => array('[email protected]'),
460
-                'subject' => 'Test Subject',
461
-                'plainMessage' => '',
462
-                'htmlMessage' => '',
463
-                'attachments' => array(
464
-                    array('path' => 'path', 'type' => 'type'),
465
-                ),
466
-            ),
467
-            array(
468
-                'expectedResult' => true,
469
-                'toAddresses' => array('[email protected]'),
470
-                'subject' => 'Test Subject',
471
-                'plainMessage' => 'Plain text message',
472
-                'htmlMessage' => '<p>An HTML message.</p>',
473
-                'attachments' => array(
474
-                    array('path' => 'path', 'type' => 'type'),
475
-                ),
476
-            ),
477
-       );
478
-    }
13
+	public function testIsInstanceOfArchangel()
14
+	{
15
+		$archangel = new Archangel();
16
+
17
+		$this->assertInstanceOf('Jacobemerick\Archangel\Archangel', $archangel);
18
+	}
19
+
20
+	public function testIsLoggerAwareInterface()
21
+	{
22
+		$archangel = new Archangel();
23
+
24
+		$this->assertInstanceOf('Psr\Log\LoggerAwareInterface', $archangel);
25
+	}
26
+
27
+	public function testConstructSetsDefaultMailer()
28
+	{
29
+		$archangel = new Archangel();
30
+		$mailer = sprintf('PHP/%s', phpversion());
31
+		$headers = array('X-Mailer' => $mailer);
32
+
33
+		$this->assertAttributeEquals($headers, 'headers', $archangel);
34
+	}
35
+
36
+	public function testConstructOverridesMailer()
37
+	{
38
+		$archangel = new Archangel('AwesomeMailer');
39
+		$headers = array('X-Mailer' => 'AwesomeMailer');
40
+
41
+		$this->assertAttributeEquals($headers, 'headers', $archangel);
42
+	}
43
+
44
+	public function testConstructSetsNullLogger()
45
+	{
46
+		$archangel = new Archangel();
47
+
48
+		$this->assertAttributeInstanceOf('Psr\Log\NullLogger', 'logger', $archangel);
49
+	}
50
+
51
+	public function testConstructSetsBoundaries()
52
+	{
53
+		$archangel = new Archangel();
54
+		$expectedBoundaryMixed = sprintf('PHP-mixed-%s', uniqid());
55
+		$expectedBoundaryAlternative = sprintf('PHP-alternative-%s', uniqid());
56
+
57
+		$this->assertAttributeEquals($expectedBoundaryMixed, 'boundaryMixed', $archangel);
58
+		$this->assertAttributeEquals($expectedBoundaryAlternative, 'boundaryAlternative', $archangel);
59
+	}
60
+
61
+	public function testSetLogger()
62
+	{
63
+		$logger = $this->getMock('Psr\Log\LoggerInterface');
64
+		$archangel = new Archangel();
65
+		$archangel->setLogger($logger);
66
+
67
+		$this->assertAttributeSame($logger, 'logger', $archangel);
68
+	}
69
+
70
+	public function testAddTo()
71
+	{
72
+		$archangel = new Archangel();
73
+		$archangel->addTo('[email protected]');
74
+
75
+		$this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
76
+	}
77
+
78
+	public function testAddToMultiple()
79
+	{
80
+		$archangel = new Archangel();
81
+		$archangel->addTo('[email protected]');
82
+		$archangel->addTo('[email protected]');
83
+
84
+		$this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
85
+		$this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
86
+	}
87
+
88
+	public function testAddToWithTitle()
89
+	{
90
+		$archangel = new Archangel();
91
+		$archangel->addTo('[email protected]', 'Mr. Test Alot');
92
+
93
+		$this->assertAttributeContains('"Mr. Test Alot" <[email protected]>', 'toAddresses', $archangel);
94
+	}
95
+
96
+	public function testAddCC()
97
+	{
98
+		$archangel = new Archangel();
99
+		$archangel->addCC('[email protected]');
100
+		$headersProperty = $this->getProtectedProperty('headers');
101
+		$headers = $headersProperty->getValue($archangel);
102
+
103
+		$this->assertArraySubset(
104
+			array('CC' => array('[email protected]')),
105
+			$headers
106
+		);
107
+	}
108
+
109
+	public function testAddCCMultiple()
110
+	{
111
+		$archangel = new Archangel();
112
+		$archangel->addCC('[email protected]');
113
+		$archangel->addCC('[email protected]');
114
+		$headersProperty = $this->getProtectedProperty('headers');
115
+		$headers = $headersProperty->getValue($archangel);
116
+
117
+		$this->assertArraySubset(
118
+			array('CC' => array('[email protected]', '[email protected]')),
119
+			$headers
120
+		);
121
+	}
122
+
123
+	public function testAddCCWithTitle()
124
+	{
125
+		$archangel = new Archangel();
126
+		$archangel->addCC('[email protected]', 'Mr. Test Alot');
127
+		$headersProperty = $this->getProtectedProperty('headers');
128
+		$headers = $headersProperty->getValue($archangel);
129
+
130
+		$this->assertArraySubset(
131
+			array('CC' => array('"Mr. Test Alot" <[email protected]>')),
132
+			$headers
133
+		);
134
+	}
135
+
136
+	public function testAddBCC()
137
+	{
138
+		$archangel = new Archangel();
139
+		$archangel->addBCC('[email protected]');
140
+		$headersProperty = $this->getProtectedProperty('headers');
141
+		$headers = $headersProperty->getValue($archangel);
142
+
143
+		$this->assertArraySubset(
144
+			array('BCC' => array('[email protected]')),
145
+			$headers
146
+		);
147
+	}
148
+
149
+	public function testAddBCCMultiple()
150
+	{
151
+		$archangel = new Archangel();
152
+		$archangel->addBCC('[email protected]');
153
+		$archangel->addBCC('[email protected]');
154
+		$headersProperty = $this->getProtectedProperty('headers');
155
+		$headers = $headersProperty->getValue($archangel);
156
+
157
+		$this->assertArraySubset(
158
+			array('BCC' => array('[email protected]', '[email protected]')),
159
+			$headers
160
+		);
161
+	}
162
+
163
+	public function testAddBCCWithTitle()
164
+	{
165
+		$archangel = new Archangel();
166
+		$archangel->addBCC('[email protected]', 'Mr. Test Alot');
167
+		$headersProperty = $this->getProtectedProperty('headers');
168
+		$headers = $headersProperty->getValue($archangel);
169
+
170
+		$this->assertArraySubset(
171
+			array('BCC' => array('"Mr. Test Alot" <[email protected]>')),
172
+			$headers
173
+		);
174
+	}
175
+
176
+	public function testSetFrom()
177
+	{
178
+		$archangel = new Archangel();
179
+		$archangel->setFrom('[email protected]');
180
+		$headersProperty = $this->getProtectedProperty('headers');
181
+		$headers = $headersProperty->getValue($archangel);
182
+
183
+		$this->assertArraySubset(array('From' => '[email protected]'), $headers);
184
+	}
185
+
186
+	public function testSetFromMultiple()
187
+	{
188
+		$archangel = new Archangel();
189
+		$archangel->setFrom('[email protected]');
190
+		$archangel->setFrom('[email protected]');
191
+		$headersProperty = $this->getProtectedProperty('headers');
192
+		$headers = $headersProperty->getValue($archangel);
193
+
194
+		$this->assertArraySubset(array('From' => '[email protected]'), $headers);
195
+		$this->assertNotContains('[email protected]', $headers);
196
+	}
197
+
198
+	public function testSetFromWithTitle()
199
+	{
200
+		$archangel = new Archangel();
201
+		$archangel->setFrom('[email protected]', 'Mr. Test Alot');
202
+		$headersProperty = $this->getProtectedProperty('headers');
203
+		$headers = $headersProperty->getValue($archangel);
204
+
205
+		$this->assertArraySubset(array('From' => '"Mr. Test Alot" <[email protected]>'), $headers);
206
+	}
207
+
208
+	public function testSetReplyTo()
209
+	{
210
+		$archangel = new Archangel();
211
+		$archangel->setReplyTo('[email protected]');
212
+		$headersProperty = $this->getProtectedProperty('headers');
213
+		$headers = $headersProperty->getValue($archangel);
214
+
215
+		$this->assertArraySubset(array('Reply-To' => '[email protected]'), $headers);
216
+	}
217
+
218
+	public function testSetReplyToMultiple()
219
+	{
220
+		$archangel = new Archangel();
221
+		$archangel->setReplyTo('[email protected]');
222
+		$archangel->setReplyTo('[email protected]');
223
+		$headersProperty = $this->getProtectedProperty('headers');
224
+		$headers = $headersProperty->getValue($archangel);
225
+
226
+		$this->assertArraySubset(array('Reply-To' => '[email protected]'), $headers);
227
+		$this->assertNotContains('[email protected]', $headers);
228
+	}
229
+
230
+	public function testSetReplyToWithTitle()
231
+	{
232
+		$archangel = new Archangel();
233
+		$archangel->setReplyTo('[email protected]', 'Mr. Test Alot');
234
+		$headersProperty = $this->getProtectedProperty('headers');
235
+		$headers = $headersProperty->getValue($archangel);
236
+
237
+		$this->assertArraySubset(array('Reply-To' => '"Mr. Test Alot" <[email protected]>'), $headers);
238
+	}
239
+
240
+	public function testFormatEmailAddress()
241
+	{
242
+		$archangel = new Archangel();
243
+		$formatMethod = $this->getProtectedMethod('formatEmailAddress');
244
+		$formattedEmail = $formatMethod->invokeArgs($archangel, array('[email protected]', ''));
245
+
246
+		$this->assertEquals('[email protected]', $formattedEmail);
247
+	}
248
+
249
+	public function testFormatEmailAddressWithTitle()
250
+	{
251
+		$archangel = new Archangel();
252
+		$formatMethod = $this->getProtectedMethod('formatEmailAddress');
253
+		$formattedEmail = $formatMethod->invokeArgs($archangel, array('[email protected]', 'Mr. Test Alot'));
254
+
255
+		$this->assertEquals('"Mr. Test Alot" <[email protected]>', $formattedEmail);
256
+	}
257
+
258
+	public function testSetSubject()
259
+	{
260
+		$archangel = new Archangel();
261
+		$archangel->setSubject('Test Subject');
262
+
263
+		$this->assertAttributeEquals('Test Subject', 'subject', $archangel);
264
+	}
265
+
266
+	public function testSetPlainMessage()
267
+	{
268
+		$archangel = new Archangel();
269
+		$archangel->setPlainMessage('Plain text message');
270
+
271
+		$this->assertAttributeEquals('Plain text message', 'plainMessage', $archangel);
272
+	}
273
+
274
+	public function testSetHTMLMessage()
275
+	{
276
+		$archangel = new Archangel();
277
+		$archangel->setHTMLMessage('<p>An HTML message.</p>');
278
+
279
+		$this->assertAttributeEquals('<p>An HTML message.</p>', 'htmlMessage', $archangel);
280
+	}
281
+
282
+	public function testAddAttachment()
283
+	{
284
+		$archangel = new Archangel();
285
+		$archangel->addAttachment('path', 'type');
286
+
287
+		$this->assertAttributeContains(
288
+			array('path' => 'path', 'type' => 'type', 'title' => ''),
289
+			'attachments',
290
+			$archangel
291
+		);
292
+	}
293
+
294
+	public function testAddAttachmentMultiple()
295
+	{
296
+		$archangel = new Archangel();
297
+		$archangel->addAttachment('pathOne', 'typeOne');
298
+		$archangel->addAttachment('pathTwo', 'typeTwo');
299
+
300
+		$this->assertAttributeContains(
301
+			array('path' => 'pathOne', 'type' => 'typeOne', 'title' => ''),
302
+			'attachments',
303
+			$archangel
304
+		);
305
+		$this->assertAttributeContains(
306
+			array('path' => 'pathTwo', 'type' => 'typeTwo', 'title' => ''),
307
+			'attachments',
308
+			$archangel
309
+		);
310
+	}
311
+
312
+	public function testAddAttachmentWithTitle()
313
+	{
314
+		$archangel = new Archangel();
315
+		$archangel->addAttachment('path', 'type', 'title');
316
+
317
+		$this->assertAttributeContains(
318
+			array('path' => 'path', 'type' => 'type', 'title' => 'title'),
319
+			'attachments',
320
+			$archangel
321
+		);
322
+	}
323
+
324
+	public function testSend()
325
+	{
326
+		$archangel = new Archangel();
327
+		$archangel->addTo('[email protected]');
328
+		$archangel->setSubject('Test Subject');
329
+		$archangel->setPlainMessage('Plain text message');
330
+		$response = $archangel->send();
331
+
332
+		$expectedResponse = array(
333
+			'to' => '[email protected]',
334
+			'subject' => 'Test Subject',
335
+			'message' => 'Plain text message',
336
+			'headers' => 'X-Mailer: PHP/6.0.0',
337
+		);
338
+
339
+		$this->assertEquals($expectedResponse, $response);
340
+	}
341
+
342
+	public function testSendFailure()
343
+	{
344
+		$archangel = new Archangel();
345
+		$response = $archangel->send();
346
+
347
+		$this->assertFalse($response);
348
+	}
349
+
350
+	/**
351
+	 * @dataProvider dataCheckRequiredFields
352
+	 */
353
+	public function testCheckRequiredFields(
354
+		$expectedResult,
355
+		$toAddresses,
356
+		$subject,
357
+		$plainMessage,
358
+		$htmlMessage,
359
+		$attachments
360
+	) {
361
+		$archangel = new Archangel();
362
+
363
+		if (!empty($toAddresses)) {
364
+			$toAddressesProperty = $this->getProtectedProperty('toAddresses');
365
+			$toAddressesProperty->setValue($archangel, $toAddresses);
366
+		}
367
+
368
+		if (!empty($subject)) {
369
+			$subjectProperty = $this->getProtectedProperty('subject');
370
+			$subjectProperty->setValue($archangel, $subject);
371
+		}
372
+
373
+		if (!empty($plainMessage)) {
374
+			$plainMessageProperty = $this->getProtectedProperty('plainMessage');
375
+			$plainMessageProperty->setValue($archangel, $plainMessage);
376
+		}
377
+
378
+		if (!empty($htmlMessage)) {
379
+			$htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
380
+			$htmlMessageProperty->setValue($archangel, $htmlMessage);
381
+		}
382
+
383
+		if (!empty($attachments)) {
384
+			$attachmentsProperty = $this->getProtectedProperty('attachments');
385
+			$attachmentsProperty->setValue($archangel, $attachments);
386
+		}
387
+
388
+		$checkMethod = $this->getProtectedMethod('checkRequiredFields');
389
+		$isValid = $checkMethod->invoke($archangel);
390
+
391
+		if ($expectedResult == true) {
392
+			$this->assertTrue($isValid);
393
+			return;
394
+		}
395
+		$this->assertNotTrue($isValid);
396
+	}
397
+
398
+	public function dataCheckRequiredFields()
399
+	{
400
+		return array(
401
+			array(
402
+				'expectedResult' => false,
403
+				'toAddresses' => array(),
404
+				'subject' => '',
405
+				'plainMessage' => '',
406
+				'htmlMessage' => '',
407
+				'attachments' => array(),
408
+			),
409
+			array(
410
+				'expectedResult' => false,
411
+				'toAddresses' => array('[email protected]'),
412
+				'subject' => '',
413
+				'plainMessage' => '',
414
+				'htmlMessage' => '',
415
+				'attachments' => array(),
416
+			),
417
+			array(
418
+				'expectedResult' => false,
419
+				'toAddresses' => array('[email protected]'),
420
+				'subject' => 'Test Subject',
421
+				'plainMessage' => '',
422
+				'htmlMessage' => '',
423
+				'attachments' => array(),
424
+			),
425
+			array(
426
+				'expectedResult' => false,
427
+				'toAddresses' => array(),
428
+				'subject' => 'Test Subject',
429
+				'plainMessage' => '',
430
+				'htmlMessage' => '',
431
+				'attachments' => array(),
432
+			),
433
+			array(
434
+				'expectedResult' => false,
435
+				'toAddresses' => array(),
436
+				'subject' => 'Test Subject',
437
+				'plainMessage' => 'Plain text message',
438
+				'htmlMessage' => '',
439
+				'attachments' => array(),
440
+			),
441
+			array(
442
+				'expectedResult' => true,
443
+				'toAddresses' => array('[email protected]'),
444
+				'subject' => 'Test Subject',
445
+				'plainMessage' => 'Plain text message',
446
+				'htmlMessage' => '',
447
+				'attachments' => array(),
448
+			),
449
+			array(
450
+				'expectedResult' => true,
451
+				'toAddresses' => array('[email protected]'),
452
+				'subject' => 'Test Subject',
453
+				'plainMessage' => '',
454
+				'htmlMessage' => '<p>An HTML message.</p>',
455
+				'attachments' => array(),
456
+			),
457
+			array(
458
+				'expectedResult' => true,
459
+				'toAddresses' => array('[email protected]'),
460
+				'subject' => 'Test Subject',
461
+				'plainMessage' => '',
462
+				'htmlMessage' => '',
463
+				'attachments' => array(
464
+					array('path' => 'path', 'type' => 'type'),
465
+				),
466
+			),
467
+			array(
468
+				'expectedResult' => true,
469
+				'toAddresses' => array('[email protected]'),
470
+				'subject' => 'Test Subject',
471
+				'plainMessage' => 'Plain text message',
472
+				'htmlMessage' => '<p>An HTML message.</p>',
473
+				'attachments' => array(
474
+					array('path' => 'path', 'type' => 'type'),
475
+				),
476
+			),
477
+	   );
478
+	}
479 479
  
480
-    public function testBuildTo()
481
-    {
482
-        $archangel = new Archangel();
483
-        $addressesProperty = $this->getProtectedProperty('toAddresses');
484
-        $addressesProperty->setValue($archangel, array('[email protected]'));
485
-        $buildMethod = $this->getProtectedMethod('buildTo');
486
-        $toAddresses = $buildMethod->invoke($archangel);
487
-
488
-        $this->assertEquals('[email protected]', $toAddresses);
489
-    }
490
-
491
-    public function testBuildToMultiple()
492
-    {
493
-        $archangel = new Archangel();
494
-        $addressesProperty = $this->getProtectedProperty('toAddresses');
495
-        $addressesProperty->setValue($archangel, array('[email protected]', '[email protected]'));
496
-        $buildMethod = $this->getProtectedMethod('buildTo');
497
-        $toAddresses = $buildMethod->invoke($archangel);
498
-
499
-        $this->assertEquals('[email protected], [email protected]', $toAddresses);
500
-    }
501
-
502
-    public function testBuildMessageEmpty()
503
-    {
504
-        $archangel = new Archangel();
505
-        $plainMessageProperty = $this->getProtectedProperty('plainMessage');
506
-        $plainMessageProperty->setValue($archangel, '');
507
-        $htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
508
-        $htmlMessageProperty->setValue($archangel, '');
509
-        $buildMethod = $this->getProtectedMethod('buildMessage');
510
-        $builtMessage = $buildMethod->invoke($archangel);
511
-
512
-        $this->assertEmpty($builtMessage);
513
-    }
514
-
515
-    public function testBuildMessagePlain()
516
-    {
517
-        $message = 'Plain text message';
518
-
519
-        $archangel = new Archangel();
520
-        $plainMessageProperty = $this->getProtectedProperty('plainMessage');
521
-        $plainMessageProperty->setValue($archangel, $message);
522
-        $htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
523
-        $htmlMessageProperty->setValue($archangel, '');
524
-        $buildMethod = $this->getProtectedMethod('buildMessage');
525
-        $builtMessage = $buildMethod->invoke($archangel);
526
-
527
-        $this->assertEquals($message, $builtMessage);
528
-    }
529
-
530
-    public function testBuildMessageHtml()
531
-    {
532
-        $message = '<p>HTML Message.</p>';
533
-
534
-        $archangel = new Archangel();
535
-        $plainMessageProperty = $this->getProtectedProperty('plainMessage');
536
-        $plainMessageProperty->setValue($archangel, '');
537
-        $htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
538
-        $htmlMessageProperty->setValue($archangel, $message);
539
-        $buildMethod = $this->getProtectedMethod('buildMessage');
540
-        $builtMessage = $buildMethod->invoke($archangel);
541
-
542
-        $this->assertEquals($message, $builtMessage);
543
-    }
544
-
545
-    public function testBuildMessageMultipart()
546
-    {
547
-        $plainMessage = 'Plain text message';
548
-        $htmlMessage = '<p>HTML Message.</p>';
549
-
550
-        $archangel = new Archangel();
551
-
552
-        $boundaryProperty = $this->getProtectedProperty('boundaryAlternative');
553
-        $boundary = $boundaryProperty->getValue($archangel);
554
-        $plainMsgMethod = $this->getProtectedMethod('buildPlainMessageHeader');
555
-        $plainMsgHeaders = $plainMsgMethod->invoke($archangel);
556
-        $htmlMsgMethod = $this->getProtectedMethod('buildHtmlMessageHeader');
557
-        $htmlMsgHeaders = $htmlMsgMethod->invoke($archangel);
558
-
559
-        $expectedMessage = array();
560
-        array_push($expectedMessage, "--{$boundary}");
561
-        $expectedMessage = array_merge($expectedMessage, $plainMsgHeaders);
562
-        array_push($expectedMessage, $plainMessage);
563
-        array_push($expectedMessage, "--{$boundary}");
564
-        $expectedMessage = array_merge($expectedMessage, $htmlMsgHeaders);
565
-        array_push($expectedMessage, $htmlMessage);
566
-        array_push($expectedMessage, "--{$boundary}--");
567
-        $expectedMessage = implode(Archangel::LINE_BREAK, $expectedMessage);
568
-
569
-        $plainMessageProperty = $this->getProtectedProperty('plainMessage');
570
-        $plainMessageProperty->setValue($archangel, $plainMessage);
571
-        $htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
572
-        $htmlMessageProperty->setValue($archangel, $htmlMessage);
573
-        $buildMethod = $this->getProtectedMethod('buildMessage');
574
-        $builtMessage = $buildMethod->invoke($archangel);
575
-
576
-        $this->assertEquals($expectedMessage, $builtMessage);
577
-    }
578
-
579
-    public function testBuildMessageWithAttachmentsEmpty()
580
-    {
581
-        $path = __DIR__ . '/test.txt';
582
-        $textContent = 'Dummy Content';
583
-        $this->makeTmpFile($path, $textContent);
584
-
585
-        $encodedContent = chunk_split(base64_encode($textContent));
586
-        $type = 'text/plain';
587
-        $title = 'Test File';
588
-
589
-        $archangel = new Archangel();
590
-
591
-        $boundaryMixedProperty = $this->getProtectedProperty('boundaryMixed');
592
-        $boundaryMixed = $boundaryMixedProperty->getValue($archangel);
593
-
594
-        $expectedMessage = array();
595
-        array_push($expectedMessage, "--{$boundaryMixed}");
596
-        array_push($expectedMessage, "Content-Type: {$type}; name=\"{$title}\"");
597
-        array_push($expectedMessage, 'Content-Transfer-Encoding: base64');
598
-        array_push($expectedMessage, 'Content-Disposition: attachment');
599
-        array_push($expectedMessage, '');
600
-        array_push($expectedMessage, $encodedContent);
601
-        array_push($expectedMessage, "--{$boundaryMixed}--");
602
-        $expectedMessage = implode(Archangel::LINE_BREAK, $expectedMessage);
603
-
604
-        $plainMessageProperty = $this->getProtectedProperty('plainMessage');
605
-        $plainMessageProperty->setValue($archangel, '');
606
-        $htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
607
-        $htmlMessageProperty->setValue($archangel, '');
608
-        $attachmentProperty = $this->getProtectedProperty('attachments');
609
-        $attachmentProperty->setValue($archangel, array(
610
-            array('path' => $path, 'type' => $type, 'title' => $title)
611
-        ));
612
-        $buildMethod = $this->getProtectedMethod('buildMessageWithAttachments');
613
-        $builtMessage = $buildMethod->invoke($archangel);
614
-
615
-        unlink($path);
616
-        $this->assertEquals($expectedMessage, $builtMessage);
617
-    }
618
-
619
-    public function testBuildMessageWithAttachmentsPlain()
620
-    {
621
-        $path = __DIR__ . '/test.txt';
622
-        $textContent = 'Dummy Content';
623
-        $this->makeTmpFile($path, $textContent);
624
-
625
-        $encodedContent = chunk_split(base64_encode($textContent));
626
-        $type = 'text/plain';
627
-        $title = 'Test File';
628
-        $message = 'Plain text message';
629
-
630
-        $archangel = new Archangel();
631
-
632
-        $boundaryMixedProperty = $this->getProtectedProperty('boundaryMixed');
633
-        $boundaryMixed = $boundaryMixedProperty->getValue($archangel);
634
-        $plainMsgMethod = $this->getProtectedMethod('buildPlainMessageHeader');
635
-        $plainMsgHeaders = $plainMsgMethod->invoke($archangel);
636
-
637
-        $expectedMessage = array();
638
-        array_push($expectedMessage, "--{$boundaryMixed}");
639
-        $expectedMessage = array_merge($expectedMessage, $plainMsgHeaders);
640
-        array_push($expectedMessage, $message);
641
-        array_push($expectedMessage, "--{$boundaryMixed}");
642
-        array_push($expectedMessage, "Content-Type: {$type}; name=\"{$title}\"");
643
-        array_push($expectedMessage, 'Content-Transfer-Encoding: base64');
644
-        array_push($expectedMessage, 'Content-Disposition: attachment');
645
-        array_push($expectedMessage, '');
646
-        array_push($expectedMessage, $encodedContent);
647
-        array_push($expectedMessage, "--{$boundaryMixed}--");
648
-        $expectedMessage = implode(Archangel::LINE_BREAK, $expectedMessage);
649
-
650
-        $plainMessageProperty = $this->getProtectedProperty('plainMessage');
651
-        $plainMessageProperty->setValue($archangel, $message);
652
-        $htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
653
-        $htmlMessageProperty->setValue($archangel, '');
654
-        $attachmentProperty = $this->getProtectedProperty('attachments');
655
-        $attachmentProperty->setValue($archangel, array(
656
-            array('path' => $path, 'type' => $type, 'title' => $title)
657
-        ));
658
-        $buildMethod = $this->getProtectedMethod('buildMessageWithAttachments');
659
-        $builtMessage = $buildMethod->invoke($archangel);
660
-
661
-        unlink($path);
662
-        $this->assertEquals($expectedMessage, $builtMessage);
663
-    }
664
-
665
-    public function testBuildMessageWithAttachmentsHtml()
666
-    {
667
-        $path = __DIR__ . '/test.txt';
668
-        $textContent = 'Dummy Content';
669
-        $this->makeTmpFile($path, $textContent);
670
-
671
-        $encodedContent = chunk_split(base64_encode($textContent));
672
-        $type = 'text/plain';
673
-        $title = 'Test File';
674
-        $message = '<p>HTML Message.</p>';
675
-
676
-        $archangel = new Archangel();
677
-
678
-        $boundaryMixedProperty = $this->getProtectedProperty('boundaryMixed');
679
-        $boundaryMixed = $boundaryMixedProperty->getValue($archangel);
680
-        $htmlMsgMethod = $this->getProtectedMethod('buildHtmlMessageHeader');
681
-        $htmlMsgHeaders = $htmlMsgMethod->invoke($archangel);
682
-
683
-        $expectedMessage = array();
684
-        array_push($expectedMessage, "--{$boundaryMixed}");
685
-        $expectedMessage = array_merge($expectedMessage, $htmlMsgHeaders);
686
-        array_push($expectedMessage, $message);
687
-        array_push($expectedMessage, "--{$boundaryMixed}");
688
-        array_push($expectedMessage, "Content-Type: {$type}; name=\"{$title}\"");
689
-        array_push($expectedMessage, 'Content-Transfer-Encoding: base64');
690
-        array_push($expectedMessage, 'Content-Disposition: attachment');
691
-        array_push($expectedMessage, '');
692
-        array_push($expectedMessage, $encodedContent);
693
-        array_push($expectedMessage, "--{$boundaryMixed}--");
694
-        $expectedMessage = implode(Archangel::LINE_BREAK, $expectedMessage);
695
-
696
-        $plainMessageProperty = $this->getProtectedProperty('plainMessage');
697
-        $plainMessageProperty->setValue($archangel, '');
698
-        $htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
699
-        $htmlMessageProperty->setValue($archangel, $message);
700
-        $attachmentProperty = $this->getProtectedProperty('attachments');
701
-        $attachmentProperty->setValue($archangel, array(
702
-            array('path' => $path, 'type' => $type, 'title' => $title)
703
-        ));
704
-        $buildMethod = $this->getProtectedMethod('buildMessageWithAttachments');
705
-        $builtMessage = $buildMethod->invoke($archangel);
706
-
707
-        unlink($path);
708
-        $this->assertEquals($expectedMessage, $builtMessage);
709
-    }
710
-
711
-    public function testBuildMessageWithAttachmentsMultipart()
712
-    {
713
-        $path = __DIR__ . '/test.txt';
714
-        $textContent = 'Dummy Content';
715
-        $this->makeTmpFile($path, $textContent);
716
-
717
-        $encodedContent = chunk_split(base64_encode($textContent));
718
-        $type = 'text/plain';
719
-        $title = 'Test File';
720
-        $plainMessage = 'Plain text message';
721
-        $htmlMessage = '<p>HTML Message.</p>';
722
-
723
-        $archangel = new Archangel();
724
-
725
-        $boundaryMixedProperty = $this->getProtectedProperty('boundaryMixed');
726
-        $boundaryMixed = $boundaryMixedProperty->getValue($archangel);
727
-        $boundaryAltProperty = $this->getProtectedProperty('boundaryAlternative');
728
-        $boundaryAlternative = $boundaryAltProperty->getValue($archangel);
729
-        $plainMsgMethod = $this->getProtectedMethod('buildPlainMessageHeader');
730
-        $plainMsgHeaders = $plainMsgMethod->invoke($archangel);
731
-        $htmlMsgMethod = $this->getProtectedMethod('buildHtmlMessageHeader');
732
-        $htmlMsgHeaders = $htmlMsgMethod->invoke($archangel);
733
-
734
-        $expectedMessage = array();
735
-        array_push($expectedMessage, "--{$boundaryMixed}");
736
-        array_push($expectedMessage, "Content-Type: multipart/alternative; boundary={$boundaryAlternative}");
737
-        array_push($expectedMessage, '');
738
-        array_push($expectedMessage, "--{$boundaryAlternative}");
739
-        $expectedMessage = array_merge($expectedMessage, $plainMsgHeaders);
740
-        array_push($expectedMessage, $plainMessage);
741
-        array_push($expectedMessage, "--{$boundaryAlternative}");
742
-        $expectedMessage = array_merge($expectedMessage, $htmlMsgHeaders);
743
-        array_push($expectedMessage, $htmlMessage);
744
-        array_push($expectedMessage, "--{$boundaryAlternative}--");
745
-        array_push($expectedMessage, '');
746
-        array_push($expectedMessage, "--{$boundaryMixed}");
747
-        array_push($expectedMessage, "Content-Type: {$type}; name=\"{$title}\"");
748
-        array_push($expectedMessage, 'Content-Transfer-Encoding: base64');
749
-        array_push($expectedMessage, 'Content-Disposition: attachment');
750
-        array_push($expectedMessage, '');
751
-        array_push($expectedMessage, $encodedContent);
752
-        array_push($expectedMessage, "--{$boundaryMixed}--");
753
-        $expectedMessage = implode(Archangel::LINE_BREAK, $expectedMessage);
754
-
755
-        $plainMessageProperty = $this->getProtectedProperty('plainMessage');
756
-        $plainMessageProperty->setValue($archangel, $plainMessage);
757
-        $htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
758
-        $htmlMessageProperty->setValue($archangel, $htmlMessage);
759
-        $attachmentProperty = $this->getProtectedProperty('attachments');
760
-        $attachmentProperty->setValue($archangel, array(
761
-            array('path' => $path, 'type' => $type, 'title' => $title)
762
-        ));
763
-        $buildMethod = $this->getProtectedMethod('buildMessageWithAttachments');
764
-        $builtMessage = $buildMethod->invoke($archangel);
765
-
766
-        unlink($path);
767
-        $this->assertEquals($expectedMessage, $builtMessage);
768
-    }
769
-
770
-    public function testBuildPlainMessageHeader()
771
-    {
772
-        $expectedMessageHeader = array(
773
-            'Content-Type: text/plain; charset="iso-8859"',
774
-            'Content-Transfer-Encoding: 7bit',
775
-            '',
776
-        );
777
-
778
-        $archangel = new Archangel();
779
-        $buildMethod = $this->getProtectedMethod('buildPlainMessageHeader');
780
-        $messageHeader = $buildMethod->invoke($archangel);
781
-
782
-        $this->assertEquals($expectedMessageHeader, $messageHeader);
783
-    }
784
-
785
-    public function testBuildHtmlMessageHeader()
786
-    {
787
-        $expectedMessageHeader = array(
788
-            'Content-Type: text/html; charset="iso-8859-1"',
789
-            'Content-Transfer-Encoding: 7bit',
790
-            '',
791
-        );
792
-
793
-        $archangel = new Archangel();
794
-        $buildMethod = $this->getProtectedMethod('buildHtmlMessageHeader');
795
-        $messageHeader = $buildMethod->invoke($archangel);
796
-
797
-        $this->assertEquals($expectedMessageHeader, $messageHeader);
798
-    }
799
-
800
-    /**
801
-     * @dataProvider dataBuildHeaders
802
-     */
803
-    public function testBuildHeaders(
804
-        $expectedHeaders,
805
-        $headers,
806
-        $attachments,
807
-        $plainMessage,
808
-        $htmlMessage
809
-    ) {
810
-        $archangel = new Archangel();
811
-        $headersProperty = $this->getProtectedProperty('headers');
812
-        $headersProperty->setValue($archangel, $headers);
813
-
814
-        if (!empty($attachments)) {
815
-            $attachmentsProperty = $this->getProtectedProperty('attachments');
816
-            $attachmentsProperty->setValue($archangel, $attachments);
817
-        }
818
-
819
-        if (!empty($plainMessage)) {
820
-            $plainMessageProperty = $this->getProtectedProperty('plainMessage');
821
-            $plainMessageProperty->setValue($archangel, $plainMessage);
822
-        }
823
-
824
-        if (!empty($htmlMessage)) {
825
-            $htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
826
-            $htmlMessageProperty->setValue($archangel, $htmlMessage);
827
-        }
828
-
829
-        $buildHeadersMethod = $this->getProtectedMethod('buildHeaders');
830
-        $builtHeaders = $buildHeadersMethod->invoke($archangel);
831
-
832
-        $this->assertEquals($expectedHeaders, $builtHeaders);
833
-    }
834
-
835
-    public function dataBuildHeaders()
836
-    {
837
-        return array(
838
-            array(
839
-                'expectedHeaders' =>
840
-                    "From: [email protected]\r\n" .
841
-                    "X-Mailer: PHP/6.0.0",
842
-                'headers' => array(
843
-                    'From' => '[email protected]',
844
-                    'X-Mailer' => sprintf('PHP/%s', phpversion())
845
-                ),
846
-                'attachments' => null,
847
-                'plainMessage' => true,
848
-                'htmlMessage' => null,
849
-            ),
850
-            array(
851
-                'expectedHeaders' =>
852
-                    "CC: [email protected], [email protected]\r\n" .
853
-                    "From: [email protected]\r\n" .
854
-                    "X-Mailer: PHP/6.0.0",
855
-                'headers' => array(
856
-                    'CC' => array('[email protected]', '[email protected]'),
857
-                    'From' => '[email protected]',
858
-                    'X-Mailer' => sprintf('PHP/%s', phpversion())
859
-                ),
860
-                'attachments' => null,
861
-                'plainMessage' => true,
862
-                'htmlMessage' => null,
863
-            ),
864
-            array(
865
-                'expectedHeaders' =>
866
-                    "BCC: [email protected], [email protected]\r\n" .
867
-                    "From: [email protected]\r\n" .
868
-                    "X-Mailer: PHP/6.0.0",
869
-                'headers' => array(
870
-                    'BCC' => array('[email protected]', '[email protected]'),
871
-                    'From' => '[email protected]',
872
-                    'X-Mailer' => sprintf('PHP/%s', phpversion())
873
-                ),
874
-                'attachments' => null,
875
-                'plainMessage' => true,
876
-                'htmlMessage' => null,
877
-            ),
878
-            array(
879
-                'expectedHeaders' =>
880
-                    "From: [email protected]\r\n" .
881
-                    "X-Mailer: PHP/6.0.0\r\n" .
882
-                    "Content-Type: multipart/mixed; boundary=\"PHP-mixed-1234567890123\"",
883
-                'headers' => array(
884
-                    'From' => '[email protected]',
885
-                    'X-Mailer' => sprintf('PHP/%s', phpversion())
886
-                ),
887
-                'attachments' => true,
888
-                'plainMessage' => true,
889
-                'htmlMessage' => null,
890
-            ),
891
-            array(
892
-                'expectedHeaders' =>
893
-                    "From: [email protected]\r\n" .
894
-                    "X-Mailer: PHP/6.0.0\r\n" .
895
-                    "Content-Type: multipart/alternative; boundary=\"PHP-alternative-1234567890123\"",
896
-                'headers' => array(
897
-                    'From' => '[email protected]',
898
-                    'X-Mailer' => sprintf('PHP/%s', phpversion())
899
-                ),
900
-                'attachments' => null,
901
-                'plainMessage' => true,
902
-                'htmlMessage' => true,
903
-            ),
904
-            array(
905
-                'expectedHeaders' =>
906
-                    "From: [email protected]\r\n" .
907
-                    "X-Mailer: PHP/6.0.0\r\n" .
908
-                    "Content-type: text/html; charset=\"iso-8859-1\"",
909
-                'headers' => array(
910
-                    'From' => '[email protected]',
911
-                    'X-Mailer' => sprintf('PHP/%s', phpversion())
912
-                ),
913
-                'attachments' => null,
914
-                'plainMessage' => null,
915
-                'htmlMessage' => true,
916
-            ),
917
-        );
918
-    }
919
-
920
-    public function testBuildAttachmentContent()
921
-    {
922
-        $path = __DIR__ . '/test.txt';
923
-        $textContent = 'Dummy Content';
924
-        $this->makeTmpFile($path, $textContent);
925
-
926
-        $expectedContent = chunk_split(base64_encode($textContent));
927
-
928
-        $archangel = new Archangel();
929
-        $buildMethod = $this->getProtectedMethod('buildAttachmentContent');
930
-        $content = $buildMethod->invokeArgs($archangel, array($path));
931
-
932
-        unlink($path);
933
-        $this->assertEquals($expectedContent, $content);
934
-    }
935
-
936
-    public function testBuildAttachmentContentFailure()
937
-    {
938
-        $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
939
-
940
-        $archangel = new Archangel();
941
-        $archangel->setLogger($logger);
942
-        $buildMethod = $this->getProtectedMethod('buildAttachmentContent');
943
-        $content = $buildMethod->invokeArgs($archangel, array('INVALID_PATH'));
944
-
945
-        $this->assertEmpty($content);
946
-    }
947
-
948
-    protected function getProtectedProperty($property)
949
-    {
950
-        $reflectedArchangel = new ReflectionClass('Jacobemerick\Archangel\Archangel');
951
-        $reflectedProperty = $reflectedArchangel->getProperty($property);
952
-        $reflectedProperty->setAccessible(true);
953
-
954
-        return $reflectedProperty;
955
-    }
956
-
957
-    protected function getProtectedMethod($method)
958
-    {
959
-        $reflectedArchangel = new ReflectionClass('Jacobemerick\Archangel\Archangel');
960
-        $reflectedMethod = $reflectedArchangel->getMethod($method);
961
-        $reflectedMethod->setAccessible(true);
962
-
963
-        return $reflectedMethod;
964
-    }
965
-
966
-    protected function makeTmpFile($path, $content)
967
-    {
968
-        $handle = fopen($path, 'w');
969
-        fwrite($handle, $content);
970
-        fclose($handle);
971
-    }
480
+	public function testBuildTo()
481
+	{
482
+		$archangel = new Archangel();
483
+		$addressesProperty = $this->getProtectedProperty('toAddresses');
484
+		$addressesProperty->setValue($archangel, array('[email protected]'));
485
+		$buildMethod = $this->getProtectedMethod('buildTo');
486
+		$toAddresses = $buildMethod->invoke($archangel);
487
+
488
+		$this->assertEquals('[email protected]', $toAddresses);
489
+	}
490
+
491
+	public function testBuildToMultiple()
492
+	{
493
+		$archangel = new Archangel();
494
+		$addressesProperty = $this->getProtectedProperty('toAddresses');
495
+		$addressesProperty->setValue($archangel, array('[email protected]', '[email protected]'));
496
+		$buildMethod = $this->getProtectedMethod('buildTo');
497
+		$toAddresses = $buildMethod->invoke($archangel);
498
+
499
+		$this->assertEquals('[email protected], [email protected]', $toAddresses);
500
+	}
501
+
502
+	public function testBuildMessageEmpty()
503
+	{
504
+		$archangel = new Archangel();
505
+		$plainMessageProperty = $this->getProtectedProperty('plainMessage');
506
+		$plainMessageProperty->setValue($archangel, '');
507
+		$htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
508
+		$htmlMessageProperty->setValue($archangel, '');
509
+		$buildMethod = $this->getProtectedMethod('buildMessage');
510
+		$builtMessage = $buildMethod->invoke($archangel);
511
+
512
+		$this->assertEmpty($builtMessage);
513
+	}
514
+
515
+	public function testBuildMessagePlain()
516
+	{
517
+		$message = 'Plain text message';
518
+
519
+		$archangel = new Archangel();
520
+		$plainMessageProperty = $this->getProtectedProperty('plainMessage');
521
+		$plainMessageProperty->setValue($archangel, $message);
522
+		$htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
523
+		$htmlMessageProperty->setValue($archangel, '');
524
+		$buildMethod = $this->getProtectedMethod('buildMessage');
525
+		$builtMessage = $buildMethod->invoke($archangel);
526
+
527
+		$this->assertEquals($message, $builtMessage);
528
+	}
529
+
530
+	public function testBuildMessageHtml()
531
+	{
532
+		$message = '<p>HTML Message.</p>';
533
+
534
+		$archangel = new Archangel();
535
+		$plainMessageProperty = $this->getProtectedProperty('plainMessage');
536
+		$plainMessageProperty->setValue($archangel, '');
537
+		$htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
538
+		$htmlMessageProperty->setValue($archangel, $message);
539
+		$buildMethod = $this->getProtectedMethod('buildMessage');
540
+		$builtMessage = $buildMethod->invoke($archangel);
541
+
542
+		$this->assertEquals($message, $builtMessage);
543
+	}
544
+
545
+	public function testBuildMessageMultipart()
546
+	{
547
+		$plainMessage = 'Plain text message';
548
+		$htmlMessage = '<p>HTML Message.</p>';
549
+
550
+		$archangel = new Archangel();
551
+
552
+		$boundaryProperty = $this->getProtectedProperty('boundaryAlternative');
553
+		$boundary = $boundaryProperty->getValue($archangel);
554
+		$plainMsgMethod = $this->getProtectedMethod('buildPlainMessageHeader');
555
+		$plainMsgHeaders = $plainMsgMethod->invoke($archangel);
556
+		$htmlMsgMethod = $this->getProtectedMethod('buildHtmlMessageHeader');
557
+		$htmlMsgHeaders = $htmlMsgMethod->invoke($archangel);
558
+
559
+		$expectedMessage = array();
560
+		array_push($expectedMessage, "--{$boundary}");
561
+		$expectedMessage = array_merge($expectedMessage, $plainMsgHeaders);
562
+		array_push($expectedMessage, $plainMessage);
563
+		array_push($expectedMessage, "--{$boundary}");
564
+		$expectedMessage = array_merge($expectedMessage, $htmlMsgHeaders);
565
+		array_push($expectedMessage, $htmlMessage);
566
+		array_push($expectedMessage, "--{$boundary}--");
567
+		$expectedMessage = implode(Archangel::LINE_BREAK, $expectedMessage);
568
+
569
+		$plainMessageProperty = $this->getProtectedProperty('plainMessage');
570
+		$plainMessageProperty->setValue($archangel, $plainMessage);
571
+		$htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
572
+		$htmlMessageProperty->setValue($archangel, $htmlMessage);
573
+		$buildMethod = $this->getProtectedMethod('buildMessage');
574
+		$builtMessage = $buildMethod->invoke($archangel);
575
+
576
+		$this->assertEquals($expectedMessage, $builtMessage);
577
+	}
578
+
579
+	public function testBuildMessageWithAttachmentsEmpty()
580
+	{
581
+		$path = __DIR__ . '/test.txt';
582
+		$textContent = 'Dummy Content';
583
+		$this->makeTmpFile($path, $textContent);
584
+
585
+		$encodedContent = chunk_split(base64_encode($textContent));
586
+		$type = 'text/plain';
587
+		$title = 'Test File';
588
+
589
+		$archangel = new Archangel();
590
+
591
+		$boundaryMixedProperty = $this->getProtectedProperty('boundaryMixed');
592
+		$boundaryMixed = $boundaryMixedProperty->getValue($archangel);
593
+
594
+		$expectedMessage = array();
595
+		array_push($expectedMessage, "--{$boundaryMixed}");
596
+		array_push($expectedMessage, "Content-Type: {$type}; name=\"{$title}\"");
597
+		array_push($expectedMessage, 'Content-Transfer-Encoding: base64');
598
+		array_push($expectedMessage, 'Content-Disposition: attachment');
599
+		array_push($expectedMessage, '');
600
+		array_push($expectedMessage, $encodedContent);
601
+		array_push($expectedMessage, "--{$boundaryMixed}--");
602
+		$expectedMessage = implode(Archangel::LINE_BREAK, $expectedMessage);
603
+
604
+		$plainMessageProperty = $this->getProtectedProperty('plainMessage');
605
+		$plainMessageProperty->setValue($archangel, '');
606
+		$htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
607
+		$htmlMessageProperty->setValue($archangel, '');
608
+		$attachmentProperty = $this->getProtectedProperty('attachments');
609
+		$attachmentProperty->setValue($archangel, array(
610
+			array('path' => $path, 'type' => $type, 'title' => $title)
611
+		));
612
+		$buildMethod = $this->getProtectedMethod('buildMessageWithAttachments');
613
+		$builtMessage = $buildMethod->invoke($archangel);
614
+
615
+		unlink($path);
616
+		$this->assertEquals($expectedMessage, $builtMessage);
617
+	}
618
+
619
+	public function testBuildMessageWithAttachmentsPlain()
620
+	{
621
+		$path = __DIR__ . '/test.txt';
622
+		$textContent = 'Dummy Content';
623
+		$this->makeTmpFile($path, $textContent);
624
+
625
+		$encodedContent = chunk_split(base64_encode($textContent));
626
+		$type = 'text/plain';
627
+		$title = 'Test File';
628
+		$message = 'Plain text message';
629
+
630
+		$archangel = new Archangel();
631
+
632
+		$boundaryMixedProperty = $this->getProtectedProperty('boundaryMixed');
633
+		$boundaryMixed = $boundaryMixedProperty->getValue($archangel);
634
+		$plainMsgMethod = $this->getProtectedMethod('buildPlainMessageHeader');
635
+		$plainMsgHeaders = $plainMsgMethod->invoke($archangel);
636
+
637
+		$expectedMessage = array();
638
+		array_push($expectedMessage, "--{$boundaryMixed}");
639
+		$expectedMessage = array_merge($expectedMessage, $plainMsgHeaders);
640
+		array_push($expectedMessage, $message);
641
+		array_push($expectedMessage, "--{$boundaryMixed}");
642
+		array_push($expectedMessage, "Content-Type: {$type}; name=\"{$title}\"");
643
+		array_push($expectedMessage, 'Content-Transfer-Encoding: base64');
644
+		array_push($expectedMessage, 'Content-Disposition: attachment');
645
+		array_push($expectedMessage, '');
646
+		array_push($expectedMessage, $encodedContent);
647
+		array_push($expectedMessage, "--{$boundaryMixed}--");
648
+		$expectedMessage = implode(Archangel::LINE_BREAK, $expectedMessage);
649
+
650
+		$plainMessageProperty = $this->getProtectedProperty('plainMessage');
651
+		$plainMessageProperty->setValue($archangel, $message);
652
+		$htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
653
+		$htmlMessageProperty->setValue($archangel, '');
654
+		$attachmentProperty = $this->getProtectedProperty('attachments');
655
+		$attachmentProperty->setValue($archangel, array(
656
+			array('path' => $path, 'type' => $type, 'title' => $title)
657
+		));
658
+		$buildMethod = $this->getProtectedMethod('buildMessageWithAttachments');
659
+		$builtMessage = $buildMethod->invoke($archangel);
660
+
661
+		unlink($path);
662
+		$this->assertEquals($expectedMessage, $builtMessage);
663
+	}
664
+
665
+	public function testBuildMessageWithAttachmentsHtml()
666
+	{
667
+		$path = __DIR__ . '/test.txt';
668
+		$textContent = 'Dummy Content';
669
+		$this->makeTmpFile($path, $textContent);
670
+
671
+		$encodedContent = chunk_split(base64_encode($textContent));
672
+		$type = 'text/plain';
673
+		$title = 'Test File';
674
+		$message = '<p>HTML Message.</p>';
675
+
676
+		$archangel = new Archangel();
677
+
678
+		$boundaryMixedProperty = $this->getProtectedProperty('boundaryMixed');
679
+		$boundaryMixed = $boundaryMixedProperty->getValue($archangel);
680
+		$htmlMsgMethod = $this->getProtectedMethod('buildHtmlMessageHeader');
681
+		$htmlMsgHeaders = $htmlMsgMethod->invoke($archangel);
682
+
683
+		$expectedMessage = array();
684
+		array_push($expectedMessage, "--{$boundaryMixed}");
685
+		$expectedMessage = array_merge($expectedMessage, $htmlMsgHeaders);
686
+		array_push($expectedMessage, $message);
687
+		array_push($expectedMessage, "--{$boundaryMixed}");
688
+		array_push($expectedMessage, "Content-Type: {$type}; name=\"{$title}\"");
689
+		array_push($expectedMessage, 'Content-Transfer-Encoding: base64');
690
+		array_push($expectedMessage, 'Content-Disposition: attachment');
691
+		array_push($expectedMessage, '');
692
+		array_push($expectedMessage, $encodedContent);
693
+		array_push($expectedMessage, "--{$boundaryMixed}--");
694
+		$expectedMessage = implode(Archangel::LINE_BREAK, $expectedMessage);
695
+
696
+		$plainMessageProperty = $this->getProtectedProperty('plainMessage');
697
+		$plainMessageProperty->setValue($archangel, '');
698
+		$htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
699
+		$htmlMessageProperty->setValue($archangel, $message);
700
+		$attachmentProperty = $this->getProtectedProperty('attachments');
701
+		$attachmentProperty->setValue($archangel, array(
702
+			array('path' => $path, 'type' => $type, 'title' => $title)
703
+		));
704
+		$buildMethod = $this->getProtectedMethod('buildMessageWithAttachments');
705
+		$builtMessage = $buildMethod->invoke($archangel);
706
+
707
+		unlink($path);
708
+		$this->assertEquals($expectedMessage, $builtMessage);
709
+	}
710
+
711
+	public function testBuildMessageWithAttachmentsMultipart()
712
+	{
713
+		$path = __DIR__ . '/test.txt';
714
+		$textContent = 'Dummy Content';
715
+		$this->makeTmpFile($path, $textContent);
716
+
717
+		$encodedContent = chunk_split(base64_encode($textContent));
718
+		$type = 'text/plain';
719
+		$title = 'Test File';
720
+		$plainMessage = 'Plain text message';
721
+		$htmlMessage = '<p>HTML Message.</p>';
722
+
723
+		$archangel = new Archangel();
724
+
725
+		$boundaryMixedProperty = $this->getProtectedProperty('boundaryMixed');
726
+		$boundaryMixed = $boundaryMixedProperty->getValue($archangel);
727
+		$boundaryAltProperty = $this->getProtectedProperty('boundaryAlternative');
728
+		$boundaryAlternative = $boundaryAltProperty->getValue($archangel);
729
+		$plainMsgMethod = $this->getProtectedMethod('buildPlainMessageHeader');
730
+		$plainMsgHeaders = $plainMsgMethod->invoke($archangel);
731
+		$htmlMsgMethod = $this->getProtectedMethod('buildHtmlMessageHeader');
732
+		$htmlMsgHeaders = $htmlMsgMethod->invoke($archangel);
733
+
734
+		$expectedMessage = array();
735
+		array_push($expectedMessage, "--{$boundaryMixed}");
736
+		array_push($expectedMessage, "Content-Type: multipart/alternative; boundary={$boundaryAlternative}");
737
+		array_push($expectedMessage, '');
738
+		array_push($expectedMessage, "--{$boundaryAlternative}");
739
+		$expectedMessage = array_merge($expectedMessage, $plainMsgHeaders);
740
+		array_push($expectedMessage, $plainMessage);
741
+		array_push($expectedMessage, "--{$boundaryAlternative}");
742
+		$expectedMessage = array_merge($expectedMessage, $htmlMsgHeaders);
743
+		array_push($expectedMessage, $htmlMessage);
744
+		array_push($expectedMessage, "--{$boundaryAlternative}--");
745
+		array_push($expectedMessage, '');
746
+		array_push($expectedMessage, "--{$boundaryMixed}");
747
+		array_push($expectedMessage, "Content-Type: {$type}; name=\"{$title}\"");
748
+		array_push($expectedMessage, 'Content-Transfer-Encoding: base64');
749
+		array_push($expectedMessage, 'Content-Disposition: attachment');
750
+		array_push($expectedMessage, '');
751
+		array_push($expectedMessage, $encodedContent);
752
+		array_push($expectedMessage, "--{$boundaryMixed}--");
753
+		$expectedMessage = implode(Archangel::LINE_BREAK, $expectedMessage);
754
+
755
+		$plainMessageProperty = $this->getProtectedProperty('plainMessage');
756
+		$plainMessageProperty->setValue($archangel, $plainMessage);
757
+		$htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
758
+		$htmlMessageProperty->setValue($archangel, $htmlMessage);
759
+		$attachmentProperty = $this->getProtectedProperty('attachments');
760
+		$attachmentProperty->setValue($archangel, array(
761
+			array('path' => $path, 'type' => $type, 'title' => $title)
762
+		));
763
+		$buildMethod = $this->getProtectedMethod('buildMessageWithAttachments');
764
+		$builtMessage = $buildMethod->invoke($archangel);
765
+
766
+		unlink($path);
767
+		$this->assertEquals($expectedMessage, $builtMessage);
768
+	}
769
+
770
+	public function testBuildPlainMessageHeader()
771
+	{
772
+		$expectedMessageHeader = array(
773
+			'Content-Type: text/plain; charset="iso-8859"',
774
+			'Content-Transfer-Encoding: 7bit',
775
+			'',
776
+		);
777
+
778
+		$archangel = new Archangel();
779
+		$buildMethod = $this->getProtectedMethod('buildPlainMessageHeader');
780
+		$messageHeader = $buildMethod->invoke($archangel);
781
+
782
+		$this->assertEquals($expectedMessageHeader, $messageHeader);
783
+	}
784
+
785
+	public function testBuildHtmlMessageHeader()
786
+	{
787
+		$expectedMessageHeader = array(
788
+			'Content-Type: text/html; charset="iso-8859-1"',
789
+			'Content-Transfer-Encoding: 7bit',
790
+			'',
791
+		);
792
+
793
+		$archangel = new Archangel();
794
+		$buildMethod = $this->getProtectedMethod('buildHtmlMessageHeader');
795
+		$messageHeader = $buildMethod->invoke($archangel);
796
+
797
+		$this->assertEquals($expectedMessageHeader, $messageHeader);
798
+	}
799
+
800
+	/**
801
+	 * @dataProvider dataBuildHeaders
802
+	 */
803
+	public function testBuildHeaders(
804
+		$expectedHeaders,
805
+		$headers,
806
+		$attachments,
807
+		$plainMessage,
808
+		$htmlMessage
809
+	) {
810
+		$archangel = new Archangel();
811
+		$headersProperty = $this->getProtectedProperty('headers');
812
+		$headersProperty->setValue($archangel, $headers);
813
+
814
+		if (!empty($attachments)) {
815
+			$attachmentsProperty = $this->getProtectedProperty('attachments');
816
+			$attachmentsProperty->setValue($archangel, $attachments);
817
+		}
818
+
819
+		if (!empty($plainMessage)) {
820
+			$plainMessageProperty = $this->getProtectedProperty('plainMessage');
821
+			$plainMessageProperty->setValue($archangel, $plainMessage);
822
+		}
823
+
824
+		if (!empty($htmlMessage)) {
825
+			$htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
826
+			$htmlMessageProperty->setValue($archangel, $htmlMessage);
827
+		}
828
+
829
+		$buildHeadersMethod = $this->getProtectedMethod('buildHeaders');
830
+		$builtHeaders = $buildHeadersMethod->invoke($archangel);
831
+
832
+		$this->assertEquals($expectedHeaders, $builtHeaders);
833
+	}
834
+
835
+	public function dataBuildHeaders()
836
+	{
837
+		return array(
838
+			array(
839
+				'expectedHeaders' =>
840
+					"From: [email protected]\r\n" .
841
+					"X-Mailer: PHP/6.0.0",
842
+				'headers' => array(
843
+					'From' => '[email protected]',
844
+					'X-Mailer' => sprintf('PHP/%s', phpversion())
845
+				),
846
+				'attachments' => null,
847
+				'plainMessage' => true,
848
+				'htmlMessage' => null,
849
+			),
850
+			array(
851
+				'expectedHeaders' =>
852
+					"CC: [email protected], [email protected]\r\n" .
853
+					"From: [email protected]\r\n" .
854
+					"X-Mailer: PHP/6.0.0",
855
+				'headers' => array(
856
+					'CC' => array('[email protected]', '[email protected]'),
857
+					'From' => '[email protected]',
858
+					'X-Mailer' => sprintf('PHP/%s', phpversion())
859
+				),
860
+				'attachments' => null,
861
+				'plainMessage' => true,
862
+				'htmlMessage' => null,
863
+			),
864
+			array(
865
+				'expectedHeaders' =>
866
+					"BCC: [email protected], [email protected]\r\n" .
867
+					"From: [email protected]\r\n" .
868
+					"X-Mailer: PHP/6.0.0",
869
+				'headers' => array(
870
+					'BCC' => array('[email protected]', '[email protected]'),
871
+					'From' => '[email protected]',
872
+					'X-Mailer' => sprintf('PHP/%s', phpversion())
873
+				),
874
+				'attachments' => null,
875
+				'plainMessage' => true,
876
+				'htmlMessage' => null,
877
+			),
878
+			array(
879
+				'expectedHeaders' =>
880
+					"From: [email protected]\r\n" .
881
+					"X-Mailer: PHP/6.0.0\r\n" .
882
+					"Content-Type: multipart/mixed; boundary=\"PHP-mixed-1234567890123\"",
883
+				'headers' => array(
884
+					'From' => '[email protected]',
885
+					'X-Mailer' => sprintf('PHP/%s', phpversion())
886
+				),
887
+				'attachments' => true,
888
+				'plainMessage' => true,
889
+				'htmlMessage' => null,
890
+			),
891
+			array(
892
+				'expectedHeaders' =>
893
+					"From: [email protected]\r\n" .
894
+					"X-Mailer: PHP/6.0.0\r\n" .
895
+					"Content-Type: multipart/alternative; boundary=\"PHP-alternative-1234567890123\"",
896
+				'headers' => array(
897
+					'From' => '[email protected]',
898
+					'X-Mailer' => sprintf('PHP/%s', phpversion())
899
+				),
900
+				'attachments' => null,
901
+				'plainMessage' => true,
902
+				'htmlMessage' => true,
903
+			),
904
+			array(
905
+				'expectedHeaders' =>
906
+					"From: [email protected]\r\n" .
907
+					"X-Mailer: PHP/6.0.0\r\n" .
908
+					"Content-type: text/html; charset=\"iso-8859-1\"",
909
+				'headers' => array(
910
+					'From' => '[email protected]',
911
+					'X-Mailer' => sprintf('PHP/%s', phpversion())
912
+				),
913
+				'attachments' => null,
914
+				'plainMessage' => null,
915
+				'htmlMessage' => true,
916
+			),
917
+		);
918
+	}
919
+
920
+	public function testBuildAttachmentContent()
921
+	{
922
+		$path = __DIR__ . '/test.txt';
923
+		$textContent = 'Dummy Content';
924
+		$this->makeTmpFile($path, $textContent);
925
+
926
+		$expectedContent = chunk_split(base64_encode($textContent));
927
+
928
+		$archangel = new Archangel();
929
+		$buildMethod = $this->getProtectedMethod('buildAttachmentContent');
930
+		$content = $buildMethod->invokeArgs($archangel, array($path));
931
+
932
+		unlink($path);
933
+		$this->assertEquals($expectedContent, $content);
934
+	}
935
+
936
+	public function testBuildAttachmentContentFailure()
937
+	{
938
+		$logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
939
+
940
+		$archangel = new Archangel();
941
+		$archangel->setLogger($logger);
942
+		$buildMethod = $this->getProtectedMethod('buildAttachmentContent');
943
+		$content = $buildMethod->invokeArgs($archangel, array('INVALID_PATH'));
944
+
945
+		$this->assertEmpty($content);
946
+	}
947
+
948
+	protected function getProtectedProperty($property)
949
+	{
950
+		$reflectedArchangel = new ReflectionClass('Jacobemerick\Archangel\Archangel');
951
+		$reflectedProperty = $reflectedArchangel->getProperty($property);
952
+		$reflectedProperty->setAccessible(true);
953
+
954
+		return $reflectedProperty;
955
+	}
956
+
957
+	protected function getProtectedMethod($method)
958
+	{
959
+		$reflectedArchangel = new ReflectionClass('Jacobemerick\Archangel\Archangel');
960
+		$reflectedMethod = $reflectedArchangel->getMethod($method);
961
+		$reflectedMethod->setAccessible(true);
962
+
963
+		return $reflectedMethod;
964
+	}
965
+
966
+	protected function makeTmpFile($path, $content)
967
+	{
968
+		$handle = fopen($path, 'w');
969
+		fwrite($handle, $content);
970
+		fclose($handle);
971
+	}
972 972
 }
Please login to merge, or discard this patch.
src/Archangel.php 1 patch
Indentation   +453 added lines, -454 removed lines patch added patch discarded remove patch
@@ -16,458 +16,457 @@
 block discarded – undo
16 16
 class Archangel implements LoggerAwareInterface
17 17
 {
18 18
 
19
-    /** @var boolean $isTestMode */
20
-    protected $isTestMode;
21
-
22
-    /** @var string $subject */
23
-    protected $subject;
24
-
25
-    /** @var array $toAddresses */
26
-    protected $toAddresses = array();
27
-
28
-    /** @var array $headers */
29
-    protected $headers = array();
30
-
31
-    /** @var string $plainMessage */
32
-    protected $plainMessage;
33
-
34
-    /** @var string $htmlMessage */
35
-    protected $htmlMessage;
36
-
37
-    /** @var array $attachments */
38
-    protected $attachments = array();
39
-
40
-    /** @var string $boundaryMixed */
41
-    protected $boundaryMixed;
42
-
43
-    /** @var string $boundaryAlternative */
44
-    protected $boundaryAlternative;
45
-
46
-    /** @var LoggerInterface */
47
-    protected $logger;
48
-
49
-    /** @var string LINE_BREAK */
50
-    const LINE_BREAK = "\r\n";
51
-
52
-    /**
53
-     * @param string  $mailer
54
-     * @param boolean $isTestMode
55
-     */
56
-    public function __construct($mailer = null, $isTestMode = false)
57
-    {
58
-        if (is_null($mailer)) {
59
-            $mailer = sprintf('PHP/%s', phpversion());
60
-        }
61
-        $this->headers['X-Mailer'] = $mailer;
62
-        $this->isTestMode = $isTestMode;
63
-
64
-        $this->logger = new NullLogger();
65
-        $this->boundaryMixed = sprintf('PHP-mixed-%s', uniqid());
66
-        $this->boundaryAlternative = sprintf('PHP-alternative-%s', uniqid());
67
-    }
68
-
69
-    /**
70
-     * @param LoggerInterface $logger
71
-     *
72
-     * @return object instantiated $this
73
-     */
74
-    public function setLogger(LoggerInterface $logger)
75
-    {
76
-        $this->logger = $logger;
77
-
78
-        return $this;
79
-    }
80
-
81
-    /**
82
-     * Setter method for adding recipients
83
-     *
84
-     * @param string $address email address for the recipient
85
-     * @param string $title   name of the recipient (optional)
86
-
87
-     * @return object instantiated $this
88
-     */
89
-    public function addTo($address, $title = '')
90
-    {
91
-        array_push(
92
-            $this->toAddresses,
93
-            $this->formatEmailAddress($address, $title)
94
-        );
95
-
96
-        return $this;
97
-    }
98
-
99
-    /**
100
-     * Setter method for adding cc recipients
101
-     *
102
-     * @param string $address email address for the cc recipient
103
-     * @param string $title   name of the cc recipient (optional)
104
-     *
105
-     * @return object instantiated $this
106
-     */
107
-    public function addCC($address, $title = '')
108
-    {
109
-        if (!isset($this->headers['CC'])) {
110
-            $this->headers['CC'] = array();
111
-        }
112
-
113
-        array_push(
114
-            $this->headers['CC'],
115
-            $this->formatEmailAddress($address, $title)
116
-        );
117
-
118
-        return $this;
119
-    }
120
-
121
-    /**
122
-     * Setter method for adding bcc recipients
123
-     *
124
-     * @param string $address email address for the bcc recipient
125
-     * @param string $title   name of the bcc recipient (optional)
126
-     *
127
-     * @return object instantiated $this
128
-     */
129
-    public function addBCC($address, $title = '')
130
-    {
131
-        if (!isset($this->headers['BCC'])) {
132
-            $this->headers['BCC'] = array();
133
-        }
134
-
135
-        array_push(
136
-            $this->headers['BCC'],
137
-            $this->formatEmailAddress($address, $title)
138
-        );
139
-
140
-        return $this;
141
-    }
142
-
143
-    /**
144
-     * Setter method for setting the single 'from' field
145
-     *
146
-     * @param string $address email address for the sender
147
-     * @param string $title   name of the sender (optional)
148
-     *
149
-     * @return object instantiated $this
150
-     */
151
-    public function setFrom($address, $title = '')
152
-    {
153
-        $this->headers['From'] = $this->formatEmailAddress($address, $title);
154
-
155
-        return $this;
156
-    }
157
-
158
-    /**
159
-     * Setter method for setting the single 'reply-to' field
160
-     *
161
-     * @param string $address email address for the reply-to
162
-     * @param string $title   name of the reply-to (optional)
163
-     *
164
-     * @return object instantiated $this
165
-     */
166
-    public function setReplyTo($address, $title = '')
167
-    {
168
-        $this->headers['Reply-To'] = $this->formatEmailAddress($address, $title);
169
-
170
-        return $this;
171
-    }
172
-
173
-    /**
174
-     * @param string $address
175
-     * @param string $title
176
-     *
177
-     * @return string
178
-     */
179
-    protected function formatEmailAddress($address, $title)
180
-    {
181
-        if (!empty($title)) {
182
-            $address = sprintf('"%s" <%s>', $title, $address);
183
-        }
184
-        return $address;
185
-    }
186
-
187
-    /**
188
-     * Setter method for setting a subject
189
-     *
190
-     * @param string $subject subject for the email
191
-     *
192
-     * @return object instantiated $this
193
-     */
194
-    public function setSubject($subject)
195
-    {
196
-        $this->subject = $subject;
197
-
198
-        return $this;
199
-    }
200
-
201
-    /**
202
-     * Setter method for the plain text message
203
-     *
204
-     * @param string $message the plain-text message
205
-     *
206
-     * @return object instantiated $this
207
-     */
208
-    public function setPlainMessage($message)
209
-    {
210
-        $this->plainMessage = $message;
211
-
212
-        return $this;
213
-    }
214
-
215
-    /**
216
-     * Setter method for the html message
217
-     *
218
-     * @param string $message the html message
219
-     *
220
-     * @return object instantiated $this
221
-     */
222
-    public function setHTMLMessage($message)
223
-    {
224
-        $this->htmlMessage = $message;
225
-
226
-        return $this;
227
-    }
228
-
229
-    /**
230
-     * Setter method for adding attachments
231
-     *
232
-     * @param string $path  the full path of the attachment
233
-     * @param string $type  mime type of the file
234
-     * @param string $title the title of the attachment (optional)
235
-     *
236
-     * @return object instantiated $this
237
-     */
238
-    public function addAttachment($path, $type, $title = '')
239
-    {
240
-        array_push($this->attachments, array(
241
-          'path' => $path,
242
-          'type' => $type,
243
-          'title' => $title,
244
-        ));
245
-
246
-        return $this;
247
-    }
248
-
249
-    /**
250
-     * The executing step, the actual sending of the email
251
-     * First checks to make sure the minimum fields are set (returns false if they are not)
252
-     * Second it attempts to send the mail with php's mail() (returns false if it fails)
253
-     *
254
-     * @return boolean whether or not the email was valid & sent
255
-     */
256
-    public function send()
257
-    {
258
-        if (!$this->checkRequiredFields()) {
259
-            $this->logger->error('Minimum required fields not filled out, cannot send Archangel mail.');
260
-            return false;
261
-        }
262
-
263
-        $recipients = $this->buildTo();
264
-        $subject = $this->subject;
265
-        $message = (empty($this->attachments)) ? $this->buildMessage() : $this->buildMessageWithAttachments();
266
-        $headers = $this->buildHeaders();
267
-
268
-        $debugMessage = array(
269
-            'Triggered send on Archangel mail.',
270
-            "Recipients: {$recipients}",
271
-            "Subject: {$subject}",
272
-            "Message: {$message}",
273
-            "Headers: {$headers}",
274
-        );
275
-        $this->logger->debug(implode(' || ', $debugMessage));
276
-
277
-        if ($this->isTestMode) {
278
-            return true;
279
-        }
280
-        return mail($recipients, $subject, $message, $headers);
281
-    }
282
-
283
-    /**
284
-     * Call to check the minimum required fields
285
-     *
286
-     * @return boolean whether or not the email meets the minimum required fields
287
-     */
288
-    protected function checkRequiredFields()
289
-    {
290
-        if (empty($this->toAddresses)) {
291
-            return false;
292
-        }
293
-        if (empty($this->subject)) {
294
-            return false;
295
-        }
296
-
297
-        if (empty($this->plainMessage) && empty($this->htmlMessage) && empty($this->attachments)) {
298
-            return false;
299
-        }
300
-
301
-        return true;
302
-    }
303
-
304
-    /**
305
-     * Build the recipients from 'to'
306
-     *
307
-     * @return string comma-separated lit of recipients
308
-     */
309
-    protected function buildTo()
310
-    {
311
-        return implode(', ', $this->toAddresses);
312
-    }
313
-
314
-    /**
315
-     * Returns a simple email message without attachments
316
-     *
317
-     * @return string email message
318
-     */
319
-    protected function buildMessage()
320
-    {
321
-        if (empty($this->plainMessage) && empty($this->htmlMessage)) {
322
-            return '';
323
-        }
324
-        if (!empty($this->plainMessage) && empty($this->htmlMessage)) {
325
-            return $this->plainMessage;
326
-        }
327
-        if (empty($this->plainMessage) && !empty($this->htmlMessage)) {
328
-            return $this->htmlMessage;
329
-        }
330
-
331
-        $message = array();
332
-        array_push($message, "--{$this->boundaryAlternative}");
333
-        $message = array_merge($message, $this->buildPlainMessageHeader());
334
-        array_push($message, $this->plainMessage);
335
-        array_push($message, "--{$this->boundaryAlternative}");
336
-        $message = array_merge($message, $this->buildHtmlMessageHeader());
337
-        array_push($message, $this->htmlMessage);
338
-        array_push($message, "--{$this->boundaryAlternative}--");
339
-
340
-        return implode(self::LINE_BREAK, $message);
341
-    }
342
-
343
-    /**
344
-     * Build multi-part message with attachments
345
-     *
346
-     * @return string email message
347
-     */
348
-    protected function buildMessageWithAttachments()
349
-    {
350
-        $message = array();
351
-
352
-        if (!empty($this->plainMessage) || !empty($this->htmlMessage)) {
353
-            array_push($message, "--{$this->boundaryMixed}");
354
-        }
355
-
356
-        if (!empty($this->plainMessage) && !empty($this->htmlMessage)) {
357
-            array_push($message, "Content-Type: multipart/alternative; boundary={$this->boundaryAlternative}");
358
-            array_push($message, '');
359
-            array_push($message, "--{$this->boundaryAlternative}");
360
-            $message = array_merge($message, $this->buildPlainMessageHeader());
361
-            array_push($message, $this->plainMessage);
362
-            array_push($message, "--{$this->boundaryAlternative}");
363
-            $message = array_merge($message, $this->buildHtmlMessageHeader());
364
-            array_push($message, $this->htmlMessage);
365
-            array_push($message, "--{$this->boundaryAlternative}--");
366
-            array_push($message, '');
367
-        } elseif (!empty($this->plainMessage)) {
368
-            $message = array_merge($message, $this->buildPlainMessageHeader());
369
-            array_push($message, $this->plainMessage);
370
-        } elseif (!empty($this->htmlMessage)) {
371
-            $message = array_merge($message, $this->buildHtmlMessageHeader());
372
-            array_push($message, $this->htmlMessage);
373
-        }
374
-        foreach ($this->attachments as $attachment) {
375
-            array_push($message, "--{$this->boundaryMixed}");
376
-            array_push($message, "Content-Type: {$attachment['type']}; name=\"{$attachment['title']}\"");
377
-            array_push($message, 'Content-Transfer-Encoding: base64');
378
-            array_push($message, 'Content-Disposition: attachment');
379
-            array_push($message, '');
380
-            array_push($message, $this->buildAttachmentContent($attachment['path']));
381
-        }
382
-        array_push($message, "--{$this->boundaryMixed}--");
383
-
384
-        return implode(self::LINE_BREAK, $message);
385
-    }
386
-
387
-
388
-    /**
389
-     * Shared holder for the plain message header
390
-     *
391
-     * @return array
392
-     */
393
-    protected function buildPlainMessageHeader()
394
-    {
395
-        return array(
396
-            'Content-Type: text/plain; charset="iso-8859"',
397
-            'Content-Transfer-Encoding: 7bit',
398
-            '',
399
-        );
400
-    }
401
-
402
-    /**
403
-     * Shared holder for the html message header
404
-     *
405
-     * @return array
406
-     */
407
-    protected function buildHtmlMessageHeader()
408
-    {
409
-        return array(
410
-            'Content-Type: text/html; charset="iso-8859-1"',
411
-            'Content-Transfer-Encoding: 7bit',
412
-            '',
413
-        );
414
-    }
415
-
416
-    /**
417
-     * Builder for the additional headers needed for multipart emails
418
-     *
419
-     * @return string headers needed for multipart
420
-     */
421
-    protected function buildHeaders()
422
-    {
423
-        $headers = array();
424
-        foreach ($this->headers as $key => $value) {
425
-            if ($key == 'CC' || $key == 'BCC') {
426
-                $value = implode(', ', $value);
427
-            }
428
-            array_push($headers, sprintf('%s: %s', $key, $value));
429
-        }
430
-
431
-        if (!empty($this->attachments)) {
432
-            array_push(
433
-                $headers,
434
-                "Content-Type: multipart/mixed; boundary=\"{$this->boundaryMixed}\""
435
-            );
436
-        } elseif (!empty($this->plainMessage) && !empty($this->htmlMessage)) {
437
-            array_push(
438
-                $headers,
439
-                "Content-Type: multipart/alternative; boundary=\"{$this->boundaryAlternative}\""
440
-            );
441
-        } elseif (!empty($this->htmlMessage)) {
442
-            array_push(
443
-                $headers,
444
-                'Content-type: text/html; charset="iso-8859-1"'
445
-            );
446
-        }
447
-
448
-        return implode(self::LINE_BREAK, $headers);
449
-    }
450
-
451
-    /**
452
-     * File reader for attachments
453
-     *
454
-     * @param string $path filepath of the attachment
455
-     *
456
-     * @return string binary representation of file, base64'd
457
-     */
458
-    protected function buildAttachmentContent($path)
459
-    {
460
-        if (!file_exists($path)) {
461
-            $this->logger->error("Could not find file {$path} for attaching to Archangel mail.");
462
-            return '';
463
-        }
464
-
465
-        $handle = fopen($path, 'r');
466
-        $contents = fread($handle, filesize($path));
467
-        fclose($handle);
468
-
469
-        $contents = base64_encode($contents);
470
-        $contents = chunk_split($contents);
471
-        return $contents;
472
-    }
19
+	/** @var boolean $isTestMode */
20
+	protected $isTestMode;
21
+
22
+	/** @var string $subject */
23
+	protected $subject;
24
+
25
+	/** @var array $toAddresses */
26
+	protected $toAddresses = array();
27
+
28
+	/** @var array $headers */
29
+	protected $headers = array();
30
+
31
+	/** @var string $plainMessage */
32
+	protected $plainMessage;
33
+
34
+	/** @var string $htmlMessage */
35
+	protected $htmlMessage;
36
+
37
+	/** @var array $attachments */
38
+	protected $attachments = array();
39
+
40
+	/** @var string $boundaryMixed */
41
+	protected $boundaryMixed;
42
+
43
+	/** @var string $boundaryAlternative */
44
+	protected $boundaryAlternative;
45
+
46
+	/** @var LoggerInterface */
47
+	protected $logger;
48
+
49
+	/** @var string LINE_BREAK */
50
+	const LINE_BREAK = "\r\n";
51
+
52
+	/**
53
+	 * @param string  $mailer
54
+	 * @param boolean $isTestMode
55
+	 */
56
+	public function __construct($mailer = null, $isTestMode = false)
57
+	{
58
+		if (is_null($mailer)) {
59
+			$mailer = sprintf('PHP/%s', phpversion());
60
+		}
61
+		$this->headers['X-Mailer'] = $mailer;
62
+		$this->isTestMode = $isTestMode;
63
+
64
+		$this->logger = new NullLogger();
65
+		$this->boundaryMixed = sprintf('PHP-mixed-%s', uniqid());
66
+		$this->boundaryAlternative = sprintf('PHP-alternative-%s', uniqid());
67
+	}
68
+
69
+	/**
70
+	 * @param LoggerInterface $logger
71
+	 *
72
+	 * @return object instantiated $this
73
+	 */
74
+	public function setLogger(LoggerInterface $logger)
75
+	{
76
+		$this->logger = $logger;
77
+
78
+		return $this;
79
+	}
80
+
81
+	/**
82
+	 * Setter method for adding recipients
83
+	 *
84
+	 * @param string $address email address for the recipient
85
+	 * @param string $title   name of the recipient (optional)
86
+	 * @return object instantiated $this
87
+	 */
88
+	public function addTo($address, $title = '')
89
+	{
90
+		array_push(
91
+			$this->toAddresses,
92
+			$this->formatEmailAddress($address, $title)
93
+		);
94
+
95
+		return $this;
96
+	}
97
+
98
+	/**
99
+	 * Setter method for adding cc recipients
100
+	 *
101
+	 * @param string $address email address for the cc recipient
102
+	 * @param string $title   name of the cc recipient (optional)
103
+	 *
104
+	 * @return object instantiated $this
105
+	 */
106
+	public function addCC($address, $title = '')
107
+	{
108
+		if (!isset($this->headers['CC'])) {
109
+			$this->headers['CC'] = array();
110
+		}
111
+
112
+		array_push(
113
+			$this->headers['CC'],
114
+			$this->formatEmailAddress($address, $title)
115
+		);
116
+
117
+		return $this;
118
+	}
119
+
120
+	/**
121
+	 * Setter method for adding bcc recipients
122
+	 *
123
+	 * @param string $address email address for the bcc recipient
124
+	 * @param string $title   name of the bcc recipient (optional)
125
+	 *
126
+	 * @return object instantiated $this
127
+	 */
128
+	public function addBCC($address, $title = '')
129
+	{
130
+		if (!isset($this->headers['BCC'])) {
131
+			$this->headers['BCC'] = array();
132
+		}
133
+
134
+		array_push(
135
+			$this->headers['BCC'],
136
+			$this->formatEmailAddress($address, $title)
137
+		);
138
+
139
+		return $this;
140
+	}
141
+
142
+	/**
143
+	 * Setter method for setting the single 'from' field
144
+	 *
145
+	 * @param string $address email address for the sender
146
+	 * @param string $title   name of the sender (optional)
147
+	 *
148
+	 * @return object instantiated $this
149
+	 */
150
+	public function setFrom($address, $title = '')
151
+	{
152
+		$this->headers['From'] = $this->formatEmailAddress($address, $title);
153
+
154
+		return $this;
155
+	}
156
+
157
+	/**
158
+	 * Setter method for setting the single 'reply-to' field
159
+	 *
160
+	 * @param string $address email address for the reply-to
161
+	 * @param string $title   name of the reply-to (optional)
162
+	 *
163
+	 * @return object instantiated $this
164
+	 */
165
+	public function setReplyTo($address, $title = '')
166
+	{
167
+		$this->headers['Reply-To'] = $this->formatEmailAddress($address, $title);
168
+
169
+		return $this;
170
+	}
171
+
172
+	/**
173
+	 * @param string $address
174
+	 * @param string $title
175
+	 *
176
+	 * @return string
177
+	 */
178
+	protected function formatEmailAddress($address, $title)
179
+	{
180
+		if (!empty($title)) {
181
+			$address = sprintf('"%s" <%s>', $title, $address);
182
+		}
183
+		return $address;
184
+	}
185
+
186
+	/**
187
+	 * Setter method for setting a subject
188
+	 *
189
+	 * @param string $subject subject for the email
190
+	 *
191
+	 * @return object instantiated $this
192
+	 */
193
+	public function setSubject($subject)
194
+	{
195
+		$this->subject = $subject;
196
+
197
+		return $this;
198
+	}
199
+
200
+	/**
201
+	 * Setter method for the plain text message
202
+	 *
203
+	 * @param string $message the plain-text message
204
+	 *
205
+	 * @return object instantiated $this
206
+	 */
207
+	public function setPlainMessage($message)
208
+	{
209
+		$this->plainMessage = $message;
210
+
211
+		return $this;
212
+	}
213
+
214
+	/**
215
+	 * Setter method for the html message
216
+	 *
217
+	 * @param string $message the html message
218
+	 *
219
+	 * @return object instantiated $this
220
+	 */
221
+	public function setHTMLMessage($message)
222
+	{
223
+		$this->htmlMessage = $message;
224
+
225
+		return $this;
226
+	}
227
+
228
+	/**
229
+	 * Setter method for adding attachments
230
+	 *
231
+	 * @param string $path  the full path of the attachment
232
+	 * @param string $type  mime type of the file
233
+	 * @param string $title the title of the attachment (optional)
234
+	 *
235
+	 * @return object instantiated $this
236
+	 */
237
+	public function addAttachment($path, $type, $title = '')
238
+	{
239
+		array_push($this->attachments, array(
240
+		  'path' => $path,
241
+		  'type' => $type,
242
+		  'title' => $title,
243
+		));
244
+
245
+		return $this;
246
+	}
247
+
248
+	/**
249
+	 * The executing step, the actual sending of the email
250
+	 * First checks to make sure the minimum fields are set (returns false if they are not)
251
+	 * Second it attempts to send the mail with php's mail() (returns false if it fails)
252
+	 *
253
+	 * @return boolean whether or not the email was valid & sent
254
+	 */
255
+	public function send()
256
+	{
257
+		if (!$this->checkRequiredFields()) {
258
+			$this->logger->error('Minimum required fields not filled out, cannot send Archangel mail.');
259
+			return false;
260
+		}
261
+
262
+		$recipients = $this->buildTo();
263
+		$subject = $this->subject;
264
+		$message = (empty($this->attachments)) ? $this->buildMessage() : $this->buildMessageWithAttachments();
265
+		$headers = $this->buildHeaders();
266
+
267
+		$debugMessage = array(
268
+			'Triggered send on Archangel mail.',
269
+			"Recipients: {$recipients}",
270
+			"Subject: {$subject}",
271
+			"Message: {$message}",
272
+			"Headers: {$headers}",
273
+		);
274
+		$this->logger->debug(implode(' || ', $debugMessage));
275
+
276
+		if ($this->isTestMode) {
277
+			return true;
278
+		}
279
+		return mail($recipients, $subject, $message, $headers);
280
+	}
281
+
282
+	/**
283
+	 * Call to check the minimum required fields
284
+	 *
285
+	 * @return boolean whether or not the email meets the minimum required fields
286
+	 */
287
+	protected function checkRequiredFields()
288
+	{
289
+		if (empty($this->toAddresses)) {
290
+			return false;
291
+		}
292
+		if (empty($this->subject)) {
293
+			return false;
294
+		}
295
+
296
+		if (empty($this->plainMessage) && empty($this->htmlMessage) && empty($this->attachments)) {
297
+			return false;
298
+		}
299
+
300
+		return true;
301
+	}
302
+
303
+	/**
304
+	 * Build the recipients from 'to'
305
+	 *
306
+	 * @return string comma-separated lit of recipients
307
+	 */
308
+	protected function buildTo()
309
+	{
310
+		return implode(', ', $this->toAddresses);
311
+	}
312
+
313
+	/**
314
+	 * Returns a simple email message without attachments
315
+	 *
316
+	 * @return string email message
317
+	 */
318
+	protected function buildMessage()
319
+	{
320
+		if (empty($this->plainMessage) && empty($this->htmlMessage)) {
321
+			return '';
322
+		}
323
+		if (!empty($this->plainMessage) && empty($this->htmlMessage)) {
324
+			return $this->plainMessage;
325
+		}
326
+		if (empty($this->plainMessage) && !empty($this->htmlMessage)) {
327
+			return $this->htmlMessage;
328
+		}
329
+
330
+		$message = array();
331
+		array_push($message, "--{$this->boundaryAlternative}");
332
+		$message = array_merge($message, $this->buildPlainMessageHeader());
333
+		array_push($message, $this->plainMessage);
334
+		array_push($message, "--{$this->boundaryAlternative}");
335
+		$message = array_merge($message, $this->buildHtmlMessageHeader());
336
+		array_push($message, $this->htmlMessage);
337
+		array_push($message, "--{$this->boundaryAlternative}--");
338
+
339
+		return implode(self::LINE_BREAK, $message);
340
+	}
341
+
342
+	/**
343
+	 * Build multi-part message with attachments
344
+	 *
345
+	 * @return string email message
346
+	 */
347
+	protected function buildMessageWithAttachments()
348
+	{
349
+		$message = array();
350
+
351
+		if (!empty($this->plainMessage) || !empty($this->htmlMessage)) {
352
+			array_push($message, "--{$this->boundaryMixed}");
353
+		}
354
+
355
+		if (!empty($this->plainMessage) && !empty($this->htmlMessage)) {
356
+			array_push($message, "Content-Type: multipart/alternative; boundary={$this->boundaryAlternative}");
357
+			array_push($message, '');
358
+			array_push($message, "--{$this->boundaryAlternative}");
359
+			$message = array_merge($message, $this->buildPlainMessageHeader());
360
+			array_push($message, $this->plainMessage);
361
+			array_push($message, "--{$this->boundaryAlternative}");
362
+			$message = array_merge($message, $this->buildHtmlMessageHeader());
363
+			array_push($message, $this->htmlMessage);
364
+			array_push($message, "--{$this->boundaryAlternative}--");
365
+			array_push($message, '');
366
+		} elseif (!empty($this->plainMessage)) {
367
+			$message = array_merge($message, $this->buildPlainMessageHeader());
368
+			array_push($message, $this->plainMessage);
369
+		} elseif (!empty($this->htmlMessage)) {
370
+			$message = array_merge($message, $this->buildHtmlMessageHeader());
371
+			array_push($message, $this->htmlMessage);
372
+		}
373
+		foreach ($this->attachments as $attachment) {
374
+			array_push($message, "--{$this->boundaryMixed}");
375
+			array_push($message, "Content-Type: {$attachment['type']}; name=\"{$attachment['title']}\"");
376
+			array_push($message, 'Content-Transfer-Encoding: base64');
377
+			array_push($message, 'Content-Disposition: attachment');
378
+			array_push($message, '');
379
+			array_push($message, $this->buildAttachmentContent($attachment['path']));
380
+		}
381
+		array_push($message, "--{$this->boundaryMixed}--");
382
+
383
+		return implode(self::LINE_BREAK, $message);
384
+	}
385
+
386
+
387
+	/**
388
+	 * Shared holder for the plain message header
389
+	 *
390
+	 * @return array
391
+	 */
392
+	protected function buildPlainMessageHeader()
393
+	{
394
+		return array(
395
+			'Content-Type: text/plain; charset="iso-8859"',
396
+			'Content-Transfer-Encoding: 7bit',
397
+			'',
398
+		);
399
+	}
400
+
401
+	/**
402
+	 * Shared holder for the html message header
403
+	 *
404
+	 * @return array
405
+	 */
406
+	protected function buildHtmlMessageHeader()
407
+	{
408
+		return array(
409
+			'Content-Type: text/html; charset="iso-8859-1"',
410
+			'Content-Transfer-Encoding: 7bit',
411
+			'',
412
+		);
413
+	}
414
+
415
+	/**
416
+	 * Builder for the additional headers needed for multipart emails
417
+	 *
418
+	 * @return string headers needed for multipart
419
+	 */
420
+	protected function buildHeaders()
421
+	{
422
+		$headers = array();
423
+		foreach ($this->headers as $key => $value) {
424
+			if ($key == 'CC' || $key == 'BCC') {
425
+				$value = implode(', ', $value);
426
+			}
427
+			array_push($headers, sprintf('%s: %s', $key, $value));
428
+		}
429
+
430
+		if (!empty($this->attachments)) {
431
+			array_push(
432
+				$headers,
433
+				"Content-Type: multipart/mixed; boundary=\"{$this->boundaryMixed}\""
434
+			);
435
+		} elseif (!empty($this->plainMessage) && !empty($this->htmlMessage)) {
436
+			array_push(
437
+				$headers,
438
+				"Content-Type: multipart/alternative; boundary=\"{$this->boundaryAlternative}\""
439
+			);
440
+		} elseif (!empty($this->htmlMessage)) {
441
+			array_push(
442
+				$headers,
443
+				'Content-type: text/html; charset="iso-8859-1"'
444
+			);
445
+		}
446
+
447
+		return implode(self::LINE_BREAK, $headers);
448
+	}
449
+
450
+	/**
451
+	 * File reader for attachments
452
+	 *
453
+	 * @param string $path filepath of the attachment
454
+	 *
455
+	 * @return string binary representation of file, base64'd
456
+	 */
457
+	protected function buildAttachmentContent($path)
458
+	{
459
+		if (!file_exists($path)) {
460
+			$this->logger->error("Could not find file {$path} for attaching to Archangel mail.");
461
+			return '';
462
+		}
463
+
464
+		$handle = fopen($path, 'r');
465
+		$contents = fread($handle, filesize($path));
466
+		fclose($handle);
467
+
468
+		$contents = base64_encode($contents);
469
+		$contents = chunk_split($contents);
470
+		return $contents;
471
+	}
473 472
 }
Please login to merge, or discard this patch.