From fb494d107005cc488865db5e25868216b51615b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Mon, 30 Dec 2024 16:02:09 +0100 Subject: [PATCH] Fix `RedisClient` to pass correct password to internal `Factory` --- src/RedisClient.php | 2 +- tests/RedisClientTest.php | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/RedisClient.php b/src/RedisClient.php index 29d4854..688e584 100644 --- a/src/RedisClient.php +++ b/src/RedisClient.php @@ -72,8 +72,8 @@ public function __construct(string $uri, ?ConnectorInterface $connector = null) $parts = parse_url($uri); } - $uri = (string) preg_replace(['/(:)[^:\/]*(@)/', '/([?&]password=).*?($|&)/'], '$1***$2', $uri); if ($parts === false || !isset($parts['scheme'], $parts['host']) || !in_array($parts['scheme'], ['redis', 'rediss', 'redis+unix'])) { + $uri = (string) preg_replace(['/(:)[^:\/]*(@)/', '/([?&]password=).*?($|&)/'], '$1***$2', $uri); throw new \InvalidArgumentException( 'Invalid Redis URI "' . $uri . '" (EINVAL)', defined('SOCKET_EINVAL') ? SOCKET_EINVAL : 22 diff --git a/tests/RedisClientTest.php b/tests/RedisClientTest.php index 4fd77a6..99d6b63 100644 --- a/tests/RedisClientTest.php +++ b/tests/RedisClientTest.php @@ -108,7 +108,27 @@ public function testCtorWithInvalidUriThrows(string $uri, string $message): void public function testPingWillCreateUnderlyingClientAndReturnPendingPromise(): void { $promise = new Promise(function () { }); - $this->factory->expects($this->once())->method('createClient')->willReturn($promise); + $this->factory->expects($this->once())->method('createClient')->with('redis://localhost')->willReturn($promise); + + $loop = $this->createMock(LoopInterface::class); + $loop->expects($this->never())->method('addTimer'); + assert($loop instanceof LoopInterface); + Loop::set($loop); + + $promise = $this->redis->ping(); + + $promise->then($this->expectCallableNever()); + } + + public function testPingWithAuthWillCreateUnderlyingClientWithAuthAndReturnPendingPromise(): void + { + $this->redis = new RedisClient('user:pass@localhost'); + $ref = new \ReflectionProperty($this->redis, 'factory'); + $ref->setAccessible(true); + $ref->setValue($this->redis, $this->factory); + + $promise = new Promise(function () { }); + $this->factory->expects($this->once())->method('createClient')->with('redis://user:pass@localhost')->willReturn($promise); $loop = $this->createMock(LoopInterface::class); $loop->expects($this->never())->method('addTimer');