@@ -47,14 +47,11 @@ public function toSendGrid($notifiable)
47
47
};
48
48
49
49
$ channel = new SendGridChannel (
50
- $ sendgrid = Mockery:: mock ( new SendGrid ( ' x ' ) )
50
+ $ this -> mockSendgrid ( )
51
51
);
52
52
53
53
$ this ->app ->instance (SendGridChannel::class, $ channel );
54
54
55
- $ response = Mockery::mock (Response::class);
56
- $ response ->shouldReceive ('statusCode ' )->andReturn (200 );
57
-
58
55
$ message = $ notification ->toSendGrid ($ notifiable );
59
56
60
57
$ this ->assertEquals ($ message ->templateId , 'sendgrid-template-id ' );
@@ -68,9 +65,6 @@ public function toSendGrid($notifiable)
68
65
$ this ->assertEquals ($ message ->replyTo ->getName (), 'Reply To ' );
69
66
$ this ->assertEquals ($ message ->sandboxMode , false );
70
67
71
- // TODO: Verify that the Mail instance passed contains all the info from above
72
- $ sendgrid ->shouldReceive ('send ' )->once ()->andReturn ($ response );
73
-
74
68
$ notifiable ->notify ($ notification );
75
69
76
70
Event::assertDispatched (
@@ -83,4 +77,76 @@ public function toSendGrid($notifiable)
83
77
fn ($ event ) => $ event ->response instanceof Response
84
78
);
85
79
}
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
+ }
86
152
}
0 commit comments