From 3fb0cddd848cb556af4b8bcb24182fcd8d3bce9c Mon Sep 17 00:00:00 2001 From: Ahmet Ozisik Date: Thu, 3 Aug 2023 13:01:07 +0300 Subject: [PATCH 1/3] return sendgrid response --- src/SendGridChannel.php | 6 ++++-- tests/SendGridChannelTest.php | 29 ++++++++++++++++++++++++++--- tests/TestCase.php | 8 ++++++++ 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/SendGridChannel.php b/src/SendGridChannel.php index 8aca8c1..b6d5574 100644 --- a/src/SendGridChannel.php +++ b/src/SendGridChannel.php @@ -29,7 +29,7 @@ public function __construct(SendGrid $sendGrid) */ public function send($notifiable, Notification $notification) { - if (! method_exists($notification, 'toSendGrid')) { + if (!method_exists($notification, 'toSendGrid')) { throw new Exception('You must implement toSendGrid in the notification class for SendGrid channel.'); } @@ -49,7 +49,7 @@ public function send($notifiable, Notification $notification) $message->to($notifiable->routeNotificationFor('mail')); } - if (! ($message instanceof SendGridMessage)) { + if (!($message instanceof SendGridMessage)) { throw new Exception('toSendGrid must return an instance of SendGridMessage.'); } @@ -60,5 +60,7 @@ public function send($notifiable, Notification $notification) $response->body() ); } + + return $response; } } diff --git a/tests/SendGridChannelTest.php b/tests/SendGridChannelTest.php index ec1dd24..882b5cd 100644 --- a/tests/SendGridChannelTest.php +++ b/tests/SendGridChannelTest.php @@ -2,11 +2,13 @@ namespace NotificationChannels\SendGrid\Test; +use Illuminate\Notifications\Events\NotificationSent; use Mockery; use SendGrid; use SendGrid\Response; use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notification; +use Illuminate\Support\Facades\Event; use NotificationChannels\SendGrid\SendGridChannel; use NotificationChannels\SendGrid\SendGridMessage; @@ -19,7 +21,15 @@ public function tearDown(): void public function testEmailIsSentViaSendGrid() { - $notification = new class extends Notification { + Event::fake(); + + $notification = new class extends Notification + { + public function via() + { + return [SendGridChannel::class]; + } + public function toSendGrid($notifiable) { return (new SendGridMessage('sendgrid-template-id')) @@ -33,7 +43,8 @@ public function toSendGrid($notifiable) } }; - $notifiable = new class { + $notifiable = new class + { use Notifiable; }; @@ -41,6 +52,8 @@ public function toSendGrid($notifiable) $sendgrid = Mockery::mock(new SendGrid('x')) ); + $this->app->instance(SendGridChannel::class, $channel); + $response = Mockery::mock(Response::class); $response->shouldReceive('statusCode')->andReturn(200); @@ -60,6 +73,16 @@ public function toSendGrid($notifiable) // TODO: Verify that the Mail instance passed contains all the info from above $sendgrid->shouldReceive('send')->once()->andReturn($response); - $channel->send($notifiable, $notification); + $notifiable->notify($notification); + + Event::assertDispatched( + NotificationSent::class, + fn ($event) => $event->channel === SendGridChannel::class + ); + + Event::assertDispatched( + NotificationSent::class, + fn ($event) => $event->response instanceof Response + ); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index b9f9f27..877e13d 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,6 +2,14 @@ namespace NotificationChannels\SendGrid\Test; +use NotificationChannels\SendGrid\SendGridServiceProvider; + class TestCase extends \Orchestra\Testbench\TestCase { + protected function getPackageProviders($app) + { + return [ + SendGridServiceProvider::class, + ]; + } } From de9b2429aaf605087073e195a72742fa1d96e2e2 Mon Sep 17 00:00:00 2001 From: Ahmet Ozisik Date: Thu, 3 Aug 2023 13:06:28 +0300 Subject: [PATCH 2/3] apply php-cs-fixer --- src/SendGridChannel.php | 4 ++-- tests/SendGridChannelTest.php | 10 ++++------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/SendGridChannel.php b/src/SendGridChannel.php index b6d5574..13a3c09 100644 --- a/src/SendGridChannel.php +++ b/src/SendGridChannel.php @@ -29,7 +29,7 @@ public function __construct(SendGrid $sendGrid) */ public function send($notifiable, Notification $notification) { - if (!method_exists($notification, 'toSendGrid')) { + if (! method_exists($notification, 'toSendGrid')) { throw new Exception('You must implement toSendGrid in the notification class for SendGrid channel.'); } @@ -49,7 +49,7 @@ public function send($notifiable, Notification $notification) $message->to($notifiable->routeNotificationFor('mail')); } - if (!($message instanceof SendGridMessage)) { + if (! ($message instanceof SendGridMessage)) { throw new Exception('toSendGrid must return an instance of SendGridMessage.'); } diff --git a/tests/SendGridChannelTest.php b/tests/SendGridChannelTest.php index 882b5cd..576e667 100644 --- a/tests/SendGridChannelTest.php +++ b/tests/SendGridChannelTest.php @@ -2,15 +2,15 @@ namespace NotificationChannels\SendGrid\Test; -use Illuminate\Notifications\Events\NotificationSent; use Mockery; use SendGrid; use SendGrid\Response; +use Illuminate\Support\Facades\Event; use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notification; -use Illuminate\Support\Facades\Event; use NotificationChannels\SendGrid\SendGridChannel; use NotificationChannels\SendGrid\SendGridMessage; +use Illuminate\Notifications\Events\NotificationSent; class SendGridChannelTest extends TestCase { @@ -23,8 +23,7 @@ public function testEmailIsSentViaSendGrid() { Event::fake(); - $notification = new class extends Notification - { + $notification = new class extends Notification { public function via() { return [SendGridChannel::class]; @@ -43,8 +42,7 @@ public function toSendGrid($notifiable) } }; - $notifiable = new class - { + $notifiable = new class { use Notifiable; }; From d565fcaa2c4e877419b7a11f17f63a8dc1e6dd36 Mon Sep 17 00:00:00 2001 From: Ahmet Ozisik Date: Sun, 6 Aug 2023 21:05:48 +0300 Subject: [PATCH 3/3] update readme --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index 1f74952..1b9de4d 100644 --- a/README.md +++ b/README.md @@ -162,6 +162,21 @@ return (new SendGridMessage('Your SendGrid template ID')) For all the options, you can see Sendgrid's Mail class [here](https://github.com/sendgrid/sendgrid-php/blob/main/lib/mail/Mail.php). +### Accessing SendGrid Response + +After the notification is sent, Laravel will emit `Illuminate\Notifications\Events\NotificationSent` event. You can listen to this event to get the SendGrid response object and/or message ID. + +```php +use Illuminate\Notifications\Events\NotificationSent; + +Event::listen(NotificationSent::class, function (NotificationSent $event) { + /** + * @var \SendGrid\Response $response + */ + $response = $event->response; +}); +``` + ## Changelog Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.