Completed
Push — master ( 4e9949...a0acb1 )
by Jacob
02:53
created

ArchangelTest::testConstructOverridesTestMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Jacobemerick\Archangel;
4
5
use Monolog\Logger;
6
use Monolog\Handler\TestHandler;
7
use PHPUnit_Framework_TestCase;
8
use ReflectionClass;
9
10
class ArchangelTest extends PHPUnit_Framework_TestCase
11
{
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 testConstructSetsDefaultTestMode()
45
    {
46
        $archangel = new Archangel();
47
48
        $this->assertAttributeEquals(false, 'isTestMode', $archangel);
49
    }
50
51
    public function testConstructOverridesTestMode()
52
    {
53
        $archangel = new Archangel(null, true);
54
55
        $this->assertAttributeEquals(true, 'isTestMode', $archangel);
56
    }
57
58
    public function testConstructSetsNullLogger()
59
    {
60
        $archangel = new Archangel();
61
62
        $this->assertAttributeInstanceOf('Psr\Log\NullLogger', 'logger', $archangel);
63
    }
64
65
    public function testConstructSetsBoundaries()
66
    {
67
        $archangel = new Archangel();
68
        $expectedBoundaryMixed = sprintf('PHP-mixed-%s', uniqid());
69
        $expectedBoundaryAlternative = sprintf('PHP-alternative-%s', uniqid());
70
71
        $this->assertAttributeEquals($expectedBoundaryMixed, 'boundaryMixed', $archangel);
72
        $this->assertAttributeEquals($expectedBoundaryAlternative, 'boundaryAlternative', $archangel);
73
    }
74
75
    public function testSetLogger()
76
    {
77
        $logger = $this->getMock('Psr\Log\LoggerInterface');
78
        $archangel = new Archangel();
79
        $archangel->setLogger($logger);
80
81
        $this->assertAttributeSame($logger, 'logger', $archangel);
82
    }
83
84
    public function testAddTo()
85
    {
86
        $archangel = new Archangel();
87
        $archangel->addTo('[email protected]');
88
89
        $this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
90
    }
91
92
    public function testAddToMultiple()
93
    {
94
        $archangel = new Archangel();
95
        $archangel->addTo('[email protected]');
96
        $archangel->addTo('[email protected]');
97
98
        $this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
99
        $this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
100
    }
101
102
    public function testAddToWithTitle()
103
    {
104
        $archangel = new Archangel();
105
        $archangel->addTo('[email protected]', 'Mr. Test Alot');
106
107
        $this->assertAttributeContains('"Mr. Test Alot" <[email protected]>', 'toAddresses', $archangel);
108
    }
109
110
    public function testAddCC()
111
    {
112
        $archangel = new Archangel();
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]')),
119
            $headers
120
        );
121
    }
122
123 View Code Duplication
    public function testAddCCMultiple()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    {
125
        $archangel = new Archangel();
126
        $archangel->addCC('[email protected]');
127
        $archangel->addCC('[email protected]');
128
        $headersProperty = $this->getProtectedProperty('headers');
129
        $headers = $headersProperty->getValue($archangel);
130
131
        $this->assertArraySubset(
132
            array('CC' => array('[email protected]', '[email protected]')),
133
            $headers
134
        );
135
    }
136
137
    public function testAddCCWithTitle()
138
    {
139
        $archangel = new Archangel();
140
        $archangel->addCC('[email protected]', 'Mr. Test Alot');
141
        $headersProperty = $this->getProtectedProperty('headers');
142
        $headers = $headersProperty->getValue($archangel);
143
144
        $this->assertArraySubset(
145
            array('CC' => array('"Mr. Test Alot" <[email protected]>')),
146
            $headers
147
        );
148
    }
149
150
    public function testAddBCC()
151
    {
152
        $archangel = new Archangel();
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]')),
159
            $headers
160
        );
161
    }
162
163 View Code Duplication
    public function testAddBCCMultiple()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164
    {
165
        $archangel = new Archangel();
166
        $archangel->addBCC('[email protected]');
167
        $archangel->addBCC('[email protected]');
168
        $headersProperty = $this->getProtectedProperty('headers');
169
        $headers = $headersProperty->getValue($archangel);
170
171
        $this->assertArraySubset(
172
            array('BCC' => array('[email protected]', '[email protected]')),
173
            $headers
174
        );
175
    }
176
177
    public function testAddBCCWithTitle()
178
    {
179
        $archangel = new Archangel();
180
        $archangel->addBCC('[email protected]', 'Mr. Test Alot');
181
        $headersProperty = $this->getProtectedProperty('headers');
182
        $headers = $headersProperty->getValue($archangel);
183
184
        $this->assertArraySubset(
185
            array('BCC' => array('"Mr. Test Alot" <[email protected]>')),
186
            $headers
187
        );
188
    }
