Skip to content

Commit

Permalink
Merge pull request #6 from kabarakh/Add-changes-from-mailhog
Browse files Browse the repository at this point in the history
Add changes from mailhog
  • Loading branch information
kabarakh authored Jan 17, 2025
2 parents d807943 + 8ca831d commit 7321e2c
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 10 deletions.
17 changes: 17 additions & 0 deletions Classes/ActorTraits/MailDev.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,21 @@ public function mailIsAddressedTo(string $address): void
{
$this->checkRecipientAddress($address);
}

/**
* @Then I should see :subject in the email subject
* @param string $subject
*/
public function iSeeSubjectOfMail(string $subject): void
{
$this->seeSubjectOfMail($subject);
}

/**
* @Then This mail is spam
*/
public function ifSpamMail(): void
{
$this->checkIfSpam();
}
}
28 changes: 26 additions & 2 deletions Classes/Domain/MailDevClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ class MailDevClient
*/
protected $client;

/**
* MailDevClient constructor.
* @param string $baseUri
* @param string $username
* @param string $password
* @param string $authenticationType
*/
public function __construct(string $baseUri, string $username = '', string $password = '', string $authenticationType = 'basic')
{
$configuration = [
Expand All @@ -41,13 +48,21 @@ public function deleteAllMails(): void
$this->client->delete('/email/all');
}

/**
* @return int
* @throws \Exception
*/
public function countAll(): int
{
$data = $this->getDataFromMailDev('/email');

return count($data);
}

/**
* @param $index
* @return Mail
*/
public function findOneByIndex(int $index): Mail
{
$data = $this->getDataFromMailDev('/email');
Expand All @@ -67,10 +82,19 @@ protected function getDataFromMailDev($apiCall): array
$data = json_decode($result, true);

if ($data === false) {
throw new \Exception('The mailhog result could not be parsed to json', 1467038556);
throw new \Exception('The maildev result could not be parsed to json', 1467038556);
}

return $data;
}

}
/**
* @param array $data
* @return Mail
*/
protected function buildMailObjectFromJson(array $data): Mail
{
return new Mail($data);
}

}
5 changes: 2 additions & 3 deletions Classes/Domain/Model/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ public function __construct(array $mailData)
$this->mailData = $mailData;

$this->body = Arrays::getValueByPath($this->mailData, 'html');
$this->recipients = Arrays::getValueByPath($this->mailData, 'headers.To');
$this->subject = Arrays::getValueByPath($this->mailData, 'headers.Subject');

$this->recipients = Arrays::getValueByPath($this->mailData, 'headers.to');
$this->subject = Arrays::getValueByPath($this->mailData, 'headers.subject');
}

/**
Expand Down
51 changes: 46 additions & 5 deletions Classes/Module/MailDev.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ class MailDev extends Module
*/
protected $currentMail = null;

public function __construct(ModuleContainer $moduleContainer, array $config = [])
/**
* Maildev constructor.
* @param ModuleContainer $moduleContainer
* @param mixed[]|null $config
*/
public function __construct(ModuleContainer $moduleContainer, array $config = null)
{
parent::__construct($moduleContainer, $config);
parent::__construct($moduleContainer, $config);
$this->mailDevClient = new MailDevClient(
$config['base_uri'] ?? 'http://127.0.0.1:8025',
$config['username'] ?? '',
Expand Down Expand Up @@ -59,7 +64,11 @@ public function openMailByNumber(int $mailNumber): void
$mailIndex = $mailNumber - 1;
$this->currentMail = $this->mailDevClient->findOneByIndex($mailIndex);

$this->assertInstanceOf(Mail::class, $this->currentMail, 'The mail with number ' . $mailNumber . ' does not exist.');
$this->assertInstanceOf(
Mail::class,
$this->currentMail,
'The mail with number ' . $mailNumber . ' does not exist.'
);
}

/**
Expand All @@ -70,7 +79,8 @@ public function followLinkInTheEmail(string $link): void
{
$mail = $this->parseMailBody($this->currentMail->getBody());
if (preg_match('/(http[^\s|^"]*' . preg_quote($link, '/') . '[^\s|^"]*)/', $mail, $links)) {
$webdriver = $this->getModule('WebDriver'); /** @var Module\WebDriver $webdriver */
$webdriver = $this->getModule('WebDriver');
/** @var Module\WebDriver $webdriver */
$targetLink = $links[0];
$targetLink = urldecode($targetLink);
$targetLink = html_entity_decode($targetLink);
Expand All @@ -87,9 +97,10 @@ public function followLinkInTheEmail(string $link): void
public function seeTextInMail(string $text): void
{
$mail = $this->parseMailBody($this->currentMail->getBody());
if (stristr($mail, $text)) {
if (stristr(html_entity_decode($mail), $text)) {
return;
}

throw new \Exception(sprintf('Did not find the text "%s" in the mail', $text));
}

Expand All @@ -108,6 +119,36 @@ public function checkRecipientAddress(string $address): void
throw new \Exception(sprintf('Did not find the recipient "%s" in the mail', $address));
}

/**
* @throws \Exception
*/
public function checkIfSpam(): void
{
$subject = $this->currentMail->getSubject();

if (strpos($subject, "[SPAM]") === 0) {
return;
}

throw new \Exception(sprintf('Could not find [SPAM] at the beginning of subject "%s"', $subject));
}


/**
* @param string $text
* @throws \Exception
*/
public function seeSubjectOfMail(string $text): void
{
$subject = $this->currentMail->getSubject();

if (stristr($subject, $text)) {
return;
}

throw new \Exception(sprintf('Did not find the subject "%s" in the mail, subject was "%s"', $text, $subject));
}

/**
* @param string $mailBody
* @return string
Expand Down

0 comments on commit 7321e2c

Please sign in to comment.