Skip to content
This repository was archived by the owner on Apr 8, 2024. It is now read-only.

Commit 6aa7b3b

Browse files
committed
Merge pull request #12 from spinen/feature/checkCcAndBccFields
Feature/check cc and bcc fields
2 parents 6fb25ab + 669203b commit 6aa7b3b

File tree

3 files changed

+103
-16
lines changed

3 files changed

+103
-16
lines changed

readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ Install the package:
2929

3030
You mixin the assertions with the ```Spinen\MailAssertions\MailTracking``` trait. You get the following assertions...
3131

32+
* seeEmailBcc
33+
* seeEmailCc
3234
* seeEmailContains
3335
* seeEmailEquals
3436
* seeEmailFrom
37+
* seeEmailReplyTo
3538
* seeEmailSubject
3639
* seeEmailTo
3740
* seeEmailWasNotSent

src/MailTracking.php

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,9 @@
2626
trait MailTracking
2727
{
2828
// TODO: Add check for attachments (number of & name)
29-
// TODO: Add check for BCC
30-
// TODO: Add check for CC
3129
// TODO: Add check for header
3230
// TODO: Add check for message type
3331
// TODO: Add check for Priority
34-
// TODO: Add check for ReplyTo
3532
// TODO: Allow checking specific message not just most recent one
3633

3734
/**
@@ -58,7 +55,7 @@ public function setUpMailTracking()
5855
/**
5956
* Retrieve the appropriate swift message.
6057
*
61-
* @param Swift_Message $message
58+
* @param Swift_Message|null $message
6259
*
6360
* @return Swift_Message
6461
*/
@@ -89,11 +86,43 @@ public function recordMail(Swift_Message $email)
8986
$this->emails[] = $email;
9087
}
9188

89+
/**
90+
* Assert that the last email was bcc'ed to the given address.
91+
*
92+
* @param string $bcc
93+
* @param Swift_Message|null $message
94+
*
95+
* @return PHPUnit_Framework_TestCase $this
96+
*/
97+
protected function seeEmailBcc($bcc, Swift_Message $message = null)
98+
{
99+
$this->assertArrayHasKey($bcc, (array)$this->getEmail($message)
100+
->getBcc(), "No email was bcc'ed to $bcc.");
101+
102+
return $this;
103+
}
104+
105+
/**
106+
* Assert that the last email was cc'ed to the given address.
107+
*
108+
* @param string $cc
109+
* @param Swift_Message|null $message
110+
*
111+
* @return PHPUnit_Framework_TestCase $this
112+
*/
113+
protected function seeEmailCc($cc, Swift_Message $message = null)
114+
{
115+
$this->assertArrayHasKey($cc, (array)$this->getEmail($message)
116+
->getCc(), "No email was cc'ed to $cc.");
117+
118+
return $this;
119+
}
120+
92121
/**
93122
* Assert that the last email's body contains the given text.
94123
*
95-
* @param string $excerpt
96-
* @param Swift_Message $message
124+
* @param string $excerpt
125+
* @param Swift_Message|null $message
97126
*
98127
* @return PHPUnit_Framework_TestCase $this
99128
*/
@@ -108,8 +137,8 @@ protected function seeEmailContains($excerpt, Swift_Message $message = null)
108137
/**
109138
* Assert that the last email's body equals the given text.
110139
*
111-
* @param string $body
112-
* @param Swift_Message $message
140+
* @param string $body
141+
* @param Swift_Message|null $message
113142
*
114143
* @return PHPUnit_Framework_TestCase $this
115144
*/
@@ -124,8 +153,8 @@ protected function seeEmailEquals($body, Swift_Message $message = null)
124153
/**
125154
* Assert that the last email was delivered by the given address.
126155
*
127-
* @param string $sender
128-
* @param Swift_Message $message
156+
* @param string $sender
157+
* @param Swift_Message|null $message
129158
*
130159
* @return PHPUnit_Framework_TestCase $this
131160
*/
@@ -138,6 +167,22 @@ protected function seeEmailFrom($sender, Swift_Message $message = null)
138167
return $this;
139168
}
140169

170+
/**
171+
* Assert that the last email was set to reply to the given address.
172+
*
173+
* @param string $reply_to
174+
* @param Swift_Message|null $message
175+
*
176+
* @return PHPUnit_Framework_TestCase $this
177+
*/
178+
protected function seeEmailReplyTo($reply_to, Swift_Message $message = null)
179+
{
180+
$this->assertArrayHasKey($reply_to, (array)$this->getEmail($message)
181+
->getReplyTo(), "No email was set to reply to $reply_to.");
182+
183+
return $this;
184+
}
185+
141186
/**
142187
* Assert that the given number of emails were sent.
143188
*
@@ -157,8 +202,8 @@ protected function seeEmailsSent($count)
157202
/**
158203
* Assert that the last email's subject matches the given string.
159204
*
160-
* @param string $subject
161-
* @param Swift_Message $message
205+
* @param string $subject
206+
* @param Swift_Message|null $message
162207
*
163208
* @return PHPUnit_Framework_TestCase $this
164209
*/
@@ -174,8 +219,8 @@ protected function seeEmailSubject($subject, Swift_Message $message = null)
174219
/**
175220
* Assert that the last email was sent to the given recipient.
176221
*
177-
* @param string $recipient
178-
* @param Swift_Message $message
222+
* @param string $recipient
223+
* @param Swift_Message|null $message
179224
*
180225
* @return PHPUnit_Framework_TestCase $this
181226
*/

tests/MailTrackingTest.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,32 @@ public function it_records_emails_in_collection()
130130
$this->assertCount(1, $this->mail_tracking->exposeMessage());
131131
}
132132

133+
/**
134+
* @test
135+
* @group unit
136+
*/
137+
public function it_checks_email_bcc_address()
138+
{
139+
$message = $this->makeMessage();
140+
$message->setBcc('bcc@domain.tld');
141+
$this->mail_tracking->recordMail($message);
142+
143+
$this->assertEquals($this->mail_tracking, $this->callProtectedMethod('seeEmailBcc', ['bcc@domain.tld']));
144+
}
145+
146+
/**
147+
* @test
148+
* @group unit
149+
*/
150+
public function it_checks_email_cc_address()
151+
{
152+
$message = $this->makeMessage();
153+
$message->setCc('cc@domain.tld');
154+
$this->mail_tracking->recordMail($message);
155+
156+
$this->assertEquals($this->mail_tracking, $this->callProtectedMethod('seeEmailCc', ['cc@domain.tld']));
157+
}
158+
133159
/**
134160
* @test
135161
* @group unit
@@ -156,7 +182,7 @@ public function it_makes_sure_email_body_is_what_is_expected()
156182

157183
/**
158184
* @test
159-
* @group
185+
* @group unit
160186
*/
161187
public function it_checks_email_from_address()
162188
{
@@ -168,7 +194,20 @@ public function it_checks_email_from_address()
168194

169195
/**
170196
* @test
171-
* @group
197+
* @group unit
198+
*/
199+
public function it_checks_email_reply_to_address()
200+
{
201+
$message = $this->makeMessage();
202+
$message->setReplyTo('replyto@domain.tld');
203+
$this->mail_tracking->recordMail($message);
204+
205+
$this->assertEquals($this->mail_tracking, $this->callProtectedMethod('seeEmailReplyTo', ['replyto@domain.tld']));
206+
}
207+
208+
/**
209+
* @test
210+
* @group unit
172211
*/
173212
public function it_knows_how_many_emails_have_been_sent()
174213
{

0 commit comments

Comments
 (0)