[Preface: I resolved my root issue, this question is geared toward understanding why it occurred in the first place]
I have been debugging a test case where I register an event expectation via $this->expectsEvents(App\Events\MyTestEvent::class). The test kept giving me the following error, even though the event listener's code had run:
Exception: The following events were not fired: [App\Events\MyTestEvent]
Through lots of trial and error, it seems the problem occurs when using two different methods of registering event handlers (in EventServiceProvider.php) at the same time. See Case 1 below, which is my original code for registering my event handlers. If I move the auth.login handler to the $listen array (see Case 2), my test works and the problem is solved.
Questions:
- Am I doing something wrong in Case 1? or...
- Is this known / expected behavior? or ...
- Is this a bug in Laravel event handlers?
Thanks for your help and time!
Case 1 (original code, testing expectsEvent() for MyTestEvent fails)
class EventServiceProvider extends ServiceProvider
{
protected $listen = [
'App\Events\MyTestEvent' => [
'App\Listeners\TestListener',
],
];
public function boot(DispatcherContract $events)
{
parent::boot($events);
Event::listen('auth.login', function()
{
Log::info("Login occurred");
});
}
}
Case 2 (modified code, expectsEvent() for MyTestEvent succeeds)
class EventServiceProvider extends ServiceProvider
{
protected $listen = [
'App\Events\MyTestEvent' => [
'App\Listeners\TestListener',
],
'auth.login' => [
'App\Listeners\LoginListener',
],
];
public function boot(DispatcherContract $events)
{
parent::boot($events);
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire