Skip to content

Commit 3d9d99a

Browse files
dmitryuknicolas-grekas
authored andcommitted
[DoctrineBridge] Prevent idle connection listener from running on subrequests
1 parent fcb8989 commit 3d9d99a

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/Symfony/Bridge/Doctrine/Middleware/IdleConnection/Listener.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\DependencyInjection\ContainerInterface;
1515
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1616
use Symfony\Component\HttpKernel\Event\RequestEvent;
17+
use Symfony\Component\HttpKernel\HttpKernelInterface;
1718
use Symfony\Component\HttpKernel\KernelEvents;
1819

1920
final class Listener implements EventSubscriberInterface
@@ -29,6 +30,9 @@ public function __construct(
2930

3031
public function onKernelRequest(RequestEvent $event): void
3132
{
33+
if (HttpKernelInterface::MAIN_REQUEST !== $event->getRequestType()) {
34+
return;
35+
}
3236
$timestamp = time();
3337

3438
foreach ($this->connectionExpiries as $name => $expiry) {

src/Symfony/Bridge/Doctrine/Tests/Middleware/IdleConnection/ListenerTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Middleware\IdleConnection;
12+
namespace Symfony\Bridge\Doctrine\Tests\Middleware\IdleConnection;
1313

1414
use Doctrine\DBAL\Connection as ConnectionInterface;
1515
use PHPUnit\Framework\TestCase;
1616
use Symfony\Bridge\Doctrine\Middleware\IdleConnection\Listener;
1717
use Symfony\Component\DependencyInjection\ContainerInterface;
1818
use Symfony\Component\HttpKernel\Event\RequestEvent;
19+
use Symfony\Component\HttpKernel\HttpKernelInterface;
1920

2021
class ListenerTest extends TestCase
2122
{
@@ -34,10 +35,24 @@ public function testOnKernelRequest()
3435
->willReturn($connectionOneMock);
3536

3637
$listener = new Listener($connectionExpiries, $containerMock);
38+
$event = $this->createMock(RequestEvent::class);
39+
$event->method('getRequestType')->willReturn(HttpKernelInterface::MAIN_REQUEST);
3740

38-
$listener->onKernelRequest($this->createMock(RequestEvent::class));
41+
$listener->onKernelRequest($event);
3942

4043
$this->assertArrayNotHasKey('connectionone', (array) $connectionExpiries);
4144
$this->assertArrayHasKey('connectiontwo', (array) $connectionExpiries);
4245
}
46+
47+
public function testOnKernelRequestShouldSkipSubrequests()
48+
{
49+
self::expectNotToPerformAssertions();
50+
$arrayObj = $this->createMock(\ArrayObject::class);
51+
$arrayObj->method('getIterator')->willThrowException(new \Exception('Invalid behavior'));
52+
$listener = new Listener($arrayObj, $this->createMock(ContainerInterface::class));
53+
54+
$event = $this->createMock(RequestEvent::class);
55+
$event->method('getRequestType')->willReturn(HttpKernelInterface::SUB_REQUEST);
56+
$listener->onKernelRequest($event);
57+
}
4358
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy