diff --git a/packages/event-bus/src/Testing/EventBusTester.php b/packages/event-bus/src/Testing/EventBusTester.php index af9941a533..f3d3a8fa71 100644 --- a/packages/event-bus/src/Testing/EventBusTester.php +++ b/packages/event-bus/src/Testing/EventBusTester.php @@ -38,7 +38,7 @@ public function assertDispatched(string|object $event, ?Closure $callback = null { $this->assertFaked(); - Assert::assertNotNull( + Assert::assertNotEmpty( actual: $dispatches = $this->findDispatches($event), message: 'The event was not dispatched.', ); diff --git a/packages/mail/src/Testing/TestingMailer.php b/packages/mail/src/Testing/TestingMailer.php index 09dafffdd6..d51b92da3f 100644 --- a/packages/mail/src/Testing/TestingMailer.php +++ b/packages/mail/src/Testing/TestingMailer.php @@ -7,11 +7,13 @@ use Tempest\Mail\EmailWasSent; use Tempest\Mail\Mailer; +use function Tempest\get; + final class TestingMailer implements Mailer { - public function __construct( - private readonly ?EventBus $eventBus = null, - ) {} + private ?EventBus $eventBus { + get => get(className: EventBus::class); + } /** * List of emails that would have been sent. diff --git a/src/Tempest/Framework/Testing/IntegrationTest.php b/src/Tempest/Framework/Testing/IntegrationTest.php index da9225197e..65eab45c2b 100644 --- a/src/Tempest/Framework/Testing/IntegrationTest.php +++ b/src/Tempest/Framework/Testing/IntegrationTest.php @@ -25,7 +25,6 @@ use Tempest\Database\Testing\DatabaseTester; use Tempest\DateTime\DateTimeInterface; use Tempest\Discovery\DiscoveryLocation; -use Tempest\EventBus\EventBus; use Tempest\EventBus\Testing\EventBusTester; use Tempest\Framework\Testing\Http\HttpRouterTester; use Tempest\Http\GenericRequest; @@ -142,9 +141,7 @@ protected function setupTesters(): self $this->eventBus = new EventBusTester($this->container); $this->storage = new StorageTester($this->container); $this->cache = new CacheTester($this->container); - $this->mailer = new MailTester(new TestingMailer( - eventBus: $this->container->get(EventBus::class), - )); + $this->mailer = new MailTester(new TestingMailer()); $this->process = $this->container->get(ProcessTester::class); $this->process->disableProcessExecution(); diff --git a/tests/Integration/EventBus/EventBusTesterTest.php b/tests/Integration/EventBus/EventBusTesterTest.php index 85f2624375..f8ee38709a 100644 --- a/tests/Integration/EventBus/EventBusTesterTest.php +++ b/tests/Integration/EventBus/EventBusTesterTest.php @@ -114,6 +114,18 @@ public function test_assert_dispatched_object_with_callback_failure(): void }); } + public function test_assert_dispatched_failure(): void + { + $this->eventBus->preventEventHandling(); + + $this->container->get(EventBus::class)->dispatch('event-bus-fake-event'); + + $this->expectException(ExpectationFailedException::class); + $this->expectExceptionMessage('The event was not dispatched.'); + + $this->eventBus->assertDispatched('this-was-not-dispatched'); + } + public function test_assert_not_dispatched(): void { $this->eventBus->preventEventHandling(); diff --git a/tests/Integration/Mailer/MailerTesterTest.php b/tests/Integration/Mailer/MailerTesterTest.php index 9c4a5acb98..f13b88f34f 100644 --- a/tests/Integration/Mailer/MailerTesterTest.php +++ b/tests/Integration/Mailer/MailerTesterTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\AssertionFailedError; use Tempest\Mail\Attachment; use Tempest\Mail\Email; +use Tempest\Mail\EmailWasSent; use Tempest\Mail\GenericEmail; use Tests\Tempest\Integration\FrameworkIntegrationTestCase; use Tests\Tempest\Integration\Mailer\Fixtures\SendWelcomeEmail; @@ -79,4 +80,22 @@ public function test_assertions(): void $this->assertSame('hello!', ($email->attachments[0]->resolve)()); }); } + + public function test_email_was_sent_event_was_dispatched(): void + { + $this->eventBus->preventEventHandling(); + + $this->mailer + ->send(email: new GenericEmail( + subject: 'Hello', + to: 'jon@doe.co', + html: 'Hello Jon', + from: 'no-reply@tempestphp.com', + attachments: [ + Attachment::fromClosure(callable: fn () => 'hello!'), + ], + )); + + $this->eventBus->assertDispatched(event: EmailWasSent::class); + } }