22
33namespace Tests ;
44
5+ use Illuminate \Support \Facades \Event ;
6+ use Stackkit \LaravelDatabaseEmails \MessageSent ;
7+ use Stackkit \LaravelDatabaseEmails \SentMessage ;
58use Swift_Events_SendEvent ;
69use Illuminate \Support \Facades \Mail ;
710use Stackkit \LaravelDatabaseEmails \Email ;
11+ use Symfony \Component \Mime \Part \DataPart ;
812
913class SenderTest extends TestCase
1014{
@@ -15,7 +19,15 @@ public function setUp(): void
1519 {
1620 parent ::setUp ();
1721
18- Mail::getSwiftMailer ()->registerPlugin (new TestingMailEventListener ($ this ));
22+ if (version_compare (app ()->version (), '9.0.0 ' , '>= ' )) {
23+ Event::listen (MessageSent::class, function (MessageSent $ event ) {
24+ $ this ->sent [] = SentMessage::createFromSymfonyMailer (
25+ $ event ->message ->getSymfonySentMessage ()->getOriginalMessage ()
26+ );
27+ });
28+ } else {
29+ Mail::getSwiftMailer ()->registerPlugin (new TestingMailEventListener ($ this ));
30+ }
1931 }
2032
2133 /** @test */
@@ -38,7 +50,7 @@ public function the_email_has_a_correct_from_email_and_from_name()
3850
3951 $ this ->artisan ('email:send ' );
4052
41- $ from = reset ($ this ->sent )->getMessage ()-> getFrom () ;
53+ $ from = reset ($ this ->sent )->from ;
4254
4355 $ this ->assertEquals ('testfromaddress@gmail.com ' , key ($ from ));
4456 $ this ->assertEquals ('From CI test ' , $ from [key ($ from )]);
@@ -48,23 +60,23 @@ public function the_email_has_a_correct_from_email_and_from_name()
4860
4961 $ this ->composeEmail ()->from ('marick@dolphiq.nl ' , 'Marick ' )->send ();
5062 $ this ->artisan ('email:send ' );
51- $ from = reset ($ this ->sent )->getMessage ()-> getFrom () ;
63+ $ from = reset ($ this ->sent )->from ;
5264 $ this ->assertEquals ('marick@dolphiq.nl ' , key ($ from ));
5365 $ this ->assertEquals ('Marick ' , $ from [key ($ from )]);
5466
5567 // only address
5668 $ this ->sent = [];
5769 $ this ->composeEmail ()->from ('marick@dolphiq.nl ' )->send ();
5870 $ this ->artisan ('email:send ' );
59- $ from = reset ($ this ->sent )->getMessage ()-> getFrom () ;
71+ $ from = reset ($ this ->sent )->from ;
6072 $ this ->assertEquals ('marick@dolphiq.nl ' , key ($ from ));
6173 $ this ->assertEquals (config ('mail.from.name ' ), $ from [key ($ from )]);
6274
6375 // only name
6476 $ this ->sent = [];
6577 $ this ->composeEmail ()->from (null , 'Marick ' )->send ();
6678 $ this ->artisan ('email:send ' );
67- $ from = reset ($ this ->sent )->getMessage ()-> getFrom () ;
79+ $ from = reset ($ this ->sent )->from ;
6880 $ this ->assertEquals (config ('mail.from.address ' ), key ($ from ));
6981 $ this ->assertEquals ('Marick ' , $ from [key ($ from )]);
7082 }
@@ -74,14 +86,14 @@ public function it_sends_emails_to_the_correct_recipients()
7486 {
7587 $ this ->sendEmail (['recipient ' => 'john@doe.com ' ]);
7688 $ this ->artisan ('email:send ' );
77- $ to = reset ($ this ->sent )->getMessage ()-> getTo () ;
89+ $ to = reset ($ this ->sent )->to ;
7890 $ this ->assertCount (1 , $ to );
7991 $ this ->assertArrayHasKey ('john@doe.com ' , $ to );
8092
8193 $ this ->sent = [];
8294 $ this ->sendEmail (['recipient ' => ['john@doe.com ' , 'john+2@doe.com ' ]]);
8395 $ this ->artisan ('email:send ' );
84- $ to = reset ($ this ->sent )->getMessage ()-> getTo () ;
96+ $ to = reset ($ this ->sent )->to ;
8597 $ this ->assertCount (2 , $ to );
8698 $ this ->assertArrayHasKey ('john@doe.com ' , $ to );
8799 $ this ->assertArrayHasKey ('john+2@doe.com ' , $ to );
@@ -92,14 +104,14 @@ public function it_adds_the_cc_addresses()
92104 {
93105 $ this ->sendEmail (['cc ' => 'cc@test.com ' ]);
94106 $ this ->artisan ('email:send ' );
95- $ cc = reset ($ this ->sent )->getMessage ()-> getCc () ;
107+ $ cc = reset ($ this ->sent )->cc ;
96108 $ this ->assertCount (1 , $ cc );
97109 $ this ->assertArrayHasKey ('cc@test.com ' , $ cc );
98110
99111 $ this ->sent = [];
100112 $ this ->sendEmail (['cc ' => ['cc@test.com ' , 'cc+2@test.com ' ]]);
101113 $ this ->artisan ('email:send ' );
102- $ cc = reset ($ this ->sent )->getMessage ()-> getCc () ;
114+ $ cc = reset ($ this ->sent )->cc ;
103115 $ this ->assertCount (2 , $ cc );
104116 $ this ->assertArrayHasKey ('cc@test.com ' , $ cc );
105117 $ this ->assertArrayHasKey ('cc+2@test.com ' , $ cc );
@@ -110,14 +122,14 @@ public function it_adds_the_bcc_addresses()
110122 {
111123 $ this ->sendEmail (['bcc ' => 'bcc@test.com ' ]);
112124 $ this ->artisan ('email:send ' );
113- $ bcc = reset ($ this ->sent )->getMessage ()-> getBcc () ;
125+ $ bcc = reset ($ this ->sent )->bcc ;
114126 $ this ->assertCount (1 , $ bcc );
115127 $ this ->assertArrayHasKey ('bcc@test.com ' , $ bcc );
116128
117129 $ this ->sent = [];
118130 $ this ->sendEmail (['bcc ' => ['bcc@test.com ' , 'bcc+2@test.com ' ]]);
119131 $ this ->artisan ('email:send ' );
120- $ bcc = reset ($ this ->sent )->getMessage ()-> getBcc () ;
132+ $ bcc = reset ($ this ->sent )->bcc ;
121133 $ this ->assertCount (2 , $ bcc );
122134 $ this ->assertArrayHasKey ('bcc@test.com ' , $ bcc );
123135 $ this ->assertArrayHasKey ('bcc+2@test.com ' , $ bcc );
@@ -130,7 +142,7 @@ public function the_email_has_the_correct_subject()
130142
131143 $ this ->artisan ('email:send ' );
132144
133- $ subject = reset ($ this ->sent )->getMessage ()-> getSubject () ;
145+ $ subject = reset ($ this ->sent )->subject ;
134146
135147 $ this ->assertEquals ('Hello World ' , $ subject );
136148 }
@@ -140,13 +152,13 @@ public function the_email_has_the_correct_body()
140152 {
141153 $ this ->sendEmail (['variables ' => ['name ' => 'John Doe ' ]]);
142154 $ this ->artisan ('email:send ' );
143- $ body = reset ($ this ->sent )->getMessage ()-> getBody () ;
144- $ this ->assertEquals (view ('tests::dummy ' , ['name ' => 'John Doe ' ]), $ body );
155+ $ body = reset ($ this ->sent )->body ;
156+ $ this ->assertEquals (( string ) view ('tests::dummy ' , ['name ' => 'John Doe ' ]), $ body );
145157
146158 $ this ->sent = [];
147159 $ this ->sendEmail (['variables ' => []]);
148160 $ this ->artisan ('email:send ' );
149- $ body = reset ($ this ->sent )->getMessage ()-> getBody () ;
161+ $ body = reset ($ this ->sent )->body ;
150162 $ this ->assertEquals (view ('tests::dummy ' ), $ body );
151163 }
152164
@@ -158,12 +170,9 @@ public function attachments_are_added_to_the_email()
158170 ->send ();
159171 $ this ->artisan ('email:send ' );
160172
161- $ attachments = reset ($ this ->sent )->getMessage ()->getChildren ();
162- $ attachment = reset ($ attachments );
173+ $ attachments = reset ($ this ->sent )->attachments ;
163174
164175 $ this ->assertCount (1 , $ attachments );
165- $ this ->assertEquals ('attachment; filename=pdf-sample.pdf ' , $ attachment ->getHeaders ()->get ('content-disposition ' )->getFieldBody ());
166- $ this ->assertEquals ('application/pdf ' , $ attachment ->getContentType ());
167176 }
168177
169178 /** @test */
@@ -172,19 +181,19 @@ public function attachments_are_not_added_if_the_data_is_not_valid()
172181 $ this ->sent = [];
173182 $ this ->composeEmail ()->attach (null )->send ();
174183 $ this ->artisan ('email:send ' );
175- $ attachments = reset ($ this ->sent )->getMessage ()-> getChildren () ;
184+ $ attachments = reset ($ this ->sent )->attachments ;
176185 $ this ->assertCount (0 , $ attachments );
177186
178187 $ this ->sent = [];
179188 $ this ->composeEmail ()->attach (false )->send ();
180189 $ this ->artisan ('email:send ' );
181- $ attachments = reset ($ this ->sent )->getMessage ()-> getChildren () ;
190+ $ attachments = reset ($ this ->sent )->attachments ;
182191 $ this ->assertCount (0 , $ attachments );
183192
184193 $ this ->sent = [];
185194 $ this ->composeEmail ()->attach ('' )->send ();
186195 $ this ->artisan ('email:send ' );
187- $ attachments = reset ($ this ->sent )->getMessage ()-> getChildren () ;
196+ $ attachments = reset ($ this ->sent )->attachments ;
188197 $ this ->assertCount (0 , $ attachments );
189198 }
190199
@@ -200,13 +209,13 @@ public function raw_attachments_are_added_to_the_email()
200209 ->send ();
201210 $ this ->artisan ('email:send ' );
202211
203- $ attachments = reset ($ this ->sent )->getMessage ()-> getChildren () ;
212+ $ attachments = reset ($ this ->sent )->attachments ;
204213 $ attachment = reset ($ attachments );
205214
206215 $ this ->assertCount (1 , $ attachments );
207- $ this ->assertEquals ( ' attachment; filename= hello-ci.pdf ' , $ attachment-> getHeaders ()-> get ( ' content- disposition ')-> getFieldBody () );
208- $ this ->assertEquals ('application/pdf ' , $ attachment-> getContentType () );
209- $ this ->assertTrue (md5 ($ attachment-> getBody () ) == md5 ($ rawData ));
216+ $ this ->assertStringContainsString ( ' hello-ci.pdf ' , $ attachment[ ' disposition '] );
217+ $ this ->assertStringContainsString ('application/pdf ' , $ attachment[ ' disposition ' ] );
218+ $ this ->assertTrue (md5 ($ attachment[ ' body ' ] ) == md5 ($ rawData ));
210219 }
211220
212221 /** @test */
@@ -246,19 +255,19 @@ public function raw_attachments_are_not_added_if_the_data_is_not_valid()
246255 $ this ->sent = [];
247256 $ this ->composeEmail ()->attachData (null , 'test.png ' )->send ();
248257 $ this ->artisan ('email:send ' );
249- $ attachments = reset ($ this ->sent )->getMessage ()-> getChildren () ;
258+ $ attachments = reset ($ this ->sent )->attachments ;
250259 $ this ->assertCount (0 , $ attachments );
251260
252261 $ this ->sent = [];
253262 $ this ->composeEmail ()->attachData (false , 'test.png ' )->send ();
254263 $ this ->artisan ('email:send ' );
255- $ attachments = reset ($ this ->sent )->getMessage ()-> getChildren () ;
264+ $ attachments = reset ($ this ->sent )->attachments ;
256265 $ this ->assertCount (0 , $ attachments );
257266
258267 $ this ->sent = [];
259268 $ this ->composeEmail ()->attachData ('' , 'test.png ' )->send ();
260269 $ this ->artisan ('email:send ' );
261- $ attachments = reset ($ this ->sent )->getMessage ()-> getChildren () ;
270+ $ attachments = reset ($ this ->sent )->attachments ;
262271 $ this ->assertCount (0 , $ attachments );
263272 }
264273}
0 commit comments