This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
/
InteractsWithMail.php
150 lines (121 loc) · 3.31 KB
/
InteractsWithMail.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace MailThief\Testing;
use MailThief\Facades\MailThief;
use MailThief\Message;
use MailThief\Support\MailThiefCollection;
use Illuminate\Contracts\Mail\Mailer;
trait InteractsWithMail
{
private $mailer;
private function setMailer(Mailer $mailer)
{
$this->mailer = $mailer;
}
private function getMailer()
{
return $this->mailer ?: MailThief::getFacadeRoot();
}
/**
* @return MailThiefCollection
*/
public function getMessages()
{
return $this->getMailer()->messages;
}
/**
* @param array|string $emails
* @return MailThiefCollection
*/
public function getMessagesFor($emails)
{
$emails = collect((array) $emails);
return $this->getMessages()->filter(function (Message $message) use ($emails) {
return $emails->contains(function ($email) use ($message) {
return $message->hasRecipient($email);
});
});
}
/**
* @param array|string $email
* @return Message
*/
public function getLastMessageFor($email)
{
return $this->getMessagesFor($email)->last();
}
/** @before */
public function hijackMail()
{
$this->getMailer()->hijack();
}
public function seeMessageFor($email)
{
$this->seeMessage();
$this->assertTrue(
$this->getMailer()->hasMessageFor($email),
sprintf('Unable to find an email addressed to [%s].', $email)
);
return $this;
}
public function seeMessageWithSubject($subject)
{
$this->seeMessage();
$lastSubject = $this->lastMessage()->subject;
$this->assertEquals(
$subject,
$lastSubject,
sprintf(
'Expected subject to be "[%s]", but found "[%s]".',
$subject,
$lastSubject
)
);
return $this;
}
public function seeMessageFrom($email)
{
$this->seeMessage();
$from = $this->lastMessage()->from->map(function ($value, $key) {
return is_numeric($key) ? $value : $key;
})->first();
$this->assertEquals(
$email,
$from,
sprintf(
'Expected to find message from "[%s]", but found "[%s]".',
$email,
$from
)
);
return $this;
}
public function lastMessage()
{
return $this->getMailer()->lastMessage();
}
public function seeHeaders($name, $value = null)
{
$this->assertTrue($this->lastMessage()->headers->contains(function ($header) use ($name, $value) {
if (is_null($value)) {
return $header['name'] === $name;
}
return $header['name'] === $name && $header['value'] === $value;
}));
return $this;
}
protected function seeInSubjects($subjects)
{
$subjects = (array) $subjects;
foreach ($subjects as $subject) {
$this->assertTrue(in_array($subject, $this->getMailer()->subjects()->all()));
}
}
protected function seeMessage()
{
$this->assertNotNull(
$this->lastMessage(),
'Unable to find a generated email.'
);
return $this;
}
}