Skip to content

Commit

Permalink
[9.x] Add support for both single email string and array of email str…
Browse files Browse the repository at this point in the history
…ings (#1810)

* Add support for both single email string and array of email strings

* Add tests to handle single and multiple email addresses
  • Loading branch information
vrusua authored Jul 4, 2024
1 parent cefd840 commit 697038a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Config/NotificationMailConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class NotificationMailConfig extends Data
{
protected function __construct(

Check failure on line 10 in src/Config/NotificationMailConfig.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Spatie\Backup\Config\NotificationMailConfig::__construct() has parameter $to with no value type specified in iterable type array.
public string $to,
public string|array $to,
public NotificationMailSenderConfig $from,
) {}

Expand All @@ -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(
Expand Down
54 changes: 54 additions & 0 deletions tests/Notifications/EventHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -50,6 +52,58 @@
Notification::assertSentTimes(BackupHasFailedNotification::class, 1);
});

it('will accept a single email address', function () {
$data = [
'to' => '[email protected]',
'from' => [
'address' => '[email protected]',
'name' => 'Backup',
],
];

$config = NotificationMailConfig::fromArray($data);

expect($config->to)->toBe(['[email protected]']);
});

it('will accept multiple email addresses', function () {
$data = [
'to' => ['[email protected]', '[email protected]'],
'from' => [
'address' => '[email protected]',
'name' => 'Backup',
],
];

$config = NotificationMailConfig::fromArray($data);

expect($config->to)->toBe(['[email protected]', '[email protected]']);
});

it('will throw an exception for invalid email', function () {
$data = [
'to' => 'invalid-email',
'from' => [
'address' => '[email protected]',
'name' => 'Backup',
],
];

expect(fn() => NotificationMailConfig::fromArray($data))->toThrow(InvalidConfig::class);
});

it('will throw an exception for invalid email in array', function () {
$data = [
'to' => ['[email protected]', 'invalid-email'],
'from' => [
'address' => '[email protected]',
'name' => 'Backup',
],
];

expect(fn() => NotificationMailConfig::fromArray($data))->toThrow(InvalidConfig::class);
});

function fireBackupHasFailedEvent(): void
{
$exception = new Exception('Dummy exception');
Expand Down

0 comments on commit 697038a

Please sign in to comment.