Skip to content

Commit ed66472

Browse files
committed
fix Check if it has session before getSession()
1 parent c9f1935 commit ed66472

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

EventListener/SessionLogoutListener.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ class SessionLogoutListener implements EventSubscriberInterface
2525
{
2626
public function onLogout(LogoutEvent $event): void
2727
{
28-
$event->getRequest()->getSession()->invalidate();
28+
if ($event->getRequest()->hasSession()) {
29+
$event->getRequest()->getSession()->invalidate();
30+
}
2931
}
3032

3133
public static function getSubscribedEvents(): array
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Security\Http\Tests\EventListener;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpFoundation\Session\Session;
17+
use Symfony\Component\Security\Http\Event\LogoutEvent;
18+
use Symfony\Component\Security\Http\EventListener\SessionLogoutListener;
19+
20+
class SessionLogoutListenerTest extends TestCase
21+
{
22+
public function testOnLogoutIfHasNoSession()
23+
{
24+
$request = $this->createMock(Request::class);
25+
$request->method('hasSession')->willReturn(false);
26+
$request->expects($this->never())->method('getSession');
27+
28+
$sessionLogoutListener = new SessionLogoutListener();
29+
$sessionLogoutListener->onLogout(new LogoutEvent($request, null));
30+
}
31+
32+
public function testOnLogoutIfHasSession()
33+
{
34+
$session = $this->createMock(Session::class);
35+
$session->expects($this->once())->method('invalidate');
36+
37+
$request = $this->createMock(Request::class);
38+
$request->method('getSession')->willReturn($session);
39+
$request->method('hasSession')->willReturn(true);
40+
41+
$sessionLogoutListener = new SessionLogoutListener();
42+
$sessionLogoutListener->onLogout(new LogoutEvent($request, null));
43+
}
44+
}

0 commit comments

Comments
 (0)