Skip to content

Commit afbeeb7

Browse files
authored
Merge pull request #4 from krakphp/support-redis-db-from-path
Support Redis DB From Path
2 parents 1649444 + 1ac8cda commit afbeeb7

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ Here are the available options that can be provided to the transport options arr
6969
- *required:* yes
7070
- *default:* N/A
7171
- *description:* The internal list name to use for storing the messages in redis. The system will also create a processing queue named `{queue}_processing` for storing the processed messages.
72+
- **db:**
73+
- *required:* no
74+
- *default:* N/A
75+
- *description:* The redis database to select when performing redis operations. This value can also be set from the dsn via a query parameter or by setting the path in the dsn (`redis//redis:6379?db=1` or `redis://redis:6379/1`).
7276

7377
### Processed Queue
7478

src/Transport/RedisTransport.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ function(Redis $conn) use ($query, $parsedUrl, $options) {
5858
if ($auth) {
5959
$conn->auth($auth);
6060
}
61-
$db = $options['db'] ?? $query['db'] ?? null;
61+
$db = $parsedUrl['path'] ?? $options['db'] ?? $query['db'] ?? null;
6262
if ($db !== null) {
63-
$db = intval($db);
63+
$db = intval(ltrim($db, '/'));
6464
$conn->select($db);
6565
}
6666
}

tests/Feature/TransportTest.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,23 @@ protected function setUp() {
2929
$this->redis->flushAll();
3030
}
3131

32-
/** @test */
33-
public function allows_custom_db() {
34-
$this->given_the_redis_transport_contains_db();
32+
/**
33+
* @test
34+
* @dataProvider provide_custom_redis_urls_with_db
35+
*/
36+
public function allows_custom_db(string $place) {
37+
$this->given_the_redis_transport_contains_db('1', $place);
3538
$this->given_there_is_a_wrapped_message();
36-
$this->when_the_message_is_sent_received_and_acked();
39+
$this->when_the_message_is_sent_on_the_transport();
3740
$this->then_the_queues_are_empty();
3841
}
3942

43+
public function provide_custom_redis_urls_with_db() {
44+
yield 'db in query params' => ['query_string'];
45+
yield 'db in path' => ['path'];
46+
yield 'db in options' => ['options'];
47+
}
48+
4049
/** @test */
4150
public function can_send_a_message() {
4251
$this->given_there_is_a_wrapped_message();
@@ -199,8 +208,17 @@ public function then_the_message_is_available_after(int $delayMs) {
199208
$this->assertCount(1, $res);
200209
}
201210

202-
public function given_the_redis_transport_contains_db()
211+
public function given_the_redis_transport_contains_db(string $db, string $place)
203212
{
204-
$this->transport = RedisTransport::fromDsn(Serializer::create(), getenv('REDIS_DSN'), ['db' => 2]);
213+
if ($place === 'options') {
214+
$this->transport = RedisTransport::fromDsn(Serializer::create(), getenv('REDIS_DSN'), ['db' => $db]);
215+
} else if ($place === 'query_string') {
216+
$this->transport = RedisTransport::fromDsn(Serializer::create(), getenv('REDIS_DSN').'&db='.$db);
217+
} else if ($place === 'path') {
218+
[$base, $query] = explode('?', getenv('REDIS_DSN'));
219+
$this->transport = RedisTransport::fromDsn(Serializer::create(), $base . '/'.$db . '?' . $query);
220+
} else {
221+
throw new \LogicException('Invalid place: ' . $place);
222+
}
205223
}
206224
}

0 commit comments

Comments
 (0)