From 697038a530ffea30d84717cf753c3fed46ff2335 Mon Sep 17 00:00:00 2001 From: Ruslan Voroshchuk Date: Thu, 4 Jul 2024 10:00:36 +0300 Subject: [PATCH] [9.x] Add support for both single email string and array of email strings (#1810) * Add support for both single email string and array of email strings * Add tests to handle single and multiple email addresses --- src/Config/NotificationMailConfig.php | 10 +++-- tests/Notifications/EventHandlerTest.php | 54 ++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/src/Config/NotificationMailConfig.php b/src/Config/NotificationMailConfig.php index 65c04231..87ffca16 100644 --- a/src/Config/NotificationMailConfig.php +++ b/src/Config/NotificationMailConfig.php @@ -8,7 +8,7 @@ class NotificationMailConfig extends Data { protected function __construct( - public string $to, + public string|array $to, public NotificationMailSenderConfig $from, ) {} @@ -19,8 +19,12 @@ protected function __construct( */ public static function fromArray(array $data): self { - if (! filter_var($data['to'], FILTER_VALIDATE_EMAIL)) { - throw InvalidConfig::invalidEmail($data['to']); + $to = is_array($data['to']) ? $data['to'] : [$data['to']]; + + foreach ($to as $email) { + if (! filter_var($email, FILTER_VALIDATE_EMAIL)) { + throw InvalidConfig::invalidEmail($email); + } } return new self( diff --git a/tests/Notifications/EventHandlerTest.php b/tests/Notifications/EventHandlerTest.php index bcfb1aee..d46adb5d 100644 --- a/tests/Notifications/EventHandlerTest.php +++ b/tests/Notifications/EventHandlerTest.php @@ -6,6 +6,8 @@ use Spatie\Backup\Events\BackupHasFailed; use Spatie\Backup\Notifications\Notifiable; use Spatie\Backup\Notifications\Notifications\BackupHasFailedNotification; +use Spatie\Backup\Exceptions\InvalidConfig; +use Spatie\Backup\Config\NotificationMailConfig; beforeEach(function () { Notification::fake(); @@ -50,6 +52,58 @@ Notification::assertSentTimes(BackupHasFailedNotification::class, 1); }); +it('will accept a single email address', function () { + $data = [ + 'to' => 'single@example.com', + 'from' => [ + 'address' => 'from@example.com', + 'name' => 'Backup', + ], + ]; + + $config = NotificationMailConfig::fromArray($data); + + expect($config->to)->toBe(['single@example.com']); +}); + +it('will accept multiple email addresses', function () { + $data = [ + 'to' => ['first@example.com', 'second@example.com'], + 'from' => [ + 'address' => 'from@example.com', + 'name' => 'Backup', + ], + ]; + + $config = NotificationMailConfig::fromArray($data); + + expect($config->to)->toBe(['first@example.com', 'second@example.com']); +}); + +it('will throw an exception for invalid email', function () { + $data = [ + 'to' => 'invalid-email', + 'from' => [ + 'address' => 'from@example.com', + 'name' => 'Backup', + ], + ]; + + expect(fn() => NotificationMailConfig::fromArray($data))->toThrow(InvalidConfig::class); +}); + +it('will throw an exception for invalid email in array', function () { + $data = [ + 'to' => ['valid@example.com', 'invalid-email'], + 'from' => [ + 'address' => 'from@example.com', + 'name' => 'Backup', + ], + ]; + + expect(fn() => NotificationMailConfig::fromArray($data))->toThrow(InvalidConfig::class); +}); + function fireBackupHasFailedEvent(): void { $exception = new Exception('Dummy exception');