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