You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/2-features/08-events.md
+14-5Lines changed: 14 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -194,11 +194,14 @@ Other events include migration-related ones, such as {b`Tempest\Database\Migrati
194
194
195
195
## Testing
196
196
197
-
By extending {`Tempest\Framework\Testing\IntegrationTest`} from your test case, you may gain access to the event bus testing utilities using the `eventBus` property.
197
+
By extending {b`Tempest\Framework\Testing\IntegrationTest`} from your test case, you gain access to the event bus testing utilities through the `eventBus` property.
198
198
199
199
These utilities include a way to replace the event bus with a testing implementation, as well as a few assertion methods to ensure that events have been dispatched or are being listened to.
When testing code that dispatches events, you may want to prevent Tempest from handling them. This can be useful when the event’s handlers are tested separately, or when the side-effects of these handlers are not desired for this test case.
228
231
229
-
To disable event handling, the event bus instance must be replaced with a testing implementation in the container. This may be achieved by calling the `preventEventHandling()` method on the `eventBus` property.
232
+
To disable event handling, the event bus instance must be replaced with a testing implementation in the container. This is achieved by calling the `preventEventHandling()` method on the `eventBus` property.
230
233
231
-
```php tests/MyServiceTest.php
234
+
```php
232
235
$this->eventBus->preventEventHandling();
233
236
```
234
237
238
+
If you want to be able to make assertions while still allowing events to be dispatched, you may instead call the `recordEventDispatches()` method.
239
+
240
+
```php
241
+
$this->eventBus->recordEventDispatches();
242
+
```
243
+
235
244
### Testing a method-based handler
236
245
237
246
When handlers are registered as methods, instead of dispatching the corresponding event to test the handler logic, you may simply call the method to test it in isolation.
@@ -252,7 +261,7 @@ final readonly class AircraftObserver
252
261
This handler may be tested by resolving the service class from the container, and calling the method with an instance of the event created for this purpose.
253
262
254
263
```php app/AircraftObserverTest.php
255
-
// Replace the event bus in the container
264
+
// Prevent events from being handled while allowing assertions
0 commit comments