|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drakakisgeo\Mailtester; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
|
|
7
|
|
|
trait InteractsWithMailCatcher |
|
8
|
|
|
{ |
|
9
|
|
|
use InteractsWithSwiftEmailer; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Init setup |
|
13
|
|
|
*/ |
|
14
|
|
|
public function setUp() |
|
15
|
|
|
{ |
|
16
|
|
|
parent::setUp(); |
|
17
|
|
|
$this->initCatcher(); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Init client and clean the messages |
|
22
|
|
|
*/ |
|
23
|
|
|
public function initCatcher() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->mailcatcher = new Client(['base_uri' => config('mailtester.url')]); |
|
|
|
|
|
|
26
|
|
|
$this->cleanEmailMessages(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Clear the list from all emails |
|
31
|
|
|
*/ |
|
32
|
|
|
public function cleanEmailMessages() |
|
33
|
|
|
{ |
|
34
|
|
|
$this->mailcatcher->delete('/messages'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Assert one email was send so list is not empty |
|
39
|
|
|
*/ |
|
40
|
|
|
public function assertEmailIsSent($description = '') |
|
41
|
|
|
{ |
|
42
|
|
|
$this->assertNotEmpty($this->getEmailMessages(), $description); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Assert that First Email's subject contains a string |
|
47
|
|
|
* |
|
48
|
|
|
* @param $needle |
|
49
|
|
|
* @param string $description |
|
50
|
|
|
*/ |
|
51
|
|
|
public function assertEmailFirstSubjectContains($needle, $description = '') |
|
52
|
|
|
{ |
|
53
|
|
|
$this->assertContains($needle, $this->getEmailFirstMessage()->subject, $description); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Assert that Last Email's subject contains a string |
|
58
|
|
|
* |
|
59
|
|
|
* @param $needle |
|
60
|
|
|
* @param string $description |
|
61
|
|
|
*/ |
|
62
|
|
|
public function assertEmailLastSubjectContains($needle, $description = '') |
|
63
|
|
|
{ |
|
64
|
|
|
$this->assertContains($needle, $this->getEmailLastMessage()->subject, $description); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Assert that certain Email's subject contains a string |
|
69
|
|
|
* |
|
70
|
|
|
* @param $needle |
|
71
|
|
|
* @param string $description |
|
72
|
|
|
*/ |
|
73
|
|
|
public function assertEmailNthSubjectContains($needle, $nth, $description = '') |
|
74
|
|
|
{ |
|
75
|
|
|
$this->assertContains($needle, $this->getEmailMessage($nth)->subject, $description); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Assert that the subject in the first Email is equal to a string |
|
81
|
|
|
* @param $expected |
|
82
|
|
|
* @param string $description |
|
83
|
|
|
*/ |
|
84
|
|
|
public function assertEmailFirstSubjectEquals($expected, $description = '') |
|
85
|
|
|
{ |
|
86
|
|
|
$this->assertEmailSubjectEquals($expected, $this->getEmailFirstMessage(), $description); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Assert that the subject in the last Email is equal to a string |
|
91
|
|
|
* @param $expected |
|
92
|
|
|
* @param string $description |
|
93
|
|
|
*/ |
|
94
|
|
|
public function assertEmailLastSubjectEquals($expected, $description = '') |
|
95
|
|
|
{ |
|
96
|
|
|
$this->assertEmailSubjectEquals($expected, $this->getEmailLastMessage(), $description); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Assert that the subject in the Nth Email is equal to a string |
|
101
|
|
|
* @param $expected |
|
102
|
|
|
* @param $nth |
|
103
|
|
|
* @param string $description |
|
104
|
|
|
*/ |
|
105
|
|
|
public function assertEmailNthSubjectEquals($expected, $nth, $description = '') |
|
106
|
|
|
{ |
|
107
|
|
|
$this->assertEmailSubjectEquals($expected, $this->getEmailMessage($nth), $description); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Assert that the subject of an Email is equal to a string |
|
113
|
|
|
* @param $expected |
|
114
|
|
|
* @param $email |
|
115
|
|
|
* @param string $description |
|
116
|
|
|
*/ |
|
117
|
|
|
public function assertEmailSubjectEquals($expected, $email, $description = '') |
|
118
|
|
|
{ |
|
119
|
|
|
$this->assertEquals($expected, $email->subject, $description); |
|
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Assert HTML body of First Email contains a certain text |
|
124
|
|
|
* |
|
125
|
|
|
* @param $needle |
|
126
|
|
|
* @param string $description |
|
127
|
|
|
*/ |
|
128
|
|
|
public function assertEmailFirstHtmlContains($needle, $description = '') |
|
129
|
|
|
{ |
|
130
|
|
|
$this->assertEmailHtmlContains($needle, $this->getEmailFirstMessage(), $description); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Assert HTML body of Last Email contains a certain text |
|
135
|
|
|
* |
|
136
|
|
|
* @param $needle |
|
137
|
|
|
* @param string $description |
|
138
|
|
|
*/ |
|
139
|
|
|
public function assertEmailLastHtmlContains($needle, $description = '') |
|
140
|
|
|
{ |
|
141
|
|
|
$this->assertEmailHtmlContains($needle, $this->getEmailLastMessage(), $description); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Assert HTML body of certain Nth Email contains a certain text |
|
147
|
|
|
* |
|
148
|
|
|
* @param $needle |
|
149
|
|
|
* @param $nth |
|
150
|
|
|
* @param string $description |
|
151
|
|
|
*/ |
|
152
|
|
|
public function assertEmailNthHtmlContains($needle, $nth, $description = '') |
|
153
|
|
|
{ |
|
154
|
|
|
$this->assertEmailHtmlContains($needle, $this->getEmailMessage($nth), $description); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Assert HTML body of Email contains a certain text |
|
159
|
|
|
* |
|
160
|
|
|
* @param $needle |
|
161
|
|
|
* @param $email |
|
162
|
|
|
* @param string $description |
|
163
|
|
|
*/ |
|
164
|
|
|
public function assertEmailHtmlContains($needle, $email, $description = '') |
|
165
|
|
|
{ |
|
166
|
|
|
$response = $this->mailcatcher->get("/messages/{$email->id}.html"); |
|
167
|
|
|
$this->assertContains($needle, (string)$response->getBody(), $description); |
|
|
|
|
|
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Assert Text body of First Email contains a certain text |
|
172
|
|
|
* |
|
173
|
|
|
* @param $needle |
|
174
|
|
|
* @param string $description |
|
175
|
|
|
*/ |
|
176
|
|
|
public function assertEmailFirstTextContains($needle, $description = '') |
|
177
|
|
|
{ |
|
178
|
|
|
$this->assertEmailTextContains($needle, $this->getEmailFirstMessage(), $description); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Assert Text body of Last Email contains a certain text |
|
183
|
|
|
* |
|
184
|
|
|
* @param $needle |
|
185
|
|
|
* @param string $description |
|
186
|
|
|
*/ |
|
187
|
|
|
public function assertEmailLastTextContains($needle, $description = '') |
|
188
|
|
|
{ |
|
189
|
|
|
$this->assertEmailTextContains($needle, $this->getEmailLastMessage(), $description); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Assert Text body of Nth Email contains a certain text |
|
194
|
|
|
* |
|
195
|
|
|
* @param $needle |
|
196
|
|
|
* @param $nth |
|
197
|
|
|
* @param string $description |
|
198
|
|
|
*/ |
|
199
|
|
|
public function assertEmailNthTextContains($needle, $nth, $description = '') |
|
200
|
|
|
{ |
|
201
|
|
|
$this->assertEmailTextContains($needle, $this->getEmailMessage($nth), $description); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Assert Text body of Email contains a certain text |
|
207
|
|
|
* |
|
208
|
|
|
* @param $needle |
|
209
|
|
|
* @param string $description |
|
210
|
|
|
*/ |
|
211
|
|
|
public function assertEmailTextContains($needle, $email, $description = '') |
|
212
|
|
|
{ |
|
213
|
|
|
$response = $this->mailcatcher->get("/messages/{$email->id}.plain"); |
|
214
|
|
|
$this->assertContains($needle, (string)$response->getBody(), $description); |
|
|
|
|
|
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Assert that the sender of first Email is equal to a string |
|
220
|
|
|
* |
|
221
|
|
|
* @param $expected |
|
222
|
|
|
* @param string $description |
|
223
|
|
|
*/ |
|
224
|
|
|
public function assertEmailFirstSenderEquals($expected, $description = '') |
|
225
|
|
|
{ |
|
226
|
|
|
$this->assertEmailSenderEquals($expected, $this->getEmailFirstMessage(), $description); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Assert that the sender of last Email is equal to a string |
|
231
|
|
|
* |
|
232
|
|
|
* @param $expected |
|
233
|
|
|
* @param string $description |
|
234
|
|
|
*/ |
|
235
|
|
|
public function assertEmailLastSenderEquals($expected, $description = '') |
|
236
|
|
|
{ |
|
237
|
|
|
$this->assertEmailSenderEquals($expected, $this->getEmailLastMessage(), $description); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Assert that the sender of Nth Email is equal to a string |
|
242
|
|
|
* |
|
243
|
|
|
* @param $expected |
|
244
|
|
|
* @param $nth |
|
245
|
|
|
* @param string $description |
|
246
|
|
|
*/ |
|
247
|
|
|
public function assertEmailNthSenderEquals($expected, $nth, $description = '') |
|
248
|
|
|
{ |
|
249
|
|
|
$this->assertEmailSenderEquals($expected, $this->getEmailMessage($nth), $description); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Assert that the sender is equal to a string |
|
254
|
|
|
* |
|
255
|
|
|
* @param $expected |
|
256
|
|
|
* @param $email |
|
257
|
|
|
* @param string $description |
|
258
|
|
|
*/ |
|
259
|
|
|
public function assertEmailSenderEquals($expected, $email, $description = '') |
|
260
|
|
|
{ |
|
261
|
|
|
$expected = '<' . $expected . '>'; |
|
262
|
|
|
$this->assertEquals($expected, $email->sender, $description); |
|
|
|
|
|
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Assert that the recipients list of First Email contains a certain one |
|
267
|
|
|
* |
|
268
|
|
|
* @param $needle |
|
269
|
|
|
* @param string $description |
|
270
|
|
|
*/ |
|
271
|
|
|
public function assertEmailFirstRecipientsContain($needle, $description = '') |
|
272
|
|
|
{ |
|
273
|
|
|
$this->assertEmailRecipientsContain($needle, $this->getEmailFirstMessage(), $description); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Assert that the recipients list of Last Email contains a certain one |
|
278
|
|
|
* |
|
279
|
|
|
* @param $needle |
|
280
|
|
|
* @param string $description |
|
281
|
|
|
*/ |
|
282
|
|
|
public function assertEmailLastRecipientsContain($needle, $description = '') |
|
283
|
|
|
{ |
|
284
|
|
|
$this->assertEmailRecipientsContain($needle, $this->getEmailLastMessage(), $description); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* Assert that the recipients list of Nth Email contains a certain one |
|
289
|
|
|
* |
|
290
|
|
|
* @param $needle |
|
291
|
|
|
* @param $nth |
|
292
|
|
|
* @param string $description |
|
293
|
|
|
*/ |
|
294
|
|
|
public function assertEmailNthRecipientsContain($needle, $nth, $description = '') |
|
295
|
|
|
{ |
|
296
|
|
|
$this->assertEmailRecipientsContain($needle, $this->getEmailMessage($nth), $description); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* Assert that the recipients list of Email contains a certain one |
|
302
|
|
|
* |
|
303
|
|
|
* @param $needle |
|
304
|
|
|
* @param string $description |
|
305
|
|
|
*/ |
|
306
|
|
|
public function assertEmailRecipientsContain($needle, $email, $description = '') |
|
307
|
|
|
{ |
|
308
|
|
|
$needle = '<' . $needle . '>'; |
|
309
|
|
|
$this->assertContains($needle, $email->recipients, $description); |
|
|
|
|
|
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* Assert that the cc list of First Mail contains a certain one |
|
314
|
|
|
* |
|
315
|
|
|
* @param $needle |
|
316
|
|
|
* @param string $description |
|
317
|
|
|
*/ |
|
318
|
|
|
public function assertEmailFirstCcContain($needle, $description = '') |
|
319
|
|
|
{ |
|
320
|
|
|
$this->assertEmailCcContain($needle, $this->getEmailFirstMessage(), $description); |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* Assert that the cc list of Last Mail contains a certain one |
|
325
|
|
|
* |
|
326
|
|
|
* @param $needle |
|
327
|
|
|
* @param string $description |
|
328
|
|
|
*/ |
|
329
|
|
|
public function assertEmailLastCcContain($needle, $description = '') |
|
330
|
|
|
{ |
|
331
|
|
|
$this->assertEmailCcContain($needle, $this->getEmailLastMessage(), $description); |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
/** |
|
335
|
|
|
* Assert that the cc list of Nth Mail contains a certain one |
|
336
|
|
|
* |
|
337
|
|
|
* @param $needle |
|
338
|
|
|
* $param $nth |
|
339
|
|
|
* @param string $description |
|
340
|
|
|
*/ |
|
341
|
|
|
public function assertEmailNthCcContain($needle, $nth, $description = '') |
|
342
|
|
|
{ |
|
343
|
|
|
$this->assertEmailCcContain($needle, $this->getEmailMessage($nth), $description); |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
/** |
|
347
|
|
|
* Assert that the cc list of Email contains a certain one |
|
348
|
|
|
* |
|
349
|
|
|
* @param $needle |
|
350
|
|
|
* @param string $description |
|
351
|
|
|
*/ |
|
352
|
|
|
public function assertEmailCcContain($needle, $email, $description = '') |
|
353
|
|
|
{ |
|
354
|
|
|
$needle = '<' . $needle . '>'; |
|
355
|
|
|
$this->assertContains($needle, $email->recipients, $description); |
|
|
|
|
|
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
/** |
|
359
|
|
|
* Assert that the Bcc exists on the First Email |
|
360
|
|
|
* Mailcatcher doesn't add a seperate bcc field, it creates a new email |
|
361
|
|
|
* with the bcc address as the recipient. |
|
362
|
|
|
* |
|
363
|
|
|
* @param $needle |
|
364
|
|
|
* @param string $description |
|
365
|
|
|
*/ |
|
366
|
|
|
public function assertEmailFirstBccContain($needle, $description = '') |
|
367
|
|
|
{ |
|
368
|
|
|
return $this->fetchEmailWithBcc($needle, $this->getEmailMessages(), $description); |
|
369
|
|
|
} |
|
370
|
|
|
|
|
371
|
|
|
|
|
372
|
|
|
/** |
|
373
|
|
|
* Assert that the Bcc list of the Last Email contains a certain one |
|
374
|
|
|
* |
|
375
|
|
|
* @param $needle |
|
376
|
|
|
* @param string $description |
|
377
|
|
|
*/ |
|
378
|
|
|
public function assertEmailLastBccContain($needle, $description = '') |
|
379
|
|
|
{ |
|
380
|
|
|
return $this->fetchEmailWithBcc($needle, array_reverse($this->getEmailMessages()), $description); |
|
381
|
|
|
} |
|
382
|
|
|
|
|
383
|
|
|
/** |
|
384
|
|
|
* Assert that the Bcc list of the Nth Email contains a certain one |
|
385
|
|
|
* |
|
386
|
|
|
* @param $needle |
|
387
|
|
|
* @param $nth |
|
388
|
|
|
* @param string $description |
|
389
|
|
|
*/ |
|
390
|
|
|
public function assertEmailNthtBccContain($needle, $nth, $description = '') |
|
391
|
|
|
{ |
|
392
|
|
|
return $this->fetchEmailWithBcc($needle, array_slice($this->getEmailMessages(), $nth - 1), $description); |
|
393
|
|
|
} |
|
394
|
|
|
|
|
395
|
|
|
/** |
|
396
|
|
|
* Assert that the Bcc list of Email contains a certain one |
|
397
|
|
|
* |
|
398
|
|
|
* @param $needle |
|
399
|
|
|
* @param string $description |
|
400
|
|
|
*/ |
|
401
|
|
|
public function assertEmailBccContain($needle, $email, $description = '') |
|
402
|
|
|
{ |
|
403
|
|
|
$this->assertContains($needle, $email->recipients, $description); |
|
|
|
|
|
|
404
|
|
|
} |
|
405
|
|
|
|
|
406
|
|
|
/** |
|
407
|
|
|
* Helper in order to hack the mailcatcher issue with Bcc field |
|
408
|
|
|
* @param $needle |
|
409
|
|
|
* @param $description |
|
410
|
|
|
* @param $messages |
|
411
|
|
|
*/ |
|
412
|
|
|
private function fetchEmailWithBcc($needle, $messages, $description) |
|
413
|
|
|
{ |
|
414
|
|
|
$needle = '<' . $needle . '>'; |
|
415
|
|
|
|
|
416
|
|
|
if (empty($messages)) { |
|
417
|
|
|
$this->fail("No messages received"); |
|
|
|
|
|
|
418
|
|
|
} |
|
419
|
|
|
foreach ($messages as $message) { |
|
420
|
|
|
if (in_array($needle, $message->recipients)) { |
|
421
|
|
|
return $this->assertEmailBccContain($needle, $message, $description); |
|
422
|
|
|
} |
|
423
|
|
|
} |
|
424
|
|
|
|
|
425
|
|
|
$this->fail("No bcc found"); |
|
|
|
|
|
|
426
|
|
|
} |
|
427
|
|
|
|
|
428
|
|
|
/** |
|
429
|
|
|
* Get the first message |
|
430
|
|
|
*/ |
|
431
|
|
|
private function getEmailFirstMessage() |
|
432
|
|
|
{ |
|
433
|
|
|
$messages = $this->getEmailMessages(); |
|
434
|
|
|
if (empty($messages)) { |
|
435
|
|
|
$this->fail("No messages received"); |
|
|
|
|
|
|
436
|
|
|
} |
|
437
|
|
|
|
|
438
|
|
|
return array_shift($messages); |
|
439
|
|
|
} |
|
440
|
|
|
|
|
441
|
|
|
/** |
|
442
|
|
|
* Get the last message |
|
443
|
|
|
*/ |
|
444
|
|
|
private function getEmailLastMessage() |
|
445
|
|
|
{ |
|
446
|
|
|
$messages = $this->getEmailMessages(); |
|
447
|
|
|
if (empty($messages)) { |
|
448
|
|
|
$this->fail("No messages received"); |
|
|
|
|
|
|
449
|
|
|
} |
|
450
|
|
|
|
|
451
|
|
|
return end($messages); |
|
452
|
|
|
} |
|
453
|
|
|
|
|
454
|
|
|
/** |
|
455
|
|
|
* Get a certain message |
|
456
|
|
|
*/ |
|
457
|
|
|
private function getEmailMessage($nth) |
|
458
|
|
|
{ |
|
459
|
|
|
$messages = $this->getEmailMessages(); |
|
460
|
|
|
if (empty($messages)) { |
|
461
|
|
|
$this->fail("No messages received"); |
|
|
|
|
|
|
462
|
|
|
} |
|
463
|
|
|
|
|
464
|
|
|
return array_values($messages)[$nth - 1]; |
|
465
|
|
|
} |
|
466
|
|
|
|
|
467
|
|
|
|
|
468
|
|
|
/** |
|
469
|
|
|
* Get all messages |
|
470
|
|
|
*/ |
|
471
|
|
|
private function getEmailMessages() |
|
472
|
|
|
{ |
|
473
|
|
|
$jsonResponse = $this->mailcatcher->get('/messages'); |
|
474
|
|
|
return json_decode($jsonResponse->getBody()); |
|
475
|
|
|
} |
|
476
|
|
|
} |
|
477
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: