Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;

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

public function onKernelRequest(RequestEvent $event): void
{
if (HttpKernelInterface::MAIN_REQUEST !== $event->getRequestType()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually use isMainRequest()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible, but the same logic, thanks

return;
}
$timestamp = time();

foreach ($this->connectionExpiries as $name => $expiry) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
* file that was distributed with this source code.
*/

namespace Middleware\IdleConnection;
namespace Symfony\Bridge\Doctrine\Tests\Middleware\IdleConnection;

use Doctrine\DBAL\Connection as ConnectionInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\Middleware\IdleConnection\Listener;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class ListenerTest extends TestCase
{
Expand All @@ -34,10 +35,24 @@ public function testOnKernelRequest()
->willReturn($connectionOneMock);

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

$listener->onKernelRequest($this->createMock(RequestEvent::class));
$listener->onKernelRequest($event);

$this->assertArrayNotHasKey('connectionone', (array) $connectionExpiries);
$this->assertArrayHasKey('connectiontwo', (array) $connectionExpiries);
}

public function testOnKernelRequestShouldSkipSubrequests()
{
self::expectNotToPerformAssertions();
$arrayObj = $this->createMock(\ArrayObject::class);
$arrayObj->method('getIterator')->willThrowException(new \Exception('Invalid behavior'));
$listener = new Listener($arrayObj, $this->createMock(ContainerInterface::class));

$event = $this->createMock(RequestEvent::class);
$event->method('getRequestType')->willReturn(HttpKernelInterface::SUB_REQUEST);
$listener->onKernelRequest($event);
}
}
Loading