Completed
Push — master ( d068c5...99fa9d )
by Antonio
03:22 queued 58s
created

TransportFactoryTest::testCreateTransport()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 64
Code Lines 34

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 64
rs 9.3957
cc 1
eloc 34
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Da\Mailer\Test\Transport;
3
4
use Da\Mailer\Transport\MailTransport;
5
use Da\Mailer\Transport\MailTransportFactory;
6
use Da\Mailer\Transport\SendMailTransport;
7
use Da\Mailer\Transport\SendMailTransportFactory;
8
use Da\Mailer\Transport\SmtpTransport;
9
use Da\Mailer\Transport\SmtpTransportFactory;
10
use Da\Mailer\Transport\TransportFactory;
11
use Da\Mailer\Transport\TransportInterface;
12
use PHPUnit_Framework_TestCase;
13
14
class TransportFactoryTest extends PHPUnit_Framework_TestCase
15
{
16
    public function testCreateTransport()
17
    {
18
        $smtpConfig = [
19
            'host' => 'localhost',
20
            'port' => 587,
21
            'options' => [
22
                'username' => 'Obiwoan',
23
                'password' => 'Kenovi',
24
                'encryption' => 'ssl',
25
                'authMode' => 'Plain'
26
            ]
27
        ];
28
        $mailConfig = ['options' => '-f%s'];
29
        $sendMailConfig = ['options' => '/usr/sbin/sendmail -s'];
30
31
        $smtpFactory = TransportFactory::create($smtpConfig, TransportInterface::TYPE_SMTP);
32
33
        $this->assertTrue($smtpFactory instanceof SmtpTransportFactory);
34
35
        $smtp = $smtpFactory->create();
36
37
        $this->assertTrue($smtp instanceof SmtpTransport);
38
39
        /**
40
         * @var \Swift_SmtpTransport $swift
41
         */
42
        $swift = $smtp->getSwiftTransportInstance();
43
44
        $this->assertEquals($smtpConfig['host'], $swift->getHost());
45
        $this->assertEquals($smtpConfig['port'], $swift->getPort());
46
        $this->assertEquals($smtpConfig['options']['username'], $swift->getUsername());
47
        $this->assertEquals($smtpConfig['options']['password'], $swift->getPassword());
48
        $this->assertEquals($smtpConfig['options']['encryption'], $swift->getEncryption());
49
        $this->assertEquals($smtpConfig['options']['authMode'], $swift->getAuthMode());
50
51
        $mailFactory = TransportFactory::create($mailConfig, TransportInterface::TYPE_MAIL);
52
53
        $this->assertTrue($mailFactory instanceof MailTransportFactory);
54
55
        $mail = $mailFactory->create();
56
57
        $this->assertTrue($mail instanceof MailTransport);
58
59
        /**
60
         * @var \Swift_MailTransport $swift
61
         */
62
        $swift = $mail->getSwiftTransportInstance();
63
64
        $this->assertEquals($mailConfig['options'], $swift->getExtraParams());
65
66
        $sendMailFactory = TransportFactory::create($sendMailConfig, TransportInterface::TYPE_SEND_MAIL);
67
68
        $this->assertTrue($sendMailFactory instanceof SendMailTransportFactory);
69
70
        $sendMail = $sendMailFactory->create();
71
72
        $this->assertTrue($sendMail instanceof SendMailTransport);
73
        /**
74
         * @var \Swift_SendMailTransport $swift
75
         */
76
        $swift = $sendMail->getSwiftTransportInstance();
77
78
        $this->assertEquals($sendMailConfig['options'], $swift->getCommand());
79
    }
80
81
    public function testDefaultParameters()
82
    {
83
        $mail = (new MailTransportFactory([]))->create();
84
        $sendMail = (new SendMailTransportFactory([]))->create();
85
86
        /**
87
         * @var \Swift_MailTransport $swift
88
         */
89
        $swift = $mail->getSwiftTransportInstance();
90
91
        $this->assertEquals('-f%s', $swift->getExtraParams());
92
93
        /**
94
         * @var \Swift_SendMailTransport $swift
95
         */
96
        $swift = $sendMail->getSwiftTransportInstance();
97
98
        $this->assertEquals('/usr/sbin/sendmail -bs', $swift->getCommand());
99
    }
100
101
    /**
102
     * @expectedException \Da\Mailer\Exception\InvalidTransportTypeArgumentException
103
     */
104
    public function testInvalidTransportTypeArgumentException()
105
    {
106
        $transport = TransportFactory::create([], 'starWars');
0 ignored issues
show
Unused Code introduced by
$transport is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
107
    }
108
}
109