Skip to content

Commit 91b13ac

Browse files
aozisiktimstl
andauthored
Remove string type cast for payload values. Add setSandboxMode method. (#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 <timstl@gmail.com>
1 parent bbef13a commit 91b13ac

File tree

5 files changed

+87
-64
lines changed

5 files changed

+87
-64
lines changed

CHANGELOG.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,24 @@
22

33
All notable changes will be documented in this file
44

5+
## 2.2.0 - 2022-09-24
6+
7+
- 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))
8+
- Added `setSandboxMode($bool)` method on the `SendGridMessage` object, so you can now control sandbox mode using a variable.
9+
510
## 2.1.0 - 2022-08-12
611

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

1015
## 2.0.0 - 2022-04-07
1116

12-
- Added support for Laravel 8 and 9.
17+
- Added support for Laravel 8 and 9.
1318

1419
## 1.0.1 - 2020-08-11
1520

16-
- stable release for Laravel 5, 6 and 7.
21+
- stable release for Laravel 5, 6 and 7.
1722

1823
## 0.0.4 - 2020-08-11
1924

20-
- initial release
25+
- initial release

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,22 @@ class ExampleNotification extends Notification
9999

100100
💡 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');`
101101

102-
### Enabling Sandbox Mode
102+
### Sandbox Mode
103103

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

106106
Example:
107107

108108
```php
109109
return (new SendGridMessage('Your SendGrid template ID'))
110-
->enableSandboxMode()
110+
->setSandboxMode(true)
111111
->payload([
112-
"template_var_1" => "template_value_1"
112+
'template_var_1' => 'template_value_1',
113+
'template_var_2' => [
114+
'value_1',
115+
'value_2',
116+
'value_3',
117+
],
113118
]);
114119
```
115120

src/SendGridMessage.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,29 @@ public function build(): Mail
114114
);
115115

116116
$email->setReplyTo($this->replyTo);
117-
118117
$email->setTemplateId($this->templateId);
119118

120-
if ($this->sandboxMode) {
121-
$email->enableSandBoxMode();
122-
}
119+
$this->sandboxMode
120+
? $email->enableSandBoxMode()
121+
: $email->disableSandBoxMode();
123122

124123
foreach ($this->payload as $key => $value) {
125-
$email->addDynamicTemplateData((string) $key, (string) $value);
124+
$email->addDynamicTemplateData((string) $key, $value);
126125
}
127126

128127
return $email;
129128
}
130129

130+
/**
131+
* @param bool $sandboxMode
132+
*/
133+
public function setSandboxMode($sandboxMode)
134+
{
135+
$this->sandboxMode = $sandboxMode;
136+
137+
return $this;
138+
}
139+
131140
/**
132141
* Enabling sandbox mode allows you to send a test email to
133142
* ensure that your request body is formatted correctly
@@ -138,8 +147,6 @@ public function build(): Mail
138147
*/
139148
public function enableSandboxMode()
140149
{
141-
$this->sandboxMode = true;
142-
143-
return $this;
150+
return $this->setSandboxMode(true);
144151
}
145152
}

tests/SendGridChannelTest.php

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -63,51 +63,4 @@ public function toSendGrid($notifiable)
6363

6464
$channel->send($notifiable, $notification);
6565
}
66-
67-
public function testEmailIsNotSentViaSendGridWithSandbox()
68-
{
69-
$notification = new class extends Notification {
70-
public function toSendGrid($notifiable)
71-
{
72-
return (new SendGridMessage('sendgrid-template-id'))
73-
->from('test@example.com', 'Example User')
74-
->to('test+test1@example.com', 'Example User1')
75-
->replyTo('replyto@example.com', 'Reply To')
76-
->payload([
77-
'bar' => 'foo',
78-
'baz' => 'foo2',
79-
])
80-
->enableSandboxMode();
81-
}
82-
};
83-
84-
$notifiable = new class {
85-
use Notifiable;
86-
};
87-
88-
$channel = new SendGridChannel(
89-
$sendgrid = Mockery::mock(new SendGrid('x'))
90-
);
91-
92-
$response = Mockery::mock(Response::class);
93-
$response->shouldReceive('statusCode')->andReturn(200);
94-
95-
$message = $notification->toSendGrid($notifiable);
96-
97-
$this->assertEquals($message->templateId, 'sendgrid-template-id');
98-
$this->assertEquals($message->from->getEmail(), 'test@example.com');
99-
$this->assertEquals($message->from->getName(), 'Example User');
100-
$this->assertEquals($message->tos[0]->getEmail(), 'test+test1@example.com');
101-
$this->assertEquals($message->tos[0]->getName(), 'Example User1');
102-
$this->assertEquals($message->payload['bar'], 'foo');
103-
$this->assertEquals($message->payload['baz'], 'foo2');
104-
$this->assertEquals($message->replyTo->getEmail(), 'replyto@example.com');
105-
$this->assertEquals($message->replyTo->getName(), 'Reply To');
106-
$this->assertEquals($message->sandboxMode, true);
107-
108-
// TODO: Verify that the Mail instance passed contains all the info from above
109-
$sendgrid->shouldReceive('send')->once()->andReturn($response);
110-
111-
$channel->send($notifiable, $notification);
112-
}
11366
}

tests/SendGridMessageTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace NotificationChannels\SendGrid\Test;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use NotificationChannels\SendGrid\SendGridMessage;
7+
8+
class SendGridMessageTest extends TestCase
9+
{
10+
public function testDynamicVariables()
11+
{
12+
$message = new SendGridMessage('template-id');
13+
$message->payload($payload = [
14+
'bar' => 'string',
15+
'baz' => null,
16+
'foo' => [
17+
'nested' => 'array',
18+
'number' => 1235,
19+
'array' => [1, 2, 3],
20+
],
21+
]);
22+
23+
$mail = $message->build();
24+
$this->assertEquals(
25+
$payload,
26+
$mail->getPersonalization()->getDynamicTemplateData()
27+
);
28+
}
29+
30+
public function testSandboxMode()
31+
{
32+
$message = new SendGridMessage('template-id');
33+
$this->assertFalse($message->sandboxMode, 'Sandbox mode is turned off by default');
34+
$this->assertFalse(
35+
$message->build()->getMailSettings()->getSandboxMode()->getEnable(),
36+
'Sandbox mode is disabled in Sendgrid mail settings'
37+
);
38+
39+
$message->enableSandboxMode();
40+
$this->assertTrue($message->sandboxMode, 'Sandbox mode can be enabled');
41+
$this->assertTrue(
42+
$message->build()->getMailSettings()->getSandboxMode()->getEnable(),
43+
'Sandbox mode is enabled in Sendgrid mail settings'
44+
);
45+
46+
$message->setSandboxMode(false);
47+
$this->assertFalse($message->sandboxMode, 'Sandbox mode can be turned off');
48+
$this->assertFalse(
49+
$message->build()->getMailSettings()->getSandboxMode()->getEnable(),
50+
'Sandbox mode is disabled in Sendgrid mail settings'
51+
);
52+
}
53+
}

0 commit comments

Comments
 (0)