Skip to content

Commit

Permalink
[feature] add ability to reset collected emails
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Oct 5, 2021
1 parent 58e3d20 commit fd083e0
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 19 additions & 3 deletions src/TestMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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.');
}
}
}
29 changes: 28 additions & 1 deletion tests/InteractsWithMailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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();
}
Expand Down
15 changes: 15 additions & 0 deletions tests/NonInteractsWithMailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit fd083e0

Please sign in to comment.