Skip to content

Commit

Permalink
fix Check if it has session before getSession()
Browse files Browse the repository at this point in the history
  • Loading branch information
mousezheng committed Jul 27, 2021
1 parent 88c5349 commit 17d54fd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class SessionLogoutListener implements EventSubscriberInterface
{
public function onLogout(LogoutEvent $event): void
{
$event->getRequest()->getSession()->invalidate();
if ($event->getRequest()->hasSession()) {
$event->getRequest()->getSession()->invalidate();
}
}

public static function getSubscribedEvents(): array
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Security\Http\Tests\EventListener;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Http\Event\LogoutEvent;
use Symfony\Component\Security\Http\EventListener\SessionLogoutListener;

class SessionLogoutListenerTest extends TestCase
{
public function testOnLogoutIfHasNoSession()
{
$request = $this->createMock(Request::class);
$request->method('hasSession')->willReturn(false);
$request->expects($this->never())->method('getSession');

$sessionLogoutListener = new SessionLogoutListener();
$sessionLogoutListener->onLogout(new LogoutEvent($request, null));
}

public function testOnLogoutIfHasSession()
{
$session = $this->createMock(Session::class);
$session->expects($this->once())->method('invalidate');

$request = $this->createMock(Request::class);
$request->method('getSession')->willReturn($session);
$request->method('hasSession')->willReturn(true);

$sessionLogoutListener = new SessionLogoutListener();
$sessionLogoutListener->onLogout(new LogoutEvent($request, null));
}
}

0 comments on commit 17d54fd

Please sign in to comment.