summaryrefslogtreecommitdiff
path: root/tests/Unit/Mail/MailerTest.php
blob: bfecdd075efef1e6510b67ae635af5f3088b3544 (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
<?php

namespace Engelsystem\Test\Unit\Mail;

use Engelsystem\Mail\Mailer;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Swift_Mailer as SwiftMailer;
use Swift_Message as SwiftMessage;

class MailerTest extends TestCase
{
    /**
     * @covers \Engelsystem\Mail\Mailer::__construct
     * @covers \Engelsystem\Mail\Mailer::getFromAddress
     * @covers \Engelsystem\Mail\Mailer::getFromName
     * @covers \Engelsystem\Mail\Mailer::setFromAddress
     * @covers \Engelsystem\Mail\Mailer::setFromName
     */
    public function testInitAndSettersAndGetters()
    {
        /** @var SwiftMailer|MockObject $swiftMailer */
        $swiftMailer = $this->createMock(SwiftMailer::class);

        $mailer = new Mailer($swiftMailer);

        $mailer->setFromName('From Name');
        $this->assertEquals('From Name', $mailer->getFromName());

        $mailer->setFromAddress('from@foo.bar');
        $this->assertEquals('from@foo.bar', $mailer->getFromAddress());
    }

    /**
     * @covers \Engelsystem\Mail\Mailer::send
     */
    public function testSend()
    {
        /** @var SwiftMessage|MockObject $message */
        $message = $this->createMock(SwiftMessage::class);
        /** @var SwiftMailer|MockObject $swiftMailer */
        $swiftMailer = $this->createMock(SwiftMailer::class);
        $swiftMailer->expects($this->once())
            ->method('createMessage')
            ->willReturn($message);
        $swiftMailer->expects($this->once())
            ->method('send')
            ->willReturn(1);

        $message->expects($this->once())
            ->method('setTo')
            ->with(['to@xam.pel'])
            ->willReturn($message);

        $message->expects($this->once())
            ->method('setFrom')
            ->with('foo@bar.baz', 'Lorem Ipsum')
            ->willReturn($message);

        $message->expects($this->once())
            ->method('setSubject')
            ->with('Foo Bar')
            ->willReturn($message);

        $message->expects($this->once())
            ->method('setBody')
            ->with('Lorem Ipsum!')
            ->willReturn($message);

        $mailer = new Mailer($swiftMailer);
        $mailer->setFromAddress('foo@bar.baz');
        $mailer->setFromName('Lorem Ipsum');

        $return = $mailer->send('to@xam.pel', 'Foo Bar', 'Lorem Ipsum!');
        $this->equalTo(1, $return);
    }
}