diff --git a/Classes/ActorTraits/MailDev.php b/Classes/ActorTraits/MailDev.php index 7256c52..706b65d 100644 --- a/Classes/ActorTraits/MailDev.php +++ b/Classes/ActorTraits/MailDev.php @@ -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(); + } } diff --git a/Classes/Domain/MailDevClient.php b/Classes/Domain/MailDevClient.php index cece171..e2e3b7c 100644 --- a/Classes/Domain/MailDevClient.php +++ b/Classes/Domain/MailDevClient.php @@ -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 = [ @@ -41,6 +48,10 @@ public function deleteAllMails(): void $this->client->delete('/email/all'); } + /** + * @return int + * @throws \Exception + */ public function countAll(): int { $data = $this->getDataFromMailDev('/email'); @@ -48,6 +59,10 @@ public function countAll(): int return count($data); } + /** + * @param $index + * @return Mail + */ public function findOneByIndex(int $index): Mail { $data = $this->getDataFromMailDev('/email'); @@ -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; } -} \ No newline at end of file + /** + * @param array $data + * @return Mail + */ + protected function buildMailObjectFromJson(array $data): Mail + { + return new Mail($data); + } + +} diff --git a/Classes/Domain/Model/Mail.php b/Classes/Domain/Model/Mail.php index c7a09fc..8701331 100644 --- a/Classes/Domain/Model/Mail.php +++ b/Classes/Domain/Model/Mail.php @@ -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'); } /** diff --git a/Classes/Module/MailDev.php b/Classes/Module/MailDev.php index cbbd4df..9e33332 100644 --- a/Classes/Module/MailDev.php +++ b/Classes/Module/MailDev.php @@ -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'] ?? '', @@ -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.' + ); } /** @@ -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); @@ -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)); } @@ -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