diff --git a/src/SendGridChannel.php b/src/SendGridChannel.php index 13a3c09..a79e47e 100644 --- a/src/SendGridChannel.php +++ b/src/SendGridChannel.php @@ -46,7 +46,15 @@ public function send($notifiable, Notification $notification) } if (empty($message->tos)) { - $message->to($notifiable->routeNotificationFor('mail')); + $to = $notifiable->routeNotificationFor('mail'); + + // Handle the case where routeNotificationForMail returns an array (email => name) + if (is_array($to)) { + reset($to); + $message->to(key($to), current($to)); + } else { + $message->to($to); + } } if (! ($message instanceof SendGridMessage)) { diff --git a/tests/SendGridChannelTest.php b/tests/SendGridChannelTest.php index 576e667..932ff9a 100644 --- a/tests/SendGridChannelTest.php +++ b/tests/SendGridChannelTest.php @@ -47,14 +47,11 @@ public function toSendGrid($notifiable) }; $channel = new SendGridChannel( - $sendgrid = Mockery::mock(new SendGrid('x')) + $this->mockSendgrid() ); $this->app->instance(SendGridChannel::class, $channel); - $response = Mockery::mock(Response::class); - $response->shouldReceive('statusCode')->andReturn(200); - $message = $notification->toSendGrid($notifiable); $this->assertEquals($message->templateId, 'sendgrid-template-id'); @@ -68,9 +65,6 @@ public function toSendGrid($notifiable) $this->assertEquals($message->replyTo->getName(), 'Reply To'); $this->assertEquals($message->sandboxMode, false); - // TODO: Verify that the Mail instance passed contains all the info from above - $sendgrid->shouldReceive('send')->once()->andReturn($response); - $notifiable->notify($notification); Event::assertDispatched( @@ -83,4 +77,76 @@ public function toSendGrid($notifiable) fn ($event) => $event->response instanceof Response ); } + + public function testDefaultToAddress() + { + Event::fake(); + + $channel = new SendGridChannel($this->mockSendgrid()); + + $notification = new class extends Notification { + public $sendgridMessage; + + public function via() + { + return [SendGridChannel::class]; + } + + public function toSendGrid($notifiable) + { + $this->sendgridMessage = (new SendGridMessage('sendgrid-template-id')) + ->from('test@example.com', 'Example User') + ->replyTo('replyto@example.com', 'Reply To') + ->payload([ + 'bar' => 'foo', + 'baz' => 'foo2', + ]); + + return $this->sendgridMessage; + } + }; + + $notifiable = new class { + use Notifiable; + + public function routeNotificationForMail() + { + return 'john@example.com'; + } + }; + + $channel->send($notifiable, $notification); + $message = $notification->sendgridMessage; + $this->assertEquals($message->tos[0]->getEmail(), 'john@example.com'); + + // Let's also support returning an array (email => name) + // https://laravel.com/docs/10.x/notifications#customizing-the-recipient + $notifiableWithEmailAndName = new class { + use Notifiable; + + public function routeNotificationForMail() + { + return [ + 'john@example.com' => 'John Doe', + ]; + } + }; + + $channel->send($notifiableWithEmailAndName, $notification); + $message = $notification->sendgridMessage; + + $this->assertEquals($message->tos[0]->getEmail(), 'john@example.com'); + $this->assertEquals($message->tos[0]->getName(), 'John Doe'); + } + + private function mockSendgrid($statusCode = 200) + { + $response = Mockery::mock(Response::class); + $response->shouldReceive('statusCode')->andReturn($statusCode); + + $sendgrid = Mockery::mock(new SendGrid('x')); + $sendgrid->shouldReceive('send')->andReturn($response); + + return $sendgrid; + } }