Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ba95d4b

Browse files
authored
Merge pull request #36 from polevaultweb/34-api-changes
Handle API deprecation of html and text body message properties
2 parents 6c59598 + 0193263 commit ba95d4b

File tree

2 files changed

+144
-42
lines changed

2 files changed

+144
-42
lines changed

‎src/Mailtrap.php‎

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Codeception\Module;
44

5+
use Codeception\Module;
56
use GuzzleHttp\Client;
67
use GuzzleHttp\Psr7\Stream;
78
use PHPUnit\Framework\Assert;
@@ -101,7 +102,7 @@ public function receiveAnEmail($params)
101102
$message = $this->fetchLastMessage();
102103

103104
foreach ($params as $param => $value) {
104-
$this->assertEquals($value, $message[$param]);
105+
$this->assertEquals($value, $message->{$param});
105106
}
106107
}
107108

@@ -120,14 +121,38 @@ public function fetchMessages()
120121

121122
$messages = json_decode($messages, true);
122123

124+
foreach ( $messages as $key => $message ) {
125+
$messages[ $key ] = new MailtrapMessage( $message, $this->client );
126+
}
127+
123128
return $messages;
124129
}
125130

126131
/**
127-
* Get the most recent message of the default inbox.
132+
* Get the most recent messages of the default inbox.
133+
*
134+
* @param int $number
128135
*
129136
* @return array
130137
*/
138+
public function fetchLastMessages($number = 1)
139+
{
140+
$messages = $this->fetchMessages();
141+
142+
$firstIndex = count($messages) - $number;
143+
144+
$messages = array_slice($messages, $firstIndex, $number);
145+
146+
$this->assertCount($number, $messages);
147+
148+
return $messages;
149+
}
150+
151+
/**
152+
* Get the most recent message of the default inbox.
153+
*
154+
* @return MailtrapMessage
155+
*/
131156
public function fetchLastMessage()
132157
{
133158
$messages = $this->fetchMessages();
@@ -143,7 +168,7 @@ public function fetchLastMessage()
143168
public function fetchAttachmentsOfLastMessage()
144169
{
145170
$email = $this->fetchLastMessage();
146-
$response = $this->client->get("inboxes/{$this->config['inbox_id']}/messages/{$email['id']}/attachments")->getBody();
171+
$response = $this->client->get("inboxes/{$this->config['inbox_id']}/messages/{$email->id}/attachments")->getBody();
147172

148173
return json_decode($response, true);
149174
}
@@ -158,7 +183,7 @@ public function fetchAttachmentsOfLastMessage()
158183
public function receiveAnEmailFromEmail($senderEmail)
159184
{
160185
$message = $this->fetchLastMessage();
161-
$this->assertEquals($senderEmail, $message['from_email']);
186+
$this->assertEquals($senderEmail, $message->from_email);
162187
}
163188

164189
/**
@@ -171,7 +196,7 @@ public function receiveAnEmailFromEmail($senderEmail)
171196
public function receiveAnEmailFromName($senderName)
172197
{
173198
$message = $this->fetchLastMessage();
174-
$this->assertEquals($senderName, $message['from_name']);
199+
$this->assertEquals($senderName, $message->from_name);
175200
}
176201

177202
/**
@@ -184,7 +209,7 @@ public function receiveAnEmailFromName($senderName)
184209
public function receiveAnEmailToEmail($recipientEmail)
185210
{
186211
$message = $this->fetchLastMessage();
187-
$this->assertEquals($recipientEmail, $message['to_email']);
212+
$this->assertEquals($recipientEmail, $message->to_email);
188213
}
189214

190215
/**
@@ -197,7 +222,7 @@ public function receiveAnEmailToEmail($recipientEmail)
197222
public function receiveAnEmailToName($recipientName)
198223
{
199224
$message = $this->fetchLastMessage();
200-
$this->assertEquals($recipientName, $message['to_name']);
225+
$this->assertEquals($recipientName, $message->to_name);
201226
}
202227

203228
/**
@@ -210,7 +235,7 @@ public function receiveAnEmailToName($recipientName)
210235
public function receiveAnEmailWithSubject($subject)
211236
{
212237
$message = $this->fetchLastMessage();
213-
$this->assertEquals($subject, $message['subject']);
238+
$this->assertEquals($subject, $message->subject);
214239
}
215240

216241
/**
@@ -223,7 +248,7 @@ public function receiveAnEmailWithSubject($subject)
223248
public function receiveAnEmailWithTextBody($textBody)
224249
{
225250
$message = $this->fetchLastMessage();
226-
$this->assertEquals($textBody, $message['text_body']);
251+
$this->assertEquals($textBody, $message->text_body);
227252
}
228253

229254
/**
@@ -236,7 +261,7 @@ public function receiveAnEmailWithTextBody($textBody)
236261
public function receiveAnEmailWithHtmlBody($htmlBody)
237262
{
238263
$message = $this->fetchLastMessage();
239-
$this->assertEquals($htmlBody, $message['html_body']);
264+
$this->assertEquals($htmlBody, $message->html_body);
240265
}
241266

242267
/**
@@ -249,7 +274,7 @@ public function receiveAnEmailWithHtmlBody($htmlBody)
249274
public function seeInEmailTextBody($expected)
250275
{
251276
$email = $this->fetchLastMessage();
252-
$this->assertContains($expected, $email['text_body'], 'Email body contains text');
277+
$this->assertContains($expected, $email->text_body, 'Email body contains text');
253278
}
254279

255280
/**
@@ -262,7 +287,7 @@ public function seeInEmailTextBody($expected)
262287
public function seeInEmailHtmlBody($expected)
263288
{
264289
$email = $this->fetchLastMessage();
265-
$this->assertContains($expected, $email['html_body'], 'Email body contains HTML');
290+
$this->assertContains($expected, $email->html_body, 'Email body contains HTML');
266291
}
267292

268293
/**
@@ -275,7 +300,7 @@ public function seeInEmailHtmlBody($expected)
275300
public function seeInEmailSubject($expected)
276301
{
277302
$email = $this->fetchLastMessage();
278-
$this->assertContains($expected, $email['subject'], 'Email subject contains text');
303+
$this->assertContains($expected, $email->subject, 'Email subject contains text');
279304
}
280305

281306
/**
@@ -300,32 +325,6 @@ public function seeAnAttachment($bool)
300325
$this->assertEquals($bool, count($attachments) > 0);
301326
}
302327

303-
/**
304-
* Get the most recent messages of the default inbox.
305-
*
306-
* @param int $number
307-
*
308-
* @return array
309-
*/
310-
public function fetchLastMessages($number = 1)
311-
{
312-
$messages = $this->client->get("inboxes/{$this->config['inbox_id']}/messages")->getBody();
313-
314-
if ($messages instanceof Stream) {
315-
$messages = $messages->getContents();
316-
}
317-
318-
$messages = json_decode($messages, true);
319-
320-
$firstIndex = count($messages) - $number;
321-
322-
$messages = array_slice($messages, $firstIndex, $number);
323-
324-
$this->assertCount($number, $messages);
325-
326-
return $messages;
327-
}
328-
329328
/**
330329
* Get the bcc property of a message
331330
*
@@ -392,7 +391,7 @@ public function waitForEmailWithSubject($subject, $timeout = 5)
392391
$emails = $this->fetchMessages();
393392
foreach ($emails as $email) {
394393
$constraint = Assert::equalTo($subject);
395-
if ($constraint->evaluate($email['subject'], '', true)) {
394+
if ($constraint->evaluate($email->subject, '', true)) {
396395
return true;
397396
}
398397
}
@@ -419,7 +418,7 @@ public function waitForEmailWithTextInTextBody($text, $timeout = 5)
419418
$emails = $this->fetchMessages();
420419
foreach ($emails as $email) {
421420
$constraint = Assert::stringContains($text);
422-
if ($constraint->evaluate($email['text_body'], '', true)) {
421+
if ($constraint->evaluate($email->text_body, '', true)) {
423422
return true;
424423
}
425424
}
@@ -446,7 +445,7 @@ public function waitForEmailWithTextInHTMLBody($text, $timeout = 5)
446445
$emails = $this->fetchMessages();
447446
foreach ($emails as $email) {
448447
$constraint = Assert::stringContains($text);
449-
if ($constraint->evaluate($email['html_body'], '', true)) {
448+
if ($constraint->evaluate($email->html_body, '', true)) {
450449
return true;
451450
}
452451
}

‎src/MailtrapMessage.php‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace Codeception\Module;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Psr7\Stream;
7+
8+
/**
9+
* Represents a message in the MailTrap inbox
10+
*
11+
*/
12+
class MailtrapMessage {
13+
14+
/**
15+
* @var array Message payload
16+
*/
17+
protected $data;
18+
19+
/**
20+
* @var Client
21+
*/
22+
protected $client;
23+
24+
/**
25+
* @var string HTML body of the message
26+
*/
27+
protected $html_body;
28+
29+
/**
30+
* @var string Text body of the message
31+
*/
32+
protected $text_body;
33+
34+
/**
35+
* MailtrapMessage constructor.
36+
*
37+
* @param array $data
38+
* @param Client $client
39+
*/
40+
public function __construct( $data = [], Client $client ) {
41+
$this->data = $data;
42+
$this->client = $client;
43+
}
44+
45+
/**
46+
* @param string $name
47+
*
48+
* @return mixed|null
49+
*/
50+
public function __get( $name ) {
51+
if ( in_array( $name, array( 'html_body', 'text_body' ) ) ) {
52+
return $this->getMessageData( $name );
53+
}
54+
55+
if ( isset( $this->data[ $name ] ) ) {
56+
return $this->data[ $name ];
57+
}
58+
59+
return null;
60+
}
61+
62+
/**
63+
* Get the body data for a message
64+
*
65+
* @param $key
66+
*
67+
* @return bool|mixed|null|string
68+
*/
69+
public function getMessageData( $key ) {
70+
if ( $this->{$key} ) {
71+
return $this->{$key};
72+
}
73+
74+
$data_key = str_replace( 'body', 'path', $key );
75+
76+
$data = $this->retrieveMessageData( $data_key );
77+
78+
if ( ! $data ) {
79+
return '';
80+
}
81+
82+
$this->{$key} = $data;
83+
84+
return $data;
85+
}
86+
87+
/**
88+
* Retrieve the body data from the MailTrap API
89+
*
90+
* @param $key
91+
*
92+
* @return bool|string
93+
*/
94+
protected function retrieveMessageData( $key ) {
95+
$data = $this->client->get( $this->data[ $key ] )->getBody();
96+
97+
if ( $data instanceof Stream ) {
98+
return $data->getContents();
99+
}
100+
101+
return false;
102+
}
103+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /