8000 bug #41446 [HttpKernel] Provide migration path for TestSessionListene… · symfony/symfony@7714648 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7714648

Browse files
bug #41446 [HttpKernel] Provide migration path for TestSessionListener (jderusse)
This PR was merged into the 5.4 branch. Discussion ---------- [HttpKernel] Provide migration path for TestSessionListener | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - When working on #41321 I realized that #41343 was wrong for `TestSessionListener`. Injecting the factory in both SessionListener AND TestSessionListener would generate 2 distinguished session instances which is the opposite of the expected behavior: TestSessionListener needs the session used by the request to set the right sessionId in the cookie. This PR fallback to `request->getSession()` when there is no session injected in the container (ie: FrameworkBundlke:6) Commits ------- b56606e Provide migration path for TestSessionListener
2 parents cfb1016 + b56606e commit 7714648

File tree

6 files changed

+23
-8
lines changed

6 files changed

+23
-8
lines changed

UPGRADE-5.4.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@ FrameworkBundle
55
---------------
66

77
* Deprecate the `AdapterInterface` autowiring alias, use `CacheItemPoolInterface` instead
8+
9+
HttpKernel
10+
----------
11+
12+
* Deprecate `AbstractTestSessionListener::getSession` inject a session in the request instead

src/Symfony/Bundle/FrameworkBundle/Resources/config/test.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
->set('test.session.listener', TestSessionListener::class)
3939
->args([
4040
service_locator([
41-
'session_factory' => service('session.factory')->ignoreOnInvalid(),
4241
'session' => service('.session.do-not-use')->ignoreOnInvalid(),
4342
]),
4443
])

src/Symfony/Component/HttpKernel/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.4
5+
---
6+
7+
* Deprecate `AbstractTestSessionListener::getSession` inject a session in the request instead
8+
49
5.3
510
---
611

src/Symfony/Component/HttpKernel/EventListener/AbstractTestSessionListener.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ public function onKernelRequest(RequestEvent $event)
4646
}
4747

4848
// bootstrap the session
49-
if (!$session = $this->getSession()) {
49+
if ($event->getRequest()->hasSession()) {
50+
$session = $event->getRequest()->getSession();
51+
} elseif (!$session = $this->getSession()) {
5052
return;
5153
}
5254

@@ -100,14 +102,16 @@ public function onKernelResponse(ResponseEvent $event)
100102
public static function getSubscribedEvents(): array
101103
{
102104
return [
103-
KernelEvents::REQUEST => ['onKernelRequest', 192],
105+
KernelEvents::REQUEST => ['onKernelRequest', 127], // AFTER SessionListener
104106
KernelEvents::RESPONSE => ['onKernelResponse', -128],
105107
];
106108
}
107109

108110
/**
109111
* Gets the session object.
110112
*
113+
* @deprecated since Symfony 5.4, will be removed in 6.0.
114+
*
111115
* @return SessionInterface|null A SessionInterface instance or null if no session is available
112116
*/
113117
abstract protected function getSession();

src/Symfony/Component/HttpKernel/EventListener/TestSessionListener.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,17 @@ public function __construct(ContainerInterface $container, array $sessionOptions
3131
parent::__construct($sessionOptions);
3232
}
3333

34+
/**
35+
* @deprecated since Symfony 5.4, will be removed in 6.0.
36+
*/
3437
protected function getSession(): ?SessionInterface
3538
{
39+
trigger_deprecation('symfony/http-kernel', '5.4', '"%s" is deprecated and will be removed in 6.0, inject a session in the request instead.', __METHOD__);
40+
3641
if ($this->container->has('session')) {
3742
return $this->container->get('session');
3843
}
3944

40-
if ($this->container->has('session_factory')) {
41-
return $this->container->get('session_factory')->createSession();
42-
}
43-
4445
return null;
4546
}
4647
}

src/Symfony/Component/HttpKernel/Tests/EventListener/TestSessionListenerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use Symfony\Component\HttpKernel\Event\RequestEvent;
2020
use Symfony\Component\HttpKernel\Event\ResponseEvent;
2121
use Symfony\Component\HttpKernel\EventListener\AbstractTestSessionListener;
22-
use Symfony\Component\HttpKernel\EventListener\SessionListener;
2322
use Symfony\Component\HttpKernel\EventListener\TestSessionListener;
2423
use Symfony\Component\HttpKernel\HttpKernelInterface;
2524

@@ -99,6 +98,7 @@ public function testEmptySessionWithNewSessionIdDoesSendCookie()
9998

10099
$kernel = $this->createMock(HttpKernelInterface::class);
101100
$request = Request::create('/', 'GET', [], ['MOCKSESSID' => '123']);
101+
$request->setSession($this->getSession());
102102
$event = new RequestEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST);
103103
$this->listener->onKernelRequest($event);
104104

@@ -118,6 +118,7 @@ public function testSessionWithNewSessionIdAndNewCookieDoesNotSendAnotherCookie(
118118

119119
$kernel = $this->createMock(HttpKernelInterface::class);
120120
$request = Request::create('/', 'GET', [], ['MOCKSESSID' => '123']);
121+
$request->setSession($this->getSession());
121122
$event = new RequestEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST);
122123
$this->listener->onKernelRequest($event);
123124

0 commit comments

Comments
 (0)
0