- Notifications
You must be signed in to change notification settings - Fork 23
New functionality to search for specific email and to delete and email #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
| @@ -4,7 +4,6 @@ | |
| ||
use Codeception\Module; | ||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Psr7\Stream; | ||
| ||
/** | ||
* This module allows you to test emails using Mailtrap <https://mailtrap.io>. | ||
| @@ -24,7 +23,6 @@ | |
* | ||
* * client_id: `string`, default `` - Your mailtrap API key. | ||
* * inbox_id: `string`, default `` - The inbox ID to use for the tests | ||
* * cleanup: `boolean`, default `true` - Clean the inbox after each scenario | ||
* | ||
* ## API | ||
* | ||
| @@ -45,7 +43,7 @@ class Mailtrap extends Module | |
/** | ||
* @var array | ||
*/ | ||
protected $config = ['client_id' => null, 'inbox_id' => null, 'cleanup' => true]; | ||
protected $config = ['client_id' => null, 'inbox_id' => null]; | ||
| ||
/** | ||
* @var array | ||
| @@ -72,23 +70,32 @@ public function _initialize() | |
* | ||
* @param \Codeception\TestCase $test | ||
*/ | ||
| ||
/* | ||
public function _after(\Codeception\TestCase $test) | ||
{ | ||
if ($this->config['cleanup']) { | ||
$this->cleanInbox(); | ||
} | ||
$this->cleanInbox(); | ||
} | ||
*/ | ||
| ||
/** | ||
* Clean all the messages from inbox. | ||
* | ||
* @return void | ||
*/ | ||
| ||
public function cleanInbox() | ||
{ | ||
$this->client->patch("inboxes/{$this->config['inbox_id']}/clean"); | ||
} | ||
| ||
public function getTestEmailAddress() | ||
{ | ||
$testEmailAddress = $this->config['testEmailAddress']; | ||
| ||
return $testEmailAddress; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Providing a default in the config would be preferred. Also, the use of a temporary variable is not necessary here and could be avoided:
| ||
| ||
/** | ||
* Check if the latest email received contains $params. | ||
* | ||
| @@ -110,15 +117,28 @@ public function receiveAnEmail($params) | |
* | ||
* @return array | ||
*/ | ||
public function fetchLastMessage() | ||
public function searchForMessage($emailSearchForString, $inboxID) | ||
{ | ||
$messages = $this->client->get("inboxes/{$this->config['inbox_id']}/messages")->getBody(); | ||
if ($messages instanceof Stream) { | ||
$messages = $messages->getContents(); | ||
} | ||
| ||
$messages = json_decode($messages, true); | ||
$counter = 0; | ||
if ($inboxID != ''){ // test sent a specific email box to search | ||
do { | ||
sleep(1); | ||
$counter++; | ||
echo " Counter = " . $counter . " : "; | ||
$messages = $this->client->get("inboxes/$inboxID/messages?search=".$emailSearchForString)->getBody(); | ||
$messages = json_decode($messages, true); | ||
} while ($counter < 60 && $messages == Null); | ||
| ||
| ||
} else { // Use the config email box | ||
do { | ||
sleep(1); | ||
$counter++; | ||
echo " Counter = " . $counter; | ||
$messages = $this->client->get("inboxes/{$this->config['inbox_id']}/messages?search=".$emailSearchForString)->getBody(); | ||
$messages = json_decode($messages, true); | ||
} while ($counter < 60 && $messages == Null); | ||
} | ||
return array_shift($messages); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also:
| ||
} | ||
| ||
| @@ -131,10 +151,46 @@ public function fetchAttachmentsOfLastMessage() | |
{ | ||
$email = $this->fetchLastMessage(); | ||
$response = $this->client->get("inboxes/{$this->config['inbox_id']}/messages/{$email['id']}/attachments")->getBody(); | ||
| ||
return json_decode($response, true); | ||
} | ||
| ||
public function fetchAttachmentsOfMessage($inboxID, $messageID) | ||
{ | ||
if ($inboxID != ''){ // test sent a specific email box to search | ||
$messages = $this->client->get("inboxes/$inboxID/messages/$messageID/attachments")->getBody(); | ||
$messages = json_decode($messages, true); | ||
} else { // Use the config email box | ||
$messages = $this->client->get("inboxes/{$this->config['inbox_id']}/messages/$messageID/attachments")->getBody(); | ||
$messages = json_decode($messages, true); | ||
} | ||
return array_shift($messages); | ||
} | ||
| ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
| ||
| ||
/** | ||
* Delete a specific message from the inbox. Must pass in the message ID to delete | ||
* | ||
* @return array | ||
*/ | ||
public function deleteMessage($messageID, $inboxID) | ||
{ | ||
if($inboxID != ''){ | ||
$messages = $this->client->delete("inboxes/$inboxID/messages/".$messageID); | ||
} else { | ||
$messages = $this->client->delete("inboxes/{$this->config['inbox_id']}/messages/".$messageID); | ||
} | ||
} | ||
| ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
| ||
public function fetchLastMessage() | ||
{ | ||
$messages = $this->client->get("inboxes/{$this->config['inbox_id']}/messages")->getBody(); | ||
$messages = json_decode($messages, true); | ||
| ||
return array_shift($messages); | ||
} | ||
| ||
| ||
| ||
/** | ||
* Check if the latest email received is from $senderEmail. | ||
* | ||
| @@ -251,28 +307,4 @@ public function seeInEmailHtmlBody($expected) | |
$email = $this->fetchLastMessage(); | ||
$this->assertContains($expected, $email['html_body'], 'Email body contains HTML'); | ||
} | ||
| ||
/** | ||
* Look for an attachment on the most recent email. | ||
* | ||
* @param $count | ||
*/ | ||
public function seeAttachments($count) | ||
{ | ||
$attachments = $this->fetchAttachmentsOfLastMessage(); | ||
| ||
$this->assertEquals($count, count($attachments)); | ||
} | ||
| ||
/** | ||
* Look for an attachment on the most recent email. | ||
* | ||
* @param $bool | ||
*/ | ||
public function seeAnAttachment($bool) | ||
{ | ||
$attachments = $this->fetchAttachmentsOfLastMessage(); | ||
| ||
$this->assertEquals($bool, count($attachments) > 0); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed with @sergeyklay, no need to delete method because they seem related to the feature you are trying to add. | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed with @sergeyklay, the cleanup functionality is useful when running test in some cases, you can always deactivate it from the config.