Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fake argument #24

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/Concerns/UsesMailcoachMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\MailcoachMailer\Concerns;

use Spatie\MailcoachMailer\Headers\FakeHeader;
use Spatie\MailcoachMailer\Headers\MailerHeader;
use Spatie\MailcoachMailer\Headers\ReplacementHeader;
use Spatie\MailcoachMailer\Headers\TransactionalMailHeader;
Expand All @@ -12,14 +13,15 @@ trait UsesMailcoachMail
{
private bool $usingMailcoachMail = false;

public function mailcoachMail(string $mailName, array $replacements = [], ?string $mailer = null): self
public function mailcoachMail(string $mailName, array $replacements = [], ?string $mailer = null, ?bool $fake = null): self
{
$this->usingMailcoachMail = true;

$this->html = 'use-mailcoach-mail';

$this->replacing($replacements);
$this->usingMailer($mailer);
$this->faking($fake);

$this->withSymfonyMessage(function (Email $email) use ($mailName) {
$transactionalHeader = new TransactionalMailHeader($mailName);
Expand Down Expand Up @@ -70,6 +72,25 @@ public function replacing(array|string $key, string|array|null $value = null): s
return $this;
}

public function faking(?bool $value): self
{
if (! $value) {
return $this;
}

$this->withSymfonyMessage(function (Email $email) use ($value) {
$fakeHeader = new FakeHeader($value);

if ($email->getHeaders()->has($fakeHeader->getName())) {
$email->getHeaders()->remove($fakeHeader->getName());
}

$email->getHeaders()->add($fakeHeader);
});

return $this;
}

protected function buildSubject($message): self
{
if (! $this->usingMailcoachMail) {
Expand Down
20 changes: 20 additions & 0 deletions src/Notifications/MailcoachMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Arr;
use Spatie\MailcoachMailer\Headers\FakeHeader;
use Spatie\MailcoachMailer\Headers\MailerHeader;
use Spatie\MailcoachMailer\Headers\ReplacementHeader;
use Spatie\MailcoachMailer\Headers\TransactionalMailHeader;
Expand All @@ -15,6 +16,8 @@ class MailcoachMessage extends MailMessage

public array $replacements = [];

public bool $fake = false;

public function usingMail(string $mailName): self
{
$this->mailName = $mailName;
Expand Down Expand Up @@ -65,4 +68,21 @@ public function replacing(array|string $key, string|array|null $value = null): s

return $this;
}

public function faking(bool $value): self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

{
$this->fake = $value;

$this->withSymfonyMessage(function (Email $email) use ($value) {
$fakeHeader = new FakeHeader($mailer);

if ($email->getHeaders()->has($fakeHeader->getName())) {
$email->getHeaders()->remove($fakeHeader->getName());
}

$email->getHeaders()->add($fakeHeader);
});

return $this;
}
}
Loading