From fd083e0e113d44dd0f5516e46c8df4e686f10f40 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Tue, 5 Oct 2021 12:24:26 -0400 Subject: [PATCH] [feature] add ability to reset collected emails --- README.md | 6 +++++- src/TestMailer.php | 22 ++++++++++++++++++--- tests/InteractsWithMailerTest.php | 29 +++++++++++++++++++++++++++- tests/NonInteractsWithMailerTest.php | 15 ++++++++++++++ 4 files changed, 67 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3340e97..8605007 100644 --- a/README.md +++ b/README.md @@ -63,11 +63,15 @@ class MyTest extends KernelTestCase // or WebTestCase // Any \Symfony\Component\Mime\Email methods can be used $this->assertSame('value', $email->getHeaders()->get('X-SOME-HEADER')->getBodyAsString()); }); + + // reset collected emails + $this->mailer()->reset(); } } ``` -**NOTE**: Emails are persisted between kernel reboots within each test. +**NOTE**: Emails are persisted between kernel reboots within each test. You can reset the +collected emails with `$this->mailer()->reset()`. ### Custom TestEmail diff --git a/src/TestMailer.php b/src/TestMailer.php index b9295c0..00867fd 100644 --- a/src/TestMailer.php +++ b/src/TestMailer.php @@ -43,9 +43,7 @@ public static function add(MessageEvent $event): void public function sentEmails(): SentEmails { - if (!self::$events) { - throw new \LogicException('Cannot access sent emails as email collection has not yet been started.'); - } + self::ensureStarted(); return SentEmails::fromEvents(self::$events); } @@ -63,4 +61,22 @@ public function sentTestEmails(string $testEmailClass = TestEmail::class): array return \array_map(static fn(Email $email) => new $testEmailClass($email), $this->sentEmails()->all()); } + + /** + * Reset the collected emails. + */ + public function reset(): self + { + self::ensureStarted(); + self::start(); + + return $this; + } + + private static function ensureStarted(): void + { + if (!self::$events) { + throw new \LogicException('Cannot access sent emails as email collection has not yet been started.'); + } + } } diff --git a/tests/InteractsWithMailerTest.php b/tests/InteractsWithMailerTest.php index c7a28d3..83d23fe 100644 --- a/tests/InteractsWithMailerTest.php +++ b/tests/InteractsWithMailerTest.php @@ -190,6 +190,33 @@ public function emails_are_persisted_between_reboots(string $environment): void $this->mailer()->assertSentEmailCount(1); } + /** + * @test + * @dataProvider environmentProvider + */ + public function can_reset_collected_emails(string $environment): void + { + self::bootKernel(['environment' => $environment]); + + $this->mailer()->assertNoEmailSent(); + + self::$container->get('mailer')->send(new Email1()); + + $this->mailer() + ->assertSentEmailCount(1) + ->reset() + ->assertNoEmailSent() + ; + + self::ensureKernelShutdown(); + + self::bootKernel(['environment' => $environment]); + + self::$container->get('mailer')->send(new Email1()); + + $this->mailer()->assertSentEmailCount(1); + } + /** * @test */ @@ -198,7 +225,7 @@ public function bundle_must_be_enabled(): void self::bootKernel(['environment' => 'no_bundle']); $this->expectException(\LogicException::class); - $this->expectExceptionMessage(\sprintf('Cannot access collected emails - is %s enabled in your test environment?', ZenstruckMailerTestBundle::class)); + $this->expectExceptionMessage(\sprintf('Cannot access test mailer - is %s enabled in your test environment?', ZenstruckMailerTestBundle::class)); $this->mailer(); } diff --git a/tests/NonInteractsWithMailerTest.php b/tests/NonInteractsWithMailerTest.php index b52d2c7..d5c4fd9 100644 --- a/tests/NonInteractsWithMailerTest.php +++ b/tests/NonInteractsWithMailerTest.php @@ -24,4 +24,19 @@ public function ensure_emails_are_not_collected(): void self::$container->get('zenstruck_mailer_test.mailer')->sentEmails(); } + + /** + * @test + */ + public function cannot_reset_if_collection_not_yet_started(): void + { + self::bootKernel(); + + self::$container->get('mailer')->send(new Email1()); + + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('Cannot access sent emails as email collection has not yet been started.'); + + self::$container->get('zenstruck_mailer_test.mailer')->reset(); + } }