Skip to content

Commit 82318a6

Browse files
authored
fix(eventbus): change dispatched assertion from not null to not empty (#1709)
1 parent 68f4aba commit 82318a6

File tree

5 files changed

+38
-8
lines changed

5 files changed

+38
-8
lines changed

packages/event-bus/src/Testing/EventBusTester.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function assertDispatched(string|object $event, ?Closure $callback = null
3838
{
3939
$this->assertFaked();
4040

41-
Assert::assertNotNull(
41+
Assert::assertNotEmpty(
4242
actual: $dispatches = $this->findDispatches($event),
4343
message: 'The event was not dispatched.',
4444
);

packages/mail/src/Testing/TestingMailer.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
use Tempest\Mail\EmailWasSent;
88
use Tempest\Mail\Mailer;
99

10+
use function Tempest\get;
11+
1012
final class TestingMailer implements Mailer
1113
{
12-
public function __construct(
13-
private readonly ?EventBus $eventBus = null,
14-
) {}
14+
private ?EventBus $eventBus {
15+
get => get(className: EventBus::class);
16+
}
1517

1618
/**
1719
* List of emails that would have been sent.

src/Tempest/Framework/Testing/IntegrationTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
use Tempest\Database\Testing\DatabaseTester;
2626
use Tempest\DateTime\DateTimeInterface;
2727
use Tempest\Discovery\DiscoveryLocation;
28-
use Tempest\EventBus\EventBus;
2928
use Tempest\EventBus\Testing\EventBusTester;
3029
use Tempest\Framework\Testing\Http\HttpRouterTester;
3130
use Tempest\Http\GenericRequest;
@@ -142,9 +141,7 @@ protected function setupTesters(): self
142141
$this->eventBus = new EventBusTester($this->container);
143142
$this->storage = new StorageTester($this->container);
144143
$this->cache = new CacheTester($this->container);
145-
$this->mailer = new MailTester(new TestingMailer(
146-
eventBus: $this->container->get(EventBus::class),
147-
));
144+
$this->mailer = new MailTester(new TestingMailer());
148145

149146
$this->process = $this->container->get(ProcessTester::class);
150147
$this->process->disableProcessExecution();

tests/Integration/EventBus/EventBusTesterTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ public function test_assert_dispatched_object_with_callback_failure(): void
114114
});
115115
}
116116

117+
public function test_assert_dispatched_failure(): void
118+
{
119+
$this->eventBus->preventEventHandling();
120+
121+
$this->container->get(EventBus::class)->dispatch('event-bus-fake-event');
122+
123+
$this->expectException(ExpectationFailedException::class);
124+
$this->expectExceptionMessage('The event was not dispatched.');
125+
126+
$this->eventBus->assertDispatched('this-was-not-dispatched');
127+
}
128+
117129
public function test_assert_not_dispatched(): void
118130
{
119131
$this->eventBus->preventEventHandling();

tests/Integration/Mailer/MailerTesterTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPUnit\Framework\AssertionFailedError;
77
use Tempest\Mail\Attachment;
88
use Tempest\Mail\Email;
9+
use Tempest\Mail\EmailWasSent;
910
use Tempest\Mail\GenericEmail;
1011
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
1112
use Tests\Tempest\Integration\Mailer\Fixtures\SendWelcomeEmail;
@@ -79,4 +80,22 @@ public function test_assertions(): void
7980
$this->assertSame('hello!', ($email->attachments[0]->resolve)());
8081
});
8182
}
83+
84+
public function test_email_was_sent_event_was_dispatched(): void
85+
{
86+
$this->eventBus->preventEventHandling();
87+
88+
$this->mailer
89+
->send(email: new GenericEmail(
90+
subject: 'Hello',
91+
92+
html: 'Hello Jon',
93+
94+
attachments: [
95+
Attachment::fromClosure(callable: fn () => 'hello!'),
96+
],
97+
));
98+
99+
$this->eventBus->assertDispatched(event: EmailWasSent::class);
100+
}
82101
}

0 commit comments

Comments
 (0)