8000 [Messenger] Fix ignored options in redis transport · symfony/symfony@c83ff94 · GitHub
[go: up one dir, main page]

Skip to content

Commit c83ff94

Browse files
committed
[Messenger] Fix ignored options in redis transport
1 parent 537d373 commit c83ff94

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/Symfony/Component/Messenger/Tests/Transport/RedisExt/ConnectionTest.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,8 @@ public function testFromDsn()
5757
public function testFromDsnWithOptions()
5858
{
5959
$this->assertEquals(
60-
new Connection(['stream' => 'queue', 'group' => 'group1', 'consumer' => 'consumer1', 'auto_setup' => false], [
61-
'host' => 'localhost',
62-
'port' => 6379,
63-
], [
64-
'serializer' => 2,
65-
]),
66-
Connection::fromDsn('redis://localhost/queue/group1/consumer1', ['serializer' => 2, 'auto_setup' => false])
60+
Connection::fromDsn('redis://localhost', ['stream' => 'queue', 'group' => 'group1', 'consumer' => 'consumer1', 'auto_setup' => false, 'serializer' => 2]),
61+
Connection::fromDsn('redis://localhost/queue/group1/consumer1?serializer=2&auto_setup=0')
6762
);
6863
}
6964

@@ -99,7 +94,21 @@ public function testAuth()
9994
$redis = $this->getMockBuilder(\Redis::class)->disableOriginalConstructor()->getMock();
10095

10196
$redis->expects($this->exactly(1))->method('auth')
102-
->with('password');
97+
->with('password')
98+
->willReturn(true);
99+
100+
Connection::fromDsn('redis://password@localhost/queue', [], $redis);
101+
}
102+
103+
public function testFailedAuth()
104+
{
105+
$this->expectException(\InvalidArgumentException::class);
106+
$this->expectExceptionMessage('Redis connection failed');
107+
$redis = $this->getMockBuilder(\Redis::class)->disableOriginalConstructor()->getMock();
108+
109+
$redis->expects($this->exactly(1))->method('auth')
110+
->with('password')
111+
->willReturn(false);
103112

104113
Connection::fromDsn('redis://password@localhost/queue', [], $redis);
105114
}

src/Symfony/Component/Messenger/Transport/RedisExt/Connection.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*
2121
* @author Alexander Schranz <alexander@sulu.io>
2222
* @author Antoine Bluchet <soyuka@gmail.com>
23+
* @author Robin Chalas <robin.chalas@gmail.com>
2324
*
2425
* @internal
2526
* @final
@@ -52,8 +53,8 @@ public function __construct(array $configuration, array $connectionCredentials =
5253
$this->connection->connect($connectionCredentials['host'] ?? '127.0.0.1', $connectionCredentials['port'] ?? 6379);
5354
$this->connection->setOption(\Redis::OPT_SERIALIZER, $redisOptions['serializer'] ?? \Redis::SERIALIZER_PHP);
5455

55-
if (isset($connectionCredentials['auth'])) {
56-
$this->connection->auth($connectionCredentials['auth']);
56+
if (isset($connectionCredentials['auth']) && !$this->connection->auth($connectionCredentials['auth'])) {
57+
throw new InvalidArgumentException(sprintf('Redis connection failed: %s', $redis->getLastError()));
5758
}
5859

5960
$this->stream = $configuration['stream'] ?? self::DEFAULT_OPTIONS['stream'];
@@ -70,9 +71,9 @@ public static function fromDsn(string $dsn, array $redisOptions = [], \Redis $re
7071

7172
$pathParts = explode('/', $parsedUrl[ 74B4 'path'] ?? '');
7273

73-
$stream = $pathParts[1] ?? null;
74-
$group = $pathParts[2] ?? null;
75-
$consumer = $pathParts[3] ?? null;
74+
$stream = $pathParts[1] ?? $redisOptions['stream'] ?? null;
75+
$group = $pathParts[2] ?? $redisOptions['group'] ?? null;
76+
$consumer = $pathParts[3] ?? $redisOptions['consumer'] ?? null;
7677

7778
$connectionCredentials = [
7879
'host' => $parsedUrl['host'] ?? '127.0.0.1',

0 commit comments

Comments
 (0)
0