189
190
    public function testSetFrom()
191
    {
192
        $archangel = new Archangel();
193
        $archangel->setFrom('[email protected]');
194
        $headersProperty = $this->getProtectedProperty('headers');
195
        $headers = $headersProperty->getValue($archangel);
196
197
        $this->assertArraySubset(array('From' => '[email protected]'), $headers);
198
    }
199
200 View Code Duplication
    public function testSetFromMultiple()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
201
    {
202
        $archangel = new Archangel();
203
        $archangel->setFrom('[email protected]');
204
        $archangel->setFrom('[email protected]');
205
        $headersProperty = $this->getProtectedProperty('headers');
206
        $headers = $headersProperty->getValue($archangel);
207
208
        $this->assertArraySubset(array('From' => '[email protected]'), $headers);
209
        $this->assertNotContains('[email protected]', $headers);
210
    }
211
212
    public function testSetFromWithTitle()
213
    {
214
        $archangel = new Archangel();
215
        $archangel->setFrom('[email protected]', 'Mr. Test Alot');
216
        $headersProperty = $this->getProtectedProperty('headers');
217
        $headers = $headersProperty->getValue($archangel);
218
219
        $this->assertArraySubset(array('From' => '"Mr. Test Alot" <[email protected]>'), $headers);
220
    }
221
222
    public function testSetReplyTo()
223
    {
224
        $archangel = new Archangel();
225
        $archangel->setReplyTo('[email protected]');
226
        $headersProperty = $this->getProtectedProperty('headers');
227
        $headers = $headersProperty->getValue($archangel);
228
229
        $this->assertArraySubset(array('Reply-To' => '[email protected]'), $headers);
230
    }
231
232 View Code Duplication
    public function testSetReplyToMultiple()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
233
    {
234
        $archangel = new Archangel();
235
        $archangel->setReplyTo('[email protected]');
236
        $archangel->setReplyTo('[email protected]');
237
        $headersProperty = $this->getProtectedProperty('headers');
238
        $headers = $headersProperty->getValue($archangel);
239
240
        $this->assertArraySubset(array('Reply-To' => '[email protected]'), $headers);
241
        $this->assertNotContains('[email protected]', $headers);
242
    }
243
244
    public function testSetReplyToWithTitle()
245
    {
246
        $archangel = new Archangel();
247
        $archangel->setReplyTo('[email protected]', 'Mr. Test Alot');
248
        $headersProperty = $this->getProtectedProperty('headers');
249
        $headers = $headersProperty->getValue($archangel);
250
251
        $this->assertArraySubset(array('Reply-To' => '"Mr. Test Alot" <[email protected]>'), $headers);
252
    }
253
254 View Code Duplication
    public function testFormatEmailAddress()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
255
    {
256
        $archangel = new Archangel();
257
        $formatMethod = $this->getProtectedMethod('formatEmailAddress');
258
        $formattedEmail = $formatMethod->invokeArgs($archangel, array('[email protected]', ''));
259
260
        $this->assertEquals('[email protected]', $formattedEmail);
261
    }
262
263 View Code Duplication
    public function testFormatEmailAddressWithTitle()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
264
    {
265
        $archangel = new Archangel();
266
        $formatMethod = $this->getProtectedMethod('formatEmailAddress');
267
        $formattedEmail = $formatMethod->invokeArgs($archangel, array('[email protected]', 'Mr. Test Alot'));
268
269
        $this->assertEquals('"Mr. Test Alot" <[email protected]>', $formattedEmail);
270
    }
271
272
    public function testSetSubject()
273
    {
274
        $archangel = new Archangel();
275
        $archangel->setSubject('Test Subject');
276
277
        $this->assertAttributeEquals('Test Subject', 'subject', $archangel);
278
    }
279
280
    public function testSetPlainMessage()
281
    {
282
        $archangel = new Archangel();
283
        $archangel->setPlainMessage('Plain text message');
284
285
        $this->assertAttributeEquals('Plain text message', 'plainMessage', $archangel);
286
    }
287
288
    public function testSetHTMLMessage()
289
    {
290
        $archangel = new Archangel();
291
        $archangel->setHTMLMessage('<p>An HTML message.</p>');
292
293
        $this->assertAttributeEquals('<p>An HTML message.</p>', 'htmlMessage', $archangel);
294
    }
295
296 View Code Duplication
    public function testAddAttachment()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
297
    {
298
        $archangel = new Archangel();
299
        $archangel->addAttachment('path', 'type');
300
301
        $this->assertAttributeContains(
302
            array('path' => 'path', 'type' => 'type', 'title' => ''),
303
            'attachments',
304
            $archangel
305
        );
306
    }
307
308
    public function testAddAttachmentMultiple()
