generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1fb4aaa
commit 4aa5100
Showing
3 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace Spatie\MailcoachMailer\Notifications; | ||
|
||
use Illuminate\Notifications\Messages\MailMessage; | ||
use Illuminate\Support\Arr; | ||
use Spatie\MailcoachMailer\Headers\MailerHeader; | ||
use Spatie\MailcoachMailer\Headers\ReplacementHeader; | ||
use Spatie\MailcoachMailer\Headers\TransactionalMailHeader; | ||
use Symfony\Component\Mime\Email; | ||
|
||
class MailcoachMessage extends MailMessage | ||
{ | ||
public string $mailName; | ||
|
||
public array $replacements = []; | ||
|
||
public function usingMail(string $mailName): self | ||
{ | ||
$this->mailName = $mailName; | ||
|
||
$this->withSymfonyMessage(function (Email $email) use ($mailName) { | ||
$transactionalHeader = new TransactionalMailHeader($mailName); | ||
|
||
if ($email->getHeaders()->has($transactionalHeader->getName())) { | ||
$email->getHeaders()->remove($transactionalHeader->getName()); | ||
} | ||
|
||
$email->getHeaders()->add($transactionalHeader); | ||
}); | ||
|
||
return $this; | ||
} | ||
|
||
public function usingMailer(string $mailer): self | ||
{ | ||
$this->withSymfonyMessage(function (Email $email) use ($mailer) { | ||
$mailerHeader = new MailerHeader($mailer); | ||
|
||
if ($email->getHeaders()->has($mailerHeader->getName())) { | ||
$email->getHeaders()->remove($mailerHeader->getName()); | ||
} | ||
|
||
$email->getHeaders()->add($mailerHeader); | ||
}); | ||
|
||
return $this; | ||
} | ||
|
||
public function replacing(array|string $key, string|array|null $value = null): self | ||
{ | ||
if (is_array($key)) { | ||
foreach ($key as $realKey => $value) { | ||
$this->replacing($realKey, $value); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
Arr::set($this->replacements, $key, $value); | ||
|
||
$this->withSymfonyMessage(function (Email $email) use ($key, $value) { | ||
$email->getHeaders()->add(new ReplacementHeader($key, $value)); | ||
}); | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Notification; | ||
use Spatie\MailcoachMailer\Tests\TestSupport\Notifications\TestNotification; | ||
|
||
beforeEach(function () { | ||
config()->set('mail.default', 'mailcoach'); | ||
|
||
config()->set('mail.mailers.mailcoach', [ | ||
'transport' => 'mailcoach', | ||
'domain' => 'test.mailcoach.app', | ||
'token' => 'fake-token', | ||
]); | ||
}); | ||
|
||
it('can send a notification that makes use of a mailcoach mail template', function () { | ||
expectResponse(function (string $method, string $url, array $options) { | ||
expect($url)->toBe('https://test.mailcoach.app/api/transactional-mails/send'); | ||
expect($method)->toBe('POST'); | ||
|
||
expect($options['headers'][1])->toBe('Authorization: Bearer fake-token'); | ||
|
||
$body = json_decode($options['body'], true); | ||
expect($body['from'])->toBe('from@example.com'); | ||
expect($body['to'])->toBe('to@example.com'); | ||
expect($body['mail_name'])->toBe('mail-name'); | ||
|
||
expect($body['replacements'])->toBe([ | ||
'singleName' => 'singleValue', | ||
'multipleName' => 'multipleValue', | ||
]); | ||
}); | ||
|
||
Notification::route('mail', 'to@example.com')->notify(new TestNotification()); | ||
}); | ||
|
||
it('can send a notification that uses a different mailer', function () { | ||
expectResponse(function (string $method, string $url, array $options) { | ||
expect($url)->toBe('https://test.mailcoach.app/api/transactional-mails/send'); | ||
expect($method)->toBe('POST'); | ||
|
||
expect($options['headers'][1])->toBe('Authorization: Bearer fake-token'); | ||
|
||
$body = json_decode($options['body'], true); | ||
expect($body['mailer'])->toBe('transactional-mailer'); | ||
}); | ||
|
||
Notification::route('mail', 'to@example.com')->notify(new TestNotification()); | ||
}); | ||
|
||
it('can inspect mailcoach message', function () { | ||
$mailcoachMessage = (new TestNotification())->toMail(); | ||
|
||
expect($mailcoachMessage->mailName)->toBe('mail-name'); | ||
expect($mailcoachMessage->replacements)->toBe([ | ||
'singleName' => 'singleValue', | ||
'multipleName' => 'multipleValue', | ||
]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Spatie\MailcoachMailer\Tests\TestSupport\Notifications; | ||
|
||
use Illuminate\Notifications\Notification; | ||
use Spatie\MailcoachMailer\Notifications\MailcoachMessage; | ||
|
||
class TestNotification extends Notification | ||
{ | ||
public function via() : array | ||
{ | ||
return ['mail']; | ||
} | ||
|
||
public function toMail() | ||
{ | ||
return (new MailcoachMessage()) | ||
->usingMail('mail-name') | ||
->usingMailer('transactional-mailer') | ||
->replacing('singleName', 'singleValue') | ||
->replacing(['multipleName' => 'multipleValue']) | ||
->from('from@example.com'); | ||
} | ||
} |