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

Commit b19364f

Browse files
committed
Merge branch 'develop' into feature/renameSeeEmailsSent
2 parents 90baa6c + 7d41dc7 commit b19364f

File tree

4 files changed

+103
-24
lines changed

4 files changed

+103
-24
lines changed

VERSION

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

readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ You mixin the assertions with the ```Spinen\MailAssertions\MailTracking``` trait
3636
* seeEmailEquals
3737
* seeEmailFrom
3838
* seeEmailReplyTo
39-
* seeEmailSubject
39+
* seeEmailSubjectContains
40+
* seeEmailSubjectDoesNotContain
41+
* seeEmailSubjectEquals
4042
* seeEmailTo
4143
* seeEmailWasNotSent
4244
* seeEmailWasSent

src/MailTracking.php

Lines changed: 73 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
*
1212
* Trait to mixin to your test to allow for custom assertions when using PHPUnit with Laravel.
1313
*
14-
* This originally started out as a copy & paste from a video series that Jeffery Way did on laracasts.com. If you do
15-
* not have an account on Laracasts, you should get one. It is an amazing resource to learn from. We used that
16-
* example & converted it to a package so that it would be easy to install. We have also expanded on initial
14+
* This originally started out as a copy & paste from a video series that Jeffrey Way did on laracasts.com. If you do
15+
* not have an account on Laracasts, you should get one. It is an amazing resource to learn from. We used that
16+
* example & converted it to a package so that it would be easy to install. We have also expanded on the initial
1717
* assertions.
1818
*
19-
* I WANT IT CLEAR THAT THIS WOULD NOT HAVE HAPPENED WITHOUT THE INITIAL WORK OF JEFFERY WAY. WE ARE NOT CLAIMING TO
19+
* I WANT IT CLEAR THAT THIS WOULD NOT HAVE HAPPENED WITHOUT THE INITIAL WORK OF JEFFREY WAY. WE ARE NOT CLAIMING TO
2020
* BE THE CREATORS OF THE CONCEPT.
2121
*
2222
* @package Spinen\MailAssertions
@@ -41,8 +41,8 @@ trait MailTracking
4141
/**
4242
* Register a listener for new emails.
4343
*
44-
* Called my PHPUnit before each test it run. It registers the MailRecorder "plugin" with Swift, so that we can
45-
* get a copy of each email that is sent during that test.
44+
* This calls my PHPUnit before each test it runs. It registers the MailRecorder "plugin" with Swift, so that we
45+
* can get a copy of each email that is sent during that test.
4646
*
4747
* @before
4848
*/
@@ -53,7 +53,7 @@ public function setUpMailTracking()
5353
}
5454

5555
/**
56-
* Retrieve the appropriate swift message.
56+
* Retrieve the appropriate Swift message.
5757
*
5858
* @param Swift_Message|null $message
5959
*
@@ -67,15 +67,15 @@ protected function getEmail(Swift_Message $message = null)
6767
}
6868

6969
/**
70-
* Retrieve the mostly recently sent swift message.
70+
* Retrieve the mostly recently sent Swift message.
7171
*/
7272
protected function lastEmail()
7373
{
7474
return end($this->emails);
7575
}
7676