309
    {
310
        $archangel = new Archangel();
311
        $archangel->addAttachment('pathOne', 'typeOne');
312
        $archangel->addAttachment('pathTwo', 'typeTwo');
313
314
        $this->assertAttributeContains(
315
            array('path' => 'pathOne', 'type' => 'typeOne', 'title' => ''),
316
            'attachments',
317
            $archangel
318
        );
319
        $this->assertAttributeContains(
320
            array('path' => 'pathTwo', 'type' => 'typeTwo', 'title' => ''),
321
            'attachments',
322
            $archangel
323
        );
324
    }
325
326 View Code Duplication
    public function testAddAttachmentWithTitle()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
327
    {
328
        $archangel = new Archangel();
329
        $archangel->addAttachment('path', 'type', 'title');
330
331
        $this->assertAttributeContains(
332
            array('path' => 'path', 'type' => 'type', 'title' => 'title'),
333
            'attachments',
334
            $archangel
335
        );
336
    }
337
338
    public function testSend()
339
    {
340
        $archangel = new Archangel();
341
        $archangel->addTo('[email protected]');
342
        $archangel->setSubject('Test Subject');
343
        $archangel->setPlainMessage('Plain text message');
344
        $response = $archangel->send();
345
346
        $expectedResponse = array(
347
            'to' => '[email protected]',
348
            'subject' => 'Test Subject',
349
            'message' => 'Plain text message',
350
            'headers' => 'X-Mailer: PHP/6.0.0',
351
        );
352
353
        $this->assertEquals($expectedResponse, $response);
354
    }
355
356
    public function testSendInTestMode()
357
    {
358
        $archangel = new Archangel(null, true);
359
        $archangel->addTo('[email protected]');
360
        $archangel->setSubject('Test Subject');
361
        $archangel->setPlainMessage('Plain text message');
362
        $response = $archangel->send();
363
364
        $this->assertTrue($response);
365
366
    }
367
368
    public function testSendFailure()
369
    {
370
        $archangel = new Archangel();
371
        $response = $archangel->send();
372
373
        $this->assertFalse($response);
374
    }
375
376
    /**
377
     * @dataProvider dataCheckRequiredFields
378
     */
379
    public function testCheckRequiredFields(
380
        $expectedResult,
381
        $toAddresses,
382
        $subject,
383
        $plainMessage,
384
        $htmlMessage,
385
        $attachments
386
    ) {
387
        $archangel = new Archangel();
388
389
        if (!empty($toAddresses)) {
390
            $toAddressesProperty = $this->getProtectedProperty('toAddresses');
391
            $toAddressesProperty->setValue($archangel, $toAddresses);
392
        }
393
394
        if (!empty($subject)) {
395
            $subjectProperty = $this->getProtectedProperty('subject');
396
            $subjectProperty->setValue($archangel, $subject);
397
        }
398
399
        if (!empty($plainMessage)) {
400
            $plainMessageProperty = $this->getProtectedProperty('plainMessage');
401
            $plainMessageProperty->setValue($archangel, $plainMessage);
402
        }
403
404
        if (!empty($htmlMessage)) {
405
            $htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
406
            $htmlMessageProperty->setValue($archangel, $htmlMessage);
407
        }
408
409
        if (!empty($attachments)) {
410
            $attachmentsProperty = $this->getProtectedProperty('attachments');
411
            $attachmentsProperty->setValue($archangel, $attachments);
412
        }
413
414
        $checkMethod = $this->getProtectedMethod('checkRequiredFields');
415
        $isValid = $checkMethod->invoke($archangel);
416
417
        if ($expectedResult == true) {
418
            $this->assertTrue($isValid);
419
            return;
420
        }
421
        $this->assertNotTrue($isValid);
422
    }
423
424
    public function dataCheckRequiredFields()
