Skip to content

Commit

Permalink
Add mailcoachmessage class
Browse files Browse the repository at this point in the history
  • Loading branch information
gdebrauwer committed Jul 23, 2024
1 parent 1fb4aaa commit 4aa5100
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/Notifications/MailcoachMessage.php
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;
}
}
59 changes: 59 additions & 0 deletions tests/Notifications/MailcoachMessageTest.php
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',
]);
});
24 changes: 24 additions & 0 deletions tests/TestSupport/Notifications/TestNotification.php
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');
}
}

0 comments on commit 4aa5100

Please sign in to comment.