Skip to content

Commit

Permalink
Ensure missing user does not throw a type error (#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald authored May 8, 2024
1 parent 6c1c174 commit b6f3ed6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/PulseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ protected function listenForEvents(): void
$this->app->booted(function () {
$this->callAfterResolving(Dispatcher::class, function (Dispatcher $event, Application $app) {
$event->listen(function (Logout $event) use ($app) {
if ($event->user === null) {
return;
}

$pulse = $app->make(Pulse::class);

$pulse->rescue(fn () => $pulse->rememberUser($event->user));
Expand Down
9 changes: 9 additions & 0 deletions tests/Feature/PulseTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

use Illuminate\Auth\Events\Logout;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Facade;
use Laravel\Pulse\Contracts\ResolvesUsers;
use Laravel\Pulse\Contracts\Storage;
Expand Down Expand Up @@ -246,6 +248,13 @@ public function find(int|string|null $key): object
expect($persistentMiddleware)->not->toContain(MyTestMiddleware::class.':admin');
});

it('handles logout events when there is no user', function () {
// This will throw a type error when unhandled...
Event::dispatch(new Logout('session', user: null));

expect(true)->toBe(true);
});

class MyTestMiddleware
{
//
Expand Down

0 comments on commit b6f3ed6

Please sign in to comment.