From defecfa1ab006b77cbd0f1b96bbb8edb76bcdf98 Mon Sep 17 00:00:00 2001 From: zbrody <102783598+zbrody@users.noreply.github.com> Date: Fri, 12 Aug 2022 15:54:58 +0200 Subject: [PATCH] Add Sandbox Mode onto SendGridMessage (#3) * feat: Add sandbox to SendGrideMessage * feat: Add sandbox to SendGrideMessage Test * feat: Updated read me for new implementation * feat: Updated test to include for sandboxmode of true and updated read me * feat: updated readme * refactor: updated changes to match conventions * refactor: updated changes to match conventions --- README.md | 51 +++++++++++++++++++++++++++++++++++ src/SendGridMessage.php | 24 +++++++++++++++++ tests/SendGridChannelTest.php | 48 +++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+) diff --git a/README.md b/README.md index f63d085..342f4df 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,57 @@ 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(true)` 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" + ]); + } +} + +``` + ## Changelog Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. diff --git a/src/SendGridMessage.php b/src/SendGridMessage.php index 9c7f7b4..6ee2e45 100644 --- a/src/SendGridMessage.php +++ b/src/SendGridMessage.php @@ -42,6 +42,13 @@ class SendGridMessage */ public $payload = []; + /** + * The sandbox mode for SendGrid + * + * @var bool + */ + public $sandbox_mode = false; + /** * Create a new SendGrid channel instance. * @@ -110,10 +117,27 @@ 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". + * + * @param bool $enabled + * @return $this + */ + public function enableSandboxMode($enabled) + { + $this->sandbox_mode = $enabled; + + return $this; + } } diff --git a/tests/SendGridChannelTest.php b/tests/SendGridChannelTest.php index b673ae8..e3dd906 100644 --- a/tests/SendGridChannelTest.php +++ b/tests/SendGridChannelTest.php @@ -56,6 +56,54 @@ 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); + + $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);