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

namespace Engelsystem\Test\Unit\Mail\Transport;

use Engelsystem\Mail\Transport\LogTransport;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Swift_Mime_SimpleMessage as SimpleMessage;

class LogTransportTest extends TestCase
{
    /**
     * @covers \Engelsystem\Mail\Transport\LogTransport::__construct
     * @covers \Engelsystem\Mail\Transport\LogTransport::send
     */
    public function testSend()
    {
        /** @var LoggerInterface|MockObject $logger */
        $logger = $this->getMockForAbstractClass(LoggerInterface::class);
        /** @var SimpleMessage|MockObject $message */
        $message = $this->createMock(SimpleMessage::class);

        $message->expects($this->once())
            ->method('getSubject')
            ->willReturn('Some subject');
        $message->expects($this->once())
            ->method('toString')
            ->willReturn("Head: er\n\nMessage body");

        $logger->expects($this->once())
            ->method('debug')
            ->willReturnCallback(function ($message, $context = []) {
                foreach (array_keys($context) as $key) {
                    $this->assertStringContainsString(sprintf('{%s}', $key), $message);
                }

                $this->assertEquals('Some subject', $context['title']);
                $this->assertEquals('foo@bar.batz,Lorem Ipsum <lor@em.ips>', $context['recipients']);
                $this->assertStringContainsString('Head: er', $context['content']);
                $this->assertStringContainsString('Message body', $context['content']);
            });

        /** @var LogTransport|MockObject $transport */
        $transport = $this->getMockBuilder(LogTransport::class)
            ->setConstructorArgs(['logger' => $logger])
            ->setMethods(['allRecipients'])
            ->getMock();
        $transport->expects($this->exactly(2))
            ->method('allRecipients')
            ->with($message)
            ->willReturn(['foo@bar.batz' => null, 'lor@em.ips' => 'Lorem Ipsum']);

        $return = $transport->send($message);
        $this->equalTo(2, $return);
    }
}