|
2 | 2 |
|
3 | 3 | namespace Tempest\Mail\Testing; |
4 | 4 |
|
| 5 | +use Closure; |
| 6 | +use InvalidArgumentException; |
| 7 | +use PHPUnit\Framework\Assert; |
| 8 | +use PHPUnit\Framework\ExpectationFailedException; |
5 | 9 | use Tempest\Container\Container; |
6 | | -use Tempest\Container\GenericContainer; |
| 10 | +use Tempest\Mail\Email; |
7 | 11 | use Tempest\Mail\Mailer; |
8 | 12 | use Tempest\Mail\MailerConfig; |
9 | | -use Tempest\Mail\MailerInitializer; |
10 | | -use Tempest\Mail\Transports\NullMailerConfig; |
11 | | -use Tempest\Mail\Transports\Smtp\SmtpMailerConfig; |
12 | | -use Tempest\Support\Str; |
| 13 | +use Tempest\Support\Arr; |
13 | 14 | use Tempest\View\ViewRenderer; |
14 | | -use UnitEnum; |
15 | 15 |
|
16 | 16 | final class MailerTester |
17 | 17 | { |
| 18 | + private(set) ?TestingMailer $mailer = null; |
| 19 | + |
18 | 20 | public function __construct( |
19 | 21 | private Container $container, |
20 | 22 | ) {} |
21 | 23 |
|
22 | 24 | /** |
23 | | - * Forces the usage of a testing mailer. |
| 25 | + * Prevents emails from being actually sent. This is the default behavior during tests. |
24 | 26 | */ |
25 | | - public function fake(null|string|UnitEnum $tag = null): TestingMailer |
| 27 | + public function preventSendingEmails(): void |
26 | 28 | { |
27 | | - if (! $this->container->has(MailerConfig::class, $tag)) { |
28 | | - $this->container->config(new NullMailerConfig($tag)); |
29 | | - } |
30 | | - |
31 | | - $mailer = new TestingMailer( |
32 | | - tag: match (true) { |
33 | | - is_string($tag) => Str\to_kebab_case($tag), |
34 | | - $tag instanceof UnitEnum => Str\to_kebab_case($tag->name), |
35 | | - default => 'default', |
36 | | - }, |
37 | | - mailerConfig: $this->container->get(MailerConfig::class, $tag), |
| 29 | + $this->mailer ??= new TestingMailer( |
| 30 | + mailerConfig: $this->container->get(MailerConfig::class), |
38 | 31 | viewRenderer: $this->container->get(ViewRenderer::class), |
39 | 32 | ); |
40 | 33 |
|
41 | | - $this->container->singleton(Mailer::class, $mailer, $tag); |
| 34 | + $this->container->singleton(Mailer::class, $this->mailer); |
| 35 | + } |
42 | 36 |
|
43 | | - return $mailer; |
| 37 | + /** |
| 38 | + * Disables the testing mailer, so emails can actually be sent. This is usually not recommended. |
| 39 | + */ |
| 40 | + public function allowSendingEmails(): void |
| 41 | + { |
| 42 | + $this->container->unregister(Mailer::class); |
44 | 43 | } |
45 | 44 |
|
46 | 45 | /** |
47 | | - * Prevents mailers from being actually used. |
| 46 | + * Asserts that the given email class was sent. |
| 47 | + * |
| 48 | + * @param class-string<Email> $email |
48 | 49 | */ |
49 | | - public function preventRealUsage(): void |
| 50 | + public function assertSent(string $email, ?\Closure $callback = null): self |
50 | 51 | { |
51 | | - if (! ($this->container instanceof GenericContainer)) { |
52 | | - throw new \RuntimeException('Container is not a GenericContainer, unable to prevent usage without fake.'); |
| 52 | + $this->ensureTestingSetUp(); |
| 53 | + $this->assertClassStringIsEmail($email); |
| 54 | + |
| 55 | + $sentEmail = Arr\first($this->mailer->sent, filter: fn (Email $sent) => $sent instanceof $email); |
| 56 | + |
| 57 | + Assert::assertTrue( |
| 58 | + condition: (bool) $sentEmail, |
| 59 | + message: sprintf('Email `%s` was not sent.', $email), |
| 60 | + ); |
| 61 | + |
| 62 | + if ($callback) { |
| 63 | + try { |
| 64 | + if ($callback($sentEmail) === false) { |
| 65 | + throw new ExpectationFailedException('The assertion callback returned `false`.'); |
| 66 | + } |
| 67 | + } catch (ExpectationFailedException $previous) { |
| 68 | + throw new ExpectationFailedException( |
| 69 | + message: sprintf('Email `%s` was sent but failed the assertion.', $email), |
| 70 | + previous: $previous, |
| 71 | + ); |
| 72 | + } |
53 | 73 | } |
54 | 74 |
|
55 | | - $this->container->unregister(Mailer::class, tagged: true); |
56 | | - $this->container->removeInitializer(MailerInitializer::class); |
57 | | - $this->container->addInitializer(RestrictedMailerInitializer::class); |
| 75 | + return $this; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Asserts that the given email class was not sent. |
| 80 | + * |
| 81 | + * @param class-string<Email> $email |
| 82 | + */ |
| 83 | + public function assertNotSent(string $email): self |
| 84 | + { |
| 85 | + $this->ensureTestingSetUp(); |
| 86 | + $this->assertClassStringIsEmail($email); |
| 87 | + |
| 88 | + Assert::assertFalse( |
| 89 | + condition: (bool) Arr\first($this->mailer->sent, filter: fn (Email $sent) => $sent instanceof $email), |
| 90 | + message: sprintf('Email `%s` was unexpectedly sent.', $email), |
| 91 | + ); |
| 92 | + |
| 93 | + return $this; |
| 94 | + } |
| 95 | + |
| 96 | + private function ensureTestingSetUp(): void |
| 97 | + { |
| 98 | + if (is_null($this->mailer)) { |
| 99 | + throw new ExpectationFailedException('Mail testing is not set up. Please call `$this->mailer->preventSendingEmails()` before running assertions.'); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private function assertClassStringIsEmail(string $email): void |
| 104 | + { |
| 105 | + if (! is_a($email, Email::class, allow_string: true)) { |
| 106 | + throw new InvalidArgumentException(sprintf('The given email class must implement `%s`.', Email::class)); |
| 107 | + } |
58 | 108 | } |
59 | 109 | } |
0 commit comments