8000 Provide migration path for TestSessionListener · symfony/symfony@b56606e · GitHub
[go: up one dir, main page]

Skip to content

Commit b56606e

Browse files
committed
Provide migration path for TestSessionListener
1 parent cfb1016 commit b56606e

File tree

6 files changed

+23
-8
lines changed

6 files changed

+23
-8
lines changed

UPGRADE-5.4.md

+5
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

-1
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

+5
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

+6-2
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

+5-4
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

+2-1
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