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

Commit 1aa097b

Browse files
committed
Merge branch 'release/0.0.2'
2 parents 4b9c43e + 3b1654d commit 1aa097b

File tree

4 files changed

+135
-19
lines changed

4 files changed

+135
-19
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.1
1+
0.0.2

readme.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# SPINEN's Laravel Mail Assertions
22

3-
NOTE: This is based off a video titled ["Testing Email With Custom Assertions"](https://laracasts.com/series/phpunit-testing-in-laravel/episodes/12) that Jeffery Way did on [Laracasts.com](https://laracasts.com). If you do not have an account on that site, then you should. It is an amazing resource. We have just take that example & made it in an easy to install package. Thanks Jeffery!
3+
NOTE: This is based off a video titled ["Testing Email With Custom Assertions"](https://laracasts.com/series/phpunit-testing-in-laravel/episodes/12) that [Jeffrey Way](https://github.com/JeffreyWay) did on [Laracasts.com](https://laracasts.com). If you do not have an account on that site, then you should make one. It is an amazing resource. We have just taken that example & made it an easy to install package. Thanks Jeffrey!
44

55
[![Latest Stable Version](https://poser.pugx.org/spinen/laravel-mail-assertions/v/stable)](https://packagist.org/packages/spinen/laravel-mail-assertions)
66
[![Total Downloads](https://poser.pugx.org/spinen/laravel-mail-assertions/downloads)](https://packagist.org/packages/spinen/laravel-mail-assertions)
@@ -19,7 +19,7 @@ PHPUnit mail assertions for testing email in Laravel.
1919

2020
## Install
2121

22-
Install Garbage Man:
22+
Install the package:
2323

2424
```bash
2525
$ composer require spinen/laravel-mail-assertions
@@ -29,9 +29,13 @@ Install Garbage Man:
2929

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

32+
* seeEmailBcc
33+
* seeEmailCc
3234
* seeEmailContains
35+
* seeEmailDoesNotContain
3336
* seeEmailEquals
3437
* seeEmailFrom
38+
* seeEmailReplyTo
3539
* seeEmailSubject
3640
* seeEmailTo
3741
* seeEmailWasNotSent

src/MailTracking.php

Lines changed: 75 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
*/
@@ -105,11 +134,27 @@ protected function seeEmailContains($excerpt, Swift_Message $message = null)
105134
return $this;
106135
}
107136

137+
/**
138+
* Assert that the last email's body does not contain the given text.
139+
*
140+
* @param string $excerpt
141+
* @param Swift_Message|null $message
142+
*
143+
* @return PHPUnit_Framework_TestCase $this
144+
*/
145+
protected function seeEmailDoesNotContain($excerpt, Swift_Message $message = null)
146+
{
147+
$this->assertNotContains($excerpt, $this->getEmail($message)
148+
->getBody(), "Email containing the provided text was found in the body.");
149+
150+
return $this;
151+
}
152+
108153
/**
109154
* Assert that the last email's body equals the given text.
110155
*
111-
* @param string $body
112-
* @param Swift_Message $message
156+
* @param string $body
157+
* @param Swift_Message|null $message
113158
*
114159
* @return PHPUnit_Framework_TestCase $this
115160
*/
@@ -124,8 +169,8 @@ protected function seeEmailEquals($body, Swift_Message $message = null)
124169
/**
125170
* Assert that the last email was delivered by the given address.
126171
*
127-
* @param string $sender
128-
* @param Swift_Message $message
172+
* @param string $sender
173+
* @param Swift_Message|null $message
129174
*
130175
* @return PHPUnit_Framework_TestCase $this
131176
*/
@@ -138,6 +183,22 @@ protected function seeEmailFrom($sender, Swift_Message $message = null)
138183
return $this;
139184
}
140185

186+
/**
187+
* Assert that the last email was set to reply to the given address.
188+
*
189+
* @param string $reply_to
190+
* @param Swift_Message|null $message
191+
*
192+
* @return PHPUnit_Framework_TestCase $this
193+
*/
194+
protected function seeEmailReplyTo($reply_to, Swift_Message $message = null)
195+
{
196+
$this->assertArrayHasKey($reply_to, (array)$this->getEmail($message)
197+
->getReplyTo(), "No email was set to reply to $reply_to.");
198+
199+
return $this;
200+
}
201+
141202
/**
142203
* Assert that the given number of emails were sent.
143204
*
@@ -157,8 +218,8 @@ protected function seeEmailsSent($count)
157218
/**
158219
* Assert that the last email's subject matches the given string.
159220
*
160-
* @param string $subject
161-
* @param Swift_Message $message
221+
* @param string $subject
222+
* @param Swift_Message|null $message
162223
*
163224
* @return PHPUnit_Framework_TestCase $this
164225
*/
@@ -174,8 +235,8 @@ protected function seeEmailSubject($subject, Swift_Message $message = null)
174235
/**
175236
* Assert that the last email was sent to the given recipient.
176237
*
177-
* @param string $recipient
178-
* @param Swift_Message $message
238+
* @param string $recipient
239+
* @param Swift_Message|null $message
179240
*
180241
* @return PHPUnit_Framework_TestCase $this
181242
*/

tests/MailTrackingTest.php

Lines changed: 53 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
@@ -142,6 +168,18 @@ public function it_checks_email_body_for_content()
142168
$this->assertEquals($this->mail_tracking, $this->callProtectedMethod('seeEmailContains', ['body']));
143169
}
144170

171+
/**
172+
* @test
173+
* @group unit
174+
*/
175+
public function it_checks_email_body_does_not_have_content()
176+
{
177+
$message = $this->makeMessage('subject', '');
178+
$this->mail_tracking->recordMail($message);
179+
180+
$this->assertEquals($this->mail_tracking, $this->callProtectedMethod('seeEmailDoesNotContain', ['body']));
181+
}
182+
145183
/**
146184
* @test
147185
* @group unit
@@ -156,7 +194,7 @@ public function it_makes_sure_email_body_is_what_is_expected()
156194

157195
/**
158196
* @test
159-
* @group
197+
* @group unit
160198
*/
161199
public function it_checks_email_from_address()
162200
{
@@ -168,7 +206,20 @@ public function it_checks_email_from_address()
168206

169207
/**
170208
* @test
171-
* @group
209+
* @group unit
210+
*/
211+
public function it_checks_email_reply_to_address()
212+
{
213+
$message = $this->makeMessage();
214+
$message->setReplyTo('replyto@domain.tld');
215+
$this->mail_tracking->recordMail($message);
216+
217+
$this->assertEquals($this->mail_tracking, $this->callProtectedMethod('seeEmailReplyTo', ['replyto@domain.tld']));
218+
}
219+
220+
/**
221+
* @test
222+
* @group unit
172223
*/
173224
public function it_knows_how_many_emails_have_been_sent()
174225
{

0 commit comments

Comments
 (0)