|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\Common\Cache; |
|
5
|
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
use Predis\Connection\Aggregate\PredisCluster; |
|
8
|
|
|
use Predis\Connection\Aggregate\RedisCluster; |
|
9
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
10
|
|
|
use Psr\Container\ContainerInterface; |
|
11
|
|
|
use Shlinkio\Shlink\Common\Cache\RedisFactory; |
|
12
|
|
|
|
|
13
|
|
|
class RedisFactoryTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var RedisFactory */ |
|
16
|
|
|
private $factory; |
|
17
|
|
|
/** @var ObjectProphecy */ |
|
18
|
|
|
private $container; |
|
19
|
|
|
|
|
20
|
|
|
public function setUp(): void |
|
21
|
|
|
{ |
|
22
|
|
|
$this->container = $this->prophesize(ContainerInterface::class); |
|
23
|
|
|
$this->factory = new RedisFactory(); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @test |
|
28
|
|
|
* @dataProvider provideRedisConfig |
|
29
|
|
|
*/ |
|
30
|
|
|
public function createsRedisClientBasedOnConfig(?array $config, string $expectedCluster): void |
|
31
|
|
|
{ |
|
32
|
|
|
$getConfig = $this->container->get('config')->willReturn([ |
|
33
|
|
|
'redis' => $config, |
|
34
|
|
|
]); |
|
35
|
|
|
|
|
36
|
|
|
$client = ($this->factory)($this->container->reveal()); |
|
37
|
|
|
|
|
38
|
|
|
$getConfig->shouldHaveBeenCalledOnce(); |
|
39
|
|
|
$this->assertInstanceOf($expectedCluster, $client->getOptions()->cluster); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function provideRedisConfig(): iterable |
|
43
|
|
|
{ |
|
44
|
|
|
yield 'no config' => [null, PredisCluster::class]; |
|
45
|
|
|
yield 'single server as string' => [[ |
|
46
|
|
|
'servers' => 'tcp://127.0.0.1:6379', |
|
47
|
|
|
], PredisCluster::class]; |
|
48
|
|
|
yield 'single server as array' => [[ |
|
49
|
|
|
'servers' => ['tcp://127.0.0.1:6379'], |
|
50
|
|
|
], PredisCluster::class]; |
|
51
|
|
|
yield 'cluster of servers' => [[ |
|
52
|
|
|
'servers' => ['tcp://1.1.1.1:6379', 'tcp://2.2.2.2:6379'], |
|
53
|
|
|
], RedisCluster::class]; |
|
54
|
|
|
yield 'empty cluster of servers' => [[ |
|
55
|
|
|
'servers' => [], |
|
56
|
|
|
], PredisCluster::class]; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|