4
4
5
5
use GuzzleHttp \Client ;
6
6
use GuzzleHttp \Psr7 \Stream ;
7
+ use PHPUnit \Framework \Assert ;
7
8
8
9
/**
9
10
* This module allows you to test emails using Mailtrap <https://mailtrap.io>.
@@ -109,7 +110,7 @@ public function receiveAnEmail($params)
109
110
*
110
111
* @return array
111
112
*/
112
- public function fetchLastMessage ()
113
+ public function fetchMessages ()
113
114
{
114
115
$ messages = $ this ->client ->get ("inboxes/ {$ this ->config ['inbox_id ' ]}/messages " )->getBody ();
115
116
@@ -119,6 +120,18 @@ public function fetchLastMessage()
119
120
120
121
$ messages = json_decode ($ messages , true );
121
122
123
+ return $ messages ;
124
+ }
125
+
126
+ /**
127
+ * Get the most recent message of the default inbox.
128
+ *
129
+ * @return array
130
+ */
131
+ public function fetchLastMessage ()
132
+ {
133
+ $ messages = $ this ->fetchMessages ();
134
+
122
135
return array_shift ($ messages );
123
136
}
124
137
@@ -334,4 +347,115 @@ public function getBccEmailOfMessage($messageId)
334
347
335
348
return $ bcc ;
336
349
}
350
+
351
+ /**
352
+ *
353
+ * @param int $timeout_in_second
354
+ * @param int $interval_in_millisecond
355
+ *
356
+ * @return MailtrapWait
357
+ */
358
+ protected function wait ($ timeout_in_second = 30 , $ interval_in_millisecond = 250 )
359
+ {
360
+ return new MailtrapWait ($ this , $ timeout_in_second , $ interval_in_millisecond );
361
+ }
362
+
363
+ /**
364
+ * Wait until an email to be received.
365
+ *
366
+ * @param int $timeout
367
+ *
368
+ * @throws \Exception
369
+ */
370
+ public function waitForEmail ($ timeout = 5 )
371
+ {
372
+ $ condition = function () {
373
+ return ! empty ($ this ->fetchLastMessage ());
374
+ };
375
+
376
+ $ message = sprintf ('Waited for %d secs but no email has arrived ' , $ timeout );
377
+
378
+ $ this ->wait ($ timeout )->until ($ condition , $ message );
379
+ }
380
+
381
+ /**
382
+ * Wait until an email has been received with specific text in the text body.
383
+ *
384
+ * @param string $subject
385
+ * @param int $timeout
386
+ *
387
+ * @throws \Exception
388
+ */
389
+ public function waitForEmailWithSubject ($ subject , $ timeout = 5 )
390
+ {
391
+ $ condition = function () use ($ subject ) {
392
+ $ emails = $ this ->fetchMessages ();
393
+ foreach ($ emails as $ email ) {
394
+ $ constraint = Assert::equalTo ($ subject );
395
+ if ($ constraint ->evaluate ($ email ['subject ' ], '' , true )) {
396
+ return true ;
397
+ }
398
+ }
399
+
400
+ return false ;
401
+ };
402
+
403
+ $ message = sprintf ('Waited for %d secs but no email with the subject of %s has arrived ' , $ timeout , $ subject );
404
+
405
+ $ this ->wait ($ timeout )->until ($ condition , $ message );
406
+ }
407
+
408
+ /**
409
+ * Wait until an email has been received with specific text in the text body.
410
+ *
411
+ * @param string $text
412
+ * @param int $timeout
413
+ *
414
+ * @throws \Exception
415
+ */
416
+ public function waitForEmailWithTextInTextBody ($ text , $ timeout = 5 )
417
+ {
418
+ $ condition = function () use ($ text ) {
419
+ $ emails = $ this ->fetchMessages ();
420
+ foreach ($ emails as $ email ) {
421
+ $ constraint = Assert::stringContains ($ text );
422
+ if ($ constraint ->evaluate ($ email ['text_body ' ], '' , true )) {
423
+ return true ;
424
+ }
425
+ }
426
+
427
+ return false ;
428
+ };
429
+
430
+ $ message = sprintf ('Waited for %d secs but no email with the text body containing %s has arrived ' , $ timeout , $ text );
431
+
432
+ $ this ->wait ($ timeout )->until ($ condition , $ message );
433
+ }
434
+
435
+ /**
436
+ * Wait until an email has been received with specific text in the text body.
437
+ *
438
+ * @param string $text
439
+ * @param int $timeout
440
+ *
441
+ * @throws \Exception
442
+ */
443
+ public function waitForEmailWithTextInHTMLBody ($ text , $ timeout = 5 )
444
+ {
445
+ $ condition = function () use ($ text ) {
446
+ $ emails = $ this ->fetchMessages ();
447
+ foreach ($ emails as $ email ) {
448
+ $ constraint = Assert::stringContains ($ text );
449
+ if ($ constraint ->evaluate ($ email ['html_body ' ], '' , true )) {
450
+ return true ;
451
+ }
452
+ }
453
+
454
+ return false ;
455
+ };
456
+
457
+ $ message = sprintf ('Waited for %d secs but no email with the html body containing %s has arrived ' , $ timeout , $ text );
458
+
459
+ $ this ->wait ($ timeout )->until ($ condition , $ message );
460
+ }
337
461
}
0 commit comments