From 890f362aeca3ae2aed4a66d58b69013c775da7e2 Mon Sep 17 00:00:00 2001 From: brodyhollins Date: Fri, 12 Aug 2022 14:19:55 +0200 Subject: [PATCH 1/7] feat: Add sandbox to SendGrideMessage --- src/SendGridMessage.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/SendGridMessage.php b/src/SendGridMessage.php index 9c7f7b4..2cd5294 100644 --- a/src/SendGridMessage.php +++ b/src/SendGridMessage.php @@ -42,6 +42,11 @@ class SendGridMessage */ public $payload = []; + /** + * The sandbox mode for SendGrid + */ + public $sandbox_mode = false; + /** * Create a new SendGrid channel instance. * @@ -110,10 +115,24 @@ public function build(): Mail $email->setTemplateId($this->templateId); + if($this->sandbox_mode){ + $email->enableSandBoxMode(); + } + foreach ($this->payload as $key => $value) { $email->addDynamicTemplateData((string) $key, (string) $value); } return $email; } + + /** + * Set the "sandbox_mode". + */ + public function enableSandboxMode($enabled): SendGridMessage + { + $this->sandbox_mode = $enabled; + + return $this; + } } From 2fc6da69f5cec6658e4eb74c33ba148391a22020 Mon Sep 17 00:00:00 2001 From: brodyhollins Date: Fri, 12 Aug 2022 14:29:01 +0200 Subject: [PATCH 2/7] feat: Add sandbox to SendGrideMessage Test --- tests/SendGridChannelTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/SendGridChannelTest.php b/tests/SendGridChannelTest.php index b673ae8..d815c4c 100644 --- a/tests/SendGridChannelTest.php +++ b/tests/SendGridChannelTest.php @@ -56,6 +56,7 @@ public function toSendGrid($notifiable) $this->assertEquals($message->payload['baz'], 'foo2'); $this->assertEquals($message->replyTo->getEmail(), 'replyto@example.com'); $this->assertEquals($message->replyTo->getName(), 'Reply To'); + $this->assertEquals($message->sandbox_mode, false); // TODO: Verify that the Mail instance passed contains all the info from above $sendgrid->shouldReceive('send')->once()->andReturn($response); From 633357e43337b437b0290276267621f9ad48f557 Mon Sep 17 00:00:00 2001 From: brodyhollins Date: Fri, 12 Aug 2022 14:42:11 +0200 Subject: [PATCH 3/7] feat: Updated read me for new implementation --- README.md | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/README.md b/README.md index 70fc12d..7fc43b5 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,61 @@ 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');` +## Sandbox Mode + +To enable sandbox mode you will need to + +1. Chain on the `enableSandboxMode(value)` to the `new SendGridMessage('template_id')` + +Example: + +```php +from('no-reply@test.com', 'App name') + */ + /** + * optionally set the recipient. + * by default it's $notifiable->email: + * ->to('hello@example.com', 'Mr. Smith') + */ + + ->enableSandboxMode(true) + ->payload([ + "template_var_1" => "template_value_1" + ]); + } +} + +``` + +`NotificationChannels\SendGrid\SendGridMessage` instance will enable sandbox mode. + +💡 You can pass through a config value into the `->enableSandboxMode(config('key'))` + ## Changelog Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. From 26e3a57f742562f91bc04396a2587012d6e6243a Mon Sep 17 00:00:00 2001 From: brodyhollins Date: Fri, 12 Aug 2022 15:00:16 +0200 Subject: [PATCH 4/7] feat: Updated test to include for sandboxmode of true and updated read me --- README.md | 6 +---- tests/SendGridChannelTest.php | 47 +++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 7fc43b5..7d87182 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ class ExampleNotification extends Notification To enable sandbox mode you will need to -1. Chain on the `enableSandboxMode(value)` to the `new SendGridMessage('template_id')` +1. Chain on the `enableSandboxMode(bool)` to the `new SendGridMessage('template_id')` Example: @@ -150,10 +150,6 @@ class ExampleNotification extends Notification ``` -`NotificationChannels\SendGrid\SendGridMessage` instance will enable sandbox mode. - -💡 You can pass through a config value into the `->enableSandboxMode(config('key'))` - ## Changelog Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. diff --git a/tests/SendGridChannelTest.php b/tests/SendGridChannelTest.php index d815c4c..e3dd906 100644 --- a/tests/SendGridChannelTest.php +++ b/tests/SendGridChannelTest.php @@ -63,4 +63,51 @@ 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('test@example.com', 'Example User') + ->to('test+test1@example.com', 'Example User1') + ->replyTo('replyto@example.com', 'Reply To') + ->payload([ + 'bar' => 'foo', + 'baz' => 'foo2', + ]) + ->enableSandboxMode(true); + } + }; + + $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(), 'test@example.com'); + $this->assertEquals($message->from->getName(), 'Example User'); + $this->assertEquals($message->tos[0]->getEmail(), 'test+test1@example.com'); + $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(), 'replyto@example.com'); + $this->assertEquals($message->replyTo->getName(), 'Reply To'); + $this->assertEquals($message->sandbox_mode, true); + + // TODO: Verify that the Mail instance passed contains all the info from above + $sendgrid->shouldReceive('send')->once()->andReturn($response); + + $channel->send($notifiable, $notification); + } } From f38c92567790953d3f8468991ff86e16a8c79bb5 Mon Sep 17 00:00:00 2001 From: brodyhollins Date: Fri, 12 Aug 2022 15:02:09 +0200 Subject: [PATCH 5/7] feat: updated readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7d87182..f8d122e 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ class ExampleNotification extends Notification To enable sandbox mode you will need to -1. Chain on the `enableSandboxMode(bool)` to the `new SendGridMessage('template_id')` +1. Chain on the `enableSandboxMode(true)` to the `new SendGridMessage('template_id')` Example: From a6facca889598a7941896046b1ae79e523b0e2cf Mon Sep 17 00:00:00 2001 From: brodyhollins Date: Fri, 12 Aug 2022 15:21:18 +0200 Subject: [PATCH 6/7] refactor: updated changes to match conventions --- src/SendGridMessage.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/SendGridMessage.php b/src/SendGridMessage.php index 2cd5294..2dafc76 100644 --- a/src/SendGridMessage.php +++ b/src/SendGridMessage.php @@ -44,6 +44,8 @@ class SendGridMessage /** * The sandbox mode for SendGrid + * + * @var bool */ public $sandbox_mode = false; @@ -128,8 +130,10 @@ public function build(): Mail /** * Set the "sandbox_mode". + * @param bool $enabled + * @return $this */ - public function enableSandboxMode($enabled): SendGridMessage + public function enableSandboxMode($enabled) { $this->sandbox_mode = $enabled; From 9608846f5d82810c6489e43337eb1200e9ec4c6a Mon Sep 17 00:00:00 2001 From: brodyhollins Date: Fri, 12 Aug 2022 15:23:48 +0200 Subject: [PATCH 7/7] refactor: updated changes to match conventions --- src/SendGridMessage.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/SendGridMessage.php b/src/SendGridMessage.php index 2dafc76..6ee2e45 100644 --- a/src/SendGridMessage.php +++ b/src/SendGridMessage.php @@ -130,6 +130,7 @@ public function build(): Mail /** * Set the "sandbox_mode". + * * @param bool $enabled * @return $this */