Skip to content

Commit 88ff336

Browse files
committed
feat: add event support
1 parent 4870d1b commit 88ff336

File tree

8 files changed

+53
-4
lines changed

8 files changed

+53
-4
lines changed

packages/mailer/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"symfony/amazon-mailer": "^7.2.0"
1515
},
1616
"suggest": {
17-
"tempest/storage": "For storage attachment support"
17+
"tempest/storage": "For storage attachment support",
18+
"tempest/event-bus": "For event support"
1819
},
1920
"autoload": {
2021
"psr-4": {

packages/mailer/src/EmailSent.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Tempest\Mail;
4+
5+
/**
6+
* An email has been sent.
7+
*/
8+
final readonly class EmailSent
9+
{
10+
public function __construct(
11+
public Email $email,
12+
) {}
13+
}

packages/mailer/src/GenericMailer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tempest\Mail;
44

55
use Symfony\Component\Mailer\Transport\TransportInterface;
6+
use Tempest\EventBus\EventBus;
67
use Tempest\Mail\Exceptions\MailerTransportWasMissing;
78
use Tempest\Mail\MailerConfig;
89
use Tempest\View\ViewRenderer;
@@ -17,6 +18,7 @@ final class GenericMailer implements Mailer
1718
public function __construct(
1819
private MailerConfig $mailerConfig,
1920
private ViewRenderer $viewRenderer,
21+
private ?EventBus $eventBus = null,
2022
private ?TransportInterface $transport = null,
2123
) {
2224
$this->transport ??= $this->createTransport();
@@ -28,6 +30,8 @@ public function send(Email $email): SentEmail
2830
->with(fn (Email $from) => new EmailToSymfonyEmailMapper($this->mailerConfig, $this->viewRenderer)->map($from, null))
2931
->do();
3032

33+
$this->eventBus?->dispatch(new EmailSent($email));
34+
3135
return new SentGenericEmail(
3236
original: $email,
3337
symfonyEmail: $symfonyEmail,

packages/mailer/src/MailerInitializer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Tempest\Container\Container;
66
use Tempest\Container\Initializer;
77
use Tempest\Container\Singleton;
8+
use Tempest\EventBus\EventBus;
89
use Tempest\Mail\MailerConfig;
910
use Tempest\View\ViewRenderer;
1011

@@ -16,6 +17,7 @@ public function initialize(Container $container): Mailer
1617
return new GenericMailer(
1718
mailerConfig: $container->get(MailerConfig::class),
1819
viewRenderer: $container->get(ViewRenderer::class),
20+
eventBus: $container->get(EventBus::class),
1921
);
2022
}
2123
}

packages/mailer/src/Testing/MailerTester.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPUnit\Framework\Assert;
88
use PHPUnit\Framework\ExpectationFailedException;
99
use Tempest\Container\Container;
10+
use Tempest\EventBus\EventBus;
1011
use Tempest\Mail\Email;
1112
use Tempest\Mail\Mailer;
1213
use Tempest\Mail\MailerConfig;
@@ -29,6 +30,7 @@ public function preventSendingEmails(): void
2930
$this->mailer ??= new TestingMailer(
3031
mailerConfig: $this->container->get(MailerConfig::class),
3132
viewRenderer: $this->container->get(ViewRenderer::class),
33+
eventBus: $this->container->get(EventBus::class),
3234
);
3335

3436
$this->container->singleton(Mailer::class, $this->mailer);

packages/mailer/src/Testing/TestingMailer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Tempest\Mail\Testing;
44

5+
use Tempest\EventBus\EventBus;
56
use Tempest\Mail\Email;
7+
use Tempest\Mail\EmailSent;
68
use Tempest\Mail\EmailToSymfonyEmailMapper;
79
use Tempest\Mail\Mailer;
810
use Tempest\Mail\MailerConfig;
@@ -15,6 +17,7 @@ final class TestingMailer implements Mailer
1517
public function __construct(
1618
private readonly MailerConfig $mailerConfig,
1719
private readonly ViewRenderer $viewRenderer,
20+
private readonly ?EventBus $eventBus = null,
1821
) {}
1922

2023
/**
@@ -32,6 +35,8 @@ public function send(Email $email): SentTestingEmail
3235
->with(fn (Email $from) => new EmailToSymfonyEmailMapper($this->mailerConfig, $this->viewRenderer)->map($from, null))
3336
->do();
3437

38+
$this->eventBus?->dispatch(new EmailSent($email));
39+
3540
return new SentTestingEmail(
3641
original: $email,
3742
symfonyEmail: $symfonyEmail,

tests/Integration/Mailer/MailerTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Tests\Tempest\Integration\Mailer;
44

5+
use Tempest\EventBus\EventBus;
56
use Tempest\Mail\Attachment;
7+
use Tempest\Mail\EmailSent;
68
use Tempest\Mail\Exceptions\RecipientWasMissing;
79
use Tempest\Mail\Exceptions\SenderWasMissing;
810
use Tempest\Mail\GenericEmail;
@@ -16,6 +18,26 @@ final class MailerTest extends FrameworkIntegrationTestCase
1618
get => $this->container->get(Mailer::class);
1719
}
1820

21+
public function test_event(): void
22+
{
23+
$sent = false;
24+
25+
$this->container
26+
->get(EventBus::class)
27+
->listen(function (EmailSent $event) use (&$sent): void {
28+
$sent = $event;
29+
});
30+
31+
$this->mailer->send(new GenericEmail(
32+
subject: 'Hello',
33+
34+
35+
text: 'Hello Jon',
36+
));
37+
38+
$this->assertInstanceOf(EmailSent::class, $sent);
39+
}
40+
1941
public function test_default_sender(): void
2042
{
2143
$this->mail->allowSendingEmails();

tests/Integration/Mailer/MailerTesterTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ public function test_assert_sent_with_class_string_and_callback(): void
5959
$this->expectExceptionMessage("Email `Tests\Tempest\Integration\Mailer\Fixtures\TextOnlyEmail` was sent but failed the assertion.");
6060

6161
$this->mailer->send(new TextOnlyEmail());
62-
$this->mail->assertSent(TextOnlyEmail::class, fn (Email $email) => false);
62+
$this->mail->assertSent(TextOnlyEmail::class, fn (Email $_email) => false);
6363
}
6464

6565
public function test_assert_sent_with_class_string_and_truthy_callback(): void
6666
{
6767
$this->mailer->send(new TextOnlyEmail());
68-
$this->mail->assertSent(TextOnlyEmail::class, fn (Email $email) => true);
68+
$this->mail->assertSent(TextOnlyEmail::class, fn (Email $_email) => true);
6969
}
7070

7171
public function test_assert_not_sent_with_class_string(): void
@@ -96,7 +96,7 @@ public function test_assertions(): void
9696
],
9797
));
9898

99-
$this->mail->assertSent(GenericEmail::class, function (Email $email) {
99+
$this->mail->assertSent(GenericEmail::class, function (Email $email): void {
100100
$this->assertCount(1, $email->content->attachments);
101101
$this->assertSame('hello!', ($email->content->attachments[0]->resolve)());
102102
});

0 commit comments

Comments
 (0)