Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] Ensure missing user does not throw a type error #366

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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