Skip to content

[HttpKernel] Provide migration path for TestSessionListener #41446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions UPGRADE-5.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ FrameworkBundle
---------------

* Deprecate the `AdapterInterface` autowiring alias, use `CacheItemPoolInterface` instead

HttpKernel
----------

* Deprecate `AbstractTestSessionListener::getSession` inject a session in the request instead
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
->set('test.session.listener', TestSessionListener::class)
->args([
service_locator([
'session_factory' => service('session.factory')->ignoreOnInvalid(),
'session' => service('.session.do-not-use')->ignoreOnInvalid(),
]),
])
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.4
---

* Deprecate `AbstractTestSessionListener::getSession` inject a session in the request instead

5.3
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ public function onKernelRequest(RequestEvent $event)
}

// bootstrap the session
if (!$session = $this->getSession()) {
if ($event->getRequest()->hasSession()) {
$session = $event->getRequest()->getSession();
} elseif (!$session = $this->getSession()) {
return;
}

Expand Down Expand Up @@ -100,14 +102,16 @@ public function onKernelResponse(ResponseEvent $event)
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => ['onKernelRequest', 192],
KernelEvents::REQUEST => ['onKernelRequest', 127], // AFTER SessionListener
KernelEvents::RESPONSE => ['onKernelResponse', -128],
];
}

/**
* Gets the session object.
*
* @deprecated since Symfony 5.4, will be removed in 6.0.
*
* @return SessionInterface|null A SessionInterface instance or null if no session is available
*/
abstract protected function getSession();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ public function __construct(ContainerInterface $container, array $sessionOptions
parent::__construct($sessionOptions);
}

/**
* @deprecated since Symfony 5.4, will be removed in 6.0.
*/
protected function getSession(): ?SessionInterface
{
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__);

if ($this->container->has('session')) {
return $this->container->get('session');
}

if ($this->container->has('session_factory')) {
return $this->container->get('session_factory')->createSession();
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\EventListener\AbstractTestSessionListener;
use Symfony\Component\HttpKernel\EventListener\SessionListener;
use Symfony\Component\HttpKernel\EventListener\TestSessionListener;
use Symfony\Component\HttpKernel\HttpKernelInterface;

Expand Down Expand Up @@ -99,6 +98,7 @@ public function testEmptySessionWithNewSessionIdDoesSendCookie()

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

Expand All @@ -118,6 +118,7 @@ public function testSessionWithNewSessionIdAndNewCookieDoesNotSendAnotherCookie(

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

Expand Down