1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spinen\MailAssertions; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Mail; |
6
|
|
|
use PHPUnit_Framework_TestCase; |
7
|
|
|
use Swift_Message; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class MailTracking |
11
|
|
|
* |
12
|
|
|
* Trait to mixin to your test to allow for custom assertions when using PHPUnit with Laravel. |
13
|
|
|
* |
14
|
|
|
* This originally started out as a copy & paste from a video series that Jeffery Way did on laracasts.com. If you do |
15
|
|
|
* not have an account on Laracasts, you should get one. It is an amazing resource to learn from. We used that |
16
|
|
|
* example & converted it to a package so that it would be easy to install. We have also expanded on initial |
17
|
|
|
* assertions. |
18
|
|
|
* |
19
|
|
|
* I WANT IT CLEAR THAT THIS WOULD NOT HAVE HAPPENED WITHOUT THE INITIAL WORK OF JEFFERY WAY. WE ARE NOT CLAIMING TO |
20
|
|
|
* BE THE CREATORS OF THE CONCEPT. |
21
|
|
|
* |
22
|
|
|
* @package Spinen\MailAssertions |
23
|
|
|
* @see https://gist.github.com/JeffreyWay/b501c53d958b07b8a332 |
24
|
|
|
* @tutorial https://laracasts.com/series/phpunit-testing-in-laravel/episodes/12 |
25
|
|
|
*/ |
26
|
|
|
trait MailTracking |
27
|
|
|
{ |
28
|
|
|
// TODO: Add check for attachments (number of & name) |
29
|
|
|
// TODO: Add check for header |
30
|
|
|
// TODO: Add check for message type |
31
|
|
|
// TODO: Add check for Priority |
32
|
|
|
// TODO: Allow checking specific message not just most recent one |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Delivered emails. |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $emails = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Register a listener for new emails. |
43
|
|
|
* |
44
|
|
|
* Called my PHPUnit before each test it run. It registers the MailRecorder "plugin" with Swift, so that we can |
45
|
|
|
* get a copy of each email that is sent during that test. |
46
|
|
|
* |
47
|
|
|
* @before |
48
|
|
|
*/ |
49
|
|
|
public function setUpMailTracking() |
50
|
|
|
{ |
51
|
|
|
Mail::getSwiftMailer() |
52
|
|
|
->registerPlugin(new MailRecorder($this)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Retrieve the appropriate swift message. |
57
|
|
|
* |
58
|
|
|
* @param Swift_Message|null $message |
59
|
|
|
* |
60
|
|
|
* @return Swift_Message |
61
|
|
|
*/ |
62
|
|
|
protected function getEmail(Swift_Message $message = null) |
63
|
|
|
{ |
64
|
|
|
$this->seeEmailWasSent(); |
65
|
|
|
|
66
|
|
|
return $message ?: $this->lastEmail(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Retrieve the mostly recently sent swift message. |
71
|
|
|
*/ |
72
|
|
|
protected function lastEmail() |
73
|
|
|
{ |
74
|
|
|
return end($this->emails); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Store a new swift message. |
79
|
|
|
* |
80
|
|
|
* Collection of emails that were received by the MailRecorder plugin during a test. |
81
|
|
|
* |
82
|
|
|
* @param Swift_Message $email |
83
|
|
|
*/ |
84
|
|
|
public function recordMail(Swift_Message $email) |
85
|
|
|
{ |
86
|
|
|
$this->emails[] = $email; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Assert that the last email was bcc'ed to the given address. |
91
|
|
|
* |
92
|
|
|
* @param string $bcc |
93
|
|
|
* @param Swift_Message|null $message |
94
|
|
|
* |
95
|
|
|
* @return PHPUnit_Framework_TestCase $this |
96
|
|
|
*/ |
97
|
|
|
protected function seeEmailBcc($bcc, Swift_Message $message = null) |
98
|
|
|
{ |
99
|
|
|
$this->assertArrayHasKey($bcc, (array)$this->getEmail($message) |
|
|
|
|
100
|
|
|
->getBcc(), "No email was bcc'ed to $bcc."); |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Assert that the last email was cc'ed to the given address. |
107
|
|
|
* |
108
|
|
|
* @param string $cc |
109
|
|
|
* @param Swift_Message|null $message |
110
|
|
|
* |
111
|
|
|
* @return PHPUnit_Framework_TestCase $this |
112
|
|
|
*/ |
113
|
|
|
protected function seeEmailCc($cc, Swift_Message $message = null) |
114
|
|
|
{ |
115
|
|
|
$this->assertArrayHasKey($cc, (array)$this->getEmail($message) |
|
|
|
|
116
|
|
|
->getCc(), "No email was cc'ed to $cc."); |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Assert that the last email's body contains the given text. |
123
|
|
|
* |
124
|
|
|
* @param string $excerpt |
125
|
|
|
* @param Swift_Message|null $message |
126
|
|
|
* |
127
|
|
|
* @return PHPUnit_Framework_TestCase $this |
128
|
|
|
*/ |
129
|
|
|
protected function seeEmailContains($excerpt, Swift_Message $message = null) |
130
|
|
|
{ |
131
|
|
|
$this->assertContains($excerpt, $this->getEmail($message) |
|
|
|
|
132
|
|
|
->getBody(), "No email containing the provided body was found."); |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Assert that the last email's body does not contain the given text. |
139
|
|
|
* |
140
|
|
|
* @param string $excerpt |
141
|
|
|
* @param Swift_Message|null $message |
142
|
|
|
* |
143
|
|
|
* @return PHPUnit_Framework_TestCase $this |
144
|
|
|
*/ |
145
|
|
|
protected function seeEmailDoesNotContain($excerpt, Swift_Message $message = null) |
146
|
|
|
{ |
147
|
|
|
$this->assertNotContains($excerpt, $this->getEmail($message) |
|
|
|
|
148
|
|
|
->getBody(), "Email containing the provided text was found in the body."); |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Assert that the last email's body equals the given text. |
155
|
|
|
* |
156
|
|
|
* @param string $body |
157
|
|
|
* @param Swift_Message|null $message |
158
|
|
|
* |
159
|
|
|
* @return PHPUnit_Framework_TestCase $this |
160
|
|
|
*/ |
161
|
|
|
protected function seeEmailEquals($body, Swift_Message $message = null) |
162
|
|
|
{ |
163
|
|
|
$this->assertEquals($body, $this->getEmail($message) |
|
|
|
|
164
|
|
|
->getBody(), "No email with the provided body was sent."); |
165
|
|
|
|
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Assert that the last email was delivered by the given address. |
171
|
|
|
* |
172
|
|
|
* @param string $sender |
173
|
|
|
* @param Swift_Message|null $message |
174
|
|
|
* |
175
|
|
|
* @return PHPUnit_Framework_TestCase $this |
176
|
|
|
*/ |
177
|
|
|
protected function seeEmailFrom($sender, Swift_Message $message = null) |
178
|
|
|
{ |
179
|
|
|
// TODO: Allow from to be an array to check email & name |
180
|
|
|
$this->assertArrayHasKey($sender, (array)$this->getEmail($message) |
|
|
|
|
181
|
|
|
->getFrom(), "No email was sent from $sender."); |
182
|
|
|
|
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Assert that the last email was set to reply to the given address. |
188
|
|
|
* |
189
|
|
|
* @param string $reply_to |
190
|
|
|
* @param Swift_Message|null $message |
191
|
|
|
* |
192
|
|
|
* @return PHPUnit_Framework_TestCase $this |
193
|
|
|
*/ |
194
|
|
|
protected function seeEmailReplyTo($reply_to, Swift_Message $message = null) |
195
|
|
|
{ |
196
|
|
|
$this->assertArrayHasKey($reply_to, (array)$this->getEmail($message) |
|
|
|
|
197
|
|
|
->getReplyTo(), "No email was set to reply to $reply_to."); |
198
|
|
|
|
199
|
|
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Assert that the given number of emails were sent. |
204
|
|
|
* |
205
|
|
|
* @param integer $count |
206
|
|
|
* |
207
|
|
|
* @return PHPUnit_Framework_TestCase $this |
208
|
|
|
*/ |
209
|
|
|
protected function seeEmailsSent($count) |
210
|
|
|
{ |
211
|
|
|
$emailsSent = count($this->emails); |
212
|
|
|
|
213
|
|
|
$this->assertCount($count, $this->emails, "Expected $count emails to have been sent, but $emailsSent were."); |
|
|
|
|
214
|
|
|
|
215
|
|
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Assert that the last email's subject matches the given string. |
220
|
|
|
* |
221
|
|
|
* @param string $subject |
222
|
|
|
* @param Swift_Message|null $message |
223
|
|
|
* |
224
|
|
|
* @return PHPUnit_Framework_TestCase $this |
225
|
|
|
*/ |
226
|
|
|
protected function seeEmailSubject($subject, Swift_Message $message = null) |
227
|
|
|
{ |
228
|
|
|
// TODO: Consider a subject contains like the message contains |
229
|
|
|
$this->assertEquals($subject, $this->getEmail($message) |
|
|
|
|
230
|
|
|
->getSubject(), "No email with a subject of $subject was found."); |
231
|
|
|
|
232
|
|
|
return $this; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Assert that the last email was sent to the given recipient. |
237
|
|
|
* |
238
|
|
|
* @param string $recipient |
239
|
|
|
* @param Swift_Message|null $message |
240
|
|
|
* |
241
|
|
|
* @return PHPUnit_Framework_TestCase $this |
242
|
|
|
*/ |
243
|
|
|
protected function seeEmailTo($recipient, Swift_Message $message = null) |
244
|
|
|
{ |
245
|
|
|
$this->assertArrayHasKey($recipient, (array)$this->getEmail($message) |
|
|
|
|
246
|
|
|
->getTo(), "No email was sent to $recipient."); |
247
|
|
|
|
248
|
|
|
return $this; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Assert that no emails were sent. |
253
|
|
|
* |
254
|
|
|
* @return PHPUnit_Framework_TestCase $this |
255
|
|
|
*/ |
256
|
|
|
protected function seeEmailWasNotSent() |
257
|
|
|
{ |
258
|
|
|
$this->assertEmpty($this->emails, 'Did not expect any emails to have been sent.'); |
|
|
|
|
259
|
|
|
|
260
|
|
|
return $this; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Assert that at least one email was sent. |
265
|
|
|
* |
266
|
|
|
* @return PHPUnit_Framework_TestCase $this |
267
|
|
|
*/ |
268
|
|
|
protected function seeEmailWasSent() |
269
|
|
|
{ |
270
|
|
|
$this->assertNotEmpty($this->emails, 'No emails have been sent.'); |
|
|
|
|
271
|
|
|
|
272
|
|
|
return $this; |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.