Created RedisFactoryTest

This commit is contained in:
Alejandro Celaya 2019-08-07 16:07:17 +02:00
parent 1e294fe1bc
commit 7aa246b550
3 changed files with 67 additions and 2 deletions

View file

@ -10,4 +10,11 @@ return [
// ],
],
'dependencies' => [
'aliases' => [
// With this config, a user could alias 'lock_store' => 'redis_lock_store' to override the default
// 'lock_store' => 'redis_lock_store',
],
],
];

View file

@ -18,13 +18,13 @@ class RedisFactory
public function __invoke(ContainerInterface $container): PredisClient
{
$redisConfig = $container->get('config')['redis'] ?? [];
$servers = $redisConfig['servers'];
$servers = $redisConfig['servers'] ?? [];
if (is_array($servers) && count($servers) === 1) {
$servers = array_shift($servers);
}
$options = is_string($servers) ? null : ['cluster' => 'redis'];
$options = is_string($servers) || count($servers) < 1 ? null : ['cluster' => 'redis'];
return new PredisClient($servers, $options);
}
}

View file

@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Common\Cache;
use PHPUnit\Framework\TestCase;
use Predis\Connection\Aggregate\PredisCluster;
use Predis\Connection\Aggregate\RedisCluster;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Container\ContainerInterface;
use Shlinkio\Shlink\Common\Cache\RedisFactory;
class RedisFactoryTest extends TestCase
{
/** @var RedisFactory */
private $factory;
/** @var ObjectProphecy */
private $container;
public function setUp(): void
{
$this->container = $this->prophesize(ContainerInterface::class);
$this->factory = new RedisFactory();
}
/**
* @test
* @dataProvider provideRedisConfig
*/
public function createsRedisClientBasedOnConfig(?array $config, string $expectedCluster): void
{
$getConfig = $this->container->get('config')->willReturn([
'redis' => $config,
]);
$client = ($this->factory)($this->container->reveal());
$getConfig->shouldHaveBeenCalledOnce();
$this->assertInstanceOf($expectedCluster, $client->getOptions()->cluster);
}
public function provideRedisConfig(): iterable
{
yield 'no config' => [null, PredisCluster::class];
yield 'single server as string' => [[
'servers' => 'tcp://127.0.0.1:6379',
], PredisCluster::class];
yield 'single server as array' => [[
'servers' => ['tcp://127.0.0.1:6379'],
], PredisCluster::class];
yield 'cluster of servers' => [[
'servers' => ['tcp://1.1.1.1:6379', 'tcp://2.2.2.2:6379'],
], RedisCluster::class];
yield 'empty cluster of servers' => [[
'servers' => [],
], PredisCluster::class];
}
}