Skip to content

Commit 8165f66

Browse files
committed
wip
1 parent 93a02c7 commit 8165f66

File tree

15 files changed

+244
-467
lines changed

15 files changed

+244
-467
lines changed

packages/mailer/src/EmailToSymfonyEmailMapper.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Tempest\Mail\Exceptions\SenderWasMissing;
1111
use Tempest\Mail\Transports\ProvidesDefaultSender;
1212
use Tempest\Mapper\Mapper;
13-
use Tempest\Support\Arr;
1413
use Tempest\View\View;
1514
use Tempest\View\ViewRenderer;
1615

packages/mailer/src/GenericMailer.php

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,27 @@
44

55
use Symfony\Component\Mailer\Transport\TransportInterface;
66
use Tempest\EventBus\EventBus;
7-
use Tempest\Mail\Exceptions\MailerTransportWasMissing;
8-
use Tempest\View\ViewRenderer;
97

108
use function Tempest\map;
119

1210
/**
1311
* Generic mailer based on Symfony transports.
1412
*/
15-
final class GenericMailer implements Mailer
13+
final readonly class GenericMailer implements Mailer
1614
{
1715
public function __construct(
18-
private readonly MailerConfig $mailerConfig,
19-
private readonly ViewRenderer $viewRenderer,
20-
private readonly ?EventBus $eventBus = null,
21-
private ?TransportInterface $transport = null,
22-
) {
23-
$this->transport ??= $this->createTransport();
24-
}
16+
private EventBus $eventBus,
17+
private TransportInterface $transport,
18+
) {}
2519

26-
public function send(Email $email): SentEmail
20+
public function send(Email $email): void
2721
{
28-
$symfonyEmail = map($email)
29-
->with(fn (Email $from) => new EmailToSymfonyEmailMapper($this->mailerConfig, $this->viewRenderer)->map($from, null))
22+
$symfonyMail = map($email)
23+
->with(EmailToSymfonyEmailMapper::class)
3024
->do();
3125

32-
$this->eventBus?->dispatch(new EmailWasSent($email));
33-
34-
return new SentGenericEmail(
35-
original: $email,
36-
symfonyEmail: $symfonyEmail,
37-
sent: $this->transport->send($symfonyEmail),
38-
);
39-
}
40-
41-
private function createTransport(): TransportInterface
42-
{
43-
$this->assertTransportInstalled($this->mailerConfig->transport);
44-
45-
return $this->mailerConfig->createTransport();
46-
}
26+
$this->transport->send($symfonyMail);
4727

48-
private function assertTransportInstalled(string $transport): void
49-
{
50-
if (! class_exists($transport)) {
51-
throw new MailerTransportWasMissing($transport);
52-
}
28+
$this->eventBus?->dispatch(new EmailWasSent($email));
5329
}
5430
}

packages/mailer/src/Mailer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ interface Mailer
1010
/**
1111
* Sends the given email.
1212
*/
13-
public function send(Email $email): SentEmail;
13+
public function send(Email $email): void;
1414
}

packages/mailer/src/MailerInitializer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Tempest\Container\Initializer;
77
use Tempest\Container\Singleton;
88
use Tempest\EventBus\EventBus;
9+
use Tempest\Mail\Exceptions\MailerTransportWasMissing;
910
use Tempest\Mail\MailerConfig;
1011
use Tempest\View\ViewRenderer;
1112

@@ -15,9 +16,8 @@ final class MailerInitializer implements Initializer
1516
public function initialize(Container $container): Mailer
1617
{
1718
return new GenericMailer(
18-
mailerConfig: $container->get(MailerConfig::class),
19-
viewRenderer: $container->get(ViewRenderer::class),
2019
eventBus: $container->get(EventBus::class),
20+
transport: $container->get(MailerConfig::class)->createTransport(),
2121
);
2222
}
2323
}

packages/mailer/src/SentEmail.php

Lines changed: 0 additions & 61 deletions
This file was deleted.

packages/mailer/src/SentGenericEmail.php

Lines changed: 0 additions & 59 deletions
This file was deleted.

packages/mailer/src/Testing/TestingAttachment.php renamed to packages/mailer/src/Testing/AttachmentTester.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
namespace Tempest\Mail\Testing;
44

55
use PHPUnit\Framework\Assert;
6-
use PHPUnit\Framework\ExpectationFailedException;
76
use Symfony\Component\Mime\Part\DataPart;
87

9-
final class TestingAttachment
8+
final class AttachmentTester
109
{
1110
/**
12-
* Headers associated to this attachment.
11+
* Headers associated with this attachment.
1312
*/
1413
public array $headers {
1514
get => $this->original->getHeaders()->toArray();
@@ -32,24 +31,40 @@ final class TestingAttachment
3231
/**
3332
* Type of this attachment.
3433
*/
35-
public string $type {
34+
public string $mediaType {
3635
get => $this->original->getMediaType();
3736
}
3837

3938
public function __construct(
4039
private DataPart $original,
4140
) {}
4241

42+
/**
43+
* Asserts that the content of the attachment matches
44+
*/
45+
public function assertContent(string $expected): self
46+
{
47+
Assert::assertSame(
48+
expected: $expected,
49+
actual: $this->body,
50+
message: "Failed asserting that attachment content is `{$expected}`. Actual content is `{$this->body}`",
51+
);
52+
53+
return $this;
54+
}
55+
4356
/**
4457
* Asserts that the attachment has the given name.
4558
*/
46-
public function assertNamed(string $name): void
59+
public function assertNamed(string $name): self
4760
{
4861
Assert::assertSame(
4962
expected: $name,
5063
actual: $this->name,
5164
message: "Failed asserting that attachment name is `{$name}`. Actual name is `{$this->name}`",
5265
);
66+
67+
return $this;
5368
}
5469

5570
/**
@@ -67,24 +82,28 @@ public function assertNotNamed(string $name): void
6782
/**
6883
* Asserts that the attachment has the given type.
6984
*/
70-
public function assertType(string $type): void
85+
public function assertType(string $type): self
7186
{
7287
Assert::assertSame(
7388
expected: $type,
74-
actual: $this->type,
75-
message: "Failed asserting that attachment type is `{$type}`. Actual type is `{$this->type}`",
89+
actual: $this->mediaType,
90+
message: "Failed asserting that attachment type is `{$type}`. Actual type is `{$this->mediaType}`",
7691
);
92+
93+
return $this;
7794
}
7895

7996
/**
8097
* Asserts that the attachment does not have the given type.
8198
*/
82-
public function assertNotType(string $type): void
99+
public function assertNotType(string $type): self
83100
{
84101
Assert::assertNotSame(
85102
expected: $type,
86-
actual: $this->type,
103+
actual: $this->mediaType,
87104
message: "Failed asserting that attachment type is not `{$type}`.",
88105
);
106+
107+
return $this;
89108
}
90109
}

0 commit comments

Comments
 (0)