7777
/**
78-
* Store a new swift message.
78+
* Store a new Swift message.
7979
*
8080
* Collection of emails that were received by the MailRecorder plugin during a test.
8181
*
@@ -97,7 +97,7 @@ public function recordMail(Swift_Message $email)
9797
protected function seeEmailBcc($bcc, Swift_Message $message = null)
9898
{
9999
$this->assertArrayHasKey($bcc, (array)$this->getEmail($message)
100-
->getBcc(), "No email was bcc'ed to $bcc.");
100+
->getBcc(), "The last email sent was not bcc'ed to $bcc.");
101101

102102
return $this;
103103
}
@@ -113,7 +113,7 @@ protected function seeEmailBcc($bcc, Swift_Message $message = null)
113113
protected function seeEmailCc($cc, Swift_Message $message = null)
114114
{
115115
$this->assertArrayHasKey($cc, (array)$this->getEmail($message)
116-
->getCc(), "No email was cc'ed to $cc.");
116+
->getCc(), "The last email sent was not cc'ed to $cc.");
117117

118118
return $this;
119119
}
@@ -129,7 +129,7 @@ protected function seeEmailCc($cc, Swift_Message $message = null)
129129
protected function seeEmailContains($excerpt, Swift_Message $message = null)
130130
{
131131
$this->assertContains($excerpt, $this->getEmail($message)
132-
->getBody(), "No email containing the provided body was found.");
132+
->getBody(), "The last email sent did not contain the provided body.");
133133

134134
return $this;
135135
}
@@ -145,7 +145,8 @@ protected function seeEmailContains($excerpt, Swift_Message $message = null)
145145
protected function seeEmailDoesNotContain($excerpt, Swift_Message $message = null)
146146
{
147147
$this->assertNotContains($excerpt, $this->getEmail($message)
148-
->getBody(), "Email containing the provided text was found in the body.");
148+
->getBody(),
149+
"The last email sent contained the provided text in its body.");
149150

150151
return $this;
151152
}
@@ -161,7 +162,7 @@ protected function seeEmailDoesNotContain($excerpt, Swift_Message $message = nul
161162
protected function seeEmailEquals($body, Swift_Message $message = null)
162163
{
163164
$this->assertEquals($body, $this->getEmail($message)
164-
->getBody(), "No email with the provided body was sent.");
165+
->getBody(), "The last email sent did not match the given email.");
165166

166167
return $this;
167168
}
@@ -178,7 +179,7 @@ protected function seeEmailFrom($sender, Swift_Message $message = null)
178179
{
179180
// TODO: Allow from to be an array to check email & name
180181
$this->assertArrayHasKey($sender, (array)$this->getEmail($message)
181-
->getFrom(), "No email was sent from $sender.");
182+
->getFrom(), "The last email sent was not sent from $sender.");
182183

183184
return $this;
184185
}
@@ -194,7 +195,8 @@ protected function seeEmailFrom($sender, Swift_Message $message = null)
194195
protected function seeEmailReplyTo($reply_to, Swift_Message $message = null)
195196
{
196197
$this->assertArrayHasKey($reply_to, (array)$this->getEmail($message)
197-
->getReplyTo(), "No email was set to reply to $reply_to.");
198+
->getReplyTo(),
199+
"The last email sent was not set to reply to $reply_to.");
198200

199201
return $this;
200202
}
@@ -222,12 +224,60 @@ protected function seeEmailCountEquals($count)
222224
* @param Swift_Message|null $message
223225
*
224226
* @return PHPUnit_Framework_TestCase $this
227+
* @deprecated in favor of seeEmailSubjectEquals
225228
*/
226229
protected function seeEmailSubject($subject, Swift_Message $message = null)
227230
{
228-
// TODO: Consider a subject contains like the message contains
231+
return $this->seeEmailSubjectEquals($subject, $message);
232+
}
233+
234+
/**
235+
* Assert that the last email's subject contains the given string.
236+
*
237+
* @param string $excerpt
238+
* @param Swift_Message|null $message
239+
*
240+
* @return PHPUnit_Framework_TestCase $this
241+
*/
242+
protected function seeEmailSubjectContains($excerpt, Swift_Message $message = null)
243+
{
244+
$this->assertContains($excerpt, $this->getEmail($message)
245+
->getSubject(),
246+
"The last email sent did not contain the provided subject.");
247+
248+
return $this;
249+
}
250+
251+
/**
252+
* Assert that the last email's subject does not contain the given string.
253+
*
254+
* @param string $excerpt
255+
* @param Swift_Message|null $message
256+
*
257+
* @return PHPUnit_Framework_TestCase $this
258+
*/
259+
protected function seeEmailSubjectDoesNotContain($excerpt, Swift_Message $message = null)
260+
{
261+
$this->assertNotContains($excerpt, $this->getEmail($message)
262+
->getSubject(),
263+
"The last email sent contained the provided text in its subject.");
264+
265+
return $this;
266+
}
267+
268+
/**
269+
* Assert that the last email's subject matches the given string.
270+
*
271+
* @param string $subject
272+
* @param Swift_Message|null $message
273+
*
274+
* @return PHPUnit_Framework_TestCase $this
275+
*/
276+
protected function seeEmailSubjectEquals($subject, Swift_Message $message = null)
277+
{
229278
$this->assertEquals($subject, $this->getEmail($message)
230-
->getSubject(), "No email with a subject of $subject was found.");
279+
->getSubject(),
280+
"The last email sent did not contain a subject of $subject.");
231281

