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 26, 2021
1 parent 88c5349 commit 3e040ef
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
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
@@ -0,0 +1,46 @@
<?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 testOnLogoutIfHasNotSession()
{
$session = $this->createMock(Session::class);
$session->expects($this->never())->method('invalidate');

$request = $this->createMock(Request::class);
$request->expects($this->once())->method('hasSession')->willReturn(false);

$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->expects($this->once())->method('getSession')->willReturn($session);
$request->expects($this->once())->method('hasSession')->willReturn(true);

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

0 comments on commit 3e040ef

Please sign in to comment.