diff --git a/events.md b/events.md index 7ad8548f8bd..aaa2f82b154 100644 --- a/events.md +++ b/events.md @@ -963,3 +963,73 @@ class ExampleTest extends TestCase } } ``` + +If you want to fake all events except for a specific set of events, you may use the `fakeExceptFor` method. This method accepts a closure that will be executed with the events faked, and an array of event classes that should not be faked: + +```php tab=Pest +create(); + + // Only OrderAudited should be dispatched as normal, OrderCreated is faked + Event::assertDispatched(OrderCreated::class); + Event::assertNotDispatched(OrderAudited::class); + + return $order; + }, [ + OrderAudited::class, + ]); + + // The events will be dispatched and listeners will run as normal... + $order->update([ + // ... + ]); +}); +``` + +```php tab=PHPUnit +create(); + + // Only OrderAudited should be dispatched as normal, OrderCreated is faked + Event::assertDispatched(OrderCreated::class); + Event::assertNotDispatched(OrderAudited::class); + + return $order; + }, [ + OrderAudited::class, + ]); + + // The events will be dispatched and listeners will run as normal... + $order->update([ + // ... + ]); + } +} +``` \ No newline at end of file