Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return Sendgrid response #17

Merged
merged 3 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
];
}
}
Loading