Passed
Push — main ( 8365e4...b2f486 )
by Peter
03:33
created

SenderTest::testSendToNobody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 16
rs 9.9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Email;
6
7
use PHPUnit\Framework\MockObject\MockObject;
8
use PHPUnit\Framework\TestCase;
9
use Symfony\Component\Mailer\Mailer;
10
use Symfony\Component\Mailer\Transport;
11
use Symfony\Component\Mime\Address;
12
use Symfony\Component\Mime\Email;
13
14
class SenderTest extends TestCase
15
{
16
    /** @var Sender - System Under Test */
17
    protected Sender $sut;
18
19
    protected Mailer $fakeMailer;
20
21
    protected Email|MockObject $emailMock;
22
23
    protected MessageFactory|MockObject $messageFactory;
24
25
    protected array $recipients = [
26
        '[email protected]',
27
        'jane' => '[email protected]'
28
    ];
29
30
    public function setUp(): void
31
    {
32
        parent::setUp();
33
34
        $this->emailMock = $this->createMock(Email::class);
35
36
        $this->messageFactory = $this->createMock(MessageFactory::class);
37
        $this->messageFactory->expects($this->any())->method('create')->willReturn($this->emailMock);
38
39
        $this->fakeMailer = new Mailer(Transport::fromDsn('null://null'));
40
41
        $this->sut = new Sender($this->fakeMailer, $this->messageFactory);
42
    }
43
44
    public function testSendToNobody(): void
45
    {
46
        $this->emailMock->expects($this->once())->method('from')->willReturnSelf();
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Symfony\Component\Mime\Email. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        $this->emailMock->/** @scrutinizer ignore-call */ 
47
                          expects($this->once())->method('from')->willReturnSelf();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
        $this->emailMock->expects($this->once())->method('subject')->willReturnSelf();
48
        $this->emailMock->expects($this->once())->method('text')->willReturnSelf();
49
        $this->emailMock->expects($this->once())->method('html')->willReturnSelf();
50
        $this->emailMock->expects($this->never())->method('to')->willReturnSelf();
51
52
        $sender   = new Address('[email protected]');
53
        $subject  = 'foo';
54
        $bodyText = 'bar';
55
        $bodyHtml = '<b>bar</b>';
56
57
        $recipients = [];
58
59
        $this->sut->send($subject, $bodyText, $bodyHtml, $sender, $recipients);
60
    }
61
62
    public function testSend(): void
63
    {
64
        $this->emailMock->expects($this->once())->method('from')->willReturnSelf();
65
        $this->emailMock->expects($this->once())->method('subject')->willReturnSelf();
66
        $this->emailMock->expects($this->once())->method('text')->willReturnSelf();
67
        $this->emailMock->expects($this->once())->method('html')->willReturnSelf();
68
        $this->emailMock->expects($this->exactly(2))->method('to')->willReturnSelf();
69
70
        $sender   = new Address('[email protected]');
71
        $subject  = 'foo';
72
        $bodyText = 'bar';
73
        $bodyHtml = '<b>bar</b>';
74
75
        $recipients = [];
76
        foreach ($this->recipients as $key => $value) {
77
            if (is_int($key)) {
78
                $recipients[] = new Address($value);
79
            } else {
80
                $recipients[] = new Address($value, $key);
81
            }
82
        }
83
84
        $this->sut->send($subject, $bodyText, $bodyHtml, $sender, $recipients);
85
    }
86
87
    public function testGetFailedRecipientsIsEmptyByDefault(): void
88
    {
89
        $recipients = $this->sut->getFailedRecipients();
90
91
        $this->assertSame([], $recipients);
92
    }
93
}
94