From 7e9f46ec514620f5c08dd247e87c7a0e633ab571 Mon Sep 17 00:00:00 2001 From: Dumazeau Date: Fri, 28 Apr 2023 14:07:24 +0200 Subject: [PATCH] Add Email class --- src/Domain/Email.php | 24 ++++++++++++++ src/Domain/Updater/FooterEmailUpdater.php | 9 ++++-- tests/SendEmailTest.php | 39 ++++++++++++++++++----- 3 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 src/Domain/Email.php diff --git a/src/Domain/Email.php b/src/Domain/Email.php new file mode 100644 index 0000000..79e211e --- /dev/null +++ b/src/Domain/Email.php @@ -0,0 +1,24 @@ +isFooterDisabled; + } + + public function setIsFooterDisabled(bool $isFooterDisabled): self + { + $this->isFooterDisabled = $isFooterDisabled; + + return $this; + } +} diff --git a/src/Domain/Updater/FooterEmailUpdater.php b/src/Domain/Updater/FooterEmailUpdater.php index d2be052..2df01a8 100644 --- a/src/Domain/Updater/FooterEmailUpdater.php +++ b/src/Domain/Updater/FooterEmailUpdater.php @@ -4,8 +4,9 @@ namespace RichId\MailerBundle\Domain\Updater; +use RichId\MailerBundle\Domain\Email; use RichId\MailerBundle\Domain\EmailFooter\EmailFooterManager; -use Symfony\Component\Mime\Email; +use Symfony\Component\Mime\Email as SymfonyEmail; use Symfony\Contracts\Service\Attribute\Required; final class FooterEmailUpdater @@ -13,12 +14,14 @@ final class FooterEmailUpdater #[Required] public EmailFooterManager $emailFooterManager; - public function __invoke(Email $email): void + public function __invoke(SymfonyEmail $email): void { $footer = $this->emailFooterManager->getFooter(); $htmlBody = $email->getHtmlBody(); - if ($footer === null || $footer === '' || $htmlBody === null || $htmlBody === '') { + $forceDisabled = $email instanceof Email && $email->isFooterDisabled(); + + if ($forceDisabled || $footer === null || $footer === '' || $htmlBody === null || $htmlBody === '') { return; } diff --git a/tests/SendEmailTest.php b/tests/SendEmailTest.php index 4ba04ce..3c3592c 100644 --- a/tests/SendEmailTest.php +++ b/tests/SendEmailTest.php @@ -6,10 +6,11 @@ use RichCongress\TestFramework\TestConfiguration\Annotation\TestConfig; use RichCongress\TestSuite\TestCase\TestCase; +use RichId\MailerBundle\Domain\Email; use RichId\MailerBundle\Infrastructure\TestCase\MailerAssertionsTrait; use RichId\MailerBundle\Tests\Resources\Stub\ParameterBagStub; use Symfony\Component\Mailer\MailerInterface; -use Symfony\Component\Mime\Email; +use Symfony\Component\Mime\Email as SymfonyEmail; use Symfony\Component\Mime\Exception\LogicException; use Symfony\Component\Mime\Message; @@ -52,7 +53,7 @@ public function testSendEmailMinimumConfiguration(): void 'rich_id_mailer.automatic_add_footer' => false, ]; - $email = new Email(); + $email = new SymfonyEmail(); $email->to('test@test.test'); $email->html('test'); @@ -78,7 +79,7 @@ public function testSendEmailWithReturnPath(): void 'rich_id_mailer.return_path_address' => 'bounces@test.test', ]; - $email = new Email(); + $email = new SymfonyEmail(); $email->to('test@test.test'); $email->html('test'); @@ -104,7 +105,7 @@ public function testSendEmailWithBcc(): void 'rich_id_mailer.bcc_address' => 'bcc@test.test', ]; - $email = new Email(); + $email = new SymfonyEmail(); $email->to('test@test.test'); $email->html('test'); @@ -130,7 +131,7 @@ public function testSendEmailWithYopmailTransformer(): void 'rich_id_mailer.transformation_type' => 'yopmail', ]; - $email = new Email(); + $email = new SymfonyEmail(); $email->to('test@test.test'); $email->bcc('bcc@test.test'); $email->html('test'); @@ -158,7 +159,7 @@ public function testSendEmailWithBccTransformer(): void 'rich_id_mailer.bcc_address' => 'bcc@bcc.bcc', ]; - $email = new Email(); + $email = new SymfonyEmail(); $email->to('test@test.test'); $email->cc('cc@test.test'); $email->bcc('bcc@test.test'); @@ -189,7 +190,7 @@ public function testSendEmailWithSubjectPrefix(): void 'rich_id_mailer.subject_prefix' => 'My prefix -', ]; - $email = new Email(); + $email = new SymfonyEmail(); $email->to('test@test.test'); $email->subject('My subject'); $email->html('test'); @@ -211,7 +212,7 @@ public function testSendEmailWithSubjectPrefix(): void public function testSendEmailWithFooter(): void { - $email = new Email(); + $email = new SymfonyEmail(); $email->to('test@test.test'); $email->html('test'); @@ -229,4 +230,26 @@ public function testSendEmailWithFooter(): void self::assertEmpty($email->getBcc()); self::assertEmpty($email->getAttachments()); } + + public function testSendEmailWithFooterButDisabled(): void + { + $email = new Email(); + $email->to('test@test.test'); + $email->html('test'); + $email->setIsFooterDisabled(true); + + $this->mailer->send($email); + + $this->assertEmailCount(1, null, false); + $email = $this->getMailerMessages(null, false)[0]; + + self::assertEmailTo('test@test.test', $email); + self::assertEmailFrom('sender@test.test', $email); + self::assertEmailBody('test', $email); + self::assertEmailSubject(null, $email); + self::assertNull($email->getReturnPath()); + self::assertEmpty($email->getCc()); + self::assertEmpty($email->getBcc()); + self::assertEmpty($email->getAttachments()); + } }