Skip to content

Commit a578fae

Browse files
Merge branch '4.4' into 5.1
* 4.4: [Process] Dont test TTY if there is no TTY support Fixing some Mongolian translating the validators for european portuguese language Fix CI Update validators.he.xlf Update security.he.xlf Update validators.he.xlf Improve performances in CircualReference detection [PHPUnitBridge] Fixed crash on Windows with PHP 8 Fix session called initized several time
2 parents 40a4b07 + 615304c commit a578fae

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

EventListener/AbstractSessionListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public function onKernelRequest(RequestEvent $event)
5959
$session = null;
6060
$request = $event->getRequest();
6161
if (!$request->hasSession()) {
62-
$request->setSessionFactory(function () { return $this->getSession(); });
62+
$sess = null;
63+
$request->setSessionFactory(function () use (&$sess) { return $sess ?? $sess = $this->getSession(); });
6364
}
6465

6566
$session = $session ?? ($this->container && $this->container->has('initialized_session') ? $this->container->get('initialized_session') : null);

Tests/EventListener/SessionListenerTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
use Symfony\Component\HttpFoundation\Session\Session;
2222
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
2323
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
24+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
2425
use Symfony\Component\HttpKernel\Event\RequestEvent;
2526
use Symfony\Component\HttpKernel\Event\ResponseEvent;
2627
use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener;
2728
use Symfony\Component\HttpKernel\EventListener\SessionListener;
2829
use Symfony\Component\HttpKernel\Exception\UnexpectedSessionUsageException;
2930
use Symfony\Component\HttpKernel\HttpKernelInterface;
31+
use Symfony\Component\HttpKernel\KernelInterface;
3032

3133
class SessionListenerTest extends TestCase
3234
{
@@ -181,6 +183,38 @@ public function testSurrogateMasterRequestIsPublic()
181183
$this->assertLessThanOrEqual((new \DateTime('now', new \DateTimeZone('UTC'))), (new \DateTime($response->headers->get('Expires'))));
182184
}
183185

186+
public function testGetSessionIsCalledOnce()
187+
{
188+
$session = $this->getMockBuilder(Session::class)->disableOriginalConstructor()->getMock();
189+
$sessionStorage = $this->getMockBuilder(NativeSessionStorage::class)->getMock();
190+
$kernel = $this->getMockBuilder(KernelInterface::class)->getMock();
191+
192+
$sessionStorage->expects($this->once())
193+
->method('setOptions')
194+
->with(['cookie_secure' => true]);
195+
196+
$requestStack = new RequestStack();
197+
$requestStack->push($masterRequest = new Request([], [], [], [], [], ['HTTPS' => 'on']));
198+
199+
$container = new Container();
200+
$container->set('session_storage', $sessionStorage);
201+
$container->set('session', $session);
202+
$container->set('request_stack', $requestStack);
203+
204+
$event = new GetResponseEvent($kernel, $masterRequest, HttpKernelInterface::MASTER_REQUEST);
205+
206+
$listener = new SessionListener($container);
207+
$listener->onKernelRequest($event);
208+
209+
$subRequest = $masterRequest->duplicate();
210+
// at this point both master and subrequest have a closure to build the session
211+
212+
$masterRequest->getSession();
213+
214+
// calling the factory on the subRequest should not trigger a second call to storage->sesOptions()
215+
$subRequest->getSession();
216+
}
217+
184218
public function testSessionUsageExceptionIfStatelessAndSessionUsed()
185219
{
186220
$session = $this->getMockBuilder(Session::class)->disableOriginalConstructor()->getMock();

0 commit comments

Comments
 (0)