Completed
Push — master ( becc57...d128ac )
by Jacob
10:02
created

ArchangelTest::testCheckRequiredFields()   C

Complexity

Conditions 7
Paths 64

Size

Total Lines 44
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 44
rs 6.7272
cc 7
eloc 29
nc 64
nop 6
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
    /**
285
     * @dataProvider dataCheckRequiredFields
286
     */
287
    public function testCheckRequiredFields(
288
        $expectedResult,
289
        $toAddresses,
290
        $subject,
291
        $plainMessage,
292
        $htmlMessage,
293
        $attachments
294
    ) {
295
        $archangel = new Archangel();
296
297
        if (!empty($toAddresses)) {
298
            $toAddressesProperty = $this->getProtectedProperty('toAddresses');
299
            $toAddressesProperty->setValue($archangel, $toAddresses);
300
        }
301
302
        if (!empty($subject)) {
303
            $subjectProperty = $this->getProtectedProperty('subject');
304
            $subjectProperty->setValue($archangel, $subject);
305
        }
306
307
        if (!empty($plainMessage)) {
308
            $plainMessageProperty = $this->getProtectedProperty('plainMessage');
309
            $plainMessageProperty->setValue($archangel, $plainMessage);
310
        }
311
312
        if (!empty($htmlMessage)) {
313
            $htmlMessageProperty = $this->getProtectedProperty('htmlMessage');
314
            $htmlMessageProperty->setValue($archangel, $htmlMessage);
315
        }
316
317
        if (!empty($attachments)) {
318
            $attachmentsProperty = $this->getProtectedProperty('attachments');
319
            $attachmentsProperty->setValue($archangel, $attachments);
320
        }
321
322
        $checkMethod = $this->getProtectedMethod('checkRequiredFields');
323
        $isValid = $checkMethod->invoke($archangel);
324
325
        if ($expectedResult == true) {
326
            $this->assertTrue($isValid);
327
            return;
328
        }
329
        $this->assertNotTrue($isValid);
330
    }
331
332
    public function dataCheckRequiredFields()
333
    {
334
        return array(
335
            array(
336
                'expectedResult' => false,
337
                'toAddresses' => array(),
338
                'subject' => '',
339
                'plainMessage' => '',
340
                'htmlMessage' => '',
341
                'attachments' => array(),
342
            ),
343
            array(
344
                'expectedResult' => false,
345
                'toAddresses' => array('[email protected]'),
346
                'subject' => '',
347
                'plainMessage' => '',
348
                'htmlMessage' => '',
349
                'attachments' => array(),
350
            ),
351
            array(
352
                'expectedResult' => false,
353
                'toAddresses' => array('[email protected]'),
354
                'subject' => 'Test Subject',
355
                'plainMessage' => '',
356
                'htmlMessage' => '',
357
                'attachments' => array(),
358
            ),
359
            array(
360
                'expectedResult' => false,
361
                'toAddresses' => array(),
362
                'subject' => 'Test Subject',
363
                'plainMessage' => '',
364
                'htmlMessage' => '',
365
                'attachments' => array(),
366
            ),
367
            array(
368
                'expectedResult' => false,
369
                'toAddresses' => array(),
370
                'subject' => 'Test Subject',
371
                'plainMessage' => 'Plain text message',
372
                'htmlMessage' => '',
373
                'attachments' => array(),
374
            ),
375
            array(
376
                'expectedResult' => true,
377
                'toAddresses' => array('[email protected]'),
378
                'subject' => 'Test Subject',
379
                'plainMessage' => 'Plain text message',
380
                'htmlMessage' => '',
381
                'attachments' => array(),
382
            ),
383
            array(
384
                'expectedResult' => true,
385
                'toAddresses' => array('[email protected]'),
386
                'subject' => 'Test Subject',
387
                'plainMessage' => '',
388
                'htmlMessage' => '<p>An HTML message.</p>',
389
                'attachments' => array(),
390
            ),
391
            array(
392
                'expectedResult' => true,
393
                'toAddresses' => array('[email protected]'),
394
                'subject' => 'Test Subject',
395
                'plainMessage' => '',
396
                'htmlMessage' => '',
397
                'attachments' => array(
398
                    array('path' => 'path', 'type' => 'type'),
399
                ),
400
            ),
401
            array(
402
                'expectedResult' => true,
403
                'toAddresses' => array('[email protected]'),
404
                'subject' => 'Test Subject',
405
                'plainMessage' => 'Plain text message',
406
                'htmlMessage' => '<p>An HTML message.</p>',
407
                'attachments' => array(
408
                    array('path' => 'path', 'type' => 'type'),
409
                ),
410
            ),
411
       );
412
    }
413
414 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...
415
    {
416
        $archangel = new Archangel();
417
        $boundaryMethod = $this->getProtectedMethod('getBoundary');
418
        $boundary = $boundaryMethod->invoke($archangel);
419
        $expectedBoundary = sprintf('PHP-mixed-%s', uniqid());
420
421
        $this->assertEquals($expectedBoundary, $boundary);
422
    }
423
424 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...
425
    {
426
        $archangel = new Archangel();
427
        $boundaryMethod = $this->getProtectedMethod('getAlternativeBoundary');
428
        $boundary = $boundaryMethod->invoke($archangel);
429
        $expectedBoundary = sprintf('PHP-alternative-%s', uniqid());
430
431
        $this->assertEquals($expectedBoundary, $boundary);
432
    }
433
434
    protected function getProtectedProperty($property)
435
    {
436
        $reflectedArchangel = new ReflectionClass('Jacobemerick\Archangel\Archangel');
437
        $reflectedProperty = $reflectedArchangel->getProperty($property);
438
        $reflectedProperty->setAccessible(true);
439
440
        return $reflectedProperty;
441
    }
442
443
    protected function getProtectedMethod($method)
444
    {
445
        $reflectedArchangel = new ReflectionClass('Jacobemerick\Archangel\Archangel');
446
        $reflectedMethod = $reflectedArchangel->getMethod($method);
447
        $reflectedMethod->setAccessible(true);
448
449
        return $reflectedMethod;
450
    }
451
}
452