Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2d6168e

Browse files
authored
Merge pull request #27 from polevaultweb/wait-support
waitFor method support
2 parents 96a6cc3 + 09c092e commit 2d6168e

File tree

2 files changed

+195
-1
lines changed

2 files changed

+195
-1
lines changed

‎src/Mailtrap.php‎

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Codeception\Module;
66
use GuzzleHttp\Client;
77
use GuzzleHttp\Psr7\Stream;
8+
use PHPUnit\Framework\Assert;
89

910
/**
1011
* This module allows you to test emails using Mailtrap <https://mailtrap.io>.
@@ -110,7 +111,7 @@ public function receiveAnEmail($params)
110111
*
111112
* @return array
112113
*/
113-
public function fetchLastMessage()
114+
public function fetchMessages()
114115
{
115116
$messages = $this->client->get("inboxes/{$this->config['inbox_id']}/messages")->getBody();
116117

@@ -120,6 +121,18 @@ public function fetchLastMessage()
120121

121122
$messages = json_decode($messages, true);
122123

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+
123136
return array_shift($messages);
124137
}
125138

@@ -335,4 +348,115 @@ public function getBccEmailOfMessage($messageId)
335348

336349
return $bcc;
337350
}
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+
}
338462
}

‎src/MailtrapWait.php‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Codeception\Module;
4+
5+
/**
6+
* A utility class, designed to help the user to wait until a condition turns true.
7+
*
8+
*/
9+
class MailtrapWait
10+
{
11+
/**
12+
* @var Mailtrap
13+
*/
14+
protected $mailtrap;
15+
/**
16+
* @var int
17+
*/
18+
protected $timeout;
19+
/**
20+
* @var int
21+
*/
22+
protected $interval;
23+
24+
/**
25+
* MailtrapWait constructor.
26+
*
27+
* @param Mailtrap $mailtrap
28+
* @param null|int $timeout_in_second
29+
* @param null|int $interval_in_millisecond
30+
*/
31+
public function __construct(Mailtrap $mailtrap, $timeout_in_second = null, $interval_in_millisecond = null)
32+
{
33+
$this->mailtrap = $mailtrap;
34+
$this->timeout = isset($timeout_in_second) ? $timeout_in_second : 30;
35+
$this->interval = $interval_in_millisecond ? : 250;
36+
}
37+
38+
/**
39+
* Calls the function provided with the driver as an argument until the return value is not falsey.
40+
*
41+
* @param callable $function
42+
* @param string $message
43+
*
44+
* @throws \Exception
45+
* @return mixed The return value of $function
46+
*/
47+
public function until($function, $message = '')
48+
{
49+
$end = microtime(true) + $this->timeout;
50+
$last_exception = null;
51+
52+
while ($end > microtime(true)) {
53+
try {
54+
$ret_val = call_user_func($function, $this->mailtrap);
55+
if ($ret_val) {
56+
return $ret_val;
57+
}
58+
} catch (\Exception $e) {
59+
$last_exception = $e;
60+
}
61+
usleep($this->interval * 1000);
62+
}
63+
64+
if ($last_exception) {
65+
throw $last_exception;
66+
}
67+
68+
throw new \Exception($message);
69+
}
70+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /