2
2
3
3
namespace Codeception \Module ;
4
4
5
+ use Codeception \Module ;
5
6
use GuzzleHttp \Client ;
6
7
use GuzzleHttp \Psr7 \Stream ;
7
8
use PHPUnit \Framework \Assert ;
@@ -101,7 +102,7 @@ public function receiveAnEmail($params)
101
102
$ message = $ this ->fetchLastMessage ();
102
103
103
104
foreach ($ params as $ param => $ value ) {
104
- $ this ->assertEquals ($ value , $ message[ $ param] );
105
+ $ this ->assertEquals ($ value , $ message->{ $ param} );
105
106
}
106
107
}
107
108
@@ -120,14 +121,38 @@ public function fetchMessages()
120
121
121
122
$ messages = json_decode ($ messages , true );
122
123
124
+ foreach ( $ messages as $ key => $ message ) {
125
+ $ messages [ $ key ] = new MailtrapMessage ( $ message , $ this ->client );
126
+ }
127
+
123
128
return $ messages ;
124
129
}
125
130
126
131
/**
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
128
135
*
129
136
* @return array
130
137
*/
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
+ */
131
156
public function fetchLastMessage ()
132
157
{
133
158
$ messages = $ this ->fetchMessages ();
@@ -143,7 +168,7 @@ public function fetchLastMessage()
143
168
public function fetchAttachmentsOfLastMessage ()
144
169
{
145
170
$ 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 ();
147
172
148
173
return json_decode ($ response , true );
149
174
}
@@ -158,7 +183,7 @@ public function fetchAttachmentsOfLastMessage()
158
183
public function receiveAnEmailFromEmail ($ senderEmail )
159
184
{
160
185
$ message = $ this ->fetchLastMessage ();
161
- $ this ->assertEquals ($ senderEmail , $ message[ ' from_email ' ] );
186
+ $ this ->assertEquals ($ senderEmail , $ message-> from_email );
162
187
}
163
188
164
189
/**
@@ -171,7 +196,7 @@ public function receiveAnEmailFromEmail($senderEmail)
171
196
public function receiveAnEmailFromName ($ senderName )
172
197
{
173
198
$ message = $ this ->fetchLastMessage ();
174
- $ this ->assertEquals ($ senderName , $ message[ ' from_name ' ] );
199
+ $ this ->assertEquals ($ senderName , $ message-> from_name );
175
200
}
176
201
177
202
/**
@@ -184,7 +209,7 @@ public function receiveAnEmailFromName($senderName)
184
209
public function receiveAnEmailToEmail ($ recipientEmail )
185
210
{
186
211
$ message = $ this ->fetchLastMessage ();
187
- $ this ->assertEquals ($ recipientEmail , $ message[ ' to_email ' ] );
212
+ $ this ->assertEquals ($ recipientEmail , $ message-> to_email );
188
213
}
189
214
190
215
/**
@@ -197,7 +222,7 @@ public function receiveAnEmailToEmail($recipientEmail)
197
222
public function receiveAnEmailToName ($ recipientName )
198
223
{
199
224
$ message = $ this ->fetchLastMessage ();
200
- $ this ->assertEquals ($ recipientName , $ message[ ' to_name ' ] );
225
+ $ this ->assertEquals ($ recipientName , $ message-> to_name );
201
226
}
202
227
203
228
/**
@@ -210,7 +235,7 @@ public function receiveAnEmailToName($recipientName)
210
235
public function receiveAnEmailWithSubject ($ subject )
211
236
{
212
237
$ message = $ this ->fetchLastMessage ();
213
- $ this ->assertEquals ($ subject , $ message[ ' subject ' ] );
238
+ $ this ->assertEquals ($ subject , $ message-> subject );
214
239
}
215
240
216
241
/**
@@ -223,7 +248,7 @@ public function receiveAnEmailWithSubject($subject)
223
248
public function receiveAnEmailWithTextBody ($ textBody )
224
249
{
225
250
$ message = $ this ->fetchLastMessage ();
226
- $ this ->assertEquals ($ textBody , $ message[ ' text_body ' ] );
251
+ $ this ->assertEquals ($ textBody , $ message-> text_body );
227
252
}
228
253
229
254
/**
@@ -236,7 +261,7 @@ public function receiveAnEmailWithTextBody($textBody)
236
261
public function receiveAnEmailWithHtmlBody ($ htmlBody )
237
262
{
238
263
$ message = $ this ->fetchLastMessage ();
239
- $ this ->assertEquals ($ htmlBody , $ message[ ' html_body ' ] );
264
+ $ this ->assertEquals ($ htmlBody , $ message-> html_body );
240
265
}
241
266
242
267
/**
@@ -249,7 +274,7 @@ public function receiveAnEmailWithHtmlBody($htmlBody)
249
274
public function seeInEmailTextBody ($ expected )
250
275
{
251
276
$ 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 ' );
253
278
}
254
279
255
280
/**
@@ -262,7 +287,7 @@ public function seeInEmailTextBody($expected)
262
287
public function seeInEmailHtmlBody ($ expected )
263
288
{
264
289
$ 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 ' );
266
291
}
267
292
268
293
/**
@@ -275,7 +300,7 @@ public function seeInEmailHtmlBody($expected)
275
300
public function seeInEmailSubject ($ expected )
276
301
{
277
302
$ email = $ this ->fetchLastMessage ();
278
- $ this ->assertContains ($ expected , $ email[ ' subject ' ] , 'Email subject contains text ' );
303
+ $ this ->assertContains ($ expected , $ email-> subject , 'Email subject contains text ' );
279
304
}
280
305
281
306
/**
@@ -300,32 +325,6 @@ public function seeAnAttachment($bool)
300
325
$ this ->assertEquals ($ bool , count ($ attachments ) > 0 );
301
326
}
302
327
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
-
329
328
/**
330
329
* Get the bcc property of a message
331
330
*
@@ -392,7 +391,7 @@ public function waitForEmailWithSubject($subject, $timeout = 5)
392
391
$ emails = $ this ->fetchMessages ();
393
392
foreach ($ emails as $ email ) {
394
393
$ constraint = Assert::equalTo ($ subject );
395
- if ($ constraint ->evaluate ($ email[ ' subject ' ] , '' , true )) {
394
+ if ($ constraint ->evaluate ($ email-> subject , '' , true )) {
396
395
return true ;
397
396
}
398
397
}
@@ -419,7 +418,7 @@ public function waitForEmailWithTextInTextBody($text, $timeout = 5)
419
418
$ emails = $ this ->fetchMessages ();
420
419
foreach ($ emails as $ email ) {
421
420
$ constraint = Assert::stringContains ($ text );
422
- if ($ constraint ->evaluate ($ email[ ' text_body ' ] , '' , true )) {
421
+ if ($ constraint ->evaluate ($ email-> text_body , '' , true )) {
423
422
return true ;
424
423
}
425
424
}
@@ -446,7 +445,7 @@ public function waitForEmailWithTextInHTMLBody($text, $timeout = 5)
446
445
$ emails = $ this ->fetchMessages ();
447
446
foreach ($ emails as $ email ) {
448
447
$ constraint = Assert::stringContains ($ text );
449
- if ($ constraint ->evaluate ($ email[ ' html_body ' ] , '' , true )) {
448
+ if ($ constraint ->evaluate ($ email-> html_body , '' , true )) {
450
449
return true ;
451
450
}
452
451
}
0 commit comments