Completed
Push — master ( d128ac...ee71d8 )
by Jacob
05:56
created

ArchangelTest::testSend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 17
rs 9.4285
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
namespace Jacobemerick\Archangel;
4
5
use PHPUnit_Framework_TestCase;
6
use ReflectionClass;
7
8
class ArchangelTest extends PHPUnit_Framework_TestCase
9
{
10
11
    public function testIsInstanceOfArchangel()
12
    {
13
        $archangel = new Archangel();
14
15
        $this->assertInstanceOf('Jacobemerick\Archangel\Archangel', $archangel);
16
    }
17
18
    public function testIsLoggerAwareInterface()
19
    {
20
        $archangel = new Archangel();
21
22
        $this->assertInstanceOf('Psr\Log\LoggerAwareInterface', $archangel);
23
    }
24
25
    public function testConstructSetsDefaultMailer()
26
    {
27
        $archangel = new Archangel();
28
        $mailer = sprintf('PHP/%s', phpversion());
29
        $headers = array('X-Mailer' => $mailer);
30
31
        $this->assertAttributeEquals($headers, 'headers', $archangel);
32
    }
33
34
    public function testConstructOverridesMailer()
35
    {
36
        $archangel = new Archangel('AwesomeMailer');
37
        $headers = array('X-Mailer' => 'AwesomeMailer');
38
39
        $this->assertAttributeEquals($headers, 'headers', $archangel);
40
    }
41
42
    public function testConstructSetsNullLogger()
43
    {
44
        $archangel = new Archangel();
45
46
        $this->assertAttributeInstanceOf('Psr\Log\NullLogger', 'logger', $archangel);
47
    }
48
49
    public function testSetLogger()
50
    {
51
        $logger = $this->getMock('Psr\Log\LoggerInterface');
52
        $archangel = new Archangel();
53
        $archangel->setLogger($logger);
54
55
        $this->assertAttributeSame($logger, 'logger', $archangel);
56
    }
57
58
    public function testAddTo()
59
    {
60
        $archangel = new Archangel();
61
        $archangel->addTo('[email protected]');
62
63
        $this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
64
    }
65
66
    public function testAddToMultiple()
67
    {
68
        $archangel = new Archangel();
69
        $archangel->addTo('[email protected]');
70
        $archangel->addTo('[email protected]');
71
72
        $this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
73
        $this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
74
    }
75
76
    public function testAddToWithTitle()
77
    {
78
        $archangel = new Archangel();
79
        $archangel->addTo('[email protected]', 'Mr. Test Alot');
80
81
        $this->assertAttributeContains('"Mr. Test Alot" <[email protected]>', 'toAddresses', $archangel);
82
    }
83
84
    public function testAddCc()
85
    {
86
        $archangel = new Archangel();
87
        $archangel->addCc('[email protected]');
88
89
        $this->assertAttributeContains('[email protected]', 'ccAddresses', $archangel);
90
    }
91
92
    public function testAddCcMultiple()
93
    {
94
        $archangel = new Archangel();
95
        $archangel->addCc('[email protected]');
96
        $archangel->addCc('[email protected]');
97
98
        $this->assertAttributeContains('[email protected]', 'ccAddresses', $archangel);
99
        $this->assertAttributeContains('[email protected]', 'ccAddresses', $archangel);
100
    }
101
102
    public function testAddCcWithTitle()
103
    {
104
        $archangel = new Archangel();
105
        $archangel->addCc('[email protected]', 'Mr. Test Alot');
106
107
        $this->assertAttributeContains('"Mr. Test Alot" <[email protected]>', 'ccAddresses', $archangel);
108
    }
109
110
    public function testAddBcc()
111
    {
112
        $archangel = new Archangel();
113
        $archangel->addBcc('[email protected]');
114
115
        $this->assertAttributeContains('[email protected]', 'bccAddresses', $archangel);
116
    }
117
118
    public function testAddBccMultiple()
119
    {
120
        $archangel = new Archangel();
121
        $archangel->addBcc('[email protected]');
122
        $archangel->addBcc('[email protected]');
123
124
        $this->assertAttributeContains('[email protected]', 'bccAddresses', $archangel);
125
        $this->assertAttributeContains('[email protected]', 'bccAddresses', $archangel);
126
    }
127
128
    public function testAddBccWithTitle()
129
    {
130
        $archangel = new Archangel();
131
        $archangel->addBcc('[email protected]', 'Mr. Test Alot');
132
133
        $this->assertAttributeContains('"Mr. Test Alot" <[email protected]>', 'bccAddresses', $archangel);
134
    }
135
136 View Code Duplication
    public function testSetFrom()
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...
137
    {
138
        $archangel = new Archangel();
139
        $archangel->setFrom('[email protected]');
140
        $headersProperty = $this->getProtectedProperty('headers');
141
        $headers = $headersProperty->getValue($archangel);
142
143
        $this->assertArraySubset(array('From' => '[email protected]'), $headers);
144
    }
145
146 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...
147
    {
148
        $archangel = new Archangel();
149
        $archangel->setFrom('[email protected]');
150
        $archangel->setFrom('[email protected]');
151
        $headersProperty = $this->getProtectedProperty('headers');
152
        $headers = $headersProperty->getValue($archangel);
153
154
        $this->assertArraySubset(array('From' => '[email protected]'), $headers);
155
        $this->assertNotContains('[email protected]', $headers);
156
    }
157
158 View Code Duplication
    public function testSetFromWithTitle()
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...
159
    {
160
        $archangel = new Archangel();
161
        $archangel->setFrom('[email protected]', 'Mr. Test Alot');
162
        $headersProperty = $this->getProtectedProperty('headers');
163
        $headers = $headersProperty->getValue($archangel);
164
165
        $this->assertArraySubset(array('From' => '"Mr. Test Alot" <[email protected]>'), $headers);
166
    }
167
168 View Code Duplication
    public function testSetReplyTo()
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...
169
    {
170
        $archangel = new Archangel();
171
        $archangel->setReplyTo('[email protected]');
172
        $headersProperty = $this->getProtectedProperty('headers');
173
        $headers = $headersProperty->getValue($archangel);
174
175
        $this->assertArraySubset(array('Reply-To' => '[email protected]'), $headers);
176
    }
177
178 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...
179
    {
180
        $archangel = new Archangel();
181
        $archangel->setReplyTo('[email protected]');
182
        $archangel->setReplyTo('[email protected]');
183
        $headersProperty = $this->getProtectedProperty('headers');
184
        $headers = $headersProperty->getValue($archangel);
185
186
        $this->assertArraySubset(array('Reply-To' => '[email protected]'), $headers);
187
        $this->assertNotContains('[email protected]', $headers);
188
    }
189
190 View Code Duplication
    public function testSetReplyToWithTitle()
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...
191
    {
192
        $archangel = new Archangel();
193
        $archangel->setReplyTo('[email protected]', 'Mr. Test Alot');
194
        $headersProperty = $this->getProtectedProperty('headers');
195
        $headers = $headersProperty->getValue($archangel);
196
197
        $this->assertArraySubset(array('Reply-To' => '"Mr. Test Alot" <[email protected]>'), $headers);
198
    }
199
200 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...
201
    {
202
        $archangel = new Archangel();
203
        $formatMethod = $this->getProtectedMethod('formatEmailAddress');
204
        $formattedEmail = $formatMethod->invokeArgs($archangel, array('[email protected]', ''));
205
206
        $this->assertEquals('[email protected]', $formattedEmail);
207
    }
208
209 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...
210
    {
211
        $archangel = new Archangel();
212
        $formatMethod = $this->getProtectedMethod('formatEmailAddress');
213
        $formattedEmail = $formatMethod->invokeArgs($archangel, array('[email protected]', 'Mr. Test Alot'));
214
215
        $this->assertEquals('"Mr. Test Alot" <[email protected]>', $formattedEmail);
216
    }
217
218
    public function testSetSubject()
219
    {
220
        $archangel = new Archangel();
221
        $archangel->setSubject('Test Subject');
222
223
        $this->assertAttributeEquals('Test Subject', 'subject', $archangel);
224
    }
225
226
    public function testSetPlainMessage()
227
    {
228
        $archangel = new Archangel();
229
        $archangel->setPlainMessage('Plain text message');
230
231
        $this->assertAttributeEquals('Plain text message', 'plainMessage', $archangel);
232
    }
233
234
    public function testSetHTMLMessage()
235
    {
236
        $archangel = new Archangel();
237
        $archangel->setHTMLMessage('<p>An HTML message.</p>');
238
239
        $this->assertAttributeEquals('<p>An HTML message.</p>', 'htmlMessage', $archangel);
240
    }
241
242 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...
243
    {
244
        $archangel = new Archangel();
245
        $archangel->addAttachment('path', 'type');
246
247
        $this->assertAttributeContains(
248
            array('path' => 'path', 'type' => 'type', 'title' => ''),
249
            'attachments',
250
            $archangel
251
        );
252
    }
253
254
    public function testAddAttachmentMultiple()
255
    {
256
        $archangel = new Archangel();
257
        $archangel->addAttachment('pathOne', 'typeOne');
258
        $archangel->addAttachment('pathTwo', 'typeTwo');
259
260
        $this->assertAttributeContains(
261
            array('path' => 'pathOne', 'type' => 'typeOne', 'title' => ''),
262
            'attachments',
263
            $archangel
264
        );
265
        $this->assertAttributeContains(
266
            array('path' => 'pathTwo', 'type' => 'typeTwo', 'title' => ''),
267
            'attachments',
268
            $archangel
269
        );
270
    }
271
272 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...
273
    {
274
        $archangel = new Archangel();
275
        $archangel->addAttachment('path', 'type', 'title');
276
277
        $this->assertAttributeContains(
278
            array('path' => 'path', 'type' => 'type', 'title' => 'title'),
279
            'attachments',
280
            $archangel
281
        );
282
    }
283
284
    public function testSend()
285
    {
286
        $archangel = new Archangel();
287
        $archangel->addTo('[email protected]');
288
        $archangel->setSubject('Test Subject');
289
        $archangel->setPlainMessage('Plain text message');
290
        $response = $archangel->send();
291
292
        $expectedResponse = array(
293
            'to' => '[email protected]',
294
            'subject' => 'Test Subject',
295
            'message' => 'Plain text message' . Archangel::LINE_BREAK,
296
            'headers' => 'X-Mailer: PHP/6.0.0' . Archangel::LINE_BREAK,
297
        );
298
299
        $this->assertEquals($expectedResponse, $response);
300
    }
301
302
    /**
303
     * @dataProvider dataCheckRequiredFields
304
     */
305
    public function testCheckRequiredFields(
306
        $expectedResult,
307
        $toAddresses,
308
        $subject,
309
        $plainMessage,
310
        $htmlMessage,
311
        $attachments
312
    ) {
313
        $archangel = new Archangel();
314
315
        if (!empty($toAddresses)) {
316
            $toAddressesProperty = $this->getProtectedProperty('toAddresses');
317
            $toAddressesProperty->setValue($archangel, $toAddresses);
318
        }
319
320
        if (!empty($subject)) {
321
            $subjectProperty = $this->getProtectedProperty('subject');
322
            $subjectProperty->setValue($archangel, $subject);
323
        }
324
325
        if (!empty($plainMessage)) {
326
            $plainMessageProperty = $this->getProtectedProperty('plainMessage');
327
            $plainMessageProperty->setValue($archangel, $plainMessage);
328
        }
329
330
        if (!empty($htmlMessage)) {
331
            $htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
332
            $htmlMessageProperty->setValue($archangel, $htmlMessage);
333
        }
334
335
        if (!empty($attachments)) {
336
            $attachmentsProperty = $this->getProtectedProperty('attachments');
337
            $attachmentsProperty->setValue($archangel, $attachments);
338
        }
339
340
        $checkMethod = $this->getProtectedMethod('checkRequiredFields');
341
        $isValid = $checkMethod->invoke($archangel);
342
343
        if ($expectedResult == true) {
344
            $this->assertTrue($isValid);
345
            return;
346
        }
347
        $this->assertNotTrue($isValid);
348
    }
349
350
    public function dataCheckRequiredFields()
351
    {
352
        return array(
353
            array(
354
                'expectedResult' => false,
355
                'toAddresses' => array(),
356
                'subject' => '',
357
                'plainMessage' => '',
358
                'htmlMessage' => '',
359
                'attachments' => array(),
360
            ),
361
            array(
362
                'expectedResult' => false,
363
                'toAddresses' => array('[email protected]'),
364
                'subject' => '',
365
                'plainMessage' => '',
366
                'htmlMessage' => '',
367
                'attachments' => array(),
368
            ),
369
            array(
370
                'expectedResult' => false,
371
                'toAddresses' => array('[email protected]'),
372
                'subject' => 'Test Subject',
373
                'plainMessage' => '',
374
                'htmlMessage' => '',
375
                'attachments' => array(),
376
            ),
377
            array(
378
                'expectedResult' => false,
379
                'toAddresses' => array(),
380
                'subject' => 'Test Subject',
381
                'plainMessage' => '',
382
                'htmlMessage' => '',
383
                'attachments' => array(),
384
            ),
385
            array(
386
                'expectedResult' => false,
387
                'toAddresses' => array(),
388
                'subject' => 'Test Subject',
389
                'plainMessage' => 'Plain text message',
390
                'htmlMessage' => '',
391
                'attachments' => array(),
392
            ),
393
            array(
394
                'expectedResult' => true,
395
                'toAddresses' => array('[email protected]'),
396
                'subject' => 'Test Subject',
397
                'plainMessage' => 'Plain text message',
398
                'htmlMessage' => '',
399
                'attachments' => array(),
400
            ),
401
            array(
402
                'expectedResult' => true,
403
                'toAddresses' => array('[email protected]'),
404
                'subject' => 'Test Subject',
405
                'plainMessage' => '',
406
                'htmlMessage' => '<p>An HTML message.</p>',
407
                'attachments' => array(),
408
            ),
409
            array(
410
                'expectedResult' => true,
411
                'toAddresses' => array('[email protected]'),
412
                'subject' => 'Test Subject',
413
                'plainMessage' => '',
414
                'htmlMessage' => '',
415
                'attachments' => array(
416
                    array('path' => 'path', 'type' => 'type'),
417
                ),
418
            ),
419
            array(
420
                'expectedResult' => true,
421
                'toAddresses' => array('[email protected]'),
422
                'subject' => 'Test Subject',
423
                'plainMessage' => 'Plain text message',
424
                'htmlMessage' => '<p>An HTML message.</p>',
425
                'attachments' => array(
426
                    array('path' => 'path', 'type' => 'type'),
427
                ),
428
            ),
429
       );
430
    }
431
432 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...
433
    {
434
        $archangel = new Archangel();
435
        $addressesProperty = $this->getProtectedProperty('toAddresses');
436
        $addressesProperty->setValue($archangel, array('[email protected]'));
437
        $buildMethod = $this->getProtectedMethod('buildTo');
438
        $toAddresses = $buildMethod->invoke($archangel);
439
440
        $this->assertEquals('[email protected]', $toAddresses);
441
    }
442
443 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...
444
    {
445
        $archangel = new Archangel();
446
        $addressesProperty = $this->getProtectedProperty('toAddresses');
447
        $addressesProperty->setValue($archangel, array('[email protected]', '[email protected]'));
448
        $buildMethod = $this->getProtectedMethod('buildTo');
449
        $toAddresses = $buildMethod->invoke($archangel);
450
451
        $this->assertEquals('[email protected], [email protected]', $toAddresses);
452
    }
453
454 View Code Duplication
    public function testGetBoundary()
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...
455
    {
456
        $archangel = new Archangel();
457
        $boundaryMethod = $this->getProtectedMethod('getBoundary');
458
        $boundary = $boundaryMethod->invoke($archangel);
459
        $expectedBoundary = sprintf('PHP-mixed-%s', uniqid());
460
461
        $this->assertEquals($expectedBoundary, $boundary);
462
    }
463
464 View Code Duplication
    public function testGetAlternativeBoundary()
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...
465
    {
466
        $archangel = new Archangel();
467
        $boundaryMethod = $this->getProtectedMethod('getAlternativeBoundary');
468
        $boundary = $boundaryMethod->invoke($archangel);
469
        $expectedBoundary = sprintf('PHP-alternative-%s', uniqid());
470
471
        $this->assertEquals($expectedBoundary, $boundary);
472
    }
473
474
    protected function getProtectedProperty($property)
475
    {
476
        $reflectedArchangel = new ReflectionClass('Jacobemerick\Archangel\Archangel');
477
        $reflectedProperty = $reflectedArchangel->getProperty($property);
478
        $reflectedProperty->setAccessible(true);
479
480
        return $reflectedProperty;
481
    }
482
483
    protected function getProtectedMethod($method)
484
    {
485
        $reflectedArchangel = new ReflectionClass('Jacobemerick\Archangel\Archangel');
486
        $reflectedMethod = $reflectedArchangel->getMethod($method);
487
        $reflectedMethod->setAccessible(true);
488
489
        return $reflectedMethod;
490
    }
491
}
492