425
    {
426
        return array(
427
            array(
428
                'expectedResult' => false,
429
                'toAddresses' => array(),
430
                'subject' => '',
431
                'plainMessage' => '',
432
                'htmlMessage' => '',
433
                'attachments' => array(),
434
            ),
435
            array(
436
                'expectedResult' => false,
437
                'toAddresses' => array('[email protected]'),
438
                'subject' => '',
439
                'plainMessage' => '',
440
                'htmlMessage' => '',
441
                'attachments' => array(),
442
            ),
443
            array(
444
                'expectedResult' => false,
445
                'toAddresses' => array('[email protected]'),
446
                'subject' => 'Test Subject',
447
                'plainMessage' => '',
448
                'htmlMessage' => '',
449
                'attachments' => array(),
450
            ),
451
            array(
452
                'expectedResult' => false,
453
                'toAddresses' => array(),
454
                'subject' => 'Test Subject',
455
                'plainMessage' => '',
456
                'htmlMessage' => '',
457
                'attachments' => array(),
458
            ),
459
            array(
460
                'expectedResult' => false,
461
                'toAddresses' => array(),
462
                'subject' => 'Test Subject',
463
                'plainMessage' => 'Plain text message',
464
                'htmlMessage' => '',
465
                'attachments' => array(),
466
            ),
467
            array(
468
                'expectedResult' => true,
469
                'toAddresses' => array('[email protected]'),
470
                'subject' => 'Test Subject',
471
                'plainMessage' => 'Plain text message',
472
                'htmlMessage' => '',
473
                'attachments' => array(),
474
            ),
475
            array(
476
                'expectedResult' => true,
477
                'toAddresses' => array('[email protected]'),
478
                'subject' => 'Test Subject',
479
                'plainMessage' => '',
480
                'htmlMessage' => '<p>An HTML message.</p>',
481
                'attachments' => array(),
482
            ),
483
            array(
484
                'expectedResult' => true,
485
                'toAddresses' => array('[email protected]'),
486
                'subject' => 'Test Subject',
487
                'plainMessage' => '',
488
                'htmlMessage' => '',
489
                'attachments' => array(
490
                    array('path' => 'path', 'type' => 'type'),
491
                ),
492
            ),
493
            array(
494
                'expectedResult' => true,
495
                'toAddresses' => array('[email protected]'),
496
                'subject' => 'Test Subject',
497
                'plainMessage' => 'Plain text message',
498
                'htmlMessage' => '<p>An HTML message.</p>',
499
                'attachments' => array(
500
                    array('path' => 'path', 'type' => 'type'),
501
                ),
502
            ),
503
       );
504
    }
505
 
506 View Code Duplication
    public function testBuildTo()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
507
    {
508
        $archangel = new Archangel();
509
        $addressesProperty = $this->getProtectedProperty('toAddresses');
510
        $addressesProperty->setValue($archangel, array('[email protected]'));
511
        $buildMethod = $this->getProtectedMethod('buildTo');
512
        $toAddresses = $buildMethod->invoke($archangel);
513
514
        $this->assertEquals('[email protected]', $toAddresses);
515
    }
516
517 View Code Duplication
    public function testBuildToMultiple()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
518
    {
519
        $archangel = new Archangel();
520
        $addressesProperty = $this->getProtectedProperty('toAddresses');
521
        $addressesProperty->setValue($archangel, array('[email protected]', '[email protected]'));
522
        $buildMethod = $this->getProtectedMethod('buildTo');
523
        $toAddresses = $buildMethod->invoke($archangel);
524
525
        $this->assertEquals('[email protected], [email protected]', $toAddresses);
526
    }
527
528
    public function testBuildMessageEmpty()
529
    {
530
        $archangel = new Archangel();
531
        $plainMessageProperty = $this->getProtectedProperty('plainMessage');
532
        $plainMessageProperty->setValue($archangel, '');
533
        $htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
534
        $htmlMessageProperty->setValue($archangel, '');
535
        $buildMethod = $this->getProtectedMethod('buildMessage');
536
        $builtMessage = $buildMethod->invoke($archangel);
537
538
        $this->assertEmpty($builtMessage);
539
    }
540
541 View Code Duplication
    public function testBuildMessagePlain()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
542
    {
543
        $message = 'Plain text message';
544
545
        $archangel = new Archangel();
546
        $plainMessageProperty = $this->getProtectedProperty('plainMessage');
547
        $plainMessageProperty->setValue($archangel, $message);
548
        $htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
549
        $htmlMessageProperty->setValue($archangel, '');
550
        $buildMethod = $this->getProtectedMethod('buildMessage');
551
        $builtMessage = $buildMethod->invoke($archangel);
552
553
        $this->assertEquals($message, $builtMessage);
554
    }
555
556 View Code Duplication
    public function testBuildMessageHtml()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
557
    {
558
        $message = '<p>HTML Message.</p>';
559
560
        $archangel = new Archangel();
561
        $plainMessageProperty = $this->getProtectedProperty('plainMessage');
562
        $plainMessageProperty->setValue($archangel, '');
563
        $htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
564
        $htmlMessageProperty->setValue($archangel, $message);
565
        $buildMethod = $this->getProtectedMethod('buildMessage');
566
        $builtMessage = $buildMethod->invoke($archangel);
567
568
        $this->assertEquals($message, $builtMessage);
569
    }
570
571
    public function testBuildMessageMultipart()