232282
return $this;
233283
}
@@ -243,7 +293,7 @@ protected function seeEmailSubject($subject, Swift_Message $message = null)
243293
protected function seeEmailTo($recipient, Swift_Message $message = null)
244294
{
245295
$this->assertArrayHasKey($recipient, (array)$this->getEmail($message)
246-
->getTo(), "No email was sent to $recipient.");
296+
->getTo(), "The last email sent was not sent to $recipient.");
247297

248298
return $this;
249299
}
@@ -255,7 +305,9 @@ protected function seeEmailTo($recipient, Swift_Message $message = null)
255305
*/
256306
protected function seeEmailWasNotSent()
257307
{
258-
$this->assertEmpty($this->emails, 'Did not expect any emails to have been sent.');
308+
$emailsSent = count($this->emails);
309+
310+
$this->assertEmpty($this->emails, "Did not expect any emails to have been sent, but found $emailsSent");
259311

260312
return $this;
261313
}
@@ -267,7 +319,7 @@ protected function seeEmailWasNotSent()
267319
*/
268320
protected function seeEmailWasSent()
269321
{
270-
$this->assertNotEmpty($this->emails, 'No emails have been sent.');
322+
$this->assertNotEmpty($this->emails, 'Expected at least one email to be sent, but found none.');
271323

272324
return $this;
273325
}

tests/MailTrackingTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,31 @@ public function it_knows_how_many_emails_have_been_sent()
238238
$this->assertEquals($this->mail_tracking, $this->callProtectedMethod('seeEmailCountEquals', [2]));
239239
}
240240

241+
/**
242+
* @test
243+
* @group unit
244+
*/
245+
public function it_makes_sure_email_subject_contains_expected_string()
246+
{
247+
$message = $this->makeMessage('full message subject');
248+
$this->mail_tracking->recordMail($message);
249+
250+
$this->assertEquals($this->mail_tracking,
251+
$this->callProtectedMethod('seeEmailSubjectContains', ['subject']));
252+
}
253+
254+
/**
255+
* @test
256+
* @group unit
257+
*/
258+
public function it_makes_sure_email_subject_does_not_contain_string()
259+
{
260+
$message = $this->makeMessage('');
261+
$this->mail_tracking->recordMail($message);
262+
263+
$this->assertEquals($this->mail_tracking, $this->callProtectedMethod('seeEmailSubjectDoesNotContain', ['subject']));
264+
}
265+
241266
/**
242267
* @test
243268
* @group unit
@@ -248,7 +273,7 @@ public function it_makes_sure_email_subject_is_what_is_expected()
248273
$this->mail_tracking->recordMail($message);
249274

250275
$this->assertEquals($this->mail_tracking,
251-
$this->callProtectedMethod('seeEmailSubject', ['full message subject']));
276+
$this->callProtectedMethod('seeEmailSubjectEquals', ['full message subject']));
252277
}
253278

254279
/**

0 commit comments

Comments
 (0)