Skip to content

Commit

Permalink
Remove string type cast for payload values. Add setSandboxMode method. (
Browse files Browse the repository at this point in the history
#8)

* add setSandboxMode method

* remove type casting to allow addDynamicTemplateData to accept arrays. (#7)

* update test cases

* add test case

* refactor

* update readme and changelog

Co-authored-by: Tim Gieseking <[email protected]>
  • Loading branch information
aozisik and timstl committed Sep 24, 2022
1 parent bbef13a commit 91b13ac
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 64 deletions.
15 changes: 10 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@

All notable changes will be documented in this file

## 2.2.0 - 2022-09-24

- You can now pass nested arrays, numbers, bools as the payload. Previously, the library only accepted strings. (Thanks [timstl](https://github.com/swiftmade/laravel-sendgrid-notification-channel/pull/7))
- Added `setSandboxMode($bool)` method on the `SendGridMessage` object, so you can now control sandbox mode using a variable.

## 2.1.0 - 2022-08-12

- Added support for Sentry SDK v8
- You can now enable sandbox mode while sending emails. (Thanks [@zbrody](https://github.com/swiftmade/laravel-sendgrid-notification-channel/pull/3))
- Added support for Sentry SDK v8
- You can now enable sandbox mode while sending emails. (Thanks [@zbrody](https://github.com/swiftmade/laravel-sendgrid-notification-channel/pull/3))

## 2.0.0 - 2022-04-07

- Added support for Laravel 8 and 9.
- Added support for Laravel 8 and 9.

## 1.0.1 - 2020-08-11

- stable release for Laravel 5, 6 and 7.
- stable release for Laravel 5, 6 and 7.

## 0.0.4 - 2020-08-11

- initial release
- initial release
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,22 @@ class ExampleNotification extends Notification

💡 Unless you set it explicitly, the **From** address will be set to `config('mail.from.address')` and the **To** value will be what returns from `$notifiable->routeNotificationFor('mail');`

### Enabling Sandbox Mode
### Sandbox Mode

To enable sandbox mode you will need to chain on the `enableSandboxMode()` to the message object.
The sandbox mode is **off** by default. You can use the `setSandboxMode($bool)` method to enable/disable it.

Example:

```php
return (new SendGridMessage('Your SendGrid template ID'))
->enableSandboxMode()
->setSandboxMode(true)
->payload([
"template_var_1" => "template_value_1"
'template_var_1' => 'template_value_1',
'template_var_2' => [
'value_1',
'value_2',
'value_3',
],
]);
```

Expand Down
23 changes: 15 additions & 8 deletions src/SendGridMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,29 @@ public function build(): Mail
);

$email->setReplyTo($this->replyTo);

$email->setTemplateId($this->templateId);

if ($this->sandboxMode) {
$email->enableSandBoxMode();
}
$this->sandboxMode
? $email->enableSandBoxMode()
: $email->disableSandBoxMode();

foreach ($this->payload as $key => $value) {
$email->addDynamicTemplateData((string) $key, (string) $value);
$email->addDynamicTemplateData((string) $key, $value);
}

return $email;
}

/**
* @param bool $sandboxMode
*/
public function setSandboxMode($sandboxMode)
{
$this->sandboxMode = $sandboxMode;

return $this;
}

/**
* Enabling sandbox mode allows you to send a test email to
* ensure that your request body is formatted correctly
Expand All @@ -138,8 +147,6 @@ public function build(): Mail
*/
public function enableSandboxMode()
{
$this->sandboxMode = true;

return $this;
return $this->setSandboxMode(true);
}
}
47 changes: 0 additions & 47 deletions tests/SendGridChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,51 +63,4 @@ public function toSendGrid($notifiable)

$channel->send($notifiable, $notification);
}

public function testEmailIsNotSentViaSendGridWithSandbox()
{
$notification = new class extends Notification {
public function toSendGrid($notifiable)
{
return (new SendGridMessage('sendgrid-template-id'))
->from('[email protected]', 'Example User')
->to('[email protected]', 'Example User1')
->replyTo('[email protected]', 'Reply To')
->payload([
'bar' => 'foo',
'baz' => 'foo2',
])
->enableSandboxMode();
}
};

$notifiable = new class {
use Notifiable;
};

$channel = new SendGridChannel(
$sendgrid = Mockery::mock(new SendGrid('x'))
);

$response = Mockery::mock(Response::class);
$response->shouldReceive('statusCode')->andReturn(200);

$message = $notification->toSendGrid($notifiable);

$this->assertEquals($message->templateId, 'sendgrid-template-id');
$this->assertEquals($message->from->getEmail(), '[email protected]');
$this->assertEquals($message->from->getName(), 'Example User');
$this->assertEquals($message->tos[0]->getEmail(), '[email protected]');
$this->assertEquals($message->tos[0]->getName(), 'Example User1');
$this->assertEquals($message->payload['bar'], 'foo');
$this->assertEquals($message->payload['baz'], 'foo2');
$this->assertEquals($message->replyTo->getEmail(), '[email protected]');
$this->assertEquals($message->replyTo->getName(), 'Reply To');
$this->assertEquals($message->sandboxMode, true);

// TODO: Verify that the Mail instance passed contains all the info from above
$sendgrid->shouldReceive('send')->once()->andReturn($response);

$channel->send($notifiable, $notification);
}
}
53 changes: 53 additions & 0 deletions tests/SendGridMessageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace NotificationChannels\SendGrid\Test;

use PHPUnit\Framework\TestCase;
use NotificationChannels\SendGrid\SendGridMessage;

class SendGridMessageTest extends TestCase
{
public function testDynamicVariables()
{
$message = new SendGridMessage('template-id');
$message->payload($payload = [
'bar' => 'string',
'baz' => null,
'foo' => [
'nested' => 'array',
'number' => 1235,
'array' => [1, 2, 3],
],
]);

$mail = $message->build();
$this->assertEquals(
$payload,
$mail->getPersonalization()->getDynamicTemplateData()
);
}

public function testSandboxMode()
{
$message = new SendGridMessage('template-id');
$this->assertFalse($message->sandboxMode, 'Sandbox mode is turned off by default');
$this->assertFalse(
$message->build()->getMailSettings()->getSandboxMode()->getEnable(),
'Sandbox mode is disabled in Sendgrid mail settings'
);

$message->enableSandboxMode();
$this->assertTrue($message->sandboxMode, 'Sandbox mode can be enabled');
$this->assertTrue(
$message->build()->getMailSettings()->getSandboxMode()->getEnable(),
'Sandbox mode is enabled in Sendgrid mail settings'
);

$message->setSandboxMode(false);
$this->assertFalse($message->sandboxMode, 'Sandbox mode can be turned off');
$this->assertFalse(
$message->build()->getMailSettings()->getSandboxMode()->getEnable(),
'Sandbox mode is disabled in Sendgrid mail settings'
);
}
}

0 comments on commit 91b13ac

Please sign in to comment.