Skip to content

Commit

Permalink
Support array returning from routeNotificationForMail (#19)
Browse files Browse the repository at this point in the history
* support address returned as array

* apply cs fixer

* update tests
  • Loading branch information
aozisik authored Dec 13, 2023
1 parent 45bee1d commit 48cee97
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 8 deletions.
10 changes: 9 additions & 1 deletion src/SendGridChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
80 changes: 73 additions & 7 deletions tests/SendGridChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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(
Expand All @@ -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;
}
}

0 comments on commit 48cee97

Please sign in to comment.