From 3f710c2e5eac0b1d19ff4c2c99012eff367becc8 Mon Sep 17 00:00:00 2001 From: David Maicher Date: Tue, 30 Nov 2021 21:15:45 +0100 Subject: [PATCH] [HttpFoundation] fix SessionHandlerFactory using connections --- .../Storage/Handler/SessionHandlerFactory.php | 2 +- .../Handler/SessionHandlerFactoryTest.php | 31 ++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/SessionHandlerFactory.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/SessionHandlerFactory.php index 0f1ce5c2b8ecd..f3f7b201d9dea 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/SessionHandlerFactory.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/SessionHandlerFactory.php @@ -30,7 +30,7 @@ public static function createHandler($connection): AbstractSessionHandler throw new \TypeError(sprintf('Argument 1 passed to "%s()" must be a string or a connection object, "%s" given.', __METHOD__, get_debug_type($connection))); } - if ($options = parse_url($connection)) { + if ($options = \is_string($connection) ? parse_url($connection) : false) { parse_str($options['query'] ?? '', $options); } diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/SessionHandlerFactoryTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/SessionHandlerFactoryTest.php index 46d6cd40151d5..9f06a7c8675da 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/SessionHandlerFactoryTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/SessionHandlerFactoryTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler; use PHPUnit\Framework\TestCase; +use Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler; use Symfony\Component\HttpFoundation\Session\Storage\Handler\SessionHandlerFactory; use Symfony\Component\HttpFoundation\Session\Storage\Handler\StrictSessionHandler; @@ -28,7 +29,7 @@ class SessionHandlerFactoryTest extends TestCase /** * @dataProvider provideConnectionDSN */ - public function testCreateHandler(string $connectionDSN, string $expectedPath, string $expectedHandlerType) + public function testCreateFileHandler(string $connectionDSN, string $expectedPath, string $expectedHandlerType) { $handler = SessionHandlerFactory::createHandler($connectionDSN); @@ -45,4 +46,32 @@ public function provideConnectionDSN(): array 'native file handler using provided save_path' => ['connectionDSN' => 'file://'.$base.'/session/storage', 'expectedPath' => $base.'/session/storage', 'expectedHandlerType' => StrictSessionHandler::class], ]; } + + /** + * @requires extension redis + */ + public function testCreateRedisHandlerFromConnectionObject() + { + $handler = SessionHandlerFactory::createHandler($this->createMock(\Redis::class)); + $this->assertInstanceOf(RedisSessionHandler::class, $handler); + } + + /** + * @requires extension redis + */ + public function testCreateRedisHandlerFromDsn() + { + $handler = SessionHandlerFactory::createHandler('redis://localhost?prefix=foo&ttl=3600&ignored=bar'); + $this->assertInstanceOf(RedisSessionHandler::class, $handler); + + $reflection = new \ReflectionObject($handler); + + $prefixProperty = $reflection->getProperty('prefix'); + $prefixProperty->setAccessible(true); + $this->assertSame('foo', $prefixProperty->getValue($handler)); + + $ttlProperty = $reflection->getProperty('ttl'); + $ttlProperty->setAccessible(true); + $this->assertSame('3600', $ttlProperty->getValue($handler)); + } }