8000 [HttpFoundation] fix SessionHandlerFactory using connections · symfony/symfony@3f710c2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3f710c2

Browse files
dmaichernicolas-grekas
authored andcommitted
[HttpFoundation] fix SessionHandlerFactory using connections
1 parent 4a07e06 commit 3f710c2

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/Symfony/Component/HttpFoundation/Session/Storage/Handler/SessionHandlerFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static function createHandler($connection): AbstractSessionHandler
3030
throw new \TypeError(sprintf('Argument 1 passed to "%s()" must be a string or a connection object, "%s" given.', __METHOD__, get_debug_type($connection)));
3131
}
3232

33-
if ($options = parse_url($connection)) {
33+
if ($options = \is_string($connection) ? parse_url($connection) : false) {
3434
parse_str($options['query'] ?? '', $options);
3535
}
3636

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/SessionHandlerFactoryTest.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler;
1516
use Symfony\Component\HttpFoundation\Session\Storage\Handler\SessionHandlerFactory;
1617
use Symfony\Component\HttpFoundation\Session\Storage\Handler\StrictSessionHandler;
1718

@@ -28,7 +29,7 @@ class SessionHandlerFactoryTest extends TestCase
2829
/**
2930
* @dataProvider provideConnectionDSN
3031
*/
31-
public function testCreateHandler(string $connectionDSN, string $expectedPath, string $expectedHandlerType)
32+
public function testCreateFileHandler(string $connectionDSN, string $expectedPath, string $expectedHandlerType)
3233
{
3334
$handler = SessionHandlerFactory::createHandler($connectionDSN);
3435

@@ -45,4 +46,32 @@ public function provideConnectionDSN(): array
4546
'native file handler using provided save_path' => ['connectionDSN' => 'file://'.$base.'/session/storage', 'expectedPath' => $base.'/session/storage', 'expectedHandlerType' => StrictSessionHandler::class],
4647
];
4748
}
49+
50+
/**
51+
* @requires extension redis
52+
*/
53+
public function testCreateRedisHandlerFromConnectionObject()
54+
{
55+
$handler = SessionHandlerFactory::createHandler($this->createMock(\Redis::class));
56+
$this->assertInstanceOf(RedisSessionHandler::class, $handler);
57+
}
58+
59+
/**
60+
* @requires extension redis
61+
*/
62+
public function testCreateRedisHandlerFromDsn()
63+
{
64+
$handler = SessionHandlerFactory::createHandler('redis://localhost?prefix=foo&ttl=3600&ignored=bar');
65+
$this->assertInstanceOf(RedisSessionHandler::class, $handler);
66+
67+
$reflection = new \ReflectionObject($handler);
68+
69+
$prefixProperty = $reflection->getProperty('prefix');
70+
$prefixProperty->setAccessible(true);
71+
$this->assertSame('foo', $prefixProperty->getValue($handler));
72+
73+
$ttlProperty = $reflection->getProperty('ttl');
74+
$ttlProperty->setAccessible(true);
75+
$this->assertSame('3600', $ttlProperty->getValue($handler));
76+
}
4877
}

0 commit comments

Comments
 (0)
0