Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 70 additions & 38 deletions src/Mailtrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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>.
Expand All @@ -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
*
Expand All @@ -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
Expand All @@ -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();
}
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Member

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.


/**
* 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;
}
Copy link
Member

Choose a reason for hiding this comment

The 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:

public function getTestEmailAddress() { return $this->config['testEmailAddress']; } 

/**
* Check if the latest email received contains $params.
*
Expand All @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method searchForMessage() could be added in addition with the fetchLastMessage method, no need to get rid of it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also:

  • the else statement could be avoided
  • the InboxID parameter should be optional, since it will default to the one in the config
  • emails are usually received by mailtrap pretty fast, not sure doing 60 attempts is necessary
  • the use of sleep() and echo is not recommended, nor necessary.
}

Expand All @@ -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);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • The else statement could be avoided here as well
  • The $inboxID should be the second parameter and optional as it will default to the config.

/**
* 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);
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Make $inboxID optional
  • Else statement unnecessary.
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.
*
Expand Down Expand Up @@ -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);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Member

Choose a reason for hiding this comment

The 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.

}