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: README.md
+35-5
Original file line number
Diff line number
Diff line change
@@ -236,22 +236,45 @@ return `null` if the DataObject has been deleted.
236
236
`DataObjectEvent::getObject(true) will attempt to retrieve the exact version of the DataObject that fired the event,
237
237
assuming it was versioned.
238
238
239
-
## Testing Your Events
239
+
## Handling Errors in Event Listeners
240
240
241
-
### Writing Event Tests
241
+
Exceptions thrown by an event listener will not stop the execution of follow events. By default, those exceptions will be sent to `EventService::handleError()` who will logged them to the default Silverstripe CMS logger.
242
+
243
+
You can provide your own error handler with Injector.
244
+
245
+
```yml
246
+
---
247
+
Name: custom-event-service
248
+
After:
249
+
- '#event-service'
250
+
---
251
+
SilverStripe\Core\Injector\Injector:
252
+
ArchiPro\EventDispatcher\AsyncEventDispatcher:
253
+
errorhandler: [MyCustomEventHandler, handleError]
254
+
```
255
+
256
+
## Testing your Events
242
257
243
258
When testing your event listeners, you'll need to:
244
259
1. Dispatch your events
245
260
2. Run the event loop
246
261
3. Assert the expected outcomes
247
262
263
+
You can also use the `TestEventService` to test your events. The `TestEventService` will replace the default `EventService` and log any exceptions thrown by listeners.
264
+
265
+
You need to require the `colinodell/psr-testlogger` package in your dev dependencies to use the `TestEventService`.
266
+
267
+
```
268
+
composer require colinodell/psr-testlogger --dev
269
+
```
270
+
248
271
Here's an example test:
249
272
250
273
```php
251
274
use Revolt\EventLoop;
252
275
use SilverStripe\Dev\SapphireTest;
253
276
use SilverStripe\Core\Injector\Injector;
254
-
use ArchiPro\Silverstripe\EventDispatcher\Service\EventService;
277
+
use ArchiPro\Silverstripe\EventDispatcher\Service\TestEventService;
255
278
256
279
class MyEventTest extends SapphireTest
257
280
{
@@ -260,8 +283,9 @@ class MyEventTest extends SapphireTest
0 commit comments