Skip to content

Commit

Permalink
Return Sendgrid response (#17)
Browse files Browse the repository at this point in the history
* return sendgrid response

* apply php-cs-fixer

* update readme
  • Loading branch information
aozisik committed Aug 6, 2023
1 parent 11e5250 commit e1c6866
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions src/SendGridChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,7 @@ public function send($notifiable, Notification $notification)
$response->body()
);
}

return $response;
}
}
23 changes: 22 additions & 1 deletion tests/SendGridChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
use Mockery;
use SendGrid;
use SendGrid\Response;
use Illuminate\Support\Facades\Event;
use Illuminate\Notifications\Notifiable;
use Illuminate\Notifications\Notification;
use NotificationChannels\SendGrid\SendGridChannel;
use NotificationChannels\SendGrid\SendGridMessage;
use Illuminate\Notifications\Events\NotificationSent;

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

public function testEmailIsSentViaSendGrid()
{
Event::fake();

$notification = new class extends Notification {
public function via()
{
return [SendGridChannel::class];
}

public function toSendGrid($notifiable)
{
return (new SendGridMessage('sendgrid-template-id'))
Expand All @@ -41,6 +50,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);

Expand All @@ -60,6 +71,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
);
}
}
8 changes: 8 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}
}

0 comments on commit e1c6866

Please sign in to comment.