@@ -47,14 +47,11 @@ public function toSendGrid($notifiable)
4747 };
4848
4949 $ channel = new SendGridChannel (
50- $ sendgrid = Mockery:: mock ( new SendGrid ( ' x ' ) )
50+ $ this -> mockSendgrid ( )
5151 );
5252
5353 $ this ->app ->instance (SendGridChannel::class, $ channel );
5454
55- $ response = Mockery::mock (Response::class);
56- $ response ->shouldReceive ('statusCode ' )->andReturn (200 );
57-
5855 $ message = $ notification ->toSendGrid ($ notifiable );
5956
6057 $ this ->assertEquals ($ message ->templateId , 'sendgrid-template-id ' );
@@ -68,9 +65,6 @@ public function toSendGrid($notifiable)
6865 $ this ->assertEquals ($ message ->replyTo ->getName (), 'Reply To ' );
6966 $ this ->assertEquals ($ message ->sandboxMode , false );
7067
71- // TODO: Verify that the Mail instance passed contains all the info from above
72- $ sendgrid ->shouldReceive ('send ' )->once ()->andReturn ($ response );
73-
7468 $ notifiable ->notify ($ notification );
7569
7670 Event::assertDispatched (
@@ -83,4 +77,76 @@ public function toSendGrid($notifiable)
8377 fn ($ event ) => $ event ->response instanceof Response
8478 );
8579 }
80+
81+ public function testDefaultToAddress ()
82+ {
83+ Event::fake ();
84+
85+ $ channel = new SendGridChannel ($ this ->mockSendgrid ());
86+
87+ $ notification = new class extends Notification {
88+ public $ sendgridMessage ;
89+
90+ public function via ()
91+ {
92+ return [SendGridChannel::class];
93+ }
94+
95+ public function toSendGrid ($ notifiable )
96+ {
97+ $ this ->sendgridMessage = (new SendGridMessage ('sendgrid-template-id ' ))
98+ ->from ('test@example.com ' , 'Example User ' )
99+ ->replyTo ('replyto@example.com ' , 'Reply To ' )
100+ ->payload ([
101+ 'bar ' => 'foo ' ,
102+ 'baz ' => 'foo2 ' ,
103+ ]);
104+
105+ return $ this ->sendgridMessage ;
106+ }
107+ };
108+
109+ $ notifiable = new class {
110+ use Notifiable;
111+
112+ public function routeNotificationForMail ()
113+ {
114+ return 'john@example.com ' ;
115+ }
116+ };
117+
118+ $ channel ->send ($ notifiable , $ notification );
119+ $ message = $ notification ->sendgridMessage ;
120+ $ this ->assertEquals ($ message ->tos [0 ]->getEmail (), 'john@example.com ' );
121+
122+ // Let's also support returning an array (email => name)
123+ // https://laravel.com/docs/10.x/notifications#customizing-the-recipient
124+ $ notifiableWithEmailAndName = new class {
125+ use Notifiable;
126+
127+ public function routeNotificationForMail ()
128+ {
129+ return [
130+ 'john@example.com ' => 'John Doe ' ,
131+ ];
132+ }
133+ };
134+
135+ $ channel ->send ($ notifiableWithEmailAndName , $ notification );
136+ $ message = $ notification ->sendgridMessage ;
137+
138+ $ this ->assertEquals ($ message ->tos [0 ]->getEmail (), 'john@example.com ' );
139+ $ this ->assertEquals ($ message ->tos [0 ]->getName (), 'John Doe ' );
140+ }
141+
142+ private function mockSendgrid ($ statusCode = 200 )
143+ {
144+ $ response = Mockery::mock (Response::class);
145+ $ response ->shouldReceive ('statusCode ' )->andReturn ($ statusCode );
146+
147+ $ sendgrid = Mockery::mock (new SendGrid ('x ' ));
148+ $ sendgrid ->shouldReceive ('send ' )->andReturn ($ response );
149+
150+ return $ sendgrid ;
151+ }
86152}
0 commit comments