Skip to content

Commit e316cac

Browse files
committed
[redis] Add ability to pass instance of Redis instance.
1 parent 5ed3224 commit e316cac

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

RedisConnectionFactory.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class RedisConnectionFactory implements PsrConnectionFactory
2525
* 'reserved' => should be null if $retry_interval is specified
2626
* 'retry_interval' => retry interval in milliseconds.
2727
* 'vendor' => 'The library used internally to interact with Redis server
28+
* 'redis' => 'Used only if vendor is custom, should contain an instance of \Enqueue\Redis\Redis interface.
2829
* 'persisted' => bool, Whether it use single persisted connection or open a new one for every context
2930
* 'lazy' => the connection will be performed as later as possible, if the option set to true
3031
* 'database' => Database index to select when connected (default value: 0)
@@ -50,7 +51,7 @@ public function __construct($config = 'redis:')
5051

5152
$this->config = array_replace($this->defaultConfig(), $config);
5253

53-
$supportedVendors = ['predis', 'phpredis'];
54+
$supportedVendors = ['predis', 'phpredis', 'custom'];
5455
if (false == in_array($this->config['vendor'], $supportedVendors, true)) {
5556
throw new \LogicException(sprintf(
5657
'Unsupported redis vendor given. It must be either "%s". Got "%s"',
@@ -90,6 +91,18 @@ private function createRedis()
9091
$this->redis = new PRedis(new Client($this->config, ['exceptions' => true]));
9192
}
9293

94+
if ('custom' == $this->config['vendor'] && false == $this->redis) {
95+
if (empty($this->config['redis'])) {
96+
throw new \LogicException('The redis option should be set if vendor is custom.');
97+
}
98+
99+
if (false == $this->config['redis'] instanceof Redis) {
100+
throw new \LogicException(sprintf('The redis option should be instance of "%s".', Redis::class));
101+
}
102+
103+
$this->redis = $this->config['redis'];
104+
}
105+
93106
$this->redis->connect();
94107
}
95108

@@ -138,6 +151,7 @@ private function defaultConfig()
138151
'reserved' => null,
139152
'retry_interval' => null,
140153
'vendor' => 'phpredis',
154+
'redis' => null,
141155
'persisted' => false,
142156
'lazy' => true,
143157
'database' => 0,

Tests/RedisConnectionFactoryConfigTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testThrowIfDsnCouldNotBeParsed()
4040
public function testThrowIfVendorIsInvalid()
4141
{
4242
$this->expectException(\LogicException::class);
43-
$this->expectExceptionMessage('Unsupported redis vendor given. It must be either "predis", "phpredis". Got "invalidVendor"');
43+
$this->expectExceptionMessage('Unsupported redis vendor given. It must be either "predis", "phpredis", "custom". Got "invalidVendor"');
4444

4545
new RedisConnectionFactory(['vendor' => 'invalidVendor']);
4646
}
@@ -72,6 +72,7 @@ public static function provideConfigs()
7272
'persisted' => false,
7373
'lazy' => true,
7474
'database' => 0,
75+
'redis' => null,
7576
],
7677
];
7778

@@ -87,6 +88,7 @@ public static function provideConfigs()
8788
'persisted' => false,
8889
'lazy' => true,
8990
'database' => 0,
91+
'redis' => null,
9092
],
9193
];
9294

@@ -102,6 +104,7 @@ public static function provideConfigs()
102104
'persisted' => false,
103105
'lazy' => true,
104106
'database' => 0,
107+
'redis' => null,
105108
],
106109
];
107110

@@ -118,6 +121,7 @@ public static function provideConfigs()
118121
'lazy' => false,
119122
'foo' => 'bar',
120123
'database' => 5,
124+
'redis' => null,
121125
],
122126
];
123127

@@ -134,6 +138,7 @@ public static function provideConfigs()
134138
'lazy' => true,
135139
'foo' => 'bar',
136140
'database' => 0,
141+
'redis' => null,
137142
],
138143
];
139144
}

Tests/RedisConnectionFactoryTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Enqueue\Redis\Tests;
44

5+
use Enqueue\Redis\Redis;
56
use Enqueue\Redis\RedisConnectionFactory;
67
use Enqueue\Redis\RedisContext;
78
use Enqueue\Test\ClassExtensionTrait;
@@ -28,4 +29,45 @@ public function testShouldCreateLazyContext()
2829
$this->assertAttributeEquals(null, 'redis', $context);
2930
$this->assertInternalType('callable', $this->readAttribute($context, 'redisFactory'));
3031
}
32+
33+
public function testShouldThrowIfVendorIsCustomButRedisInstanceNotSet()
34+
{
35+
$factory = new RedisConnectionFactory([
36+
'vendor' => 'custom',
37+
'redis' => null,
38+
'lazy' => false,
39+
]);
40+
41+
$this->expectException(\LogicException::class);
42+
$this->expectExceptionMessage('The redis option should be set if vendor is custom.');
43+
$factory->createContext();
44+
}
45+
46+
public function testShouldThrowIfVendorIsCustomButRedisIsNotInstanceOfRedis()
47+
{
48+
$factory = new RedisConnectionFactory([
49+
'vendor' => 'custom',
50+
'redis' => new \stdClass(),
51+
'lazy' => false,
52+
]);
53+
54+
$this->expectException(\LogicException::class);
55+
$this->expectExceptionMessage('The redis option should be instance of "Enqueue\Redis\Redis".');
56+
$factory->createContext();
57+
}
58+
59+
public function testShouldUseCustomRedisInstance()
60+
{
61+
$redisMock = $this->createMock(Redis::class);
62+
63+
$factory = new RedisConnectionFactory([
64+
'vendor' => 'custom',
65+
'redis' => $redisMock,
66+
'lazy' => false,
67+
]);
68+
69+
$context = $factory->createContext();
70+
71+
$this->assertAttributeSame($redisMock, 'redis', $context);
72+
}
3173
}

0 commit comments

Comments
 (0)