From 7aa246b550548c7f544dcc27a34da979101a8177 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya <alejandro@alejandrocelaya.com> Date: Wed, 7 Aug 2019 16:07:17 +0200 Subject: [PATCH] Created RedisFactoryTest --- config/autoload/redis.local.php.local | 7 +++ module/Common/src/Cache/RedisFactory.php | 4 +- module/Common/test/Cache/RedisFactoryTest.php | 58 +++++++++++++++++++ 3 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 module/Common/test/Cache/RedisFactoryTest.php diff --git a/config/autoload/redis.local.php.local b/config/autoload/redis.local.php.local index d6256cca..f89201f1 100644 --- a/config/autoload/redis.local.php.local +++ b/config/autoload/redis.local.php.local @@ -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', + ], + ], + ]; diff --git a/module/Common/src/Cache/RedisFactory.php b/module/Common/src/Cache/RedisFactory.php index c35508f2..6e7c864b 100644 --- a/module/Common/src/Cache/RedisFactory.php +++ b/module/Common/src/Cache/RedisFactory.php @@ -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); } } diff --git a/module/Common/test/Cache/RedisFactoryTest.php b/module/Common/test/Cache/RedisFactoryTest.php new file mode 100644 index 00000000..765a3e8f --- /dev/null +++ b/module/Common/test/Cache/RedisFactoryTest.php @@ -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]; + } +}