Completed
Push — master ( df6049...d068c5 )
by Antonio
07:08 queued 04:48
created

TestSwiftPlugin   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 8
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0
Metric Value
wmc 1
lcom 0
cbo 0
dl 0
loc 8
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A commandSent() 0 4 1
1
<?php
2
namespace Da\Mailer\Test;
3
4
use Da\Mailer\Mailer;
5
use Da\Mailer\Transport\MailTransport;
6
use Da\Mailer\Transport\MailTransportFactory;
7
use Da\Mailer\Transport\SendMailTransport;
8
use Da\Mailer\Transport\SendMailTransportFactory;
9
use Da\Mailer\Transport\SmtpTransport;
10
use Da\Mailer\Transport\SmtpTransportFactory;
11
use Da\Mailer\Transport\TransportFactory;
12
use Da\Mailer\Test\Fixture\FixtureHelper;
13
use Mockery;
14
use PHPUnit_Framework_TestCase;
15
use Swift_Events_CommandEvent;
16
use Swift_Mailer;
17
18
/**
19
 * @runTestsInSeparateProcesses
20
 * @preserveGlobalState disabled
21
 */
22
class MailerTest extends PHPUnit_Framework_TestCase
23
{
24
    public function testFromMailMessageMethod()
25
    {
26
        $mailMessage = FixtureHelper::getMailMessage();
27
        $options = [
28
            'host' => $mailMessage->host,
29
            'port' => $mailMessage->port,
30
            'options' => $mailMessage->transportOptions,
31
        ];
32
33
        Mockery::mock('alias:' . TransportFactory::class)
34
            ->shouldReceive('create')
35
            ->once()
36
            ->withArgs([$options, $mailMessage->transportType])
37
            ->andReturnUsing(
38
                function ($options) {
39
                    return new SmtpTransportFactory($options);
40
                }
41
            );
42
43
        $mailer = Mailer::fromMailMessage($mailMessage);
44
45
        $this->assertTrue($mailer instanceof Mailer);
46
        $this->assertTrue($mailer->getTransport() instanceof SmtpTransport);
47
    }
48
49
    public function testConstructionOptions()
50
    {
51
        $mailMessage = FixtureHelper::getMailMessage();
52
        $mailTransport = (new MailTransportFactory([]))->create();
53
        $mailer = new Mailer($mailTransport, true);
54
55
        $this->assertTrue($mailer->getTransport() instanceof MailTransport);
56
        $this->assertTrue($mailer->getSwiftMailerInstance() instanceof Swift_Mailer);
57
        $this->assertTrue($mailer->getLog() === null);
58
59
        $sendMailTransport = (new SendMailTransportFactory([]))->create();
60
        $mailer->updateTransport($sendMailTransport);
61
62
        $this->assertTrue($mailer->getTransport() instanceof SendMailTransport);
63
        $this->assertTrue($mailer->getSwiftMailerInstance() instanceof Swift_Mailer);
64
65
        $plugin = new TestSwiftPlugin();
66
        $this->assertSame($mailer, $mailer->addPlugin($plugin));
67
        $this->assertSame($mailer, $mailer->registerPlugins());
68
        $this->assertEquals("", $mailer->getLog());
69
        // is dry run, should be fine sending as it will return number of message sent
70
71
        $this->assertEquals(
72
            1,
73
            $mailer->send(
74
                $mailMessage,
75
                ['text' => __DIR__ . '/data/test_view.php'],
76
                ['force' => 'force', 'with' => 'with', 'you' => 'you']
77
            )
78
        );
79
        $this->assertEquals(1, $mailer->sendSwiftMessage($mailMessage->asSwiftMessage()));
80
    }
81
82
    public function testSendSwiftMailer()
83
    {
84
        $mailMessage = FixtureHelper::getMailMessage()->asSwiftMessage();
85
        Mockery::mock('overload:Swift_Mailer')
86
            ->shouldIgnoreMissing()
87
            ->shouldReceive('send')
88
            ->withAnyArgs()
89
            ->once();
90
        $mailTransport = (new MailTransportFactory([]))->create();
91
        $mailer = new Mailer($mailTransport);
92
        date_default_timezone_set('UTC');
93
        $this->assertEquals(null, $mailer->sendSwiftMessage($mailMessage));
94
    }
95
}
96
97
class TestSwiftPlugin implements \Swift_Events_CommandListener
98
{
99
    public function commandSent(Swift_Events_CommandEvent $evt)
100
    {
101
        // TODO: Implement commandSent() method.
102
    }
103
104
}
105