Skip to content

Commit 48b17d4

Browse files
committed
Fix: autowiring
1 parent 36212bb commit 48b17d4

2 files changed

Lines changed: 13 additions & 10 deletions

File tree

src/EventSubscriber/UnauthorizedSubscriber.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
88
use Symfony\Component\HttpFoundation\JsonResponse;
99
use Symfony\Component\HttpFoundation\RedirectResponse;
10-
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
1110
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
1211
use Symfony\Component\HttpKernel\KernelEvents;
1312
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
@@ -17,7 +16,6 @@ class UnauthorizedSubscriber implements EventSubscriberInterface
1716
{
1817
public function __construct(
1918
private readonly UrlGeneratorInterface $urlGenerator,
20-
private readonly FlashBagInterface $flashBag,
2119
) {
2220
}
2321

@@ -53,7 +51,11 @@ public function onKernelException(ExceptionEvent $event): void
5351
}
5452

5553
if ($request->hasSession()) {
56-
$this->flashBag->add('error', 'Your session has expired. Please log in again.');
54+
$session = $request->getSession();
55+
56+
if (method_exists($session, 'getFlashBag')) {
57+
$session->getFlashBag()->add('error', 'Your session has expired. Please log in again.');
58+
}
5759
}
5860

5961
$event->setResponse(new RedirectResponse($loginUrl));

tests/Unit/EventSubscriber/UnauthorizedSubscriberTest.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Symfony\Component\HttpFoundation\RedirectResponse;
1414
use Symfony\Component\HttpFoundation\Request;
1515
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
16+
use Symfony\Component\HttpFoundation\Session\Session;
1617
use Symfony\Component\HttpFoundation\Session\SessionInterface;
1718
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
1819
use Symfony\Component\HttpKernel\HttpKernelInterface;
@@ -23,13 +24,11 @@ class UnauthorizedSubscriberTest extends TestCase
2324
{
2425
private UnauthorizedSubscriber $subscriber;
2526
private UrlGeneratorInterface&MockObject $urlGenerator;
26-
private FlashBagInterface&MockObject $flashBag;
2727

2828
protected function setUp(): void
2929
{
3030
$this->urlGenerator = $this->createMock(UrlGeneratorInterface::class);
31-
$this->flashBag = $this->createMock(FlashBagInterface::class);
32-
$this->subscriber = new UnauthorizedSubscriber($this->urlGenerator, $this->flashBag);
31+
$this->subscriber = new UnauthorizedSubscriber($this->urlGenerator);
3332
}
3433

3534
public function testGetSubscribedEvents(): void
@@ -44,13 +43,15 @@ public function testOnKernelExceptionWithUnauthorizedException(): void
4443
{
4544
$authException = new AuthenticationException('Unauthorized');
4645

47-
$session = $this->createMock(SessionInterface::class);
48-
$session->expects($this->once())->method('invalidate');
49-
50-
$this->flashBag->expects($this->once())
46+
$flashBag = $this->createMock(FlashBagInterface::class);
47+
$flashBag->expects($this->once())
5148
->method('add')
5249
->with('error', 'Your session has expired. Please log in again.');
5350

51+
$session = $this->createMock(Session::class);
52+
$session->expects($this->once())->method('invalidate');
53+
$session->method('getFlashBag')->willReturn($flashBag);
54+
5455
$request = $this->createMock(Request::class);
5556
$request->method('hasSession')->willReturn(true);
5657
$request->method('getSession')->willReturn($session);

0 commit comments

Comments
 (0)