|
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; |
|
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
|
|
|
// @phpstan-ignore-next-line |
|
78
|
|
|
self::assertFalse($message->getFrom()->current()); |
|
79
|
|
|
// @phpstan-ignore-next-line |
|
80
|
|
|
self::assertFalse($message->getTo()->current()); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function testSendMessage(): void |
|
84
|
|
|
{ |
|
85
|
|
|
$mailer = $this->createMockMailer(); |
|
86
|
|
|
$message = $this->createMockMessage(); |
|
87
|
|
|
|
|
88
|
|
|
$this->expectOutputRegex('~email from noreply@example\.com sent to: john\.doe@example\.com~'); |
|
89
|
|
|
$mailer->sendMessage($message); |
|
90
|
|
|
self::assertNotNull($message->getDateSent()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
private function createMockMessage(): Message |
|
94
|
|
|
{ |
|
95
|
|
|
return new class() implements Message { |
|
96
|
|
|
private ?Chronos $dateSent = null; |
|
97
|
|
|
|
|
98
|
|
|
public function getSubject(): string |
|
99
|
|
|
{ |
|
100
|
|
|
return 'my subject'; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function getBody(): string |
|
104
|
|
|
{ |
|
105
|
|
|
return 'my body'; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function setDateSent(?Chronos $dateSent): void |
|
109
|
|
|
{ |
|
110
|
|
|
$this->dateSent = $dateSent; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function getEmail(): string |
|
114
|
|
|
{ |
|
115
|
|
|
return '[email protected]'; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function getRecipient(): ?User |
|
119
|
|
|
{ |
|
120
|
|
|
return null; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function getId(): ?int |
|
124
|
|
|
{ |
|
125
|
|
|
return null; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function setSubject(string $subject): void |
|
129
|
|
|
{ |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function setBody(string $body): void |
|
133
|
|
|
{ |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function getDateSent(): ?Chronos |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->dateSent; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function setEmail(string $email): void |
|
142
|
|
|
{ |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function getType(): string |
|
146
|
|
|
{ |
|
147
|
|
|
return 'my_type'; |
|
148
|
|
|
} |
|
149
|
|
|
}; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths