|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Spiral Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @author Anton Titov (Wolfy-J) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Tests\SendIt; |
|
13
|
|
|
|
|
14
|
|
|
use Mockery as m; |
|
15
|
|
|
use PHPUnit\Framework\TestCase; |
|
16
|
|
|
use Psr\Log\LoggerInterface; |
|
17
|
|
|
use Spiral\Mailer\Message; |
|
18
|
|
|
use Spiral\SendIt\Config\MailerConfig; |
|
19
|
|
|
use Spiral\SendIt\MailJob; |
|
20
|
|
|
use Spiral\SendIt\MailQueue; |
|
21
|
|
|
use Spiral\SendIt\MessageSerializer; |
|
22
|
|
|
use Spiral\SendIt\RendererInterface; |
|
23
|
|
|
use Symfony\Component\Mailer\Exception\TransportException; |
|
24
|
|
|
use Symfony\Component\Mailer\MailerInterface; |
|
25
|
|
|
use Symfony\Component\Mime\Email; |
|
26
|
|
|
|
|
27
|
|
|
class JobTest extends TestCase |
|
28
|
|
|
{ |
|
29
|
|
|
/** @var MailerInterface */ |
|
30
|
|
|
protected $mailer; |
|
31
|
|
|
/** @var RendererInterface */ |
|
32
|
|
|
protected $renderer; |
|
33
|
|
|
/** @var LoggerInterface */ |
|
34
|
|
|
protected $logger; |
|
35
|
|
|
|
|
36
|
|
|
public function setUp(): void |
|
37
|
|
|
{ |
|
38
|
|
|
parent::setUp(); |
|
39
|
|
|
|
|
40
|
|
|
$this->mailer = m::mock(MailerInterface::class); |
|
|
|
|
|
|
41
|
|
|
$this->renderer = m::mock(RendererInterface::class); |
|
|
|
|
|
|
42
|
|
|
$this->logger = m::mock(LoggerInterface::class); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testHandler(): void |
|
46
|
|
|
{ |
|
47
|
|
|
$email = $this->getEmail(); |
|
48
|
|
|
|
|
49
|
|
|
$this->expectRenderer($email); |
|
50
|
|
|
|
|
51
|
|
|
$this->mailer->expects('send')->with($email); |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
$this->logger->expects('debug')->with( |
|
|
|
|
|
|
54
|
|
|
'Sent `test` to "[email protected]"', |
|
55
|
|
|
['emails' => ['[email protected]']] |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
$this->getHandler()->handle( |
|
59
|
|
|
MailQueue::JOB_NAME, |
|
60
|
|
|
'id', |
|
61
|
|
|
json_encode(MessageSerializer::pack($this->getMail())) |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function testHandlerError(): void |
|
66
|
|
|
{ |
|
67
|
|
|
$email = $this->getEmail(); |
|
68
|
|
|
|
|
69
|
|
|
$this->expectRenderer($email); |
|
70
|
|
|
|
|
71
|
|
|
$this->mailer->expects('send')->with($email)->andThrow(new TransportException('failed')); |
|
72
|
|
|
|
|
73
|
|
|
$this->logger->expects('error')->with( |
|
74
|
|
|
'Failed to send `test` to "[email protected]": failed', |
|
75
|
|
|
['emails' => ['[email protected]']] |
|
76
|
|
|
); |
|
77
|
|
|
|
|
78
|
|
|
try { |
|
79
|
|
|
$this->getHandler()->handle( |
|
80
|
|
|
MailQueue::JOB_NAME, |
|
81
|
|
|
'id', |
|
82
|
|
|
json_encode(MessageSerializer::pack($this->getMail())) |
|
83
|
|
|
); |
|
84
|
|
|
} catch (TransportException $e) { |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$this->logger->mockery_verify(); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
private function getEmail(): Email |
|
91
|
|
|
{ |
|
92
|
|
|
$email = new Email(); |
|
93
|
|
|
$email->to('[email protected]'); |
|
94
|
|
|
$email->html('message body'); |
|
95
|
|
|
return $email; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
private function expectRenderer(Email $email): void |
|
99
|
|
|
{ |
|
100
|
|
|
$this->renderer->expects('render')->withArgs( |
|
|
|
|
|
|
101
|
|
|
function (Message $message) { |
|
102
|
|
|
$this->assertSame($message->getSubject(), 'test'); |
|
103
|
|
|
return true; |
|
104
|
|
|
} |
|
105
|
|
|
)->andReturn($email); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
private function getHandler(): MailJob |
|
109
|
|
|
{ |
|
110
|
|
|
$handler = new MailJob( |
|
111
|
|
|
new MailerConfig(['from' => '[email protected]']), |
|
112
|
|
|
$this->mailer, |
|
113
|
|
|
$this->renderer |
|
114
|
|
|
); |
|
115
|
|
|
$handler->setLogger($this->logger); |
|
116
|
|
|
return $handler; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
private function getMail(): Message |
|
120
|
|
|
{ |
|
121
|
|
|
$mail = new Message('test', ['[email protected]'], ['key' => 'value']); |
|
122
|
|
|
$mail->setFrom('[email protected]'); |
|
123
|
|
|
$mail->setReplyTo('[email protected]'); |
|
124
|
|
|
$mail->setCC('[email protected]'); |
|
125
|
|
|
$mail->setBCC('[email protected]'); |
|
126
|
|
|
return $mail; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..