Skip to content

Commit

Permalink
[redis] Add ability to pass instance of Redis instance.
Browse files Browse the repository at this point in the history
  • Loading branch information
makasim committed Feb 15, 2018
1 parent a9357a9 commit 3791b63
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 7 deletions.
10 changes: 5 additions & 5 deletions bin/dev
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ set -e
while getopts "bustefcdp" OPTION; do
case $OPTION in
b)
COMPOSE_PROJECT_NAME=mqdev docker-compose pull && COMPOSE_PROJECT_NAME=mqdev docker-compose build
docker-compose pull && docker-compose build
;;
u)
COMPOSE_PROJECT_NAME=mqdev docker-compose up
docker-compose up
;;
s)
COMPOSE_PROJECT_NAME=mqdev docker-compose stop
docker-compose stop
;;
e)
docker exec -it mqdev_dev_1 /bin/bash
Expand All @@ -24,10 +24,10 @@ while getopts "bustefcdp" OPTION; do
./bin/run-fun-test.sh "$2"
;;
c)
COMPOSE_PROJECT_NAME=mqdev docker-compose run -e CHANGELOG_GITHUB_TOKEN=${CHANGELOG_GITHUB_TOKEN:-""} --workdir="/mqdev" --rm generate-changelog github_changelog_generator --future-release "$2" --simple-list
docker-compose run -e CHANGELOG_GITHUB_TOKEN=${CHANGELOG_GITHUB_TOKEN:-""} --workdir="/mqdev" --rm generate-changelog github_changelog_generator --future-release "$2" --simple-list
;;

d) COMPOSE_PROJECT_NAME=mqdev docker-compose run --workdir="/mqdev" --rm dev php pkg/enqueue-bundle/Tests/Functional/app/console.php config:dump-reference enqueue -vvv
d) docker-compose run --workdir="/mqdev" --rm dev php pkg/enqueue-bundle/Tests/Functional/app/console.php config:dump-reference enqueue -vvv
;;
\?)
echo "Invalid option: -$OPTARG" >&2
Expand Down
17 changes: 17 additions & 0 deletions docs/transport/redis.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@ $connectionFactory = new RedisConnectionFactory([
$psrContext = $connectionFactory->createContext();
```

* With custom redis instance:

It gives you more control over vendor specific features.

```php
<?php
use Enqueue\Redis\RedisConnectionFactory;
use Enqueue\Redis\PRedis;

$config = [];
$options = [];

$redis = new PRedis(new \PRedis\Client($config, $options));

$factory = new RedisConnectionFactory(['vendor' => 'custom', 'redis' => $redis]);
```

## Send message to topic

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

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

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

if ('custom' == $this->config['vendor'] && false == $this->redis) {
if (empty($this->config['redis'])) {
throw new \LogicException('The redis option should be set if vendor is custom.');
}

if (false == $this->config['redis'] instanceof Redis) {
throw new \LogicException(sprintf('The redis option should be instance of "%s".', Redis::class));
}

$this->redis = $this->config['redis'];
}

$this->redis->connect();
}

Expand Down Expand Up @@ -138,6 +151,7 @@ private function defaultConfig()
'reserved' => null,
'retry_interval' => null,
'vendor' => 'phpredis',
'redis' => null,
'persisted' => false,
'lazy' => true,
'database' => 0,
Expand Down
7 changes: 6 additions & 1 deletion pkg/redis/Tests/RedisConnectionFactoryConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testThrowIfDsnCouldNotBeParsed()
public function testThrowIfVendorIsInvalid()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Unsupported redis vendor given. It must be either "predis", "phpredis". Got "invalidVendor"');
$this->expectExceptionMessage('Unsupported redis vendor given. It must be either "predis", "phpredis", "custom". Got "invalidVendor"');

new RedisConnectionFactory(['vendor' => 'invalidVendor']);
}
Expand Down Expand Up @@ -72,6 +72,7 @@ public static function provideConfigs()
'persisted' => false,
'lazy' => true,
'database' => 0,
'redis' => null,
],
];

Expand All @@ -87,6 +88,7 @@ public static function provideConfigs()
'persisted' => false,
'lazy' => true,
'database' => 0,
'redis' => null,
],
];

Expand All @@ -102,6 +104,7 @@ public static function provideConfigs()
'persisted' => false,
'lazy' => true,
'database' => 0,
'redis' => null,
],
];

Expand All @@ -118,6 +121,7 @@ public static function provideConfigs()
'lazy' => false,
'foo' => 'bar',
'database' => 5,
'redis' => null,
],
];

Expand All @@ -134,6 +138,7 @@ public static function provideConfigs()
'lazy' => true,
'foo' => 'bar',
'database' => 0,
'redis' => null,
],
];
}
Expand Down
42 changes: 42 additions & 0 deletions pkg/redis/Tests/RedisConnectionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Enqueue\Redis\Tests;

use Enqueue\Redis\Redis;
use Enqueue\Redis\RedisConnectionFactory;
use Enqueue\Redis\RedisContext;
use Enqueue\Test\ClassExtensionTrait;
Expand All @@ -28,4 +29,45 @@ public function testShouldCreateLazyContext()
$this->assertAttributeEquals(null, 'redis', $context);
$this->assertInternalType('callable', $this->readAttribute($context, 'redisFactory'));
}

public function testShouldThrowIfVendorIsCustomButRedisInstanceNotSet()
{
$factory = new RedisConnectionFactory([
'vendor' => 'custom',
'redis' => null,
'lazy' => false,
]);

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The redis option should be set if vendor is custom.');
$factory->createContext();
}

public function testShouldThrowIfVendorIsCustomButRedisIsNotInstanceOfRedis()
{
$factory = new RedisConnectionFactory([
'vendor' => 'custom',
'redis' => new \stdClass(),
'lazy' => false,
]);

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The redis option should be instance of "Enqueue\Redis\Redis".');
$factory->createContext();
}

public function testShouldUseCustomRedisInstance()
{
$redisMock = $this->createMock(Redis::class);

$factory = new RedisConnectionFactory([
'vendor' => 'custom',
'redis' => $redisMock,
'lazy' => false,
]);

$context = $factory->createContext();

$this->assertAttributeSame($redisMock, 'redis', $context);
}
}

0 comments on commit 3791b63

Please sign in to comment.