summaryrefslogtreecommitdiff
path: root/tests/Unit/Mail/Transport/TransportTest.php
blob: 8dd78d783cb7ef3739493bc725ae11295ee6fdd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php

namespace Engelsystem\Test\Unit\Mail\Transport;

use Engelsystem\Test\Unit\Mail\Transport\Stub\TransportImplementation;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Swift_Events_EventListener;
use Swift_Mime_SimpleMessage as SimpleMessage;

class TransportTest extends TestCase
{
    /**
     * @covers \Engelsystem\Mail\Transport\Transport::isStarted
     * @covers \Engelsystem\Mail\Transport\Transport::ping
     * @covers \Engelsystem\Mail\Transport\Transport::registerPlugin
     * @covers \Engelsystem\Mail\Transport\Transport::start
     * @covers \Engelsystem\Mail\Transport\Transport::stop
     */
    public function testMethods()
    {
        /** @var Swift_Events_EventListener|MockObject $plugin */
        $plugin = $this->getMockForAbstractClass(Swift_Events_EventListener::class);

        $transport = new TransportImplementation();

        $transport->start();
        $transport->registerPlugin($plugin);

        $this->assertTrue($transport->isStarted());
        $this->assertTrue($transport->ping());

        $transport->stop();
    }

    /**
     * @covers \Engelsystem\Mail\Transport\Transport::allRecipients
     */
    public function testAllRecipients()
    {
        /** @var SimpleMessage|MockObject $message */
        $message = $this->createMock(SimpleMessage::class);
        $transport = new TransportImplementation();
        $message->expects($this->once())
            ->method('getTo')
            ->willReturn([
                'foo@bar.batz'      => 'Foo Bar',
                'lorem@ipsum.dolor' => null,
            ]);
        $message->expects($this->once())
            ->method('getCc')
            ->willReturn([
                'to@bar.batz' => null,
            ]);
        $message->expects($this->once())
            ->method('getBcc')
            ->willReturn([
                'secret@bar.batz' => 'I\'m secret!',
            ]);

        $this->assertEquals(
            [
                'foo@bar.batz'      => 'Foo Bar',
                'lorem@ipsum.dolor' => null,
                'to@bar.batz'       => null,
                'secret@bar.batz'   => 'I\'m secret!',
            ],
            $transport->getAllRecipients($message)
        );
    }

    /**
     * @covers \Engelsystem\Mail\Transport\Transport::formatTo
     * @covers \Engelsystem\Mail\Transport\Transport::getTo
     */
    public function testGetTo()
    {
        /** @var SimpleMessage|MockObject $message */
        $message = $this->createMock(SimpleMessage::class);
        /** @var TransportImplementation|MockObject $transport */
        $transport = $this->getMockBuilder(TransportImplementation::class)
            ->onlyMethods(['allRecipients'])
            ->getMock();
        $transport->expects($this->once())
            ->method('allRecipients')
            ->with($message)
            ->willReturn([
                'foo@bar.batz'      => null,
                'lorem@ipsum.dolor' => 'Developer',
            ]);

        $return = $transport->getGetTo($message);
        $this->assertEquals('foo@bar.batz,Developer <lorem@ipsum.dolor>', $return);
    }
}