Skip to content

Commit 697038a

Browse files
authored
[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
1 parent cefd840 commit 697038a

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

src/Config/NotificationMailConfig.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class NotificationMailConfig extends Data
99
{
1010
protected function __construct(
11-
public string $to,
11+
public string|array $to,
1212
public NotificationMailSenderConfig $from,
1313
) {}
1414

@@ -19,8 +19,12 @@ protected function __construct(
1919
*/
2020
public static function fromArray(array $data): self
2121
{
22-
if (! filter_var($data['to'], FILTER_VALIDATE_EMAIL)) {
23-
throw InvalidConfig::invalidEmail($data['to']);
22+
$to = is_array($data['to']) ? $data['to'] : [$data['to']];
23+
24+
foreach ($to as $email) {
25+
if (! filter_var($email, FILTER_VALIDATE_EMAIL)) {
26+
throw InvalidConfig::invalidEmail($email);
27+
}
2428
}
2529

2630
return new self(

tests/Notifications/EventHandlerTest.php

+54
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use Spatie\Backup\Events\BackupHasFailed;
77
use Spatie\Backup\Notifications\Notifiable;
88
use Spatie\Backup\Notifications\Notifications\BackupHasFailedNotification;
9+
use Spatie\Backup\Exceptions\InvalidConfig;
10+
use Spatie\Backup\Config\NotificationMailConfig;
911

1012
beforeEach(function () {
1113
Notification::fake();
@@ -50,6 +52,58 @@
5052
Notification::assertSentTimes(BackupHasFailedNotification::class, 1);
5153
});
5254

55+
it('will accept a single email address', function () {
56+
$data = [
57+
'to' => '[email protected]',
58+
'from' => [
59+
'address' => '[email protected]',
60+
'name' => 'Backup',
61+
],
62+
];
63+
64+
$config = NotificationMailConfig::fromArray($data);
65+
66+
expect($config->to)->toBe(['[email protected]']);
67+
});
68+
69+
it('will accept multiple email addresses', function () {
70+
$data = [
71+
72+
'from' => [
73+
'address' => '[email protected]',
74+
'name' => 'Backup',
75+
],
76+
];
77+
78+
$config = NotificationMailConfig::fromArray($data);
79+
80+
expect($config->to)->toBe(['[email protected]', '[email protected]']);
81+
});
82+
83+
it('will throw an exception for invalid email', function () {
84+
$data = [
85+
'to' => 'invalid-email',
86+
'from' => [
87+
'address' => '[email protected]',
88+
'name' => 'Backup',
89+
],
90+
];
91+
92+
expect(fn() => NotificationMailConfig::fromArray($data))->toThrow(InvalidConfig::class);
93+
});
94+
95+
it('will throw an exception for invalid email in array', function () {
96+
$data = [
97+
'to' => ['[email protected]', 'invalid-email'],
98+
'from' => [
99+
'address' => '[email protected]',
100+
'name' => 'Backup',
101+
],
102+
];
103+
104+
expect(fn() => NotificationMailConfig::fromArray($data))->toThrow(InvalidConfig::class);
105+
});
106+
53107
function fireBackupHasFailedEvent(): void
54108
{
55109
$exception = new Exception('Dummy exception');

0 commit comments

Comments
 (0)