Skip to content

Commit 08fe12f

Browse files
author
Herbie (Christian) Keuerleber
committed
Initial commit
0 parents  commit 08fe12f

7 files changed

Lines changed: 1214 additions & 0 deletions

File tree

Classes/ActorTraits/MailPit.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
namespace PunktDe\Codeception\MailPit\ActorTraits;
3+
4+
/*
5+
* This file is part of the PunktDe\Codeception-MailPit package.
6+
*
7+
* This package is open source software. For the full copyright and license
8+
* information, please view the LICENSE file which was distributed with this
9+
* source code.
10+
*/
11+
12+
trait MailPit
13+
{
14+
/**
15+
* @Then /^the inbox contains (\d+) mails?$/
16+
* @param int|string $numberOfMails
17+
*/
18+
public function theInboxContainsMails(int|string $numberOfMails): void
19+
{
20+
$this->inboxContainsNumberOfMails((int)$numberOfMails);
21+
}
22+
23+
/**
24+
* @When I follow :link in the email
25+
* @param string $link
26+
*/
27+
public function iFollowInTheEmailSentTo(string $link): void
28+
{
29+
$this->openMailByNumber(1);
30+
$this->followLinkInTheEmail($link);
31+
}
32+
33+
/**
34+
* @When I clear my inbox
35+
*/
36+
public function iClearMyInbox(): void
37+
{
38+
$this->clearInbox();
39+
}
40+
41+
/**
42+
* @When I open the first mail
43+
*/
44+
public function iOpenTheFirstMail(): void
45+
{
46+
$this->openMailByNumber(1);
47+
}
48+
49+
/**
50+
* @When I open the second mail
51+
*/
52+
public function iOpenTheSecondMail(): void
53+
{
54+
$this->openMailByNumber(2);
55+
}
56+
57+
/**
58+
* @Then I should see :text in the email
59+
* @param string $text
60+
*/
61+
public function iSeeInMail(string $text): void
62+
{
63+
$this->seeTextInMail($text);
64+
}
65+
66+
/**
67+
* @Then The email is addressed to :address
68+
* @param string $address
69+
*/
70+
public function mailIsAddressedTo(string $address): void
71+
{
72+
$this->checkRecipientAddress($address);
73+
}
74+
75+
/**
76+
* @Then I should see :subject in the email subject
77+
* @param string $subject
78+
*/
79+
public function iSeeSubjectOfMail(string $subject): void
80+
{
81+
$this->seeSubjectOfMail($subject);
82+
}
83+
84+
/**
85+
* @Then This mail is spam
86+
*/
87+
public function ifSpamMail(): void
88+
{
89+
$this->checkIfSpam();
90+
}
91+
}

Classes/Domain/MailDevClient.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
namespace PunktDe\Codeception\MailPit\Domain;
3+
4+
/*
5+
* This file is part of the PunktDe\Codeception-MailPit package.
6+
*
7+
* This package is open source software. For the full copyright and license
8+
* information, please view the LICENSE file which was distributed with this
9+
* source code.
10+
*/
11+
12+
use GuzzleHttp\Client;
13+
use PunktDe\Codeception\MailPit\Domain\Model\Mail;
14+
15+
class MailPitClient
16+
{
17+
/**
18+
* @var Client
19+
*/
20+
protected $client;
21+
22+
protected const BASE_PATH = '/api/v1';
23+
24+
/**
25+
* MailPitClient constructor.
26+
* @param string $baseUri
27+
* @param string $username
28+
* @param string $password
29+
* @param string $authenticationType
30+
*/
31+
public function __construct(string $baseUri, string $username = '', string $password = '', string $authenticationType = 'basic')
32+
{
33+
$configuration = [
34+
'base_uri' => $baseUri,
35+
'cookies' => true,
36+
'headers' => [
37+
'User-Agent' => 'FancyPunktDeGuzzleTestingAgent'
38+
],
39+
];
40+
41+
if($username !== '' && $password !== '') {
42+
$configuration = array_merge($configuration, ['auth' => [$username, $password, $authenticationType]]);
43+
}
44+
45+
$this->client = new Client($configuration);
46+
}
47+
48+
public function deleteAllMails(): void
49+
{
50+
$this->client->delete(self::BASE_PATH . '/messages');
51+
}
52+
53+
/**
54+
* @return int
55+
* @throws \Exception
56+
*/
57+
public function countAll(): int
58+
{
59+
$data = $this->getDataFromMailPit('/messages');
60+
61+
return $data['total'];
62+
}
63+
64+
/**
65+
* @param $index
66+
* @return Mail
67+
*/
68+
public function findOneByIndex(int $index): Mail
69+
{
70+
$list = $this->getDataFromMailPit('/messages');
71+
72+
$id = $list['messages'][$index]['ID'];
73+
74+
$data = $this->getDataFromMailPit('/message/' . $id);
75+
76+
return new Mail($data);
77+
}
78+
79+
/**
80+
* @param $apiCall
81+
* @return array
82+
* @throws \Exception
83+
*/
84+
protected function getDataFromMailPit($apiCall): array
85+
{
86+
$result = $this->client->get(self::BASE_PATH . $apiCall)->getBody();
87+
88+
$data = json_decode($result, true);
89+
90+
if ($data === false) {
91+
throw new \Exception('The mailpit result could not be parsed to json', 1774452891);
92+
}
93+
94+
return $data;
95+
}
96+
97+
/**
98+
* @param array $data
99+
* @return Mail
100+
*/
101+
protected function buildMailObjectFromJson(array $data): Mail
102+
{
103+
return new Mail($data);
104+
}
105+
106+
}

Classes/Domain/Model/Mail.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
namespace PunktDe\Codeception\MailPit\Domain\Model;
3+
/*
4+
* This file is part of the PunktDe\Codeception-MailPit package.
5+
*
6+
* This package is open source software. For the full copyright and license
7+
* information, please view the LICENSE file which was distributed with this
8+
* source code.
9+
*/
10+
11+
use Neos\Utility\Arrays;
12+
13+
class Mail
14+
{
15+
16+
/**
17+
* @var array
18+
*/
19+
protected $mailData = [];
20+
21+
/**
22+
* @var array
23+
*/
24+
protected $recipients;
25+
26+
/**
27+
* @var string
28+
*/
29+
protected $subject;
30+
31+
/**
32+
* @var string
33+
*/
34+
protected $body;
35+
36+
37+
/**
38+
* Mail constructor.
39+
* @param array $mailData
40+
*/
41+
public function __construct(array $mailData)
42+
{
43+
$this->mailData = $mailData;
44+
45+
$this->body = Arrays::getValueByPath($this->mailData, 'HTML');
46+
$this->recipients = Arrays::getValueByPath($this->mailData, 'To.0.Address');
47+
$this->subject = Arrays::getValueByPath($this->mailData, 'Subject');
48+
}
49+
50+
/**
51+
* @return string
52+
*/
53+
public function getBody()
54+
{
55+
return $this->body;
56+
}
57+
58+
/**
59+
* @return string
60+
*/
61+
public function getSubject()
62+
{
63+
return $this->subject;
64+
}
65+
66+
/**
67+
* @return array
68+
*/
69+
public function getRecipients()
70+
{
71+
return $this->recipients;
72+
}
73+
}

0 commit comments

Comments
 (0)