Failed Conditions
Pull Request — master (#11)
by Adrien
15:48 queued 12:33
created

MailerTest::createMockTransport()

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 12

1 Method

Rating   Name   Duplication   Size   Complexity  
A MailerTest.php$1 ➔ send() 0 9 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EcodevTests\Felix\Service;
6
7
use Cake\Chronos\Chronos;
8
use Doctrine\ORM\EntityManager;
9
use Ecodev\Felix\Model\Message;
10
use Ecodev\Felix\Model\User;
11
use Ecodev\Felix\Repository\MessageRepository;
12
use Ecodev\Felix\Service\Mailer;
1 ignored issue
show
Bug introduced by
The type Ecodev\Felix\Service\Mailer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Laminas\Mail;
14
use Laminas\Mail\Address;
15
use Laminas\Mail\Transport\TransportInterface;
16
use PHPUnit\Framework\TestCase;
17
18
final class MailerTest extends TestCase
19
{
20
    private function createMockMailer(): Mailer
21
    {
22
        /** @var EntityManager $entityManager */
23
        $entityManager = $this->createMock(EntityManager::class);
24
        $transport = $this->createMockTransport();
25
26
        $messageRepository = new class() implements MessageRepository {
27
            public function getAllMessageToSend(): array
28
            {
29
                return [];
30
            }
31
        };
32
33
        $mailer = new Mailer(
34
            $entityManager,
35
            $messageRepository,
36
            $transport,
37
            '/user/bin/php',
38
            null,
39
            '[email protected]',
40
            'Epicerio'
41
        );
42
43
        return $mailer;
44
    }
45
46
    private function createMockTransport(): TransportInterface
47
    {
48
        return new class() implements TransportInterface {
49
            public function send(Mail\Message $message): void
50
            {
51
                // Purposefully place current cursor at the end of list
52
                foreach ($message->getFrom() as $a) {
53
                    $a->getEmail();
54
                }
55
56
                foreach ($message->getTo() as $a) {
57
                    $a->getEmail();
58
                }
59
            }
60
        };
61
    }
62
63
    public function testMockTransportHasCursorAtEndOfList(): void
64
    {
65
        $message = new Mail\Message();
66
        $message->setFrom('[email protected]');
67
        $message->setTo('[email protected]');
68
69
        // New message has current cursor on first element
70
        self::assertInstanceOf(Address::class, $message->getFrom()->current());
71
        self::assertInstanceOf(Address::class, $message->getTo()->current());
72
73
        $transport = $this->createMockTransport();
74
        $transport->send($message);
75
76
        // After transport, message has current cursor on end of list
77
        self::assertFalse($message->getFrom()->current());
78
        self::assertFalse($message->getTo()->current());
79
    }
80
81
    public function testSendMessage(): void
82
    {
83
        $mailer = $this->createMockMailer();
84
        $message = $this->createMockMessage();
85
86
        $this->expectOutputRegex('~email from noreply@example\.com sent to: john\.doe@example\.com~');
87
        $mailer->sendMessage($message);
88
        self::assertNotNull($message->getDateSent());
89
    }
90
91
    private function createMockMessage(): Message
92
    {
93
        return new class() implements Message {
94
            private ?Chronos $dateSent = null;
95
96
            public function getSubject(): string
97
            {
98
                return 'my subject';
99
            }
100
101
            public function getBody(): string
102
            {
103
                return 'my body';
104
            }
105
106
            public function setDateSent(?Chronos $dateSent): void
107
            {
108
                $this->dateSent = $dateSent;
109
            }
110
111
            public function getEmail(): string
112
            {
113
                return '[email protected]';
114
            }
115
116
            public function getRecipient(): ?User
117
            {
118
                return null;
119
            }
120
121
            public function getId(): ?int
122
            {
123
                return null;
124
            }
125
126
            public function setSubject(string $subject): void
127
            {
128
            }
129
130
            public function setBody(string $body): void
131
            {
132
            }
133
134
            public function getDateSent(): ?Chronos
135
            {
136
                return $this->dateSent;
137
            }
138
139
            public function setEmail(string $email): void
140
            {
141
            }
142
143
            public function getType(): string
144
            {
145
                return 'my_type';
146
            }
147
        };
148
    }
149
}
150