572
    {
573
        $plainMessage = 'Plain text message';
574
        $htmlMessage = '<p>HTML Message.</p>';
575
576
        $archangel = new Archangel();
577
578
        $boundaryProperty = $this->getProtectedProperty('boundaryAlternative');
579
        $boundary = $boundaryProperty->getValue($archangel);
580
        $plainMsgMethod = $this->getProtectedMethod('buildPlainMessageHeader');
581
        $plainMsgHeaders = $plainMsgMethod->invoke($archangel);
582
        $htmlMsgMethod = $this->getProtectedMethod('buildHtmlMessageHeader');
583
        $htmlMsgHeaders = $htmlMsgMethod->invoke($archangel);
584
585
        $expectedMessage = array();
586
        array_push($expectedMessage, "--{$boundary}");
587
        $expectedMessage = array_merge($expectedMessage, $plainMsgHeaders);
588
        array_push($expectedMessage, $plainMessage);
589
        array_push($expectedMessage, "--{$boundary}");
590
        $expectedMessage = array_merge($expectedMessage, $htmlMsgHeaders);
591
        array_push($expectedMessage, $htmlMessage);
592
        array_push($expectedMessage, "--{$boundary}--");
593
        $expectedMessage = implode(Archangel::LINE_BREAK, $expectedMessage);
594
595
        $plainMessageProperty = $this->getProtectedProperty('plainMessage');
596
        $plainMessageProperty->setValue($archangel, $plainMessage);
597
        $htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
598
        $htmlMessageProperty->setValue($archangel, $htmlMessage);
599
        $buildMethod = $this->getProtectedMethod('buildMessage');
600
        $builtMessage = $buildMethod->invoke($archangel);
601
602
        $this->assertEquals($expectedMessage, $builtMessage);
603
    }
604
605
    public function testBuildMessageWithAttachmentsEmpty()
606
    {
607
        $path = __DIR__ . '/test.txt';
608
        $textContent = 'Dummy Content';
609
        $this->makeTmpFile($path, $textContent);
610
611
        $encodedContent = chunk_split(base64_encode($textContent));
612
        $type = 'text/plain';
613
        $title = 'Test File';
614
615
        $archangel = new Archangel();
616
617
        $boundaryMixedProperty = $this->getProtectedProperty('boundaryMixed');
618
        $boundaryMixed = $boundaryMixedProperty->getValue($archangel);
619
620
        $expectedMessage = array();
621
        array_push($expectedMessage, "--{$boundaryMixed}");
622
        array_push($expectedMessage, "Content-Type: {$type}; name=\"{$title}\"");
623
        array_push($expectedMessage, 'Content-Transfer-Encoding: base64');
624
        array_push($expectedMessage, 'Content-Disposition: attachment');
625
        array_push($expectedMessage, '');
626
        array_push($expectedMessage, $encodedContent);
627
        array_push($expectedMessage, "--{$boundaryMixed}--");
628
        $expectedMessage = implode(Archangel::LINE_BREAK, $expectedMessage);
629
630
        $plainMessageProperty = $this->getProtectedProperty('plainMessage');
631
        $plainMessageProperty->setValue($archangel, '');
632
        $htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
633
        $htmlMessageProperty->setValue($archangel, '');
634
        $attachmentProperty = $this->getProtectedProperty('attachments');
635
        $attachmentProperty->setValue($archangel, array(
636
            array('path' => $path, 'type' => $type, 'title' => $title)
637
        ));
638
        $buildMethod = $this->getProtectedMethod('buildMessageWithAttachments');
639
        $builtMessage = $buildMethod->invoke($archangel);
640
641
        unlink($path);
642
        $this->assertEquals($expectedMessage, $builtMessage);
643
    }
644
645 View Code Duplication
    public function testBuildMessageWithAttachmentsPlain()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
646
    {
647
        $path = __DIR__ . '/test.txt';
648
        $textContent = 'Dummy Content';
649
        $this->makeTmpFile($path, $textContent);
650
651
        $encodedContent = chunk_split(base64_encode($textContent));
652
        $type = 'text/plain';
653
        $title = 'Test File';
654
        $message = 'Plain text message';
655
656
        $archangel = new Archangel();
657
658
        $boundaryMixedProperty = $this->getProtectedProperty('boundaryMixed');
659
        $boundaryMixed = $boundaryMixedProperty->getValue($archangel);
660
        $plainMsgMethod = $this->getProtectedMethod('buildPlainMessageHeader');
661
        $plainMsgHeaders = $plainMsgMethod->invoke($archangel);
662
663
        $expectedMessage = array();
664
        array_push($expectedMessage, "--{$boundaryMixed}");
665
        $expectedMessage = array_merge($expectedMessage, $plainMsgHeaders);
666
        array_push($expectedMessage, $message);
667
        array_push($expectedMessage, "--{$boundaryMixed}");
668
        array_push($expectedMessage, "Content-Type: {$type}; name=\"{$title}\"");
669
        array_push($expectedMessage, 'Content-Transfer-Encoding: base64');
670
        array_push($expectedMessage, 'Content-Disposition: attachment');
671
        array_push($expectedMessage, '');
672
        array_push($expectedMessage, $encodedContent);
673
        array_push($expectedMessage, "--{$boundaryMixed}--");
674
        $expectedMessage = implode(Archangel::LINE_BREAK, $expectedMessage);
675
676
        $plainMessageProperty = $this->getProtectedProperty('plainMessage');
677
        $plainMessageProperty->setValue($archangel, $message);
678
        $htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
679
        $htmlMessageProperty->setValue($archangel, '');
680
        $attachmentProperty = $this->getProtectedProperty('attachments');
681
        $attachmentProperty->setValue($archangel, array(
682
            array('path' => $path, 'type' => $type, 'title' => $title)
683
        ));
684
        $buildMethod = $this->getProtectedMethod('buildMessageWithAttachments');
685
        $builtMessage = $buildMethod->invoke($archangel);
686
687
        unlink($path);
688
        $this->assertEquals($expectedMessage, $builtMessage);
689
    }
