Skip to content

Commit e1c6866

Browse files
authored
Return Sendgrid response (#17)
* return sendgrid response * apply php-cs-fixer * update readme
1 parent 11e5250 commit e1c6866

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,21 @@ return (new SendGridMessage('Your SendGrid template ID'))
162162

163163
For all the options, you can see Sendgrid's Mail class [here](https://github.com/sendgrid/sendgrid-php/blob/main/lib/mail/Mail.php).
164164

165+
### Accessing SendGrid Response
166+
167+
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.
168+
169+
```php
170+
use Illuminate\Notifications\Events\NotificationSent;
171+
172+
Event::listen(NotificationSent::class, function (NotificationSent $event) {
173+
/**
174+
* @var \SendGrid\Response $response
175+
*/
176+
$response = $event->response;
177+
});
178+
```
179+
165180
## Changelog
166181

167182
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

src/SendGridChannel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,7 @@ public function send($notifiable, Notification $notification)
6060
$response->body()
6161
);
6262
}
63+
64+
return $response;
6365
}
6466
}

tests/SendGridChannelTest.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
use Mockery;
66
use SendGrid;
77
use SendGrid\Response;
8+
use Illuminate\Support\Facades\Event;
89
use Illuminate\Notifications\Notifiable;
910
use Illuminate\Notifications\Notification;
1011
use NotificationChannels\SendGrid\SendGridChannel;
1112
use NotificationChannels\SendGrid\SendGridMessage;
13+
use Illuminate\Notifications\Events\NotificationSent;
1214

1315
class SendGridChannelTest extends TestCase
1416
{
@@ -19,7 +21,14 @@ public function tearDown(): void
1921

2022
public function testEmailIsSentViaSendGrid()
2123
{
24+
Event::fake();
25+
2226
$notification = new class extends Notification {
27+
public function via()
28+
{
29+
return [SendGridChannel::class];
30+
}
31+
2332
public function toSendGrid($notifiable)
2433
{
2534
return (new SendGridMessage('sendgrid-template-id'))
@@ -41,6 +50,8 @@ public function toSendGrid($notifiable)
4150
$sendgrid = Mockery::mock(new SendGrid('x'))
4251
);
4352

53+
$this->app->instance(SendGridChannel::class, $channel);
54+
4455
$response = Mockery::mock(Response::class);
4556
$response->shouldReceive('statusCode')->andReturn(200);
4657

@@ -60,6 +71,16 @@ public function toSendGrid($notifiable)
6071
// TODO: Verify that the Mail instance passed contains all the info from above
6172
$sendgrid->shouldReceive('send')->once()->andReturn($response);
6273

63-
$channel->send($notifiable, $notification);
74+
$notifiable->notify($notification);
75+
76+
Event::assertDispatched(
77+
NotificationSent::class,
78+
fn ($event) => $event->channel === SendGridChannel::class
79+
);
80+
81+
Event::assertDispatched(
82+
NotificationSent::class,
83+
fn ($event) => $event->response instanceof Response
84+
);
6485
}
6586
}

tests/TestCase.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
namespace NotificationChannels\SendGrid\Test;
44

5+
use NotificationChannels\SendGrid\SendGridServiceProvider;
6+
57
class TestCase extends \Orchestra\Testbench\TestCase
68
{
9+
protected function getPackageProviders($app)
10+
{
11+
return [
12+
SendGridServiceProvider::class,
13+
];
14+
}
715
}

0 commit comments

Comments
 (0)