33namespace Spinen \MailAssertions ;
44
55use Illuminate \Support \Facades \Mail ;
6- use PHPUnit_Framework_TestCase ;
76use Swift_Message ;
87
98/**
109 * Class MailTracking
1110 *
12- * Trait to mixin to your test to allow for custom assertions when using PHPUnit with Laravel.
11+ * Trait to mixin to your test to allow for custom assertions when using PHPUnit with Laravel. This trait assumes
12+ * you are extending from the PHPUnit TestCase class (or a child of it).
1313 *
1414 * This originally started out as a copy & paste from a video series that Jeffrey Way did on laracasts.com. If you do
1515 * not have an account on Laracasts, you should get one. It is an amazing resource to learn from. We used that
@@ -48,9 +48,21 @@ trait MailTracking
4848 */
4949 public function setUpMailTracking ()
5050 {
51- $ this -> afterApplicationCreated ( function () {
51+ $ register_plugin = function () {
5252 Mail::getSwiftMailer ()
5353 ->registerPlugin (new MailRecorder ($ this ));
54+ };
55+
56+ // To support Phpunit 5 and Laravel < 5.2, register the plugin normally
57+ if (!method_exists ($ this , 'afterApplicationCreated ' )) {
58+ $ register_plugin ();
59+
60+ return ;
61+ }
62+
63+ // For PhpUnit 6 and Laravel > 5.1, register the plugin after the app is booted
64+ $ this ->afterApplicationCreated (function () use ($ register_plugin ) {
65+ $ register_plugin ();
5466 });
5567 }
5668
@@ -94,7 +106,7 @@ public function recordMail(Swift_Message $email)
94106 * @param string $bcc
95107 * @param Swift_Message|null $message
96108 *
97- * @return PHPUnit_Framework_TestCase $this
109+ * @return $this
98110 */
99111 protected function seeEmailBcc ($ bcc , Swift_Message $ message = null )
100112 {
@@ -110,7 +122,7 @@ protected function seeEmailBcc($bcc, Swift_Message $message = null)
110122 * @param string $cc
111123 * @param Swift_Message|null $message
112124 *
113- * @return PHPUnit_Framework_TestCase $this
125+ * @return $this
114126 */
115127 protected function seeEmailCc ($ cc , Swift_Message $ message = null )
116128 {
@@ -126,7 +138,7 @@ protected function seeEmailCc($cc, Swift_Message $message = null)
126138 * @param string $excerpt
127139 * @param Swift_Message|null $message
128140 *
129- * @return PHPUnit_Framework_TestCase $this
141+ * @return $this
130142 */
131143 protected function seeEmailContains ($ excerpt , Swift_Message $ message = null )
132144 {
@@ -143,7 +155,7 @@ protected function seeEmailContains($excerpt, Swift_Message $message = null)
143155 * @param string $content_type
144156 * @param Swift_Message|null $message
145157 *
146- * @return PHPUnit_Framework_TestCase $this
158+ * @return $this
147159 */
148160 protected function seeEmailContentTypeEquals ($ content_type , Swift_Message $ message = null )
149161 {
@@ -160,7 +172,7 @@ protected function seeEmailContentTypeEquals($content_type, Swift_Message $messa
160172 * @param string $excerpt
161173 * @param Swift_Message|null $message
162174 *
163- * @return PHPUnit_Framework_TestCase $this
175+ * @return $this
164176 */
165177 protected function seeEmailDoesNotContain ($ excerpt , Swift_Message $ message = null )
166178 {
@@ -177,7 +189,7 @@ protected function seeEmailDoesNotContain($excerpt, Swift_Message $message = nul
177189 * @param string $body
178190 * @param Swift_Message|null $message
179191 *
180- * @return PHPUnit_Framework_TestCase $this
192+ * @return $this
181193 */
182194 protected function seeEmailEquals ($ body , Swift_Message $ message = null )
183195 {
@@ -193,7 +205,7 @@ protected function seeEmailEquals($body, Swift_Message $message = null)
193205 * @param string $sender
194206 * @param Swift_Message|null $message
195207 *
196- * @return PHPUnit_Framework_TestCase $this
208+ * @return $this
197209 */
198210 protected function seeEmailFrom ($ sender , Swift_Message $ message = null )
199211 {
@@ -211,7 +223,7 @@ protected function seeEmailFrom($sender, Swift_Message $message = null)
211223 * @param integer $priority
212224 * @param Swift_Message|null $message
213225 *
214- * @return PHPUnit_Framework_TestCase $this
226+ * @return $this
215227 */
216228 protected function seeEmailPriorityEquals ($ priority , Swift_Message $ message = null )
217229 {
@@ -230,7 +242,7 @@ protected function seeEmailPriorityEquals($priority, Swift_Message $message = nu
230242 * @param string $reply_to
231243 * @param Swift_Message|null $message
232244 *
233- * @return PHPUnit_Framework_TestCase $this
245+ * @return $this
234246 */
235247 protected function seeEmailReplyTo ($ reply_to , Swift_Message $ message = null )
236248 {
@@ -246,7 +258,7 @@ protected function seeEmailReplyTo($reply_to, Swift_Message $message = null)
246258 *
247259 * @param integer $count
248260 *
249- * @return PHPUnit_Framework_TestCase $this
261+ * @return TestCase $this
250262 * @deprecated in favor of seeEmailCountEquals
251263 */
252264 protected function seeEmailsSent ($ count )
@@ -259,7 +271,7 @@ protected function seeEmailsSent($count)
259271 *
260272 * @param integer $count
261273 *
262- * @return PHPUnit_Framework_TestCase $this
274+ * @return $this
263275 */
264276 protected function seeEmailCountEquals ($ count )
265277 {
@@ -276,7 +288,7 @@ protected function seeEmailCountEquals($count)
276288 * @param string $subject
277289 * @param Swift_Message|null $message
278290 *
279- * @return PHPUnit_Framework_TestCase $this
291+ * @return TestCase $this
280292 * @deprecated in favor of seeEmailSubjectEquals
281293 */
282294 protected function seeEmailSubject ($ subject , Swift_Message $ message = null )
@@ -290,7 +302,7 @@ protected function seeEmailSubject($subject, Swift_Message $message = null)
290302 * @param string $excerpt
291303 * @param Swift_Message|null $message
292304 *
293- * @return PHPUnit_Framework_TestCase $this
305+ * @return $this
294306 */
295307 protected function seeEmailSubjectContains ($ excerpt , Swift_Message $ message = null )
296308 {
@@ -307,7 +319,7 @@ protected function seeEmailSubjectContains($excerpt, Swift_Message $message = nu
307319 * @param string $excerpt
308320 * @param Swift_Message|null $message
309321 *
310- * @return PHPUnit_Framework_TestCase $this
322+ * @return $this
311323 */
312324 protected function seeEmailSubjectDoesNotContain ($ excerpt , Swift_Message $ message = null )
313325 {
@@ -324,7 +336,7 @@ protected function seeEmailSubjectDoesNotContain($excerpt, Swift_Message $messag
324336 * @param string $subject
325337 * @param Swift_Message|null $message
326338 *
327- * @return PHPUnit_Framework_TestCase $this
339+ * @return $this
328340 */
329341 protected function seeEmailSubjectEquals ($ subject , Swift_Message $ message = null )
330342 {
@@ -341,7 +353,7 @@ protected function seeEmailSubjectEquals($subject, Swift_Message $message = null
341353 * @param string $recipient
342354 * @param Swift_Message|null $message
343355 *
344- * @return PHPUnit_Framework_TestCase $this
356+ * @return $this
345357 */
346358 protected function seeEmailTo ($ recipient , Swift_Message $ message = null )
347359 {
@@ -354,7 +366,7 @@ protected function seeEmailTo($recipient, Swift_Message $message = null)
354366 /**
355367 * Assert that no emails were sent.
356368 *
357- * @return PHPUnit_Framework_TestCase $this
369+ * @return $this
358370 */
359371 protected function seeEmailWasNotSent ()
360372 {
@@ -368,7 +380,7 @@ protected function seeEmailWasNotSent()
368380 /**
369381 * Assert that at least one email was sent.
370382 *
371- * @return PHPUnit_Framework_TestCase $this
383+ * @return $this
372384 */
373385 protected function seeEmailWasSent ()
374386 {
0 commit comments