690
691 View Code Duplication
    public function testBuildMessageWithAttachmentsHtml()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
692
    {
693
        $path = __DIR__ . '/test.txt';
694
        $textContent = 'Dummy Content';
695
        $this->makeTmpFile($path, $textContent);
696
697
        $encodedContent = chunk_split(base64_encode($textContent));
698
        $type = 'text/plain';
699
        $title = 'Test File';
700
        $message = '<p>HTML Message.</p>';
701
702
        $archangel = new Archangel();
703
704
        $boundaryMixedProperty = $this->getProtectedProperty('boundaryMixed');
705
        $boundaryMixed = $boundaryMixedProperty->getValue($archangel);
706
        $htmlMsgMethod = $this->getProtectedMethod('buildHtmlMessageHeader');
707
        $htmlMsgHeaders = $htmlMsgMethod->invoke($archangel);
708
709
        $expectedMessage = array();
710
        array_push($expectedMessage, "--{$boundaryMixed}");
711
        $expectedMessage = array_merge($expectedMessage, $htmlMsgHeaders);
712
        array_push($expectedMessage, $message);
713
        array_push($expectedMessage, "--{$boundaryMixed}");
714
        array_push($expectedMessage, "Content-Type: {$type}; name=\"{$title}\"");
715
        array_push($expectedMessage, 'Content-Transfer-Encoding: base64');
716
        array_push($expectedMessage, 'Content-Disposition: attachment');
717
        array_push($expectedMessage, '');
718
        array_push($expectedMessage, $encodedContent);
719
        array_push($expectedMessage, "--{$boundaryMixed}--");
720
        $expectedMessage = implode(Archangel::LINE_BREAK, $expectedMessage);
721
722
        $plainMessageProperty = $this->getProtectedProperty('plainMessage');
723
        $plainMessageProperty->setValue($archangel, '');
724
        $htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
725
        $htmlMessageProperty->setValue($archangel, $message);
726
        $attachmentProperty = $this->getProtectedProperty('attachments');
727
        $attachmentProperty->setValue($archangel, array(
728
            array('path' => $path, 'type' => $type, 'title' => $title)
729
        ));
730
        $buildMethod = $this->getProtectedMethod('buildMessageWithAttachments');
731
        $builtMessage = $buildMethod->invoke($archangel);
732
733
        unlink($path);
734
        $this->assertEquals($expectedMessage, $builtMessage);
735
    }
736
737
    public function testBuildMessageWithAttachmentsMultipart()
