|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TonicHealthCheck\Test\Incident\Siren\NotificationType; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit_Framework_MockObject_MockObject; |
|
6
|
|
|
use PHPUnit_Framework_TestCase; |
|
7
|
|
|
use Swift_Mailer; |
|
8
|
|
|
use Swift_Message; |
|
9
|
|
|
use Swift_Transport; |
|
10
|
|
|
use TonicHealthCheck\Incident\IncidentInterface; |
|
11
|
|
|
use TonicHealthCheck\Incident\Siren\NotificationType\EmailNotificationType; |
|
12
|
|
|
use TonicHealthCheck\Test\Incident\IncidentCreateTrait; |
|
13
|
|
|
use TonicHealthCheck\Test\Incident\Subject\SubjectCreateTrait; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class EmailNotificationTypeTest. |
|
17
|
|
|
*/ |
|
18
|
|
|
class EmailNotificationTypeTest extends PHPUnit_Framework_TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
use SubjectCreateTrait; |
|
21
|
|
|
use IncidentCreateTrait; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
private $from = '[email protected]'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private $fromName = 'Tester Test'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var PHPUnit_Framework_MockObject_MockObject|Swift_Mailer; |
|
34
|
|
|
*/ |
|
35
|
|
|
private $mailer; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var EmailNotificationType; |
|
39
|
|
|
*/ |
|
40
|
|
|
private $mailNType; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* set base env for test EmailNotificationTypeTest. |
|
44
|
|
|
*/ |
|
45
|
|
|
public function setUp() |
|
46
|
|
|
{ |
|
47
|
|
|
$this->setMailer( |
|
48
|
|
|
$this |
|
49
|
|
|
->getMockBuilder(Swift_Mailer::class) |
|
50
|
|
|
->disableOriginalConstructor() |
|
51
|
|
|
->getMock() |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
$this->setMailNType( |
|
55
|
|
|
new EmailNotificationType( |
|
56
|
|
|
$this->getMailer(), |
|
|
|
|
|
|
57
|
|
|
$this->getFrom(), |
|
58
|
|
|
$this->getFromName() |
|
59
|
|
|
) |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* test email notify ok. |
|
65
|
|
|
*/ |
|
66
|
|
|
public function testNotify() |
|
67
|
|
|
{ |
|
68
|
|
|
$incident = $this->createIncidentMock(); |
|
69
|
|
|
|
|
70
|
|
|
$incident->expects($this->any())->method('getStatus')->willReturn(IncidentInterface::STATUS_OK + 1); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
$subject = $this->createSubject('[email protected]', '* * * * *'); |
|
73
|
|
|
|
|
74
|
|
|
$mailerTransport = $this->getMockBuilder(Swift_Transport::class)->getMock(); |
|
75
|
|
|
|
|
76
|
|
|
$mailerTransport->expects($this->once())->method('stop'); |
|
77
|
|
|
|
|
78
|
|
|
$this |
|
|
|
|
|
|
79
|
|
|
->getMailer() |
|
80
|
|
|
->expects($this->any()) |
|
81
|
|
|
->method('getTransport') |
|
82
|
|
|
->willReturn($mailerTransport); |
|
83
|
|
|
|
|
84
|
|
|
$message = $this->createMessage($subject, $incident); |
|
85
|
|
|
|
|
86
|
|
|
$this |
|
87
|
|
|
->getMailer() |
|
88
|
|
|
->expects($this->any()) |
|
89
|
|
|
->method('send') |
|
90
|
|
|
->with($this->callback($this->isMessageSame($message))); |
|
91
|
|
|
|
|
92
|
|
|
$this->getMailNType()->notify($subject, $incident); |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function getFrom() |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->from; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function getFromName() |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->fromName; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return PHPUnit_Framework_MockObject_MockObject|Swift_Mailer |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function getMailer() |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->mailer; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param Swift_Mailer $mailer |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function setMailer(Swift_Mailer $mailer) |
|
123
|
|
|
{ |
|
124
|
|
|
$this->mailer = $mailer; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return EmailNotificationType |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function getMailNType() |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->mailNType; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param EmailNotificationType $mailNType |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function setMailNType(EmailNotificationType $mailNType) |
|
139
|
|
|
{ |
|
140
|
|
|
$this->mailNType = $mailNType; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param Swift_Message $message |
|
145
|
|
|
* |
|
146
|
|
|
* @return \Closure |
|
147
|
|
|
*/ |
|
148
|
|
|
protected function isMessageSame(Swift_Message $message) |
|
149
|
|
|
{ |
|
150
|
|
|
return function (Swift_Message $messageI) use ($message) { |
|
151
|
|
|
return $messageI->getFrom() == $message->getFrom() |
|
152
|
|
|
&& $messageI->getTo() == $message->getTo() |
|
153
|
|
|
&& $messageI->getSubject() == $message->getSubject() |
|
154
|
|
|
&& $messageI->getBody() == $message->getBody(); |
|
155
|
|
|
}; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param $subject |
|
160
|
|
|
* @param $incident |
|
161
|
|
|
* |
|
162
|
|
|
* @return Swift_Message |
|
163
|
|
|
*/ |
|
164
|
|
|
protected function createMessage($subject, $incident) |
|
165
|
|
|
{ |
|
166
|
|
|
$message = Swift_Message::newInstance() |
|
167
|
|
|
->setTo($subject->getTarget()) |
|
168
|
|
|
->setFrom($this->getFrom(), $this->getFromName()) |
|
169
|
|
|
->setSubject(sprintf(EmailNotificationType::EMAIL_SUBJECT_T, $incident->getIdent())) |
|
170
|
|
|
->setBody(sprintf(EmailNotificationType::EMAIL_BODY_T, $incident->getMessage(), $incident->getType())); |
|
171
|
|
|
|
|
172
|
|
|
return $message; |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.