Skip to content

Commit fc85af8

Browse files
committed
Merge branch 'master' into 0.8
2 parents b625fff + 61698a9 commit fc85af8

File tree

3 files changed

+201
-45
lines changed

3 files changed

+201
-45
lines changed

RedisConnectionFactory.php

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,25 @@ class RedisConnectionFactory implements PsrConnectionFactory
2929
* 'lazy' => the connection will be performed as later as possible, if the option set to true
3030
* ].
3131
*
32-
* @param $config
32+
* or
33+
*
34+
* redis:
35+
* redis:?vendor=predis
36+
*
37+
* @param array|string|null $config
3338
*/
34-
public function __construct(array $config)
39+
public function __construct($config = 'redis:')
3540
{
36-
$this->config = array_replace([
37-
'host' => null,
38-
'port' => null,
39-
'timeout' => null,
40-
'reserved' => null,
41-
'retry_interval' => null,
42-
'vendor' => 'phpredis',
43-
'persisted' => false,
44-
'lazy' => true,
45-
], $config);
41+
if (empty($config) || 'redis:' === $config) {
42+
$config = [];
43+
} elseif (is_string($config)) {
44+
$config = $this->parseDsn($config);
45+
} elseif (is_array($config)) {
46+
} else {
47+
throw new \LogicException('The config must be either an array of options, a DSN string or null');
48+
}
49+
50+
$this->config = array_replace($this->defaultConfig(), $config);
4651

4752
$supportedVendors = ['predis', 'phpredis'];
4853
if (false == in_array($this->config['vendor'], $supportedVendors, true)) {
@@ -89,4 +94,51 @@ private function createRedis()
8994

9095
return $this->redis;
9196
}
97+
98+
/**
99+
* @param string $dsn
100+
*
101+
* @return array
102+
*/
103+
private function parseDsn($dsn)
104+
{
105+
if (false === strpos($dsn, 'redis:')) {
106+
throw new \LogicException(sprintf('The given DSN "%s" is not supported. Must start with "redis:".', $dsn));
107+
}
108+
109+
if (false === $config = parse_url($dsn)) {
110+
throw new \LogicException(sprintf('Failed to parse DSN "%s"', $dsn));
111+
}
112+
113+
if ($query = parse_url($dsn, PHP_URL_QUERY)) {
114+
$queryConfig = [];
115+
parse_str($query, $queryConfig);
116+
117+
$config = array_replace($queryConfig, $config);
118+
}
119+
120+
unset($config['query'], $config['scheme']);
121+
122+
$config['lazy'] = empty($config['lazy']) ? false : true;
123+
$config['persisted'] = empty($config['persisted']) ? false : true;
124+
125+
return $config;
126+
}
127+
128+
/**
129+
* @return array
130+
*/
131+
private function defaultConfig()
132+
{
133+
return [
134+
'host' => 'localhost',
135+
'port' => 6379,
136+
'timeout' => null,
137+
'reserved' => null,
138+
'retry_interval' => null,
139+
'vendor' => 'phpredis',
140+
'persisted' => false,
141+
'lazy' => true,
142+
];
143+
}
92144
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
3+
namespace Enqueue\Redis\Tests;
4+
5+
use Enqueue\Redis\RedisConnectionFactory;
6+
use Enqueue\Test\ClassExtensionTrait;
7+
use PHPUnit\Framework\TestCase;
8+
9+
/**
10+
* The class contains the factory tests dedicated to configuration.
11+
*/
12+
class RedisConnectionFactoryConfigTest extends TestCase
13+
{
14+
use ClassExtensionTrait;
15+
16+
public function testThrowNeitherArrayStringNorNullGivenAsConfig()
17+
{
18+
$this->expectException(\LogicException::class);
19+
$this->expectExceptionMessage('The config must be either an array of options, a DSN string or null');
20+
21+
new RedisConnectionFactory(new \stdClass());
22+
}
23+
24+
public function testThrowIfSchemeIsNotAmqp()
25+
{
26+
$this->expectException(\LogicException::class);
27+
$this->expectExceptionMessage('The given DSN "http://example.com" is not supported. Must start with "redis:".');
28+
29+
new RedisConnectionFactory('http://example.com');
30+
}
31+
32+
public function testThrowIfDsnCouldNotBeParsed()
33+
{
34+
$this->expectException(\LogicException::class);
35+
$this->expectExceptionMessage('Failed to parse DSN "redis://:@/"');
36+
37+
new RedisConnectionFactory('redis://:@/');
38+
}
39+
40+
public function testThrowIfVendorIsInvalid()
41+
{
42+
$this->expectException(\LogicException::class);
43+
$this->expectExceptionMessage('Unsupported redis vendor given. It must be either "predis", "phpredis". Got "invalidVendor"');
44+
45+
new RedisConnectionFactory(['vendor' => 'invalidVendor']);
46+
}
47+
48+
/**
49+
* @dataProvider provideConfigs
50+
*
51+
* @param mixed $config
52+
* @param mixed $expectedConfig
53+
*/
54+
public function testShouldParseConfigurationAsExpected($config, $expectedConfig)
55+
{
56+
$factory = new RedisConnectionFactory($config);
57+
58+
$this->assertAttributeEquals($expectedConfig, 'config', $factory);
59+
}
60+
61+
public static function provideConfigs()
62+
{
63+
yield [
64+
null,
65+
[
66+
'host' => 'localhost',
67+
'port' => 6379,
68+
'timeout' => null,
69+
'reserved' => null,
70+
'retry_interval' => null,
71+
'vendor' => 'phpredis',
72+
'persisted' => false,
73+
'lazy' => true,
74+
],
75+
];
76+
77+
yield [
78+
'redis:',
79+
[
80+
'host' => 'localhost',
81+
'port' => 6379,
82+
'timeout' => null,
83+
'reserved' => null,
84+
'retry_interval' => null,
85+
'vendor' => 'phpredis',
86+
'persisted' => false,
87+
'lazy' => true,
88+
],
89+
];
90+
91+
yield [
92+
[],
93+
[
94+
'host' => 'localhost',
95+
'port' => 6379,
96+
'timeout' => null,
97+
'reserved' => null,
98+
'retry_interval' => null,
99+
'vendor' => 'phpredis',
100+
'persisted' => false,
101+
'lazy' => true,
102+
],
103+
];
104+
105+
yield [
106+
'redis://localhost:1234?foo=bar&lazy=0&persisted=true',
107+
[
108+
'host' => 'localhost',
109+
'port' => 1234,
110+
'timeout' => null,
111+
'reserved' => null,
112+
'retry_interval' => null,
113+
'vendor' => 'phpredis',
114+
'persisted' => true,
115+
'lazy' => false,
116+
'foo' => 'bar',
117+
],
118+
];
119+
120+
yield [
121+
['host' => 'localhost', 'port' => 1234, 'foo' => 'bar'],
122+
[
123+
'host' => 'localhost',
124+
'port' => 1234,
125+
'timeout' => null,
126+
'reserved' => null,
127+
'retry_interval' => null,
128+
'vendor' => 'phpredis',
129+
'persisted' => false,
130+
'lazy' => true,
131+
'foo' => 'bar',
132+
],
133+
];
134+
}
135+
}

Tests/RedisConnectionFactoryTest.php

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
use Enqueue\Redis\RedisContext;
77
use Enqueue\Test\ClassExtensionTrait;
88
use Interop\Queue\PsrConnectionFactory;
9+
use PHPUnit\Framework\TestCase;
910

10-
class RedisConnectionFactoryTest extends \PHPUnit\Framework\TestCase
11+
class RedisConnectionFactoryTest extends TestCase
1112
{
1213
use ClassExtensionTrait;
1314

@@ -16,38 +17,6 @@ public function testShouldImplementConnectionFactoryInterface()
1617
$this->assertClassImplements(PsrConnectionFactory::class, RedisConnectionFactory::class);
1718
}
1819

19-
public function testCouldBeConstructedWithEmptyConfiguration()
20-
{
21-
$factory = new RedisConnectionFactory([]);
22-
23-
$this->assertAttributeEquals([
24-
'host' => null,
25-
'port' => null,
26-
'timeout' => null,
27-
'reserved' => null,
28-
'retry_interval' => null,
29-
'persisted' => false,
30-
'lazy' => true,
31-
'vendor' => 'phpredis',
32-
], 'config', $factory);
33-
}
34-
35-
public function testCouldBeConstructedWithCustomConfiguration()
36-
{
37-
$factory = new RedisConnectionFactory(['host' => 'theCustomHost']);
38-
39-
$this->assertAttributeEquals([
40-
'host' => 'theCustomHost',
41-
'port' => null,
42-
'timeout' => null,
43-
'reserved' => null,
44-
'retry_interval' => null,
45-
'persisted' => false,
46-
'lazy' => true,
47-
'vendor' => 'phpredis',
48-
], 'config', $factory);
49-
}
50-
5120
public function testShouldCreateLazyContext()
5221
{
5322
$factory = new RedisConnectionFactory(['lazy' => true]);

0 commit comments

Comments
 (0)