738
    {
739
        $path = __DIR__ . '/test.txt';
740
        $textContent = 'Dummy Content';
741
        $this->makeTmpFile($path, $textContent);
742
743
        $encodedContent = chunk_split(base64_encode($textContent));
744
        $type = 'text/plain';
745
        $title = 'Test File';
746
        $plainMessage = 'Plain text message';
747
        $htmlMessage = '<p>HTML Message.</p>';
748
749
        $archangel = new Archangel();
750
751
        $boundaryMixedProperty = $this->getProtectedProperty('boundaryMixed');
752
        $boundaryMixed = $boundaryMixedProperty->getValue($archangel);
753
        $boundaryAltProperty = $this->getProtectedProperty('boundaryAlternative');
754
        $boundaryAlternative = $boundaryAltProperty->getValue($archangel);
755
        $plainMsgMethod = $this->getProtectedMethod('buildPlainMessageHeader');
756
        $plainMsgHeaders = $plainMsgMethod->invoke($archangel);
757
        $htmlMsgMethod = $this->getProtectedMethod('buildHtmlMessageHeader');
758
        $htmlMsgHeaders = $htmlMsgMethod->invoke($archangel);
759
760
        $expectedMessage = array();
761
        array_push($expectedMessage, "--{$boundaryMixed}");
762
        array_push($expectedMessage, "Content-Type: multipart/alternative; boundary={$boundaryAlternative}");
763
        array_push($expectedMessage, '');
764
        array_push($expectedMessage, "--{$boundaryAlternative}");
765
        $expectedMessage = array_merge($expectedMessage, $plainMsgHeaders);
766
        array_push($expectedMessage, $plainMessage);
767
        array_push($expectedMessage, "--{$boundaryAlternative}");
768
        $expectedMessage = array_merge($expectedMessage, $htmlMsgHeaders);
769
        array_push($expectedMessage, $htmlMessage);
770
        array_push($expectedMessage, "--{$boundaryAlternative}--");
771
        array_push($expectedMessage, '');
772
        array_push($expectedMessage, "--{$boundaryMixed}");
773
        array_push($expectedMessage, "Content-Type: {$type}; name=\"{$title}\"");
774
        array_push($expectedMessage, 'Content-Transfer-Encoding: base64');
775
        array_push($expectedMessage, 'Content-Disposition: attachment');
776
        array_push($expectedMessage, '');
777
        array_push($expectedMessage, $encodedContent);
778
        array_push($expectedMessage, "--{$boundaryMixed}--");
779
        $expectedMessage = implode(Archangel::LINE_BREAK, $expectedMessage);
780
781
        $plainMessageProperty = $this->getProtectedProperty('plainMessage');
782
        $plainMessageProperty->setValue($archangel, $plainMessage);
783
        $htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
784
        $htmlMessageProperty->setValue($archangel, $htmlMessage);
785
        $attachmentProperty = $this->getProtectedProperty('attachments');
786
        $attachmentProperty->setValue($archangel, array(
787
            array('path' => $path, 'type' => $type, 'title' => $title)
788
        ));
789
        $buildMethod = $this->getProtectedMethod('buildMessageWithAttachments');
790
        $builtMessage = $buildMethod->invoke($archangel);
791
792
        unlink($path);
793
        $this->assertEquals($expectedMessage, $builtMessage);
794
    }
795
796 View Code Duplication
    public function testBuildPlainMessageHeader()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
797
    {
798
        $expectedMessageHeader = array(
799
            'Content-Type: text/plain; charset="iso-8859"',
800
            'Content-Transfer-Encoding: 7bit',
801
            '',
802
        );
803
804
        $archangel = new Archangel();
805
        $buildMethod = $this->getProtectedMethod('buildPlainMessageHeader');
806
        $messageHeader = $buildMethod->invoke($archangel);
807
808
        $this->assertEquals($expectedMessageHeader, $messageHeader);
809
    }
810
811 View Code Duplication
    public function testBuildHtmlMessageHeader()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
812
    {
813
        $expectedMessageHeader = array(
814
            'Content-Type: text/html; charset="iso-8859-1"',
815
            'Content-Transfer-Encoding: 7bit',
816
            '',
817
        );
818
819
        $archangel = new Archangel();
820
        $buildMethod = $this->getProtectedMethod('buildHtmlMessageHeader');
821
        $messageHeader = $buildMethod->invoke($archangel);
822
823
        $this->assertEquals($expectedMessageHeader, $messageHeader);
824
    }
825
826
    /**
827
     * @dataProvider dataBuildHeaders
828
     */
829
    public function testBuildHeaders(
830
        $expectedHeaders,
831
        $headers,
832
        $attachments,
833
        $plainMessage,
834
        $htmlMessage
835
    ) {
836
        $archangel = new Archangel();
837
        $headersProperty = $this->getProtectedProperty('headers');
838
        $headersProperty->setValue($archangel, $headers);
839
840
        if (!empty($attachments)) {
841
            $attachmentsProperty = $this->getProtectedProperty('attachments');
842
            $attachmentsProperty->setValue($archangel, $attachments);
843
        }
844
845
        if (!empty($plainMessage)) {
846
            $plainMessageProperty = $this->getProtectedProperty('plainMessage');
847
            $plainMessageProperty->setValue($archangel, $plainMessage);
848
        }
849
850
        if (!empty($htmlMessage)) {
851
            $htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
852
            $htmlMessageProperty->setValue($archangel, $htmlMessage);
853
        }
854
855
        $buildHeadersMethod = $this->getProtectedMethod('buildHeaders');
856
        $builtHeaders = $buildHeadersMethod->invoke($archangel);
857
858
        $this->assertEquals($expectedHeaders, $builtHeaders);
859
    }
860
861
    public function dataBuildHeaders()
