Skip to content

Commit 41ddf23

Browse files
committed
Merge branch 'master' into pr-350
2 parents efde844 + 1cc7c76 commit 41ddf23

File tree

6 files changed

+109
-11
lines changed

6 files changed

+109
-11
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,

Symfony/RedisTransportFactory.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,14 @@ public function addConfiguration(ArrayNodeDefinition $builder)
4949
->end()
5050
->integerNode('port')->end()
5151
->enumNode('vendor')
52-
->values(['phpredis', 'predis'])
52+
->values(['phpredis', 'predis', 'custom'])
5353
->cannotBeEmpty()
5454
->info('The library used internally to interact with Redis server')
5555
->end()
56+
->scalarNode('redis')
57+
->cannotBeEmpty()
58+
->info('A custom redis service id, used with vendor true only')
59+
->end()
5660
->booleanNode('persisted')
5761
->defaultFalse()
5862
->info('bool, Whether it use single persisted connection or open a new one for every context')
@@ -73,6 +77,10 @@ public function addConfiguration(ArrayNodeDefinition $builder)
7377
*/
7478
public function createConnectionFactory(ContainerBuilder $container, array $config)
7579
{
80+
if (false == empty($config['redis'])) {
81+
$config['redis'] = new Reference($config['redis']);
82+
}
83+
7684
$factory = new Definition(RedisConnectionFactory::class);
7785
$factory->setArguments([isset($config['dsn']) ? $config['dsn'] : $config]);
7886

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
}

Tests/Symfony/RedisTransportFactoryTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,35 @@ public function testShouldCreateConnectionFactory()
104104
]], $factory->getArguments());
105105
}
106106

107+
public function testShouldCreateConnectionFactoryWithCustomRedisInstance()
108+
{
109+
$container = new ContainerBuilder();
110+
111+
$transport = new RedisTransportFactory();
112+
113+
$serviceId = $transport->createConnectionFactory($container, [
114+
'host' => 'localhost',
115+
'port' => 123,
116+
'vendor' => 'custom',
117+
'redis' => 'a.redis.service',
118+
]);
119+
120+
$this->assertTrue($container->hasDefinition($serviceId));
121+
$factory = $container->getDefinition($serviceId);
122+
$this->assertEquals(RedisConnectionFactory::class, $factory->getClass());
123+
124+
$config = $factory->getArgument(0);
125+
126+
$this->assertInternalType('array', $config);
127+
128+
$this->assertArrayHasKey('vendor', $config);
129+
$this->assertSame('custom', $config['vendor']);
130+
131+
$this->assertArrayHasKey('redis', $config);
132+
$this->assertInstanceOf(Reference::class, $config['redis']);
133+
$this->assertSame('a.redis.service', (string) $config['redis']);
134+
}
135+
107136
public function testShouldCreateContext()
108137
{
109138
$container = new ContainerBuilder();

composer.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
"homepage": "https://enqueue.forma-pro.com/",
77
"license": "MIT",
88
"require": {
9-
"php": ">=5.6",
10-
"queue-interop/queue-interop": "^0.6@dev"
9+
"php": "^7.1.3",
10+
"queue-interop/queue-interop": "^0.6.2"
1111
},
1212
"require-dev": {
1313
"phpunit/phpunit": "~5.4.0",
1414
"predis/predis": "^1.1",
15-
"enqueue/test": "^0.8@dev",
16-
"enqueue/enqueue": "^0.8@dev",
17-
"enqueue/null": "^0.8@dev",
15+
"enqueue/test": "0.9.x-dev",
16+
"enqueue/enqueue": "0.9.x-dev",
17+
"enqueue/null": "0.9.x-dev",
1818
"queue-interop/queue-spec": "^0.5.3@dev",
19-
"symfony/dependency-injection": "^2.8|^3|^4",
20-
"symfony/config": "^2.8|^3|^4"
19+
"symfony/dependency-injection": "^3.4|^4",
20+
"symfony/config": "^3.4|^4"
2121
},
2222
"support": {
2323
"email": "opensource@forma-pro.com",
@@ -40,7 +40,7 @@
4040
"minimum-stability": "dev",
4141
"extra": {
4242
"branch-alias": {
43-
"dev-master": "0.8.x-dev"
43+
"dev-master": "0.9.x-dev"
4444
}
4545
}
4646
}

0 commit comments

Comments
 (0)