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 6602ec2

Browse files
committed
Merge branch 'develop'
2 parents a82ba8c + 2d6168e commit 6602ec2

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
@@ -4,6 +4,7 @@
44

55
use GuzzleHttp\Client;
66
use GuzzleHttp\Psr7\Stream;
7+
use PHPUnit\Framework\Assert;
78

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

@@ -119,6 +120,18 @@ public function fetchLastMessage()
119120

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

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

@@ -334,4 +347,115 @@ public function getBccEmailOfMessage($messageId)
334347

335348
return $bcc;
336349
}
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+
}
337461
}

‎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 によって変換されたページ (->オリジナル) /