862
    {
863
        return array(
864
            array(
865
                'expectedHeaders' =>
866
                    "From: [email protected]\r\n" .
867
                    "X-Mailer: PHP/6.0.0",
868
                'headers' => array(
869
                    'From' => '[email protected]',
870
                    'X-Mailer' => sprintf('PHP/%s', phpversion())
871
                ),
872
                'attachments' => null,
873
                'plainMessage' => true,
874
                'htmlMessage' => null,
875
            ),
876
            array(
877
                'expectedHeaders' =>
878
                    "CC: [email protected], [email protected]\r\n" .
879
                    "From: [email protected]\r\n" .
880
                    "X-Mailer: PHP/6.0.0",
881
                'headers' => array(
882
                    'CC' => array('[email protected]', '[email protected]'),
883
                    'From' => '[email protected]',
884
                    'X-Mailer' => sprintf('PHP/%s', phpversion())
885
                ),
886
                'attachments' => null,
887
                'plainMessage' => true,
888
                'htmlMessage' => null,
889
            ),
890
            array(
891
                'expectedHeaders' =>
892
                    "BCC: [email protected], [email protected]\r\n" .
893
                    "From: [email protected]\r\n" .
894
                    "X-Mailer: PHP/6.0.0",
895
                'headers' => array(
896
                    'BCC' => array('[email protected]', '[email protected]'),
897
                    'From' => '[email protected]',
898
                    'X-Mailer' => sprintf('PHP/%s', phpversion())
899
                ),
900
                'attachments' => null,
901
                'plainMessage' => true,
902
                'htmlMessage' => null,
903
            ),
904
            array(
905
                'expectedHeaders' =>
906
                    "From: [email protected]\r\n" .
907
                    "X-Mailer: PHP/6.0.0\r\n" .
908
                    "Content-Type: multipart/mixed; boundary=\"PHP-mixed-1234567890123\"",
909
                'headers' => array(
910
                    'From' => '[email protected]',
911
                    'X-Mailer' => sprintf('PHP/%s', phpversion())
912
                ),
913
                'attachments' => true,
914
                'plainMessage' => true,
915
                'htmlMessage' => null,
916
            ),
917
            array(
918
                'expectedHeaders' =>
919
                    "From: [email protected]\r\n" .
920
                    "X-Mailer: PHP/6.0.0\r\n" .
921
                    "Content-Type: multipart/alternative; boundary=\"PHP-alternative-1234567890123\"",
922
                'headers' => array(
923
                    'From' => '[email protected]',
924
                    'X-Mailer' => sprintf('PHP/%s', phpversion())
925
                ),
926
                'attachments' => null,
927
                'plainMessage' => true,
928
                'htmlMessage' => true,
929
            ),
930
            array(
931
                'expectedHeaders' =>
932
                    "From: [email protected]\r\n" .
933
                    "X-Mailer: PHP/6.0.0\r\n" .
934
                    "Content-type: text/html; charset=\"iso-8859-1\"",
935
                'headers' => array(
936
                    'From' => '[email protected]',
937
                    'X-Mailer' => sprintf('PHP/%s', phpversion())
938
                ),
939
                'attachments' => null,
940
                'plainMessage' => null,
941
                'htmlMessage' => true,
942
            ),
943
        );
944
    }
945
946
    public function testBuildAttachmentContent()
947
    {
948
        $path = __DIR__ . '/test.txt';
949
        $textContent = 'Dummy Content';
950
        $this->makeTmpFile($path, $textContent);
951
952
        $expectedContent = chunk_split(base64_encode($textContent));
953
954
        $archangel = new Archangel();
955
        $buildMethod = $this->getProtectedMethod('buildAttachmentContent');
956
        $content = $buildMethod->invokeArgs($archangel, array($path));
957
958
        unlink($path);
959
        $this->assertEquals($expectedContent, $content);
960
    }
961
962
    public function testBuildAttachmentContentFailure()
963
    {
964
        $archangel = new Archangel();
965
        $buildMethod = $this->getProtectedMethod('buildAttachmentContent');
966
        $content = $buildMethod->invokeArgs($archangel, array('INVALID_PATH'));
967
968
        $this->assertEmpty($content);
969
    }
970
971
    protected function getProtectedProperty($property)
972
    {
973
        $reflectedArchangel = new ReflectionClass('Jacobemerick\Archangel\Archangel');
974
        $reflectedProperty = $reflectedArchangel->getProperty($property);
975
        $reflectedProperty->setAccessible(true);
976
977
        return $reflectedProperty;
978
    }
979
980
    protected function getProtectedMethod($method)
981
    {
982
        $reflectedArchangel = new ReflectionClass('Jacobemerick\Archangel\Archangel');
983
        $reflectedMethod = $reflectedArchangel->getMethod($method);
984
        $reflectedMethod->setAccessible(true);
985
986
        return $reflectedMethod;
987
    }
988
989
    protected function makeTmpFile($path, $content)
990
    {
991
        $handle = fopen($path, 'w');
992
        fwrite($handle, $content);
993
        fclose($handle);
994
    }
995
}
996