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 }
@@ -225,9 +227,43 @@ protected function seeEmailsSent($count)
225227 */
226228 protected function seeEmailSubject ($ subject , Swift_Message $ message = null )
227229 {
228- // TODO: Consider a subject contains like the message contains
229230 $ this ->assertEquals ($ subject , $ this ->getEmail ($ message )
230- ->getSubject (), "No email with a subject of $ subject was found. " );
231+ ->getSubject (),
232+ "The last email sent did not contain a subject of $ subject. " );
233+
234+ return $ this ;
235+ }
236+
237+ /**
238+ * Assert that the last email's subject contains the given string.
239+ *
240+ * @param string $excerpt
241+ * @param Swift_Message|null $message
242+ *
243+ * @return PHPUnit_Framework_TestCase $this
244+ */
245+ protected function seeEmailSubjectContains ($ excerpt , Swift_Message $ message = null )
246+ {
247+ $ this ->assertContains ($ excerpt , $ this ->getEmail ($ message )
248+ ->getSubject (),
249+ "The last email sent did not contain the provided subject. " );
250+
251+ return $ this ;
252+ }
253+
254+ /**
255+ * Assert that the last email's subject does not contain the given string.
256+ *
257+ * @param string $excerpt
258+ * @param Swift_Message|null $message
259+ *
260+ * @return PHPUnit_Framework_TestCase $this
261+ */
262+ protected function seeEmailSubjectDoesNotContain ($ excerpt , Swift_Message $ message = null )
263+ {
264+ $ this ->assertNotContains ($ excerpt , $ this ->getEmail ($ message )
265+ ->getSubject (),
266+ "The last email sent contained the provided text in its subject. " );
231267
232268 return $ this ;
233269 }
@@ -243,7 +279,7 @@ protected function seeEmailSubject($subject, Swift_Message $message = null)
243279 protected function seeEmailTo ($ recipient , Swift_Message $ message = null )
244280 {
245281 $ this ->assertArrayHasKey ($ recipient , (array )$ this ->getEmail ($ message )
246- ->getTo (), "No email was sent to $ recipient. " );
282+ ->getTo (), "The last email sent was not sent to $ recipient. " );
247283
248284 return $ this ;
249285 }
@@ -255,7 +291,9 @@ protected function seeEmailTo($recipient, Swift_Message $message = null)
255291 */
256292 protected function seeEmailWasNotSent ()
257293 {
258- $ this ->assertEmpty ($ this ->emails , 'Did not expect any emails to have been sent. ' );
294+ $ emailsSent = count ($ this ->emails );
295+
296+ $ this ->assertEmpty ($ this ->emails , "Did not expect any emails to have been sent, but found $ emailsSent " );
259297
260298 return $ this ;
261299 }
@@ -267,7 +305,7 @@ protected function seeEmailWasNotSent()
267305 */
268306 protected function seeEmailWasSent ()
269307 {
270- $ this ->assertNotEmpty ($ this ->emails , 'No emails have been sent. ' );
308+ $ this ->assertNotEmpty ($ this ->emails , 'Expected at least one email to be sent, but found none . ' );
271309
272310 return $ this ;
273311 }
0 commit comments