app(TransactionManager::class)->run(function (): void { CustomerModel::factory()->create(); throw new RuntimeException('fail'); }))->toThrow(RuntimeException::class); expect(CustomerModel::query()->where('is_internal', false)->count())->toBe(0); }); it('publishes events only after the transaction commits', function () { Event::fake([CustomerRegistered::class]); app(TransactionManager::class)->run(function (): void { app(EventDispatcher::class)->dispatch(new CustomerRegistered(1)); Event::assertNotDispatched(CustomerRegistered::class); }); Event::assertDispatched(CustomerRegistered::class); }); it('drops events of a rolled back transaction', function () { Event::fake([CustomerRegistered::class]); rescue(fn () => app(TransactionManager::class)->run(function (): void { app(EventDispatcher::class)->dispatch(new CustomerRegistered(1)); throw new RuntimeException('fail'); }), report: false); Event::assertNotDispatched(CustomerRegistered::class); }); it('announces a registered customer to listeners', function () { Event::fake([CustomerRegistered::class]); $customerId = app(RegisterCustomer::class)->execute(new CustomerDetailsData('Mainos Oy', CustomerType::Company)); Event::assertDispatched(CustomerRegistered::class, fn (CustomerRegistered $event) => $event->customerId === $customerId); });