Skip to content

Commit dfbc2c0

Browse files
Merge branch '5.4' into 6.0
* 5.4: Avoid duplicated session listener registration in tests [HttpFoundation] fix SessionHandlerFactory using connections [gha] swap the php versions we use in jobs [DoctrineBridge] fix calling get_class on non-object Update PR template ResponseListener needs only 2 parameters [Lock] create lock table if it does not exist [HttpClient] Fix handling error info in MockResponse [SecurityBundle] Fix invalid reference with `always_authenticate_before_granting` Bump Symfony version to 5.4.1 Update VERSION for 5.4.0 Update CHANGELOG for 5.4.0
2 parents 4c4b5be + f07daa1 commit dfbc2c0

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

Session/Storage/Handler/SessionHandlerFactory.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@ class SessionHandlerFactory
2323
{
2424
public static function createHandler(object|string $connection): AbstractSessionHandler
2525
{
26-
if (\is_string($connection) && $options = parse_url($connection)) {
26+
if ($options = \is_string($connection) ? parse_url($connection) : false) {
2727
parse_str($options['query'] ?? '', $options);
28-
} else {
29-
$options = [];
3028
}
3129

3230
switch (true) {
@@ -60,7 +58,7 @@ public static function createHandler(object|string $connection): AbstractSession
6058
$handlerClass = str_starts_with($connection, 'memcached:') ? MemcachedSessionHandler::class : RedisSessionHandler::class;
6159
$connection = AbstractAdapter::createConnection($connection, ['lazy' => true]);
6260

63-
return new $handlerClass($connection, array_intersect_key($options, ['prefix' => 1, 'ttl' => 1]));
61+
return new $handlerClass($connection, array_intersect_key($options ?: [], ['prefix' => 1, 'ttl' => 1]));
6462

6563
case str_starts_with($connection, 'pdo_oci://'):
6664
if (!class_exists(DriverManager::class)) {

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)