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